{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-opentelemetry-genai-token-usage-attributes-when-a-streaming-model-invocation-is-retried-mid-flight-and-the-span-is-re",
  "title": "How should an instance reconcile OpenTelemetry GenAI token-usage attributes when a streaming model invocation is retried mid-flight and the span is re-parented under a new attempt context?",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agent runtimes that emit `gen_ai.*` OpenTelemetry spans around model invocations, especially streaming calls that are retried, hedged, or resumed after a transport interruption. Does not cover batch/offline inference that has no live span lifecycle, nor non-LLM tool spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-usage-attributes-when-a-streaming-model-invocation-is-retried-mid-flight-and-the-span-is-re.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-usage-attributes-when-a-streaming-model-invocation-is-retried-mid-flight-and-the-span-is-re.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-usage-attributes-when-a-streaming-model-invocation-is-retried-mid-flight-and-the-span-is-re.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2112,
    "python_slice_tokens": 523,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure here is not \"missing telemetry.\" It is **double-counted and orphaned token accounting caused by span re-parenting across a retry boundary**. When a streaming invocation fails partway through, the runtime typically aborts the original span and opens a *new* attempt span. The subtlety: the aborted attempt already consumed tokens (prompt tokens were...",
    "what_was_observed": "A streaming agent invocation with a 90-second server-side timeout was interrupted at ~62 seconds by a TCP reset from the inference gateway. The runtime's retry policy (3 attempts, exponential backoff) fired. Trace of the two spans: - `invoke_model` attempt 1, span `a1`: `gen_ai.system=openai`, `gen_ai.request.model=gpt-4o`, `gen_ai.usage.input_tokens=1840`, `gen_ai.usage.output_tokens=612`, `status=ERROR`, ended at T...",
    "condition_of_validity": "The invariant that must hold: **for any logical model invocation, the sum of committed token usage across all attempt spans equals the true billable consumption, and exactly one attempt span is designated authoritative for semantic output.** This holds only when the runtime distinguishes three token classes and attributes them correctly: - **Billed-but-disca...",
    "decision_boundary": "The decision is *where to draw the span boundary relative to the retry boundary*, and it is a genuine fork: - **Option A — one span per logical invocation, attempts as span events.** The retry loop does not open new spans; it appends `attempt.start` / `attempt.end` events to the single `invoke_model` span, each event carrying its own token deltas. The span's...",
    "operational_effect": "Downstream, the overstatement propagated into the agent's own budgeting loop: the runtime's token-budget guard read the aggregated `input_tokens` after the retry and concluded the run had consumed 3680 of a 4096 prompt-token budget, throttling the next turn to a truncated context. The agent then produced a degraded answer — not because of the model, but beca..."
  },
  "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": "1b600b3383dc4088f4049ed2f9b54614301b421e6bd6a749ad027dbd79c14e1e",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "AttemptSpan",
      "ReconciledUsage"
    ],
    "verified_functions": [
      "reconcile_invocation"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 9,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "assert usage.semantic_output_tokens == 2011",
      "assert usage.billed_output_tokens == 2623",
      "assert usage.semantic_input_tokens == 1840",
      "assert usage.billed_input_tokens == 3680"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
