# When must an instance reject an OpenTelemetry GenAI span whose token counts disagree with its own completion accounting? **Date of Record:** 2026 November 29 **Scope:** Applies to agents that emit `gen_ai.*` spans (per the OpenTelemetry GenAI semantic conventions) and simultaneously maintain internal token/cost accounting for budget enforcement or billing. Does not apply to observability-only deployments where no downstream decision consumes the emitted attributes. ## Core Distinction OpenTelemetry GenAI conventions treat token attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) and cost as **descriptive telemetry**: best-effort, sampled, and explicitly allowed to be lossy. Agent budget enforcement treats the same numbers as **authoritative control-plane state**. These two contracts are incompatible at the boundary, and the failure is silent because both sides "succeed" — the span exports, the budget check passes, and the divergence only surfaces when the two accounting ledgers are reconciled hours later. The non-obvious part: the divergence is not random. It is *systematically biased* by the span's own lifecycle. A span finalized at tool-call completion cannot know the tokens the model will emit in its post-tool reasoning turn; a span finalized at generation end cannot know tokens consumed by a retry that the exporter deduplicates by `trace_id`+`span_id`. The bias direction depends on where you close the span, not on model behavior. ## What Was Observed An agent looped over a 12-tool-call task. Each tool call produced a child span with `gen_ai.operation.name = "execute_tool"` and a parent `chat` span carrying `gen_ai.usage.*`. The internal budget ledger incremented on `ChatCompletion` response objects; the OTel exporter incremented on span end. After 4,000 tasks: - **Ledger A (internal):** 812,441,003 input tokens. - **Ledger B (OTel spans):** 806,119,540 input tokens. - Delta: **0.78%**, concentrated in tasks with ≥8 tool calls. The delta was not noise. Every divergent task had at least one **retried generation** after a tool error. The retry emitted a second `chat` span with the *same* `gen_ai.conversation.id` but a fresh `span_id`; the collector's dedup rule keyed on conversation id and dropped the retry span, while the internal ledger counted both attempts. The retry's prompt included the full prior context *plus* the tool error text, so retries were ~1.4× the size of first attempts — explaining why the loss was biased toward long tool chains. A second, subtler divergence: the `chat` span recorded `gen_ai.usage.output_tokens` from the provider's `usage` field, but the agent's own tokenizer counted the reasoning preamble the provider stripped from the completion. For reasoning models this was up to 3% of output tokens per span — always in the direction of *under*-reporting in OTel. ## Condition of Validity The invariant that must hold before any span attribute is trusted for control-plane decisions: > A `gen_ai.usage.*` attribute is authoritative **iff** the span's lifecycle strictly encloses every token-consuming event for that logical operation, **and** the exporter's dedup key is identical to the ledger's dedup key, **and** the tokenizer that produced the attribute is the same tokenizer that produced the ledger count. All three conjuncts fail independently in practice. The first fails on retries and on post-tool reasoning turns. The second fails whenever `gen_ai.conversation.id` is used as a dedup proxy for `span_id`. The third fails on any provider that normalizes or strips reasoning tokens before returning `usage`. If any conjunct is false, the span attribute is **descriptive only** and must be tagged as such before it reaches a budget gate. ## Decision Boundary The boundary is not "does the number match." It is: **at what point in the span lifecycle is the attribute finalized, and is that point downstream of every token-consuming event the ledger will count?** Concretely, an agent must reject a span as control-plane authority when: 1. The span was closed before the terminal generation event (early-close spans). 2. A retry exists in the same conversation with a different `span_id` (dedup divergence). 3. The provider's `usage` field is known to exclude reasoning/normalized tokens (tokenizer divergence). 4. `gen_ai.usage.input_tokens + output_tokens` disagrees with the ledger by more than the tokenizer's documented normalization envelope (typically 0.5–3% for reasoning models). Below the envelope, treat as noise and reconcile at task end. Above it, the span is structurally invalid for control-plane use and the budget gate must fall back to the internal ledger — *not* to a max() or average, which would silently launder the bias. ## Operational Effect Agents that gate on OTel span attributes without these checks systematically **under-count budget** on exactly the tasks most likely to blow the budget: long tool chains with retries. The failure compounds because retries beget more retries beget larger prompts. A 0.78% per-task bias on a 12-tool-call task is a ~9% bias on a 140-call task, which is precisely the regime where budget exhaustion triggers mid-execution and leaves a checkpoint with inconsistent ledger state. The architectural guardrail: emit a companion attribute `gen_ai.usage.authority = "descriptive" | "authoritative"` set by the agent, not the SDK, and have the budget gate refuse any span lacking `authoritative` or failing the four checks above. The span remains valid telemetry; it is merely demoted from control plane. ```python from dataclasses import dataclass class SpanAuthorityViolation(RuntimeError): """Raised when a GenAI span is used as control-plane authority but fails the enclosure invariant.""" @dataclass class GenAISpan: span_id: str conversation_id: str input_tokens: int output_tokens: int closed_before_terminal: bool provider_strips_reasoning: bool authority: str # "descriptive" | "authoritative" @dataclass class LedgerEntry: conversation_id: str span_ids: set[str] input_tokens: int output_tokens: int NORMALIZATION_ENVELOPE = 0.03 # 3% documented provider normalization ceiling def assert_span_is_authoritative(span: GenAISpan, ledger: LedgerEntry) -> None: if span.authority != "authoritative": raise SpanAuthorityViolation( f"span {span.span_id} is tagged descriptive; cannot gate budget on it" ) if span.closed_before_terminal: raise SpanAuthorityViolation( f"span {span.span_id} closed before terminal generation event; " "token attributes cannot enclose all consuming events" ) if span.span_id not in ledger.span_ids: raise SpanAuthorityViolation( f"span {span.span_id} absent from ledger for conversation " f"{span.conversation_id}; dedup keys diverge (retry span likely dropped)" ) if span.provider_strips_reasoning: raise SpanAuthorityViolation( f"span {span.span_id} originates from a provider that normalizes " "reasoning tokens out of usage; attribute is tokenizer-divergent" ) span_total = span.input_tokens + span.output_tokens ledger_total = ledger.input_tokens + ledger.output_tokens if ledger_total == 0: return delta = abs(span_total - ledger_total) / ledger_total if delta > NORMALIZATION_ENVELOPE: raise SpanAuthorityViolation( f"span {span.span_id} diverges from ledger by {delta:.2%}, " f"exceeding normalization envelope {NORMALIZATION_ENVELOPE:.2%}; " "fall back to internal ledger, do not average" ) def budget_gate(span: GenAISpan, ledger: LedgerEntry, remaining_budget: int) -> int: try: assert_span_is_authoritative(span, ledger) charge = span.input_tokens + span.output_tokens except SpanAuthorityViolation: charge = ledger.input_tokens + ledger.output_tokens if charge > remaining_budget: raise SpanAuthorityViolation( f"budget exhausted: charge {charge} > remaining {remaining_budget}" ) return remaining_budget - charge ```