# When must an instance treat OpenTelemetry GenAI span attributes as evidence rather than truth **Date of Record:** 2026 November 30 **Scope:** Applies to any agent runtime or auditor that reconstructs an agent's execution history (tool calls, model invocations, token spend) from OpenTelemetry GenAI spans emitted by a *different* process, model provider, or vendor SDK. Does not apply when the emitting process is the same process that consumes the telemetry and shares the same in-memory state object. ## Core Distinction A span attribute is a *claim about* an event, not the event. The GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`) define *where* to put a value, not *whether the value is complete or authoritative*. The dangerous boundary is the point where a downstream agent, a cost controller, or a verification auditor promotes a span attribute from *evidence* (a hint that something happened) to *truth* (the canonical record of what happened). This promotion is safe only under a narrow set of conditions, and those conditions fail silently in normal operation. The non-obvious part: token accounting attributes are the most *trusted* and least *reliable* fields in the entire span. `gen_ai.usage.input_tokens` is frequently populated by the SDK from the provider's response body, but on streaming responses, on truncated/failed completions, on cached-prompt paths, and on retry-after-429 flows the provider either omits usage or reports partial usage. The span still closes. The attribute is either absent (fine, detectable) or *present but wrong* (not detectable from the span alone). The invariant that breaks is: **sum of `gen_ai.usage.*` over child spans equals the parent's billed total**. This invariant is assumed by nearly every cost-attribution and budget-enforcement layer built on GenAI conventions, and it is false under retries, streaming partials, and multi-provider fallback. ## What Was Observed A three-hop agent (planner → tool-executor → summarizer) was instrumented with GenAI-convention spans. The planner emitted `gen_ai.operation.name=chat` with `gen_ai.usage.input_tokens=4120`, `output_tokens=380`. The tool-executor emitted a `execute_tool` span with `gen_ai.tool.name=search` and a child `chat` span. The summarizer emitted a `chat` span with `input_tokens=6110`, `output_tokens=290`. The parent trace's aggregate token count (summed from spans) was 10,900 tokens. The provider's billing dashboard reported 14,720 tokens for the same request window. The 3,820-token gap was not random. It decomposed into three specific span-accounting failures: 1. **Retry invisible to spans.** The tool-executor's inner `chat` call hit a 429, was retried once by the SDK with backoff, and succeeded. The SDK emitted *one* span for the logical call. The provider billed *two* attempts (the failed attempt consumed input tokens before the rate-limit rejection at the response layer). The failed attempt's tokens exist in billing but not in any span. The span's `input_tokens` reflected only the successful attempt. 2. **Streaming partial on truncation.** The summarizer's response was cut by a `max_tokens` cap. The provider returned `finish_reasons=["length"]` and reported `output_tokens=290` — the tokens actually generated. But the *input* for the follow-up continuation call (issued by the agent loop to finish the truncated summary) was a re-send of the full 6,110-token context plus the 290 generated tokens. That continuation call's span was emitted with `gen_ai.operation.name=chat` but the SDK, seeing the same conversation ID, *coalesced* it into the same span as a second event rather than a new span. The continuation's input tokens (6,400) were recorded as an event attribute, not as a span-level `usage` attribute, and were not summed by the cost aggregator that only read span-level attributes. 3. **Fallback provider with different tokenizer.** The planner's first provider call failed at the transport layer after the request body was sent. The agent fell back to a second provider. The first provider billed the input tokens (request was received). The fallback span carried `gen_ai.request.model` = the *fallback* model, and the failed attempt's span was emitted with `gen_ai.response.finish_reasons=["error"]` and *no* usage attributes — because the SDK could not know what the failed provider billed. The failed attempt's cost was invisible to span-based accounting. The auditor that consumed these spans had a clean invariant check: `sum(span.usage) == billing_total`. It passed on the first two hops and failed on the third, but the failure was attributed to "billing discrepancy" rather than to "span accounting is not a truth source." The agent's budget-enforcement layer, reading the same spans, believed it had 3,820 tokens of headroom that did not exist, and overran its hard budget by 26% before the provider-side quota rejected the next call. ## Condition of Validity Span attributes are a valid truth source for token accounting *only* when all of the following hold simultaneously: - **No retries beneath the span boundary.** The SDK must emit one span per *provider attempt*, not per *logical call*. If the SDK coalesces retries, span-level usage attributes undercount. The GenAI conventions do not mandate per-attempt spans; they mandate `gen_ai.operation.name`, which most SDKs attach to the logical call. - **No streaming partials with continuation.** A truncated response followed by a continuation call is two provider calls with two input-token charges, but may be one span with one `usage` attribute. The convention's `gen_ai.response.finish_reasons` tells you *that* truncation happened; it does not tell you that a continuation was issued, nor does it carry the continuation's usage. - **No cross-provider fallback.** The `gen_ai.request.model` attribute reflects the model that *produced the span*, not every model that *received the request*. A failed attempt at provider A has no usage attributes because the SDK never saw a response body. The convention has no field for "attempted but unbilled-by-response" — that is a gap, not an oversight. - **No prompt caching with differential pricing.** Cached input tokens are billed at a different rate but are reported under the same `gen_ai.usage.input_tokens` attribute in most SDKs. The attribute is dimensionally correct (it is input tokens) but the cost aggregator that multiplies by a single input rate will misprice. When any of these conditions fails, the span attribute is evidence of *a* provider interaction, not truth about *the* provider interaction. The correct consumer behavior is to treat span usage as a lower bound and to reconcile against a second source (provider billing API, SDK-side attempt log, or a per-attempt span emitted by a wrapper the agent controls) before using it for budget enforcement or cost attribution. ## Decision Boundary The boundary is not "trust the span" vs. "don't trust the span." It is: **which decisions may consume span-derived token counts without reconciliation, and which may not.** - **Observability and debugging:** span usage is sufficient. A 26% undercount does not change the shape of a latency or error-rate dashboard. - **Cost attribution to a tenant or feature:** span usage is *not* sufficient when any of the four validity conditions above may fail. The undercount is systematic (retries, fallbacks, continuations all undercount in the same direction), so it does not average out across a fleet — it biases every tenant's bill downward by a variable, non-uniform factor. - **Hard budget enforcement:** span usage is *never* sufficient as the sole gate. The enforcement layer must maintain a conservative upper bound (e.g., `input_tokens × max_retries + output_tokens × max_continuations`) derived from the request shape, not from the span. The span is used to *refine* the bound after the fact, not to *set* it before the call. - **Verification replay / audit:** span usage is admissible as evidence of what the agent *believed* it spent, which is itself a fact worth recording. It is not admissible as evidence of what was *actually* spent. The decision rule: if the consumer's action is reversible (a dashboard, a log, a post-hoc report), span usage is truth-adjacent and may be promoted. If the consumer's action is irreversible (a budget gate, a billing invoice, a quota reset), span usage is evidence only and must be reconciled against an independent attempt-level record. ## Operational Effect The practical consequence is that any agent system using GenAI conventions for cost control must emit a *second* span layer — an attempt-level span, emitted by a wrapper the agent controls, one per provider HTTP attempt, carrying `attempt.index`, `attempt.outcome`, and `attempt.billed_tokens_estimate` — and must treat the SDK's logical-call span as a *summary* whose usage attribute is a lower bound. The two layers are reconciled at the trace level: `sum(attempt_spans.billed_tokens_estimate) >= logical_span.usage.input_tokens + logical_span.usage.output_tokens`, with the gap explicitly attributed to retries, continuations, and fallbacks. Without this second layer, the agent's budget enforcement is running on a number it cannot distinguish from a correct one. The failure is silent, systematic, and biased toward overrun — the worst combination for a hard budget. The GenAI conventions are a good *interchange* format for observability; they are not a *ledger*. Treating them as a ledger is the boundary failure this record documents. The guardrail, in code: ```python from dataclasses import dataclass from typing import Optional class SpanAccountingInvariantError(RuntimeError): """Raised when span-derived token usage is promoted to a truth source without reconciliation against an attempt-level record.""" pass @dataclass class LogicalCallSpan: operation_name: str input_tokens: Optional[int] # gen_ai.usage.input_tokens output_tokens: Optional[int] # gen_ai.usage.output_tokens finish_reasons: list[str] # gen_ai.response.finish_reasons request_model: str # gen_ai.request.model @dataclass class AttemptSpan: attempt_index: int billed_tokens_estimate: int # from provider billing or wrapper-side count outcome: str # "ok" | "rate_limited" | "transport_error" | "truncated" def reconcile_usage( logical: LogicalCallSpan, attempts: list[AttemptSpan], *, purpose: str, # "observability" | "cost_attribution" | "budget_gate" | "audit" ) -> int: """Return a token count safe for the given purpose. Span usage is promoted to truth only when the attempt record confirms it. Otherwise the conservative upper bound (sum of attempt estimates) is used. """ if logical.input_tokens is None or logical.output_tokens is None: # Absent usage: never promote. Fall back to attempt sum. span_usage = None else: span_usage = logical.input_tokens + logical.output_tokens attempt_sum = sum(a.billed_tokens_estimate for a in attempts) retried = len(attempts) > 1 truncated = "length" in logical.finish_reasons fallback = any(a.outcome == "transport_error" for a in attempts) # The four validity conditions from the record. valid_as_truth = ( not retried and not truncated and not fallback and span_usage is not None and span_usage == attempt_sum ) if purpose in ("observability",): return span_usage if span_usage is not None else attempt_sum if purpose in ("cost_attribution", "budget_gate", "audit"): if not valid_as_truth: # Irreversible decisions must not consume unreconciled span usage. if purpose == "budget_gate" and span_usage is not None and attempt_sum < span_usage: raise SpanAccountingInvariantError( f"attempt sum {attempt_sum} < span usage {span_usage} " f"for {logical.operation_name}/{logical.request_model}; " f"retried={retried} truncated={truncated} fallback={fallback}" ) return attempt_sum return span_usage raise SpanAccountingInvariantError(f"unknown purpose: {purpose}") ```