{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-opentelemetry-genai-token-accounting-when-streaming-tool-call-spans-and-final-usage-records-disagree",
  "title": "Where must an instance reconcile OpenTelemetry GenAI token accounting when streaming tool-call spans and final usage records disagree",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agent runtimes that emit `gen_ai.*` spans per model turn and per tool call, where streaming deltas are counted as they arrive and a terminal `usage` object is also received. Does not apply to non-streaming, single-shot inference calls that receive exactly one usage record.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-token-accounting-when-streaming-tool-call-spans-and-final-usage-records-disagree.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-token-accounting-when-streaming-tool-call-spans-and-final-usage-records-disagree.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-token-accounting-when-streaming-tool-call-spans-and-final-usage-records-disagree.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1741,
    "python_slice_tokens": 310,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat token usage as a property of a *model turn*: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` are set on the span that represents the inference request. The conventions do not specify how those attributes relate to *streamed* output, and they say nothing about tool-call spans, which carry `gen_ai.tool.name`, `...",
    "what_was_observed": "A single agent turn that streamed one tool call and then completed produced the following sequence: 1. Client opens span `gen_ai.chat` with `gen_ai.operation.name=chat`. 2. Stream begins. Client increments `output_tokens_local` on each `content` delta. Tool-call argument fragments arrive as `tool_calls` deltas and are appended to a buffer, **not** counted. 3. `finish_reason=tool_calls`. Client emits a child span `gen...",
    "condition_of_validity": "The invariant holds only under these conditions: - The provider emits a terminal `usage` object on the stream. Some providers emit it as a separate final chunk, some inline on the last content chunk, some not at all on aborted streams. - The client's incremental counter and the server's count are reconciled *before the span ends*, or the span records which o...",
    "decision_boundary": "The boundary is the moment the span ends relative to the moment the terminal usage record arrives. There are exactly two safe orderings: - **Reconcile-then-close:** buffer the stream, hold the span open until the usage chunk is consumed, set attributes from the authoritative count, close. Correct, but holds spans open across the full stream and risks orphane...",
    "operational_effect": "Without provenance, three concrete failures follow. First, cost attribution drifts low on every tool-using turn by the argument-token gap, and the drift is proportional to argument size — so agents that pass large payloads to tools are under-billed and, worse, under-*budgeted*, since a context-window enforcer reading `gen_ai.usage.output_tokens` will admit o..."
  },
  "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": "2b1a95c3682fc5988ef392c5c5cb58078012be2c92d9c9827d7b106e8d08fa69",
    "verified_classes": [
      "TokenAccountingError",
      "UsageSource",
      "GenAiChatSpan"
    ],
    "verified_functions": [
      "finalize_streamed_turn",
      "set_usage",
      "close"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "enum"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
