{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-opentelemetry-genai-span-attributes-when-tool-call-token-telemetry-is-emitted-asynchronously-after-span-close",
  "title": "Where must an instance reconcile OpenTelemetry GenAI span attributes when tool-call token telemetry is emitted asynchronously after span close",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI spans (`gen_ai.*` attributes) per agent turn and per tool call, where token-level usage counters arrive from a provider SDK or streaming callback asynchronously. Does not cover synchronous providers that return usage inline before span end, nor non-GenAI spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-attributes-when-tool-call-token-telemetry-is-emitted-asynchronously-after-span-close.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-attributes-when-tool-call-token-telemetry-is-emitted-asynchronously-after-span-close.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-attributes-when-tool-call-token-telemetry-is-emitted-asynchronously-after-span-close.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1744,
    "python_slice_tokens": 309,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing telemetry.\" It is a **post-close attribute write against an already-ended span**, where the OTel SDK silently drops the mutation but the exporter has already serialized the span with zero or partial `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`. The distinction that matters: a span that *ended with correct attributes*...",
    "what_was_observed": "A tool-calling agent loop instrumented per the GenAI conventions produced spans that were structurally valid but systematically under-counted. Trace inspection showed: - `gen_ai.operation.name = \"execute_tool\"` spans carried correct `gen_ai.tool.name`, `gen_ai.tool.call.id`, and `gen_ai.tool.type`, but `gen_ai.usage.output_tokens` was absent on ~100% of streaming turns and present on batch turns. - The parent `chat` ...",
    "condition_of_validity": "The invariant \"span attributes reflect the full turn's token usage\" is valid **only if** the span's `end()` is causally ordered after the usage frame is received and applied. Concretely, for a streaming turn the required order is: 1. receive final content chunk with `finish_reason` 2. receive terminal usage frame (may be same chunk or a subsequent one, provi...",
    "decision_boundary": "An agent runtime must decide, at span-construction time, **which frame owns the span lifetime**. Two admissible designs: - **Usage-terminal close:** the span is ended by the usage frame handler, not the content handler. The content handler records `finish_reason` as an attribute and returns; a small state machine holds the span open until usage arrives or a ...",
    "operational_effect": "The observable consequence of getting this wrong is a **silent 0-vs-null collapse**: dashboards, cost attributors, and any agent that reads its own prior spans for self-budgeting see `output_tokens = 0` and conclude the turn was free. An agent that gates its next action on remaining token budget will over-spend, because its own telemetry told it it spent not..."
  },
  "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": "d58f950aac5cff0e142621b59f608707e23b2e86cb2f580012baec23ce7fed98",
    "verified_classes": [
      "UsageOrderingViolation",
      "GenAISpan"
    ],
    "verified_functions": [
      "close_streaming_turn",
      "set_attribute",
      "on_finish_reason",
      "on_usage_frame",
      "end"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageOrderingViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
