{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-bound-the-genai-span-s-gen-ai-usage-token-counters-when-a-streaming-tool-call-loop-emits-partial-deltas-before-the-model-s-fini",
  "title": "Where must an instance bound the GenAI span’s `gen_ai.usage.*` token counters when a streaming tool-call loop emits partial deltas before the model’s finish reason is known?",
  "date_of_record": "2026 December 1",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) around streaming chat completions that interleave tool calls, and that finalizes token usage from streamed deltas rather than from a single terminal response object. Does not apply to non-streaming request/response completions where a single response envelope carries authoritative `usage`.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-bound-the-genai-span-s-gen-ai-usage-token-counters-when-a-streaming-tool-call-loop-emits-partial-deltas-before-the-model-s-fini.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-bound-the-genai-span-s-gen-ai-usage-token-counters-when-a-streaming-tool-call-loop-emits-partial-deltas-before-the-model-s-fini.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-bound-the-genai-span-s-gen-ai-usage-token-counters-when-a-streaming-tool-call-loop-emits-partial-deltas-before-the-model-s-fini.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1661,
    "python_slice_tokens": 272,
    "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 *settled, monotonic, terminal* attributes of a span. The streaming transport, however, delivers usage as *incremental and often absent until the final chunk*. These two contracts are structurally incompatible at the boundary where a tool-call loop re-enters t...",
    "what_was_observed": "A planner agent runs a ReAct loop: `chat.completions` (stream) → parse `tool_calls` → invoke tool → re-enter model with tool result. The runtime opens one span per model invocation, tagged `gen_ai.operation.name=chat`, and a child span per tool call tagged `gen_ai.operation.name=execute_tool`. Usage is accumulated as follows: each stream chunk carrying a `usage` field is added to a running counter; on `finish_reason`...",
    "condition_of_validity": "The invariant that makes a span’s usage attributes trustworthy is: > A span’s `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` are valid **iff** they are written exactly once, from a single authoritative terminal observation, and are never mutated after `End()`. Any accumulation across chunks must be proven to be *additive deltas*, not *cumulative ...",
    "decision_boundary": "The runtime must decide, per span, between two mutually exclusive finalization rules, and must *refuse* to finalize otherwise: - If `finish_reason == tool_calls`: the model span may record `gen_ai.usage.*` **only** if a usage chunk already arrived for this turn; otherwise it must mark usage as *unknown* (omit the attribute, set a `gen_ai.usage.estimated=fals...",
    "operational_effect": "With the gate in place: long tool loops produce spans where early turns carry *no* usage attribute and the terminal turn carries full-context usage; a parent span or metric sums them correctly, and cost dashboards track true superlinear spend. The trace becomes honest about *where* accounting is unavailable instead of silently zeroing it. The cost is that pe..."
  },
  "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": "012d450bb20a8466f5744822fa1ce0dbe2c16136faf4008d22554a7e1899bdeb",
    "verified_classes": [
      "GenAIUsageFinalizationError",
      "StreamState"
    ],
    "verified_functions": [
      "finalize_genai_usage"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAIUsageFinalizationError"
    ],
    "sample_fail_closed_invariants": [
      "stream ended without finish_reason; usage cannot be settled",
      "tool_calls finish with no usage chunk; refusing to write 0/0",
      "terminal finish but usage chunk never arrived"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
