# How does an instance detect silent span-attribute truncation when OpenTelemetry GenAI semantic conventions collide with token-budget accounting at the exporter boundary **Date of Record:** 2026 December 3 **Scope:** Applies to LLM agent runtimes that emit OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.*` attributes, tool-call child spans, `gen_ai.usage.input_tokens`/`output_tokens`) and that reconcile those spans against an internal token budget for admission control or cost gating. Does not apply to pure trace-only observability where no downstream decision consumes the span attributes. ## Core Distinction There are two separate token counts in any instrumented agent, and the GenAI semantic conventions only define one of them cleanly. The **billing count** is what the provider charged: `gen_ai.usage.input_tokens` plus `gen_ai.usage.output_tokens`, recorded on the span that wraps the model invocation. The **budget count** is what the agent's own admission controller decremented when it decided to make the call—computed *before* the call, from a tokenizer estimate, and never reconciled against the span. These diverge, and the divergence is invisible because the span is the only artifact that survives the turn. The non-obvious failure is not that they diverge. It is that when the exporter applies attribute-value limits (the default `SpanLimits`/`AttributeValueLengthLimit` and the per-span attribute count cap), the `gen_ai.usage.*` attributes are *not* privileged. They sit in the same attribute bag as `gen_ai.prompt.*`, tool arguments, and tool results. A long tool result on the same span can push the usage attributes past the attribute-count cap and they are silently dropped—the span still exports, still looks healthy, and the budget reconciler reads a missing value as zero. The span is structurally valid and semantically lying. ## What Was Observed A multi-step agent loop emits one parent span per turn (`gen_ai.operation.name = "chat"`) with child spans for each tool call (`gen_ai.operation.name = "execute_tool"`). The parent carries `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and—because the runtime logs the full prompt for debugging—`gen_ai.prompt.0.content`, `gen_ai.prompt.1.content`, plus the serialized tool schema. The tool child span carries `gen_ai.tool.name`, `gen_ai.tool.call.id`, and the full tool result as an attribute. The trigger condition is precise: a tool returns a result whose serialized length exceeds the exporter's `AttributeValueLengthLimit` (commonly 4096 by default in several SDK configurations). The SDK does not error. Depending on version it either truncates the value in place or, when the per-span attribute count cap (`AttributeCountLimit`, often 128) is also hit because the tool result was chunked into many `gen_ai.tool.result.N` attributes, it drops the *last-inserted* attributes. Attribute insertion order follows code order, and `gen_ai.usage.*` is typically set *after* the tool result is attached to the parent—so usage attributes are the ones evicted. The trace shows a parent span with `gen_ai.operation.name = "chat"`, correct `trace_id`, correct `span_id`, a `status.code = UNSET`, and **no** `gen_ai.usage.*` attributes at all. The downstream reconciler, which sums `gen_ai.usage.input_tokens` across spans to compute turn cost, sums to zero for that turn. The budget ledger and the trace disagree by the entire cost of the turn, and nothing in the span signals the omission. ## Condition of Validity The invariant that must hold for span-based token accounting to be trustworthy is: > For every span with `gen_ai.operation.name = "chat"`, the pair (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) is present, or the span carries an explicit sentinel attribute (`gen_ai.usage.missing = true` or equivalent) indicating the value was not recorded. This invariant is violated whenever usage attributes share a span with unbounded-cardinality or unbounded-length attributes (prompt content, tool results, schemas) and the exporter enforces limits. The condition of validity is therefore not "the SDK is configured correctly"—it is **attribute isolation**: usage attributes must be structurally protected from eviction, either by living on a dedicated child span with no other large attributes, or by being inserted first and the span's other attributes bounded by policy. A second, subtler condition: the reconciler must treat *absence* differently from *zero*. `gen_ai.usage.input_tokens` absent means "unknown"; `= 0` means "measured zero." Conflating them is what turns a dropped attribute into a silent budget leak. ## Decision Boundary The instance must decide, at reconcile time, whether a missing usage attribute is a *transport defect* (retry/repair) or a *legitimate absence* (e.g., a span that genuinely made no model call). The boundary: - **Repair path:** if the span's `gen_ai.operation.name = "chat"` and `gen_ai.request.model` is present but usage attributes are absent, the span is malformed by the invariant above. Do not read it as zero. Quarantine the turn from budget reconciliation and emit a `span_accounting_anomaly` counter. - **Legitimate absence:** a span with no `gen_ai.request.model` and no `gen_ai.operation.name` in the chat namespace is not a model call; absence of usage is expected and reads as zero. - **Ambiguous:** tool child spans never carry usage; their tokens are attributed to the parent chat span. A tool span with usage attributes is itself a conformance violation. The decision boundary cannot be drawn from the span alone if the exporter already stripped `gen_ai.request.model` too—which is why the guardrail must be installed at the *producer* (attribute isolation) rather than the *consumer* (reconciler heuristics). ## Operational Effect Once the invariant is enforced by isolation, the observable behavior changes in a measurable way: the reconciler's turn-cost sum matches the provider's billing statement within the tokenizer's error band, and any residual mismatch is attributable to a *named* anomaly rather than an unexplained gap. Without isolation, the agent's budget controller drifts optimistic over long sessions—each dropped-usage turn under-counts, the controller believes it has headroom it does not have, and the eventual failure is a mid-task hard stop at the provider's rate limit rather than a graceful budget-exhaustion checkpoint. The failure surfaces far from its cause, which is exactly the property that makes it a boundary condition worth recording. ```python class SpanAccountingAnomaly(RuntimeError): """Raised when a chat span violates the GenAI usage-attribute invariant.""" pass USAGE_KEYS = ("gen_ai.usage.input_tokens", "gen_ai.usage.output_tokens") def reconcile_turn(span: dict, *, quarantine: list) -> int: """ Reconcile a single chat span against the token budget ledger. Invariant: a span with gen_ai.operation.name == "chat" and a gen_ai.request.model MUST carry both usage keys, or carry an explicit gen_ai.usage.missing sentinel. Absence is NOT zero. """ attrs = span.get("attributes", {}) op = attrs.get("gen_ai.operation.name") model = attrs.get("gen_ai.request.model") if op != "chat": # Non-chat spans carry no usage by convention; contributes zero. return 0 if attrs.get("gen_ai.usage.missing") is True: quarantine.append(span.get("span_id")) return 0 missing = [k for k in USAGE_KEYS if k not in attrs] if missing and model is not None: # Malformed: a real model call with no usage recorded. The exporter # likely evicted the attributes behind a large tool result. Do not # read as zero; refuse to reconcile and surface the defect. raise SpanAccountingAnomaly( f"span {span.get('span_id')} op=chat model={model} " f"missing usage keys {missing}; attribute eviction suspected" ) return int(attrs.get(USAGE_KEYS[0], 0)) + int(attrs.get(USAGE_KEYS[1], 0)) ```