{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-genai-span-token-accounting-when-a-tool-call-child-span-terminates-after-its-parent-agent-span-has-already-been-expor",
  "title": "Where must an instance reconcile GenAI span token accounting when a tool-call child span terminates after its parent agent span has already been exported",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where tool invocations are emitted as child spans under an agent invocation span, and where span export is asynchronous and non-ordered. Does not apply to synchronous in-process exporters that flush strictly in span-close order, nor to batch pipelines that buffer entire traces before export.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-span-token-accounting-when-a-tool-call-child-span-terminates-after-its-parent-agent-span-has-already-been-expor.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-span-token-accounting-when-a-tool-call-child-span-terminates-after-its-parent-agent-span-has-already-been-expor.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-genai-span-token-accounting-when-a-tool-call-child-span-terminates-after-its-parent-agent-span-has-already-been-expor.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1902,
    "python_slice_tokens": 225,
    "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 attributes of the *invocation* span — the LLM call. Tool-call spans carry `gen_ai.tool.name`, `gen_ai.tool.call.id`, and result attributes, but no token usage of their own. The operational trap is the assumption that a parent agent span's token totals can be ...",
    "what_was_observed": "A retrieval-augmented agent emitted, per turn: one `invoke_agent` span, one or more `chat` spans (the LLM calls), and one `execute_tool` span per tool call. Under normal latency the tool span closed inside the parent's turn and the parent's `gen_ai.usage.*` attributes, computed by summing child `chat` spans at parent close, were correct. Under a specific trigger — a tool call whose backend latency exceeded the agent'...",
    "condition_of_validity": "The reconciliation requirement binds only when all three hold: 1. **Asynchronous tool execution outlives the agent turn.** If tool calls are synchronous and the agent blocks until the tool returns, the parent cannot close before the child, and parent-close aggregation is safe. 2. **Parent attributes are finalized at `on_end`.** If the exporter or processor d...",
    "decision_boundary": "The architect must choose, at instrumentation time, between two mutually exclusive policies: - **Eager finalization (parent carries totals):** acceptable only if the agent guarantees that no descendant span can outlive the parent. This requires the agent to either block on every tool call or to explicitly mark the parent span as \"open for late children\" and ...",
    "operational_effect": "Agents that finalize usage at parent close and run tools asynchronously will undercount tokens on every trace where a tool outlives the turn — typically the slowest, most expensive traces (large retrievals, long code execution, retried external APIs), which are exactly the ones cost attribution cares about most. The error is one-directional (always an underc..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "c23d1f7b5501d0dda44b012ceb1648e0941c2383746c7c5fc30df2fa1a662cd9",
    "verified_classes": [
      "TokenAccountingDivergence",
      "Span"
    ],
    "verified_functions": [
      "reconcile_trace"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "TokenAccountingDivergence"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
