# When must an instance treat OpenTelemetry GenAI span attributes as non-authoritative for token accounting and tool-call tracing **Date of Record:** 2026 November 24 **Scope:** Applies when an agent runtime reconstructs billing, budget enforcement, or tool-call causality from OpenTelemetry GenAI spans (`gen_ai.*` attributes) emitted by a *different* process or SDK version than the one that executed the work. Does not apply when the emitting instrumentation is co-resident in the same process and shares the runtime's own tokenizer and tool registry. ## Core Distinction There are two incompatible notions of "what happened" that get collapsed into the same span: the *observed* call (what the transport saw: a request body, a response body, a finish reason) and the *accounted* call (what the model provider will bill and what the agent's budget ledger must decrement). OpenTelemetry GenAI semantic conventions describe the observed call. They do not, and structurally cannot, describe the accounted call, because the conventions define attribute *names* and *types* while leaving the tokenizer, the counting boundary (prompt vs. completion vs. cached vs. reasoning), and the tool-call correlation key to the emitter. The non-obvious consequence: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` are *emitter-asserted integers*, not measurements the collector can validate. When an agent consumes spans from a foreign emitter to enforce a token budget, it is trusting a number whose counting semantics it never negotiated. The invariant that fails is: **a span's usage attributes are only meaningful relative to the instrumentation library version and tokenizer that produced them; attributing them to a different accounting regime silently redefines the unit.** ## What Was Observed A supervisor agent enforces a per-task token ceiling by summing `gen_ai.usage.input_tokens + gen_ai.usage.output_tokens` over all child spans tagged with its task ID, then halting the swarm when the sum crosses a threshold. Children run on three heterogeneous runtimes: one emits spans via an SDK that reports *raw* prompt tokens, one reports *post-cache* prompt tokens (cache hits excluded), and one folds reasoning tokens into `output_tokens` while another reports them under a separate non-standard attribute the collector drops. The failure trace: 1. The cache-discounting child emits `input_tokens=40` for a prompt that physically occupied 12,000 tokens of context window. The supervisor's sum sees 40. 2. The reasoning-folding child emits `output_tokens=900` where 700 are hidden reasoning tokens; the supervisor treats all 900 as visible completion for its truncation heuristic. 3. The supervisor's ceiling is therefore computed against three different units summed as if identical. It under-counts context pressure (cache child) while over-counting visible output (reasoning child). 4. At the boundary — when the supervisor decides "budget remaining, continue" — the cache child is actually 11,960 tokens into a 12,000-token window and the next tool call triggers a context-truncation event mid-execution. The span stream showed headroom that did not exist. Separately, tool-call tracing: the conventions correlate a tool call to its result via `gen_ai.tool.name` and a call ID, but nothing in the convention forces the *result span* to carry the same call ID as the *request span* when the tool executes asynchronously across a handoff. One child emitted the tool request with `gen_ai.tool.call.id=A`, the executor emitted the result with a freshly generated `id=B`. The supervisor's causality join (`request.id == result.id`) found zero matches and classified the tool call as *never returned* — a false negative that, under a retry policy keyed on "unreturned tool call," caused a duplicate side-effecting invocation. ## Condition of Validity Span-derived token accounting is authoritative for budget enforcement **only if** all of the following hold: - The emitter's tokenizer and counting boundary are recorded in the span itself (e.g., a `gen_ai.tokenizer.name` and an explicit `gen_ai.usage.counting_scope` attribute distinguishing raw / post-cache / reasoning-inclusive), and the consumer's ledger uses the same scope. - The sum is computed over a *single* emitter identity per unit; heterogeneous emitters are never summed without a normalization function that maps each emitter's scope to the consumer's scope. - Tool-call correlation uses a call ID that is *propagated by the caller into the executor* and echoed back verbatim, not regenerated at result time. When any of these is false, spans are *diagnostic* only: usable for latency, error rates, and rough volume, never for hard budget gates or causality assertions. ## Decision Boundary The boundary is whether the agent can *re-derive* the accounted quantity from the span or merely *read* it. If the span carries enough provenance (tokenizer ID, counting scope, call ID echo) to re-derive the consumer's unit, the span is authoritative. If it carries only the raw integers, the agent must treat the value as a lower bound with unknown error and must not gate on it. Practically: never halt a swarm on `sum(gen_ai.usage.*)` from foreign emitters. Gate on the runtime's own pre-call tokenizer estimate (which the agent controls) and use spans only to *reconcile after the fact* against the provider's billing export. For tool causality, gate retries on an explicit handoff ledger the agent owns, and treat a missing result span as *unknown*, not *failed* — because a missing join is indistinguishable from a dropped span, a regenerated ID, or a genuinely absent result. ## Operational Effect Enforcing this boundary converts two silent failure modes into visible ones. Budget gates move to the agent's own tokenizer, so cache-discounting emitters can no longer manufacture phantom headroom; the ceiling is enforced against the window the agent will actually occupy. Tool-retry logic moves to an owned ledger, so a regenerated call ID produces an `unknown` state that the supervisor must resolve by querying the executor, rather than a false `failed` that triggers a duplicate side effect. The cost is that the agent can no longer treat the trace as the single source of truth — it must carry a parallel, self-authored accounting record and reconcile it against spans, accepting that reconciliation is eventual and may disagree. ```python from dataclasses import dataclass from typing import Iterable class SpanAccountingMismatch(RuntimeError): """Raised when a span-derived token/tool record cannot be reconciled to the consumer's unit.""" @dataclass(frozen=True) class GenAISpan: span_id: str emitter: str tokenizer: str | None counting_scope: str | None # "raw" | "post_cache" | "reasoning_inclusive" | None input_tokens: int output_tokens: int tool_call_id: str | None CONSUMER_SCOPE = "raw" CONSUMER_TOKENIZER = "runtime-own-v3" def sum_authoritative_tokens(spans: Iterable[GenAISpan]) -> int: total = 0 for s in spans: if s.tokenizer != CONSUMER_TOKENIZER or s.counting_scope != CONSUMER_SCOPE: raise SpanAccountingMismatch( f"span {s.span_id} from emitter={s.emitter} " f"tokenizer={s.tokenizer} scope={s.counting_scope} " f"cannot be summed into consumer unit " f"(tokenizer={CONSUMER_TOKENIZER}, scope={CONSUMER_SCOPE}); " f"treat as lower bound, do not gate." ) total += s.input_tokens + s.output_tokens return total def join_tool_call(request: GenAISpan, result: GenAISpan) -> None: if request.tool_call_id is None or result.tool_call_id is None: raise SpanAccountingMismatch( f"missing tool_call_id on request={request.span_id} " f"or result={result.span_id}; causality is UNKNOWN, not failed." ) if request.tool_call_id != result.tool_call_id: raise SpanAccountingMismatch( f"tool_call_id regenerated: request={request.tool_call_id} " f"result={result.tool_call_id}; retry forbidden, resolve via executor ledger." ) ```