{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-opentelemetry-genai-spans-for-a-tool-call-whose-token-usage-is-reported-after-the-span-has-already-ended",
  "title": "What happens when an instance emits OpenTelemetry GenAI spans for a tool call whose token usage is reported after the span has already ended",
  "date_of_record": "2026 November 21",
  "scope": "Applies to LLM agent instances instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where token usage is reported asynchronously or out-of-band relative to the span lifecycle. Does not apply to synchronous in-process instrumentation where usage is known before `span.end()`.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-opentelemetry-genai-spans-for-a-tool-call-whose-token-usage-is-reported-after-the-span-has-already-ended.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-opentelemetry-genai-spans-for-a-tool-call-whose-token-usage-is-reported-after-the-span-has-already-ended.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-opentelemetry-genai-spans-for-a-tool-call-whose-token-usage-is-reported-after-the-span-has-already-ended.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1618,
    "python_slice_tokens": 318,
    "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 *span attributes* — immutable once the span is ended and exported. But many provider SDKs (streaming completions, batch inference, tool-calling loops) return usage only in a terminal chunk or a follow-up response. The boundary condition is this: **the span li...",
    "what_was_observed": "A ReAct-style agent executes a tool call. The instrumentation wraps the LLM invocation in a span `chat gpt-4o` and the tool execution in a child span `execute_tool search`. The provider streams tokens; the terminal SSE chunk carries `usage: {prompt_tokens: 3120, completion_tokens: 480}`. The agent's span wrapper, written against the non-streaming path, calls `span.end()` as soon as the stream's `on_complete` fires — ...",
    "condition_of_validity": "The invariant holds only when, for every span carrying `gen_ai.usage.*`, the following is true at export time: - `span.end()` was called *after* the usage-bearing response element was consumed, and - the usage values were written to that same span object while `is_recording()` was true, and - no other span in the trace claims the same usage (no double-count ...",
    "decision_boundary": "The forcing decision: **when the usage chunk is not guaranteed to precede stream completion, the span must not be ended on stream completion.** Two legal resolutions: 1. **Defer end.** Hold the span open until the terminal usage chunk is parsed (or a bounded timeout elapses), then write attributes and end. Cost: span duration now includes the usage-chunk lat...",
    "operational_effect": "Unbounded, the failure produces **silent zero-cost telemetry**: spans exist, traces are complete, but token accounting is empty, and the absence is indistinguishable from genuine zero usage. Cost dashboards, budget circuit-breakers, and rate-limit governors keyed on `gen_ai.usage.*` all read zero and never trip. The agent runs until the provider's hard quota..."
  },
  "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": "11a0d53bedbe5297e285c9bad89ba272dc43bb6860a66dba32d843c75dbae7f9",
    "verified_classes": [
      "UsageAttributionError",
      "GenAISpan"
    ],
    "verified_functions": [
      "export_span",
      "run_streaming_call",
      "end",
      "is_recording",
      "set_usage"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "UsageAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
