{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-genai-span-attributes-before-the-token-accounting-closes-the-mid-stream-usage-gap-and-its-downstream-attribution-c",
  "title": "What happens when an instance emits GenAI span attributes before the token accounting closes: the mid-stream usage gap and its downstream attribution corruption",
  "date_of_record": "2026 November 26",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for LLM invocations, tool calls, or agent turns, where the span lifecycle is decoupled from the actual token-usage completion event (streaming responses, async tool dispatch, or provider-side usage reconciliation). Does not apply to fully buffered, non-streaming invocations where `usage` is known before the span ends.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-span-attributes-before-the-token-accounting-closes-the-mid-stream-usage-gap-and-its-downstream-attribution-c.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-span-attributes-before-the-token-accounting-closes-the-mid-stream-usage-gap-and-its-downstream-attribution-c.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-span-attributes-before-the-token-accounting-closes-the-mid-stream-usage-gap-and-its-downstream-attribution-c.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2386,
    "python_slice_tokens": 622,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two distinct moments in an LLM invocation that look interchangeable but are not: the moment the **response payload is structurally complete** (last content delta received, finish reason observed) and the moment the **usage accounting is authoritative** (the provider's `usage` object, which for streaming responses arrives in a terminal chunk that is...",
    "what_was_observed": "A tool-using agent loop streamed completions from a provider that emits usage in a trailing `message_delta` event after the `message_stop` content event. The runtime's span wrapper ended the invocation span on `message_stop` because that is where the content promise resolved. Observed trace sequence for a single agent turn with three tool calls: 1. Turn 1 invocation span ends with `gen_ai.usage.output_tokens = 0` (th...",
    "condition_of_validity": "The invariant holds — and the failure is bounded — only under the following condition: - **The span's usage attributes are written exactly once, from an authoritative source, and the span is ended only after that write completes.** For streaming, this means the span-end hook must be bound to the usage-completion event, not the content-completion event. If th...",
    "decision_boundary": "The boundary between \"safe to end span\" and \"must defer\" is determined by a single question: **is the usage object part of the content-completion event or a subsequent event?** If subsequent, the span-end must be deferred to a usage-completion future with a bounded timeout. The timeout matters: providers can drop the usage chunk on error paths. The decision ...",
    "operational_effect": "The downstream effects are asymmetric and compound across turns: - **Budget governors** that default absent-to-zero systematically undercount output-heavy sessions and never trip their warnings. The overshoot is silent until a hard external limit fires. - **Cost attribution** shifts spend to whichever span *did* carry usage, producing phantom expensive turns..."
  },
  "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": "0af80d081dd6a1f4f7270638a4b1fb6285f53666344bfebcf7a6100b2d19d282",
    "verified_classes": [
      "ProvisionalUsageExportError",
      "GenAIInvocationSpan"
    ],
    "verified_functions": [
      "finalize_invocation",
      "governor_decrement",
      "record_usage",
      "record_estimate",
      "end"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "ProvisionalUsageExportError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
