{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counting-of-llm-token-usage-when-an-agent-retries-a-tool-call-span-under-opentelemetry-genai-semantic-conventions",
  "title": "Which invariants prevent double-counting of LLM token usage when an agent retries a tool-call span under OpenTelemetry GenAI semantic conventions",
  "date_of_record": "2026 November 18",
  "scope": "Applies to agent runtimes emitting OpenTelemetry spans that follow the GenAI semantic conventions (`gen_ai.*`) for inference and tool-execution telemetry, where the agent framework may retry, re-invoke, or re-emit a logical operation. Does not apply to single-shot, non-retrying inference clients, nor to span processors that perform server-side aggregation independent of span identity.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-llm-token-usage-when-an-agent-retries-a-tool-call-span-under-opentelemetry-genai-semantic-conventions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-llm-token-usage-when-an-agent-retries-a-tool-call-span-under-opentelemetry-genai-semantic-conventions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-llm-token-usage-when-an-agent-retries-a-tool-call-span-under-opentelemetry-genai-semantic-conventions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1830,
    "python_slice_tokens": 288,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions model two distinct things that look identical in a span dump: **the logical operation** (one user-visible inference or tool call) and **the emitted span** (one unit of telemetry that carries `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and `gen_ai.request.model`). The invariant that matters is not \"one span per ca...",
    "what_was_observed": "In an agent runtime wiring tool calls through an instrumented HTTP client, a transient 503 from a tool endpoint triggered the framework's retry policy. The framework wrapped the tool call in a `gen_ai.operation.name=execute_tool` span. On retry it closed the first span with `status.code=ERROR` and opened a second span with the same `gen_ai.tool.name` and the same parent inference span. The first span carried no usage...",
    "condition_of_validity": "The additive interpretation (sum all usage attributes across spans) is valid **only if** every emitted span with usage attributes corresponds to a distinct provider-billed request. This holds when: - The instrumentation wraps the actual SDK call and emits exactly one span per HTTP request to the provider. - Retries at the transport layer (e.g., SDK-level ret...",
    "decision_boundary": "The boundary is between **retry-as-new-request** and **retry-as-correction**. Choose retry-as-new-request (emit a fresh span, count it) when the retry actually re-invokes the provider and the provider bills it — this is the default and is spec-compliant. Choose retry-as-correction (emit a fresh span but tag `app.retry.correction=true` and exclude from usage ...",
    "operational_effect": "The practical effect is that token-cost dashboards built on naive `sum(gen_ai.usage.input_tokens)` over `gen_ai.operation.name=chat` spans will over-report during retry storms — precisely when cost visibility matters most. The over-report is bounded by the retry rate and the context-growth factor per retry, which for agent loops with accumulating tool result..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "77cdbe0c7c785fbc98c13d7e8a5ff6caeafdc54584df1921edf4223c830a5029",
    "verified_classes": [
      "RetryUsageAmbiguityError",
      "UsageSpan"
    ],
    "verified_functions": [
      "sum_billable_tokens",
      "reconcile_against_invoice"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "RetryUsageAmbiguityError"
    ],
    "sample_fail_closed_invariants": [
      "invoice baseline is zero; cannot reconcile"
    ],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
