{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-the-token-accounting-attributes-it-emitted-mid-stream",
  "title": "Where must an instance reconcile OpenTelemetry GenAI span closure against the token-accounting attributes it emitted mid-stream?",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic conventions (`gen_ai.*`) for LLM and tool-call spans, where token usage is recorded incrementally during streaming generation and the span is closed after the response terminates. Does not apply to batch, non-streaming inference where usage is known atomically before span end, nor to spans emitted by a gateway that buffers the full response before export.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-the-token-accounting-attributes-it-emitted-mid-stream.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-the-token-accounting-attributes-it-emitted-mid-stream.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-the-token-accounting-attributes-it-emitted-mid-stream.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1404,
    "python_slice_tokens": 145,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing token counts.\" It is a **closure-ordering divergence** between two independently-timed facts: (1) the *usage record* the model provider emits at end-of-stream, and (2) the *span end event* the agent runtime emits when its streaming loop unwinds. In the OTel GenAI conventions, `gen_ai.usage.input_tokens` and `gen_ai.usage.output_to...",
    "what_was_observed": "In a streaming tool-using agent, the trace showed a `gen_ai.chat` span with `gen_ai.usage.output_tokens = 0` and `gen_ai.usage.input_tokens = 0`, yet the downstream `execute_tool` child span carried a prompt fragment of ~1,900 tokens and the model had clearly produced a tool-call argument payload. The trigger sequence: 1. Runtime opens `gen_ai.chat` span, sets request attributes (`gen_ai.request.model`, `gen_ai.reque...",
    "condition_of_validity": "This boundary holds whenever **all** of the following are true: - Usage is delivered as a discrete terminal event in the stream rather than as a header or a pre-computed field on the request. - The runtime closes the span in a `finally`/`with`-block whose exit is not conditioned on receiving that terminal usage event. - The provider does not guarantee a usag...",
    "decision_boundary": "The invariant to enforce: **a `gen_ai.chat` span must not be closed until either a usage fact has been recorded or an explicit \"usage-unknown\" marker has been set, and the two must be mutually exclusive.** Concretely: - If the terminal usage event was received → set `gen_ai.usage.input_tokens` / `output_tokens`, then `end()`. - If the stream ended without us...",
    "operational_effect": "Unreconciled closure produces a trace that is syntactically valid and semantically false. Downstream cost attribution, token-budget enforcement, and any agent that uses prior spans to estimate context pressure all read zero and under-provision. Worse, because the error is a *missing attribute* rather than a malformed one, schema validation passes and no aler..."
  },
  "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": "23258459e6018287bd9c18efab77b5c901b3f4d83b5f541d6f94f488a6f8507b",
    "verified_classes": [
      "UsageReconciliationError"
    ],
    "verified_functions": [
      "close_chat_span"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "UsageReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
