# In what scenarios must an instance treat a span's `gen_ai.usage.input_tokens` as non-reconcilable with the sum of its recorded `gen_ai.tool` child spans **Date of Record:** 2026 November 25 **Scope:** Applies to any instrumented agent that emits OpenTelemetry GenAI spans across a multi-turn tool-using loop, where parent model-invocation spans and child tool-execution spans are both exported and token accounting is derived by aggregating span attributes. Does not apply to single-shot completions with no tool children, nor to systems that record usage only at the root transaction span. ## Core Distinction The GenAI semantic conventions define `gen_ai.usage.input_tokens` on the model-invocation span as the tokens *the model consumed to produce that response*. They define `gen_ai.tool.name`, `gen_ai.tool.call.id`, and related attributes on tool-execution child spans, but they deliberately do **not** define a token cost for a tool span — a tool call consumes zero model tokens at execution time. The failure arises when an observer (billing reconciler, cost dashboard, or a downstream agent auditing its own budget) treats the parent's `input_tokens` as if it were the *incremental* cost of the turn, when in fact it is the *cumulative* prompt length including the entire replayed history plus every prior tool result. This is not a rounding error; it is a structural double-count that grows with loop depth. The distinction that matters: `input_tokens` is a **snapshot of the prompt at invocation**, not a **delta attributable to the children spawned in that invocation**. The two are only equal when the loop has depth zero. ## What Was Observed An agent looped five turns against a retrieval tool. Each model-invocation span carried `gen_ai.usage.input_tokens` of roughly 1.2k, 2.4k, 3.6k, 4.8k, 6.0k — the prompt grows by the appended assistant message and tool result each turn. A naive reconciler summed the five parent spans and reported 18.0k input tokens for the task. The actual billed input was 18.0k — that part is correct, because each invocation genuinely re-sends the accumulated prompt. The error surfaced on the *tool* side: the reconciler also attributed each tool child span a "share" of its parent's input tokens proportional to tool latency, producing a per-tool cost that summed to 18.0k *again*, and a per-tool attribution that charged the fifth tool call for history it never touched. The dashboard reported 36.0k against an invoice of 18.0k, and the discrepancy was misattributed to a caching bug for two days. A second, subtler observation: when a turn issued **parallel** tool calls (three children under one model span), the semantic conventions place a single `input_tokens` on the parent. There is no convention for splitting it across the three children. Any split is a fabrication. An auditor agent that summed `input_tokens` across *all* spans in the trace — parents and children — got a number that depended on how many children happened to be nested, not on what was billed. ## Condition of Validity Reconciliation of `input_tokens` against child tool spans is valid **only** under the conjunction: 1. Every tool child span is a leaf with no model-invocation descendants of its own (i.e., the tool did not spawn a sub-agent that itself emitted GenAI spans). If a tool internally invokes a model — a common "retriever that reranks with an LLM" pattern — that descendant's `input_tokens` is a *separate* billed quantity and must not be folded into the parent's. 2. The parent span's `input_tokens` is read as the full prompt length for that single invocation, and the reconciler computes task cost as the **sum over invocation spans only**, never over the union of all spans. 3. No span in the trace was sampled out. Tail-sampling that drops a middle invocation span while retaining its parent breaks the sum silently — the parent's `input_tokens` still reflects the dropped child's contribution to history, but the child's own invocation cost vanished from the ledger, undercounting while the tool-attribution still overcounts. When any of these fails, the invariant `task_input = Σ invocation.input_tokens` is void and the reconciler must refuse to produce a per-tool cost rather than emit a plausible-looking wrong one. ## Decision Boundary The boundary is whether the observer is computing **cost** or **attribution**. - For **cost**, aggregate `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` across spans where `gen_ai.operation.name == "chat"` (or the model-invocation equivalent) and the span has no model-invocation ancestor. Deduplicate by `gen_ai.response.id` where the backend may have emitted the same invocation twice on retry. - For **attribution**, do not use token counts at all. Attribute by `gen_ai.tool.call.id` and wall-clock or by explicit per-tool result byte length. Token attribution to a tool is not defined by the conventions and must not be synthesized from parent `input_tokens`. The trigger to raise rather than degrade: if a reconciler encounters a tool child span that *also* carries `gen_ai.usage.input_tokens` (because the tool internally called a model), it is at the boundary where parent and child token domains overlap. Summing them double-counts; ignoring the child undercounts. The only correct action is to partition by `gen_ai.operation.name` and treat the tool-internal model call as its own invocation root. ## Operational Effect Systems that skip this partition exhibit a characteristic signature: reported cost scales super-linearly with loop depth and with fan-out width, while the invoice scales linearly with depth and is flat in fan-out. The divergence is invisible in aggregate (both grow) and only appears when you regress reported cost against `turn_count` versus `turn_count × fan_out`. A reconciler that emits per-tool token attribution will additionally show tools late in a loop appearing "more expensive" than identical early tools, purely because they inherited a longer history. The guardrail is a two-domain ledger: invocation spans form the cost domain (summed, deduplicated by response id), tool spans form the attribution domain (keyed by call id, valued in latency and payload size, never in tokens). Any code path that multiplies a tool span by a parent token count is a defect regardless of how small the resulting number looks. ```python from dataclasses import dataclass class TokenReconciliationError(RuntimeError): """Raised when token accounting crosses the invocation/tool domain boundary.""" @dataclass class Span: span_id: str parent_id: str | None operation_name: str # "chat" | "execute_tool" | ... input_tokens: int | None output_tokens: int | None response_id: str | None tool_call_id: str | None def reconcile_cost(spans: list[Span]) -> int: """Sum input tokens over invocation roots only, deduped by response id. Invocation root = operation 'chat' with no 'chat' ancestor. Tool spans and tool-internal model calls are partitioned out of the cost domain. """ by_id = {s.span_id: s for s in spans} def has_chat_ancestor(span: Span) -> bool: cur = by_id.get(span.parent_id) if span.parent_id else None while cur is not None: if cur.operation_name == "chat": return True cur = by_id.get(cur.parent_id) if cur.parent_id else None return False seen_response: set[str] = set() total = 0 for s in spans: if s.operation_name != "chat": continue if has_chat_ancestor(s): # A tool that internally invoked a model: its own invocation root, # but must not be folded into the outer parent's accounting. continue if s.input_tokens is None: raise TokenReconciliationError( f"invocation span {s.span_id} missing gen_ai.usage.input_tokens; " "cannot reconcile" ) if s.response_id is not None: if s.response_id in seen_response: continue # retry duplicate seen_response.add(s.response_id) total += s.input_tokens return total def attribute_tool_cost(spans: list[Span], parent_input_tokens: int) -> dict[str, int]: """REFUSES to attribute tokens to tool children: not defined by conventions.""" tool_children = [s for s in spans if s.operation_name == "execute_tool"] if tool_children: raise TokenReconciliationError( f"{len(tool_children)} tool child span(s) present; per-tool token " "attribution is undefined. Attribute by gen_ai.tool.call.id and " "payload size instead. Parent input_tokens=" f"{parent_input_tokens} is a prompt snapshot, not a per-child delta." ) return {} ```