{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-opentelemetry-genai-gen-ai-usage-token-counts-when-a-streamed-tool-call-span-closes-before-the-provider-s-final-usage",
  "title": "How should an instance reconcile OpenTelemetry GenAI `gen_ai.usage.*` token counts when a streamed tool-call span closes before the provider's final usage chunk arrives",
  "date_of_record": "2026 December 1",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.*`) and that stream provider responses where usage metadata is delivered out-of-band (a terminal `usage` chunk, a `message_delta` event, or a trailer frame) rather than in the assistant message body. Does not apply to non-streaming completion paths where the full response object is available before span end, nor to purely local tokenizers used for pre-flight budget estimation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-gen-ai-usage-token-counts-when-a-streamed-tool-call-span-closes-before-the-provider-s-final-usage.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-gen-ai-usage-token-counts-when-a-streamed-tool-call-span-closes-before-the-provider-s-final-usage.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-gen-ai-usage-token-counts-when-a-streamed-tool-call-span-closes-before-the-provider-s-final-usage.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1915,
    "python_slice_tokens": 271,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as **span-final attributes**: they are set on the span that represents the model invocation, and they are expected to be present and correct at `span.end()`. But in the streaming protocol used by every major provider, the token counts are the **last** thing to a...",
    "what_was_observed": "A tool-calling agent loop over a streaming chat endpoint. The runtime opened a `gen_ai.chat` span per model turn, set `gen_ai.request.model`, `gen_ai.operation.name`, and request parameters at start, and accumulated streamed deltas. When the provider emitted a `tool_use` block, the runtime assembled the JSON arguments from `input_json_delta` fragments, invoked the tool, and closed the span with `gen_ai.tool.name` and...",
    "condition_of_validity": "The invariant that must hold is: **the span representing a model invocation must not be ended until either (a) the provider's terminal usage frame has been consumed and its counts attached, or (b) a bounded deadline has elapsed, at which point the span is ended with an explicit `gen_ai.usage.*` absence marker rather than a fabricated or stale value.** This i...",
    "decision_boundary": "The runtime must decide, at the moment a tool-call block completes assembly, whether to end the span now or hold it open. The decision boundary is **whether the provider's usage frame is known to be pending**. Two protocol shapes exist and they require opposite handling: - **Usage-in-message-body providers** (usage present on the final assistant message): en...",
    "operational_effect": "With the ordering invariant enforced, token accounting becomes trustworthy at the cost of a small, bounded span-lifetime extension on terminal-frame providers — spans close a few milliseconds later, well within typical batch export windows. Cost dashboards, per-session budget enforcement, and context-window headroom calculations all read correct `gen_ai.usag..."
  },
  "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": "ee8562e5d3b798ec322fc3bbd36c98c1b298925a925252c8533cd2150bacd5a6",
    "verified_classes": [
      "UsageAccountingViolation",
      "ChatSpan"
    ],
    "verified_functions": [
      "close_chat_span",
      "__init__",
      "attach_usage",
      "mark_frame_lost",
      "end"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UsageAccountingViolation"
    ],
    "sample_fail_closed_invariants": [
      "terminal-frame provider: span ended before usage frame consumed; hold open until frame arrives or deadline marks frame_l",
      "message-body provider: usage must be attached at assembly time"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
