# Where must an instance reconcile OpenTelemetry GenAI tool-call spans against token accounting before emitting a parent agent span? **Date of Record:** 2026 November 23 **Scope:** Applies to services emitting OpenTelemetry GenAI semantic-convention spans (agent, LLM, tool) where a single agent turn fans out into multiple tool calls and a single LLM completion, and where per-span `gen_ai.usage.*` token attributes are derived from provider responses. Does not apply to single-shot, tool-free LLM calls, nor to streaming-only traces where usage is reported asynchronously after span end. ## Core Distinction The GenAI semantic conventions treat token usage as a *per-inference* property attached to the LLM span (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`), while tool calls are modeled as *sibling* spans under the agent span with `gen_ai.operation.name = "execute_tool"` and `gen_ai.tool.name` / `gen_ai.tool.call.id` attributes. There is no normative parent-level rollup attribute: the agent span carries no `gen_ai.usage.*` of its own. The boundary failure is assuming an agent span's aggregate cost equals the sum of its descendant LLM spans — true only when every tool result re-fed into a subsequent completion is accounted as *new input tokens* on that LLM span. When an orchestrator memoizes, truncates, or replays tool output, the sum of descendant usage diverges from billed inference, and a naive-sum agent span silently under- or over-counts. ## What Was Observed In a multi-step agent (planner → tool executor → synthesizer) instrumented per semconv: - The planner LLM span reported `input_tokens=812`, `output_tokens=140` (proposing a tool call). - The tool span carried `gen_ai.tool.call.id` and returned a 9 KB payload. - The synthesizer LLM span reported `input_tokens=812`, `output_tokens=95` — the orchestrator had *re-sent the original prompt* rather than appending the tool result, because a retry path reconstructed the message list from the system prompt. The 9 KB tool output was never billed as input. - A naive `sum(gen_ai.usage.input_tokens)` over descendants yielded 1624, coincidentally matching the invoice (812 + 812), yet the *semantic* mismatch is that the tool output is absent from any input-token count even though the agent's decision depended on it. The non-obvious part: the divergence is invisible when the orchestrator happens to re-send the same prefix. It becomes a hard accounting error when the tool payload is non-trivially large and *is* appended, or when a cache hit (`gen_ai.usage.cache_read_input_tokens`) is present — in which case summing `input_tokens` across children double-counts cached prefixes the provider bills once. ## Condition of Validity A parent agent span may legitimately report a rollup of descendant `gen_ai.usage.*` **only if** all three hold: 1. Every tool result consumed by a downstream completion appears verbatim in that completion's input, so its tokens are counted on the child LLM span. 2. No prompt-cache read spans a boundary between sibling LLM spans such that the same cached prefix is reported as `input_tokens` on more than one child. 3. The orchestrator does not synthesize or replay a message list independently of the traced tool spans (i.e., the `gen_ai.tool.call.id` on the tool span matches the tool-call id referenced in the LLM span's request, where the convention permits it). If any condition fails, the rollup is invalid and must not be emitted as a parent attribute without a reconciliation step. ## Decision Boundary The correct decision at span-emit time is a hard gate, not a heuristic: - If the set of `gen_ai.tool.call.id` values on child tool spans is not a subset of the tool-call ids referenced by the child LLM spans' requests → **do not roll up**; emit the agent span with no `gen_ai.usage.*` and record a `reconciliation.failed` event. - If any child LLM span reports `cache_read_input_tokens > 0` and shares a prefix with a sibling → **do not sum**; the parent must either omit usage or emit a provider-billed total obtained out-of-band, tagged with a non-standard attribute. - Otherwise → summing is permitted, and the parent may carry `gen_ai.usage.input_tokens` / `output_tokens` as the sum, with an explicit `reconciliation.mode = "sum"` marker. ## Operational Effect Emitting an unreconciled rollup produces cost dashboards that drift from invoices by the size of tool payloads — a drift that grows with tool verbosity, not with model usage, so it evades per-model anomaly detection. Downstream budget enforcers keyed on the parent span then either throttle healthy agents or fail to throttle runaway ones. The gate above converts a silent accounting error into an observable `reconciliation.failed` event at the exact span where the invariant broke, preserving trace-local diagnosability. ```python class TokenReconciliationError(RuntimeError): """Raised when an agent span's descendant usage cannot be validly rolled up.""" pass def rollup_agent_usage(agent_span, child_llm_spans, child_tool_spans): referenced_ids = set() for llm in child_llm_spans: referenced_ids.update(llm.get("request_tool_call_ids", [])) emitted_ids = {t["gen_ai.tool.call.id"] for t in child_tool_spans} if not emitted_ids.issubset(referenced_ids): missing = emitted_ids - referenced_ids raise TokenReconciliationError( f"tool calls {sorted(missing)} emitted but absent from any LLM request; " f"rollup invalid for agent span {agent_span.get('span_id')}" ) if any(llm.get("gen_ai.usage.cache_read_input_tokens", 0) > 0 for llm in child_llm_spans) and len(child_llm_spans) > 1: raise TokenReconciliationError( "shared cached prefix across sibling LLM spans; summing input_tokens " "double-counts cache reads" ) return { "gen_ai.usage.input_tokens": sum( l["gen_ai.usage.input_tokens"] for l in child_llm_spans), "gen_ai.usage.output_tokens": sum( l["gen_ai.usage.output_tokens"] for l in child_llm_spans), "reconciliation.mode": "sum", } ```