{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-emit-duplicate-genai-token-usage-spans-when-a-tool-call-is-retried-after-a-streaming-partial-failure",
  "title": "Why does an instance emit duplicate GenAI token-usage spans when a tool call is retried after a streaming partial failure",
  "date_of_record": "2026 November 28",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) that emit per-tool-call and per-inference spans while executing streaming model calls with retry logic. Does not apply to non-streaming synchronous completion paths, nor to providers that emit a single terminal usage record per request.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-duplicate-genai-token-usage-spans-when-a-tool-call-is-retried-after-a-streaming-partial-failure.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-duplicate-genai-token-usage-spans-when-a-tool-call-is-retried-after-a-streaming-partial-failure.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-duplicate-genai-token-usage-spans-when-a-tool-call-is-retried-after-a-streaming-partial-failure.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1664,
    "python_slice_tokens": 334,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"the retry double-counts tokens.\" The retry is correct. The failure is that the *span lifecycle* and the *token accounting record* are decoupled across the retry boundary: a streaming inference span is opened once, the upstream stream fails mid-flight after emitting a partial `usage` chunk (or after the provider bills prompt tokens but no ...",
    "what_was_observed": "Trace from a tool-calling agent (model: a streaming chat completion endpoint, tool `search_index`). Timeline: 1. `t=0ms` — Span `chat` opened, `gen_ai.operation.name=chat`, `gen_ai.request.model=...`, no response id yet. 2. `t=340ms` — First stream chunk with `delta.content`. Agent begins buffering tool-call arguments. 3. `t=1180ms` — Stream emits a chunk containing `usage: {input_tokens: 2048, output_tokens: 0}` (so...",
    "condition_of_validity": "This failure mode is valid (and the guardrail below is required) when *all* of the following hold: - The inference is **streaming** and usage is emitted **incrementally** (cumulative or per-chunk), not only on a terminal chunk. - The agent has **retry-on-stream-error** semantics that re-invoke the SDK call rather than resuming the stream. - Instrumentation w...",
    "decision_boundary": "The boundary is: **does the retry represent a new billable operation or a continuation of one?** The conventions have no attribute to express \"this span is attempt 2 of logical operation X.\" The implementer must choose one of two disciplines: - **Attempt-scoped spans (convention-literal):** emit one span per attempt, set `gen_ai.response.finish_reasons=[\"err...",
    "operational_effect": "Without the guardrail, three effects compound: 1. **Cost overcount** proportional to retry rate × mean prompt size. For a 2048-token prompt and 15% retry rate, reported cost is ~15% high—enough to misroute budget alerts. 2. **Latency undercount**: aborted spans with `output_tokens: 0` are filtered from p99 by consumers that require non-zero output, hiding th..."
  },
  "matched_failure_symptoms": [
    "OOM_KILL_OR_MEMORY_EXHAUSTION"
  ],
  "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": "1e99acee083ea5239eba297516822ac77e65f07b6adb4f6f0004836c9249fc9a",
    "verified_classes": [
      "GenAIUsageInvariantError",
      "GenAISpan"
    ],
    "verified_functions": [
      "finalize_span",
      "record_attempt"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "GenAIUsageInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
