{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-genai-token-attributes-when-a-streaming-tool-call-span-closes-before-its-usage-chunk-arrives",
  "title": "Where must an instance reconcile GenAI token attributes when a streaming tool-call span closes before its usage chunk arrives",
  "date_of_record": "2026 November 28",
  "scope": "Applies to agents that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) while streaming model responses that include tool calls, and that derive cost/latency telemetry from `gen_ai.usage.*` attributes. Does not apply to non-streaming completion calls, where usage is returned atomically with the final response body, nor to spans that carry no tool-call child.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-token-attributes-when-a-streaming-tool-call-span-closes-before-its-usage-chunk-arrives.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-token-attributes-when-a-streaming-tool-call-span-closes-before-its-usage-chunk-arrives.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-token-attributes-when-a-streaming-tool-call-span-closes-before-its-usage-chunk-arrives.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1753,
    "python_slice_tokens": 298,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat a model-invocation span and its tool-call child spans as a single logical unit whose attributes are *finalized at span end*. But a streaming provider emits `tool_calls` deltas in the content stream and emits the `usage` object in a **separate trailing chunk** — frequently in a chunk that arrives *after* the last tool-call...",
    "what_was_observed": "A tool-using agent streams a chat completion. The wire sequence, in order, is: 1. `delta.role = \"assistant\"`, empty content. 2. One or more `delta.content` chunks (often empty for pure tool calls). 3. `delta.tool_calls[i]` chunks carrying `index`, `id`, `function.name`, and incrementally concatenated `function.arguments` fragments. 4. `finish_reason = \"tool_calls\"` on the final content delta. 5. `[DONE]` or a final c...",
    "condition_of_validity": "The invariant — *an invocation span's `gen_ai.usage.*` attributes must be written from a source that is guaranteed to be observed before the span is finalized* — holds only when **all** of the following are true: - The provider emits usage in a chunk strictly after `finish_reason`. If usage is embedded in the `finish_reason` chunk itself (some gateways do th...",
    "decision_boundary": "The agent must choose between two span-finalization strategies at the point `finish_reason` is observed: - **Finalize-on-finish:** close invocation span immediately. Cheap, low-latency export, but violates the usage invariant whenever usage trails `finish_reason`. - **Finalize-on-usage-or-drain:** keep the invocation span open and continue reading until eith...",
    "operational_effect": "Without the guardrail, cost telemetry under-reports precisely on the calls that matter most — long tool-call streams that are most likely to hit trailing-chunk timeouts — and the under-reporting is *correlated with tool activity*, biasing any per-tool cost attribution toward zero. Auditors replaying spans to reconstruct spend will find tool-call child spans ..."
  },
  "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": "ff6d0f16bc837add3052550806fef03b11fcb53661d6c39efafccf32b3fe6fe0",
    "verified_classes": [
      "UsageFinalizationError",
      "InvocationSpan"
    ],
    "verified_functions": [
      "close_invocation_span",
      "handle_stream_end",
      "finalize"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageFinalizationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
