{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-across-a-genai-agent-span-when-tool-call-attributes-are-emitted-after-child-span-closure",
  "title": "Why does an instance lose token accounting across a GenAI agent span when tool-call attributes are emitted after child span closure",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*`) that emit a parent `invoke_agent` span plus child `execute_tool` spans and aggregate `gen_ai.usage.input_tokens`/`output_tokens` at the parent. Does not apply to single-call, single-span LLM wrappers where all attributes are set before span end.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-across-a-genai-agent-span-when-tool-call-attributes-are-emitted-after-child-span-closure.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-across-a-genai-agent-span-when-tool-call-attributes-are-emitted-after-child-span-closure.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-across-a-genai-agent-span-when-tool-call-attributes-are-emitted-after-child-span-closure.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1857,
    "python_slice_tokens": 373,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two distinct notions of \"token accounting\" in a GenAI agent trace, and conflating them is the root of the failure: - **Per-span usage** — the `gen_ai.usage.*` attributes bound to the span whose `gen_ai.operation.name` produced the tokens (an `chat` span, or the `invoke_agent` span that wraps a reasoning turn). - **Rollup usage** — the sum a parent ...",
    "what_was_observed": "A ReAct-style agent (one `invoke_agent` span per user turn, N child `execute_tool` spans, and one child `chat` span per model call) was instrumented so that the parent span computed its rollup in an `on_end` hook by summing `gen_ai.usage.input_tokens` and `output_tokens` across already-exported children. Trace excerpt from a failing run (fields abbreviated): The parent rollup captured steps 1 and 2 (`2048+2072 = 4120...",
    "condition_of_validity": "The rollup invariant holds only under a conjunction that is easy to violate: 1. **Span lifetime containment.** For every child span `c` carrying `gen_ai.usage.*`, `c.end_time < parent.end_time` in the export order observed by the rollup computation. OpenTelemetry does not enforce this; spans are independent objects and a parent may end while children are liv...",
    "decision_boundary": "The correct guardrail is not \"sum harder\" — it is to make the parent's finalization conditional on quiescence of the tool executor, and to treat missing-child-usage as a detectable fault rather than silence. Concretely: - **Track outstanding tool futures in the agent span.** Increment a counter on tool dispatch, decrement on completion callback. The parent's...",
    "operational_effect": "Without the guardrail, the failure is silent and compounding. Each turn with async tool fan-out under-reports cost, so a token-budget agent that should halt at 80% of a 200k window instead halts at ~140k of actual consumption — or, in the observed run, never halts on budget at all and dies on wall-clock. The under-report is non-deterministic: it depends on t..."
  },
  "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": "2d06667094aba7ede8f751e098816f4695e4e25131d04556639e14bc7bc7f778",
    "verified_classes": [
      "IncompleteTokenRollupError",
      "AgentSpanRollup"
    ],
    "verified_functions": [
      "budget_guard",
      "dispatch_tool",
      "complete_tool",
      "finalize"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "IncompleteTokenRollupError"
    ],
    "sample_fail_closed_invariants": [
      "budget guard received a rollup without rollup.complete=true; reconcile against child chat spans before trusting it"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
