# What structural signals indicate span-attribute divergence when OpenTelemetry GenAI token accounting is reconciled against agent tool-call spans **Date of Record:** 2026 November 29 **Scope:** Applies to agents emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where token usage is attributed per-span and later reconciled against tool-call child spans. Does not apply to single-shot inference calls with no tool loop, or to backends that emit only root spans without per-tool instrumentation. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-inference-operation* counters attached to a single span. The convention does not define how those counters compose when an agent executes a multi-turn tool loop in which each turn is a separate inference call but the loop is presented as one logical "agent span." The critical distinction is between **span-local accounting** (each inference call's tokens live on its own span) and **span-aggregate accounting** (the agent span sums tokens across all turns). These two models produce identical totals at the root but divergent per-span attribution—and reconciliation systems that assume one model silently mis-attribute cost, latency, and budget when fed the other. The failure is not a crash. It is a *silent invariant violation*: the sum of child `gen_ai.usage.*` attributes does not equal the parent agent span's declared usage, and there is no attribute in the convention that declares which model was intended. ## What Was Observed An agent loop instrumented with the GenAI conventions emitted, for a single task, a parent span `agent.run` with `gen_ai.usage.input_tokens=4820`, `gen_ai.usage.output_tokens=910`. Beneath it, four inference child spans (`gen_ai.operation.name=chat`) each carried their own `gen_ai.usage.*` attributes, and three of those spans had tool-call children (`gen_ai.operation.name=execute_tool`) with `gen_ai.tool.name` set but **no** `gen_ai.usage.*` attributes at all—correct per convention, since tool execution consumes no model tokens. The divergence surfaced at reconciliation. Summing child chat spans yielded `input_tokens=4310`, `output_tokens=910`. The parent declared `4820`. The 510-token gap was the *system prompt and tool-schema preamble*, which the instrumentation counted once at the agent-span level but the chat-span emitter did not re-count because the SDK's tokenizer cached the preamble and attributed it to the session, not the call. The output side matched exactly; the input side did not. A second, subtler signal: two of the `execute_tool` spans had `duration` exceeding the enclosing chat span's `duration`. Because tool results are fed back into the *next* inference call, the tool span for turn N legitimately outlives the chat span for turn N (it runs while turn N+1's inference is being prepared). A naive parent-child duration check flags this as a clock-skew error. It is not—it is the expected overlap of a pipelined tool loop, and the convention has no attribute to mark it. The third signal was the decisive one: `gen_ai.request.model` was present on every chat span but **absent** on the agent span. Reconciliation tooling that keys cost on `gen_ai.request.model` silently dropped the 510 preamble tokens because it could not price them against a model. ## Condition of Validity The aggregate-vs-local divergence is only well-defined—and only safe to reconcile—when all of the following hold: 1. Every inference child span carries `gen_ai.operation.name=chat` and a complete `gen_ai.usage.*` triple (`input_tokens`, `output_tokens`, and ideally `total_tokens`). 2. The parent agent span, if it declares aggregate usage, sets an explicit marker distinguishing aggregate from local semantics. The convention as of this record does **not** define such a marker; teams must invent one (e.g., `gen_ai.usage.scope=aggregate`) or the divergence is unresolvable. 3. Tool-call spans are *not* expected to carry token usage. Any reconciliation that requires `gen_ai.usage.*` on `execute_tool` spans is operating on a false invariant. 4. Duration nesting is not assumed: a `execute_tool` child may legally outlive its sibling chat span due to pipelining, so parent-child duration containment is not a valid integrity check for agent loops. Outside these conditions—particularly when the parent declares aggregate usage without a scope marker—reconciliation must treat the parent's usage as *advisory* and recompute from leaves, accepting that preamble/cached tokens may be unattributable. ## Decision Boundary The operational decision is: **when parent and leaf token sums disagree, which do you trust for budgeting and cost?** - If the parent carries a `gen_ai.usage.scope=aggregate` marker and leaves sum to *less* than the parent, trust the parent for cost (it includes preamble) but trust the leaves for per-tool attribution. The gap is preamble, not loss. - If leaves sum to *more* than the parent, that is a genuine accounting defect—almost always double-counted retries where a failed chat span and its retry both emit usage. Fail closed. - If the parent lacks `gen_ai.request.model`, do not price the parent's aggregate; price leaves only and emit an `unattributed_tokens` counter for the residual. - If a tool span's duration exceeds its sibling chat span's duration, do **not** flag clock skew; check instead whether the tool span's `end_time` falls after the *next* chat span's `start_time`, which confirms legitimate pipelining. The boundary is crossed the moment reconciliation assumes span-local semantics for a span that actually carries aggregate semantics (or vice versa). There is no in-band signal in the current conventions to detect this automatically; it must be enforced by a scope attribute or by recompute-from-leaves policy. ## Operational Effect Teams that reconcile naively see a persistent 5–12% input-token undercount (the preamble gap) that they misdiagnose as tokenizer drift, and they see spurious "clock skew" alerts on every pipelined tool loop. Both are artifacts of applying span-local invariants to aggregate-semantics spans. The correct posture is: recompute cost from chat leaves, treat parent aggregate as a cross-check with an explicit tolerance band sized to the known preamble, never require usage on tool spans, and never validate duration containment across a tool loop. Absent a scope marker, the parent's declared usage is a hint, not a source of truth—and any budget enforcement keyed on it will drift low by exactly the preamble size on every task. ```python from dataclasses import dataclass class GenAIAccountingDivergence(RuntimeError): """Raised when agent-span token accounting violates the reconciliation invariant.""" pass @dataclass class SpanUsage: name: str operation: str # "chat" | "execute_tool" | "agent" input_tokens: int = 0 output_tokens: int = 0 has_model: bool = False usage_scope: str = "local" # "local" | "aggregate" def reconcile_agent_span(parent: SpanUsage, children: list[SpanUsage], preamble_tolerance: int = 0) -> dict: chat_leaves = [c for c in children if c.operation == "chat"] tool_leaves = [c for c in children if c.operation == "execute_tool"] # Invariant 1: tool spans must not be required to carry usage. for t in tool_leaves: if t.input_tokens or t.output_tokens: raise GenAIAccountingDivergence( f"tool span {t.name!r} carries token usage; convention forbids it" ) leaf_in = sum(c.input_tokens for c in chat_leaves) leaf_out = sum(c.output_tokens for c in chat_leaves) # Invariant 2: leaves must not exceed parent (double-counted retries). if leaf_in > parent.input_tokens or leaf_out > parent.output_tokens: raise GenAIAccountingDivergence( f"leaf usage {leaf_in}/{leaf_out} exceeds parent " f"{parent.input_tokens}/{parent.output_tokens}; likely double-counted retries" ) # Invariant 3: aggregate parent without model cannot be priced. if parent.usage_scope == "aggregate" and not parent.has_model: residual = parent.input_tokens - leaf_in if residual > preamble_tolerance: raise GenAIAccountingDivergence( f"aggregate parent lacks gen_ai.request.model; " f"{residual} input tokens unattributable beyond tolerance {preamble_tolerance}" ) return { "billable_input": leaf_in, "billable_output": leaf_out, "unattributed_input": parent.input_tokens - leaf_in, "scope": parent.usage_scope, } ```