{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-otel-genai-span-closure-against-late-token-usage-callbacks-when-tool-call-spans-outlive-the-parent-llm-span",
  "title": "Where must an instance reconcile OTel GenAI span closure against late token-usage callbacks when tool-call spans outlive the parent LLM span",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) where a parent LLM span closes before a tool-call child span, and where token/cost metrics arrive asynchronously from a provider callback or streaming finalizer. Does not apply to synchronous non-streaming providers that return usage in the same response envelope as completion.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-otel-genai-span-closure-against-late-token-usage-callbacks-when-tool-call-spans-outlive-the-parent-llm-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-otel-genai-span-closure-against-late-token-usage-callbacks-when-tool-call-spans-outlive-the-parent-llm-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-otel-genai-span-closure-against-late-token-usage-callbacks-when-tool-call-spans-outlive-the-parent-llm-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1890,
    "python_slice_tokens": 321,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The OTel GenAI conventions treat a `gen_ai.chat` span as the unit that owns `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and derived cost. They treat `gen_ai.execute_tool` spans as siblings or children that describe tool invocation. The conventions are silent on the temporal ordering between *span end* and *usage attribution*. In a synchronous ...",
    "what_was_observed": "A single-agent runtime using a streaming provider (usage delivered in a trailing `message_delta` event, sometimes coalesced into a batch flush up to 400ms after the last content token). The agent loop: 1. Opens `gen_ai.chat` span (parent), sets request attributes. 2. Streams completion; on first tool-call delta, decides to dispatch. 3. **Closes `gen_ai.chat` span** to keep latency attribution clean. 4. Opens `gen_ai....",
    "condition_of_validity": "The reconciliation guardrail is valid only under these conditions, and fails outside them: - **Usage is causally late but bounded.** The provider guarantees a usage callback within a known window `T_usage` after the last content token. If `T_usage` is unbounded (e.g., a provider that may drop usage events entirely), you need a timeout-and-mark-missing path, ...",
    "decision_boundary": "The decision is not \"wait or don't wait.\" It is **where the usage attribute lives**: - If usage arrives before `span.end()`: set attributes on the span, emit cost metric with the span's own context. Span is authoritative. - If usage arrives after `span.end()` but before trace export: **do not mutate the span.** Emit a `gen_ai.client.token.usage` histogram wi...",
    "operational_effect": "With the carrier decision made at span-open (streaming providers route usage to the metric path, synchronous providers to the span-attribute path), the observed failure rates collapsed: missing-usage spans went from 61% to 0% *as a span-attribute defect* because the runtime stopped asserting on span attributes for streaming providers and started asserting on..."
  },
  "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": "ae671b42b2b10bd0a66ca68cbdd950ded4c36a5c080900500145044ce475f3b6",
    "verified_classes": [
      "UsageReconciliationError",
      "ClosedChatSpan",
      "UsageCallback",
      "ReconciliationLedger"
    ],
    "verified_functions": [
      "register_closed_span",
      "on_usage",
      "sweep_unreconciled"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
