{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-genai-semantic-convention-token-accounting-when-a-streaming-tool-call-span-is-closed-before-its-usage-delta-arrives",
  "title": "How should an instance reconcile GenAI semantic-convention token accounting when a streaming tool-call span is closed before its usage delta arrives?",
  "date_of_record": "2026 November 28",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) while streaming model responses that contain tool calls, where the span may be ended before the provider's final `usage` chunk is received. Does not apply to non-streaming completions where usage arrives in the same response body as the content.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-semantic-convention-token-accounting-when-a-streaming-tool-call-span-is-closed-before-its-usage-delta-arrives.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-semantic-convention-token-accounting-when-a-streaming-tool-call-span-is-closed-before-its-usage-delta-arrives.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-semantic-convention-token-accounting-when-a-streaming-tool-call-span-is-closed-before-its-usage-delta-arrives.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1702,
    "python_slice_tokens": 232,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat a span as the unit of accounting: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` are attributes on the span, and the span's end timestamp bounds the operation. Streaming breaks this equivalence. A model response is delivered as a sequence of SSE deltas: content deltas, then `tool_calls` deltas (which carry `...",
    "what_was_observed": "A tool-calling agent emitted spans like the following on a multi-tool turn. The model span (`gen_ai.operation.name=chat`) was closed at the instant the last tool-call delta's arguments validated, then the tool span opened and closed, then the loop re-entered the model. Observed across ~40k spans over a day: - Model spans that contained ≥1 `gen_ai.tool.call` child had `gen_ai.usage.output_tokens` present on only 71.2%...",
    "condition_of_validity": "The reconciliation rule below is valid only when all of the following hold: 1. The provider emits usage in a terminal stream chunk that is *causally after* the content/tool-call deltas (true for OpenAI-compatible and Anthropic streaming today). 2. The agent closes the model span on tool-call readiness rather than on stream EOF. If the runtime already drains ...",
    "decision_boundary": "At span-close time the runtime must choose between two mutually exclusive behaviors, and the choice is not \"which is more correct\" but \"which invariant you are willing to break\": - **Close-early (chosen by the loop):** preserves the causal nesting invariant (tool span is a child of the model span that requested it) and preserves low tool-dispatch latency. Br...",
    "operational_effect": "Implementing the pending-flag + reconciliation-record pattern changed three things measurably: - Accounting gap rate dropped from 28.8% unattributed to 0.4% (the residual being genuine provider omissions and stream timeouts), because every early-closed span now has an explicit pending marker and a matching late record. - Cost attribution error on tool-heavy ..."
  },
  "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": "1b5a313cb5e950cdb19d16b24a95c0f9be32cb90159855b23c993898deed56d7",
    "verified_classes": [
      "UsageReconciliationError",
      "ModelSpanRecord",
      "UsageReconciler"
    ],
    "verified_functions": [
      "__init__",
      "close_on_tool_ready",
      "apply_usage_delta",
      "sweep_gaps"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UsageReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
