# What structural signals indicate span-attribute truncation in OpenTelemetry GenAI agent traces: the `gen_ai.usage.input_tokens` desynchronization boundary **Date of Record:** 2026 November 29 **Scope:** Applies to agent runtimes emitting OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) where tool-call child spans are nested under an agent invocation span and token usage is recorded per-span. Does not apply to flat, non-nested trace topologies or to runtimes that aggregate usage only at the root span. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-span* attributes describing the model call that span represents. They are not a running ledger. This distinction is load-bearing: an agent invocation span that fans out to N tool calls, each of which triggers a sub-model call, produces N+1 spans each carrying their own usage attributes. The invariant that operators assume — that the root agent span's `input_tokens` equals the sum of its descendants — is **not** part of the spec and does not hold under truncation. The non-obvious boundary is this: when an agent runtime truncates or elides tool-call child spans (e.g., to bound trace cardinality, to fit a collector's batch limit, or because a sampling policy drops non-error children), the parent span's usage attributes remain intact but the *sum of visible children* no longer reconciles. The trace is structurally valid, passes schema validation, and silently misreports cost. There is no error span, no dropped-span counter, and no `otel.status_code` deviation. The signal is arithmetic, not semantic. ## What Was Observed A multi-step agent executing a retrieval-augmented task emitted a root span `agent.invoke` with `gen_ai.usage.input_tokens=18422`, `gen_ai.usage.output_tokens=3110`. Beneath it, four `gen_ai.tool.call` child spans were exported. Summing the children's `input_tokens` yielded 9,041 — roughly half the parent. The parent was correct; two child spans had been dropped by a tail-sampling policy that retained only the first two tool calls plus the root. The failure trace: 1. Runtime emits 6 tool-call spans (4 successful, 2 that returned empty results and were flagged `otel.status_code=UNSET`, not `ERROR`). 2. Tail-sampling processor retains spans with `status_code=ERROR` or belonging to the first K spans per trace. The two empty-result spans were neither errors nor within the first-K window. 3. Exported trace contains root + 4 children. Sum of child `input_tokens` = 9,041. Root `input_tokens` = 18,422. Discrepancy = 9,381 tokens, unattributed. 4. A downstream cost-attribution job, which sums leaf `input_tokens` and reconciles against the root, flagged no anomaly because it used the *root* value as ground truth and treated children as a breakdown. The breakdown silently under-summed by 51%. The critical structural signal was not an error. It was the **orphaned token delta**: `root.input_tokens − Σ(children.input_tokens) > 0` with no corresponding child span carrying the difference. A second signal: the root span's `gen_ai.operation.name=invoke_agent` had a `gen_ai.agent.tool_calls` count attribute (a runtime extension, not in the base spec) of 6, contradicting the 4 visible children. The count attribute was the only surviving witness to the elision. ## Condition of Validity The reconciliation invariant `root.input_tokens == Σ(descendant leaf input_tokens)` is valid **only if** all of the following hold: - The trace is complete (no sampling, no tail-drop, no batch overflow). - Every model call is represented by exactly one span carrying usage attributes. - No span aggregates usage from a child that also reports it (double-counting guard). - The runtime does not perform *span merging* — some runtimes coalesce repeated tool calls into a single span with summed usage, which breaks the 1:1 span-to-call mapping and makes per-call attribution impossible even on a complete trace. When any condition fails, reconciliation must be abandoned in favor of **delta accounting**: treat the root's usage as authoritative and treat the unaccounted delta as an explicit `unattributed_tokens` quantity, never as zero. The delta is itself a first-class signal — a persistently nonzero delta across traces indicates an elision policy, not a runtime bug. ## Decision Boundary The boundary between "safe to reconcile" and "must delta-account" is crossed the moment any span in the trace is dropped, merged, or sampled, and the crossing is **not observable from the exported trace alone**. A collector-side decision is required: - If the sampling policy is *head-based* and deterministic per-trace, reconciliation is safe for retained traces (the whole trace is present or wholly absent). - If the policy is *tail-based* or per-span, reconciliation is unsafe and the runtime MUST emit either (a) a `gen_ai.agent.tool_calls` count attribute on the root span, or (b) an explicit `gen_ai.usage.unattributed_tokens` attribute computed at span-close time before sampling runs. The architectural guardrail: **compute the reconciliation delta at the runtime, before the sampler sees the span.** Once a span leaves the runtime, the information needed to detect elision is gone. The root span must carry a self-describing witness (expected child count, or pre-computed unattributed delta) so that any downstream consumer can detect truncation without needing the dropped spans. ## Operational Effect Without the witness attribute, cost dashboards under-report by the elision fraction, and the under-report is *correlated with task complexity* — complex tasks fan out more, get sampled more aggressively, and lose more tokens. The bias is systematic, not random, so it does not average out across a fleet. Agents doing the hardest work appear cheapest per task. With the witness attribute present, the consumer can compute `unattributed = root.input_tokens − Σ(children.input_tokens)` and compare against the declared `expected_child_count`. A mismatch between visible child count and declared count is a hard truncation signal; a nonzero unattributed delta with matching counts is a span-merge signal. Both are actionable, both are detectable from the exported trace, and neither requires recovering the dropped spans. The invariant to enforce: **every agent root span must be self-reconciling.** It must carry enough metadata — expected child count and either summed child usage or an explicit unattributed delta — that a consumer holding only the exported trace can distinguish "complete trace" from "truncated trace" without access to the sampler's configuration. ```python from dataclasses import dataclass, field from typing import Optional class TraceReconciliationError(RuntimeError): """Raised when an agent root span cannot be reconciled against its children.""" pass @dataclass class Span: name: str input_tokens: int output_tokens: int operation: str # e.g. "invoke_agent", "execute_tool" expected_child_count: Optional[int] = None # witness attribute on root spans declared_unattributed: Optional[int] = None # pre-computed at runtime def reconcile(root: Span, children: list[Span]) -> dict: """Reconcile an agent root span against its visible children. Returns a dict with reconciliation status. Raises TraceReconciliationError only when the trace is structurally incoherent (e.g. children claim more tokens than the root). Truncation is reported, not raised, because it is a valid-but-lossy state that downstream cost jobs must handle explicitly. """ if root.operation != "invoke_agent": raise TraceReconciliationError( f"reconcile() requires an invoke_agent root, got {root.operation!r}" ) child_sum = sum(c.input_tokens for c in children) delta = root.input_tokens - child_sum # Structural incoherence: children cannot exceed the parent's reported usage. if delta < 0: raise TraceReconciliationError( f"child input_tokens ({child_sum}) exceed root ({root.input_tokens}); " "possible span merging or double-counted usage" ) visible = len(children) truncated = ( root.expected_child_count is not None and visible < root.expected_child_count ) if truncated and root.declared_unattributed is None: # Truncation detected but runtime omitted the witness delta: unrecoverable. raise TraceReconciliationError( f"root declares {root.expected_child_count} children but only " f"{visible} exported, and no unattributed_tokens witness present; " "elision is undetectable downstream without runtime-side fix" ) return { "complete": not truncated and delta == 0, "truncated": truncated, "visible_children": visible, "expected_children": root.expected_child_count, "unattributed_tokens": delta, "witness_consistent": ( root.declared_unattributed is None or root.declared_unattributed == delta ), } ```