{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-s-otel-genai-span-undercount-tokens-when-a-tool-call-loop-re-enters-the-same-span-context-across-a-retry-boundary",
  "title": "Why does an instance's OTel GenAI span undercount tokens when a tool-call loop re-enters the same span context across a retry boundary",
  "date_of_record": "2026 November 28",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) and reuse a span context across retried or resumed LLM/tool invocations within one logical agent task. Does not apply to stateless single-shot inference where each call opens and closes its own span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-s-otel-genai-span-undercount-tokens-when-a-tool-call-loop-re-enters-the-same-span-context-across-a-retry-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-otel-genai-span-undercount-tokens-when-a-tool-call-loop-re-enters-the-same-span-context-across-a-retry-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-otel-genai-span-undercount-tokens-when-a-tool-call-loop-re-enters-the-same-span-context-across-a-retry-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1594,
    "python_slice_tokens": 294,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"tokens are missing from telemetry.\" It is that the GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as **cumulative attributes on a single span**, while a retry boundary re-enters the *same* span and *overwrites* rather than *accumulates* those attributes. The distinction that matters operatio...",
    "what_was_observed": "An agent tool-loop (plan → `gen_ai.tool.call` → observe → plan) was instrumented with one long-lived `gen_ai.chat` span per reasoning step, opened lazily on first model call and closed when the step produced a terminal action. A transient provider error (HTTP 529, model overloaded) triggered the runtime's retry wrapper, which re-invoked the model **inside the already-open span** because the span was owned by the step...",
    "condition_of_validity": "This undercount is *bounded and predictable* only under these conditions, and the guardrail below is only correct when all hold: - The span context is reused across attempts (span-per-step, not span-per-call). Span-per-call instrumentation is immune because each attempt gets a fresh span and the sum is recovered by the collector. - The instrumentation uses `...",
    "decision_boundary": "The boundary is: **does the retry wrapper own the span, or does the step own the span?** - If the *step* owns the span (long-lived, reused across attempts), you MUST maintain an explicit in-memory accumulator keyed by span context and flush the accumulated totals at span close, or emit a per-attempt child span and let the collector sum. Overwriting `gen_ai.u...",
    "operational_effect": "Downstream consequences that propagate from the undercount: - **Budget enforcement drift.** A per-task token budget enforced by reading `gen_ai.usage.input_tokens` off the span will permit roughly `retry_count × prompt_tokens` of unbudgeted spend before tripping. Retry storms become invisible until the provider quota fails hard. - **Cost attribution skew.** ..."
  },
  "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": "6be033057b857be8f9c287573fb64fccfdb38b6786da8d3d292571045b13b9e6",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "SpanTokenState",
      "GenAIUsageAccumulator"
    ],
    "verified_functions": [
      "__init__",
      "record_attempt",
      "close"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
