{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-s-genai-token-accounting-silently-double-count-when-a-tool-call-span-is-retried-under-opentelemetry-semantic-conventions",
  "title": "Why does an instance's GenAI token accounting silently double-count when a tool-call span is retried under OpenTelemetry semantic conventions",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agents emitting OpenTelemetry spans following the GenAI semantic conventions (`gen_ai.*`) where tool invocations are wrapped in child spans and usage is recorded as attributes. Does not apply to single-shot inference with no tool loop, nor to billing reconciliation performed against provider-side ledgers rather than collected spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-s-genai-token-accounting-silently-double-count-when-a-tool-call-span-is-retried-under-opentelemetry-semantic-conventions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-genai-token-accounting-silently-double-count-when-a-tool-call-span-is-retried-under-opentelemetry-semantic-conventions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-genai-token-accounting-silently-double-count-when-a-tool-call-span-is-retried-under-opentelemetry-semantic-conventions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1659,
    "python_slice_tokens": 307,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There is a distinction between *span cardinality* and *token cardinality*. The GenAI semantic conventions attach `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` to the span that represents the model call. When an agent loop retries a tool invocation—because the tool returned a transient error, a malformed schema, or a timeout—the retry produces ...",
    "what_was_observed": "A research agent executing a 9-step tool loop emitted a trace with 27 spans: 9 `gen_ai.chat` spans, 9 `gen_ai.execute_tool` spans, and 9 retry `gen_ai.chat` spans generated when the `execute_tool` span returned `status.code = ERROR` and the orchestrator re-prompted the model with the error text appended. Each retry `gen_ai.chat` span carried `gen_ai.usage.input_tokens` reflecting the *full* conversation up to that po...",
    "condition_of_validity": "The invariant \"sum of `gen_ai.usage.*` across spans in a trace equals tokens attributable to the task\" holds **only if** every `gen_ai.chat` span in the trace carries a *delta* usage (tokens newly consumed by that single call) **and** retries are represented as spans that are either (a) explicitly marked `gen_ai.request.retry = true` and excluded from task a...",
    "decision_boundary": "The boundary is between two aggregation regimes, and the choice must be made explicitly at the collector, not left to the dashboard: - **Billing regime:** sum all `gen_ai.usage.*` across all `gen_ai.chat` spans, including retries, without de-duplication. This matches the provider ledger because the provider bills every replay. Use this for cost. - **Task reg...",
    "operational_effect": "The practical consequence is that token dashboards silently drift from the invoice by a factor that grows with retry rate and context length—roughly `Σ_{i=1}^{N} L_i` versus `L_N`, where `L_i` is the prompt length at attempt `i`. For an agent with a 5% per-step tool failure rate and a 30k-token context, the billing-regime total runs 1.4–1.9× the task-regime ..."
  },
  "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": "f7247f1247aa04966736be01818e709979993e4b468d3e328fffef7bd9edfb80",
    "verified_classes": [
      "AmbiguousUsageSpanError",
      "GenAISpan"
    ],
    "verified_functions": [
      "emit_chat_span",
      "_looks_like_uuid"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "AmbiguousUsageSpanError"
    ],
    "sample_fail_closed_invariants": [
      "Span reports cumulative input tokens but is not marked as a retry; billing-regime and task-regime sums will diverge sile",
      "Retry span reports non-cumulative usage; provider will bill the full prompt, so the reported figure understates cost."
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "uuid"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
