# What structural signals indicate an OpenTelemetry GenAI span tree has silently desynchronized from its agent's actual token ledger **Date of Record:** 2026 November 25 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes, `gen_ai.client.token.usage` metrics) for multi-turn tool-calling loops, where a single logical agent run fans out into nested `execute_tool` and `chat` spans. Does not apply to single-shot inference calls with no tool loop, nor to runtimes that compute token counts only at the root span after the fact. ## Core Distinction The failure is not "telemetry is missing." It is **telemetry that is present, well-formed, and internally consistent per-span, yet structurally divergent from the agent's true execution ledger** because the conventions encode token usage as a *per-span point-in-time measurement* while the model's context is a *cumulative, monotonically growing object*. OpenTelemetry GenAI conventions model `gen_ai.usage.input_tokens` as the input token count for *that* span's request. But in a tool-calling loop, each successive `chat` span re-sends the entire accumulated history — system prompt, all prior assistant turns, all prior tool results — plus the new tool output. The per-span attribute is *correct* for the request it describes. The sum across spans is *not* the run's true billed input, because it double-counts every prior turn that got re-transmitted. An agent that trusts the span tree as its cost/context ledger will systematically under-estimate context-window saturation (it sees each span at ~2k tokens) while the real context is at 90% of the window and about to truncate. The invariant that silently breaks: **`sum(gen_ai.usage.input_tokens over chat spans) == true billed input`** holds only for independent calls, not for a re-transmitting loop. The conventions provide no attribute to reconstruct the *overlap* between consecutive requests, so the divergence is unrecoverable from the span tree alone. ## What Was Observed A 14-turn tool-calling agent (search → fetch → summarize → verify, repeating) emitted a clean span tree: one root `invoke_agent` span, 14 child `chat` spans, 14 sibling `execute_tool` spans, each with `gen_ai.usage.input_tokens` / `output_tokens` populated and `gen_ai.operation.name` set correctly. A collector-side dashboard summed `input_tokens` across `chat` spans and reported 31,400 input tokens for the run. The actual provider bill for the run was 187,900 input tokens. The ratio (≈6×) matched the average re-transmission depth — expected for a loop where turn *n* re-sends tokens 1..*n*−1. Three structural signals distinguished the desynchronized tree from a healthy one: 1. **`input_tokens` was near-constant across successive `chat` spans** (~2,100–2,400 each) while `output_tokens` grew slightly. In a genuinely cumulative loop, a correct per-request count should *grow monotonically* turn over turn. Flat input counts across a tool loop is the signature of a runtime that reports only the *delta* (the new tool result + new user turn), not the full re-sent context. The convention has no field distinguishing delta-reporting from full-context-reporting. 2. **No `gen_ai.request.max_tokens` or context-utilization attribute was present**, and no span carried the cumulative context size. The span tree therefore had no node from which to derive "how full is the window." The agent's own context-management logic (which *did* track cumulative size) lived in application state, not in telemetry, so the two ledgers diverged with no reconciliation point. 3. **`execute_tool` spans carried no `gen_ai.usage.*` at all** — correct per convention (tools don't consume model tokens) — but the tool *outputs* were the dominant contributor to the next turn's input. The token cost of a tool result was attributed to the following `chat` span's input, severing the causal link between "this tool returned 40k tokens of fetched HTML" and "the next request ballooned." An operator reading the tree could not attribute context growth to the tool call that caused it. ## Condition of Validity The per-span token attributes are a faithful ledger **if and only if** each span's request is independent — i.e., the runtime does not re-transmit prior context. This holds for stateless single-turn calls and for runtimes using provider-side conversation state (where the provider holds history and the client sends only the delta). It fails for the common client-side-history pattern, which is the default for most agent frameworks. The structural tell is whether `input_tokens` is monotonic non-decreasing across sibling `chat` spans within one `invoke_agent` root. Monotonic growth ⇒ cumulative re-transmission ⇒ per-span sum is an over-count of *distinct* tokens and an under-count of *billed* tokens relative to any single-span reading. Flat or delta-sized counts ⇒ the runtime is reporting deltas, and the span tree cannot reconstruct true billed input at all. ## Decision Boundary An agent or monitor must treat the span tree as a **cost ledger** only when it can prove independence; otherwise it must treat it as a **per-request sample stream** and refuse to aggregate. The boundary: - If sibling `chat` spans show monotonic `input_tokens` growth *and* the runtime emits a cumulative-context attribute (or a `gen_ai.request.max_tokens` against which to ratio), aggregate freely — the tree is reconcilable. - If sibling `chat` spans are flat while a tool loop is active, the tree is **delta-reporting** and uninvertible. Do not sum. Escalate to the runtime's application-level context tracker, or mark the run's cost as `unknown` rather than reporting a plausible-but-wrong number. - If neither monotonic growth nor a cumulative attribute is present, the tree is **ambiguous** — the single most dangerous state, because it looks healthy. Treat as delta-reporting by default. The guardrail: a validation pass over each `invoke_agent` subtree that checks the monotonicity invariant and the presence of a cumulative-context signal, and raises rather than emits an aggregate when the invariant is unprovable. ## Operational Effect Without the guardrail, context-window saturation is invisible until truncation fires mid-task — the agent's own tracker knew it was at 92% of the window, but the telemetry said 2.3k tokens per turn, so no alert triggered. The failure surfaces as a sudden mid-loop quality collapse (the model loses early instructions after silent truncation) attributed to "model degradation" rather than to the telemetry blind spot that hid the saturation. With the guardrail, the runtime refuses to publish a summed token metric for delta-reporting loops, forcing cost attribution through the application ledger and forcing context-utilization to be emitted as an explicit attribute on each `chat` span (`gen_ai.request.context_utilization` or equivalent) so that saturation is observable from the tree. The desynchronization becomes a loud `TelemetryLedgerDesync` at emit time instead of a silent 6× cost surprise and an unexplained mid-task truncation. ```python class TelemetryLedgerDesync(RuntimeError): """Raised when a GenAI span subtree cannot be reconciled to a true token ledger.""" pass def validate_agent_token_ledger(chat_spans: list[dict]) -> dict: """ chat_spans: ordered list of dicts, each with keys 'input_tokens' (int), 'has_cumulative_context' (bool), 'tool_loop_active' (bool) Returns an aggregate ONLY if the tree is provably independent or cumulative. """ if not chat_spans: raise TelemetryLedgerDesync("no chat spans; cannot validate ledger") inputs = [s["input_tokens"] for s in chat_spans] monotonic = all(b >= a for a, b in zip(inputs, inputs[1:])) any_cumulative = any(s["has_cumulative_context"] for s in chat_spans) tool_loop = any(s["tool_loop_active"] for s in chat_spans) # Delta-reporting signature: flat input counts across an active tool loop. flat_under_loop = tool_loop and not monotonic and len(set(inputs)) <= 2 if flat_under_loop: raise TelemetryLedgerDesync( f"delta-reporting loop detected: inputs={inputs[:5]}... " "per-span sum is uninvertible; refuse to aggregate." ) if tool_loop and not any_cumulative and not monotonic: raise TelemetryLedgerDesync( "ambiguous tree: no monotonic growth and no cumulative-context attribute; " "treat as delta-reporting by default." ) # Provably cumulative or independent: sum is meaningful. return { "aggregate_input_tokens": sum(inputs), "reconcilable": True, "mode": "cumulative" if monotonic else "independent", } ```