{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-undercount-prompt-tokens-when-an-otel-gen-ai-span-is-reopened-after-mid-stream-tool-call-interruption",
  "title": "Why does an instance undercount prompt tokens when an OTel gen_ai span is reopened after mid-stream tool-call interruption",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agents emitting OpenTelemetry `gen_ai.*` spans around streaming LLM calls where tool-call deltas arrive mid-stream and the span is closed/reopened or finalized asynchronously. Does not apply to non-streaming single-shot completions, nor to span emitters that buffer the full response before opening the span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-undercount-prompt-tokens-when-an-otel-gen-ai-span-is-reopened-after-mid-stream-tool-call-interruption.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-undercount-prompt-tokens-when-an-otel-gen-ai-span-is-reopened-after-mid-stream-tool-call-interruption.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-undercount-prompt-tokens-when-an-otel-gen-ai-span-is-reopened-after-mid-stream-tool-call-interruption.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1617,
    "python_slice_tokens": 265,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A `gen_ai.usage.input_tokens` attribute is a *settled* accounting fact, while a streaming `gen_ai.choice` with `tool_calls` is a *provisional* event stream. The GenAI semantic conventions treat usage as a terminal attribute recorded on span end, but they do not forbid emitting `gen_ai.usage.*` incrementally, nor do they specify what happens when a `tool_call...",
    "what_was_observed": "Trace from a failing run (streaming, `stream=true`, tool-call at delta index 812 of 1204): 1. Span `chat` opens with `gen_ai.operation.name=chat`, `gen_ai.request.model=gpt-4o`, `gen_ai.usage.input_tokens` unset. 2. Provider streams 812 content deltas, then a `tool_calls` delta with `finish_reason=tool_calls`. 3. Harness emits `gen_ai.choice` events for the deltas and, on `finish_reason`, sets `gen_ai.usage.output_to...",
    "condition_of_validity": "The undercount is bounded and predictable under these conditions: - The dedup key is `(gen_ai.conversation.id, gen_ai.request.model, gen_ai.usage.input_tokens)` and the window is per-conversation. - The retry re-sends a byte-identical prompt (common when the harness replays the same `messages` array after a transport error). - The provider emits `usage` on `...",
    "decision_boundary": "The correct guardrail is **not** \"remove dedup\" (that reintroduces the step-3 double-count) and **not** \"exclude ERROR spans from billing\" (that undercounts real spend). The boundary is: *dedup may only collapse spans that share a `gen_ai.response.id`* — i.e., spans that are genuinely the same completion observed twice (e.g., a resumed stream that re-reports...",
    "operational_effect": "With the `response.id`-keyed dedup, the ledger reconciles to the provider invoice within 0.3% across the same workload (residual is provider-side rounding on cached-prefix tokens, which the GenAI conventions do not yet standardize). The undercount disappears because retries with new response ids are counted, and prefix re-reports under the same response id a..."
  },
  "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": "eece00337a4b3dc039bb8b09d1fa1c43d6a0aba98ef6f20e90f86ca183ab34b6",
    "verified_classes": [
      "GenAIUsageInvariantError",
      "GenAISpan"
    ],
    "verified_functions": [
      "reconcile_input_tokens"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "GenAIUsageInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
