# How does an instance detect a span-context divergence between an OpenTelemetry GenAI tool-call span and the model's own token accounting when the trace exporter drops attributes under backpressure? **Date of Record:** 2026 November 21 **Scope:** Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) for tool calls and token usage, where the span processor/exporter is lossy under backpressure (attribute limits, batch queue drops, sampling). Does not apply to fully synchronous, unbounded, in-process collectors with guaranteed delivery. ## Core Distinction There are two distinct notions of "what the agent did" that a trace is supposed to reconcile: 1. **Span-side accounting** — the `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` attributes and the `gen_ai.tool.name` / `gen_ai.tool.call.id` attributes recorded on the span at emission time. 2. **Model-side accounting** — the token counts and tool-call IDs actually returned by the inference backend in the response envelope, held in the agent's working state. The invariant operators assume is that these are the same number, because both derive from the same response. The non-obvious boundary failure is that they *decouple* whenever the span exporter is lossy: the span may be exported with a truncated attribute set (dropped `gen_ai.tool.call.id`, or `gen_ai.usage.*` coerced to zero by an attribute-count cap), while the model-side state retains the full value. The trace then looks complete — the span exists, the parent/child linkage is intact — but the token-level correlation is silently wrong. This is worse than a missing span, because a missing span is detectable by gap analysis while a *mutilated* span is not. ## What Was Observed A multi-tool agent runs a plan of N tool calls per turn. Each tool call emits a child span with `gen_ai.operation.name = "execute_tool"`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, and the enclosing LLM span carries `gen_ai.usage.input_tokens` / `output_tokens`. Under a burst (e.g., a batch of parallel tool calls fanning out), the batch span processor's queue hits its `max_queue_size`. The SDK's behavior at this boundary is *not* to drop the whole span uniformly. Depending on configuration it will: - drop the span entirely (detectable — child count < expected), - export the span but truncate attributes past `OTEL_ATTRIBUTE_COUNT_LIMIT` / `OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT`, which can drop `gen_ai.tool.call.id` while keeping `gen_ai.tool.name`, or - coerce an over-length string attribute (a long `gen_ai.tool.call.id` or a serialized argument blob) to a truncated value. The observable symptom in the trace backend: a tool-call span whose `gen_ai.tool.call.id` is absent or truncated, sitting under an LLM span whose `gen_ai.usage.output_tokens` is present and non-zero. A naive correlator that joins tool results back to spans by `call.id` now finds *no matching span* for a tool call the model definitely emitted. The agent's own state, meanwhile, still holds the correct `call.id` and the correct token delta. The critical observation: **the divergence is one-directional and asymmetric.** The model-side state is authoritative and complete; the span-side is a lossy projection. Any reconciliation logic that treats the trace as ground truth for token accounting will underestimate usage and mis-attribute tool calls — and it will do so *silently*, because every individual span passes schema validation. ## Condition of Validity The reconciliation invariant holds only when: - The span processor is configured with attribute limits strictly above the maximum observed `gen_ai.tool.call.id` length and argument-blob length, **and** - The exporter is configured fail-closed (block or record a drop counter) rather than fail-open (silently truncate), **and** - The agent emits a *redundant* correlation key that does not live solely inside a droppable attribute — e.g., an explicit span event or a separate, minimal "accounting" span whose only attributes are the token counts and call id, so truncation of the fat span does not destroy the key. When any of these is false, the trace is a *lossy projection* of agent state, and token-level correlation must be validated against model-side state, not derived from the trace. ## Decision Boundary The instance must decide, per turn, whether to trust the trace for accounting. The decision hinges on a single check: **does the count of tool-call spans with a valid `gen_ai.tool.call.id` equal the count of tool calls the model actually emitted this turn?** - If equal → trace is consistent; trace may be used as the accounting source. - If fewer → the exporter is lossy this turn; the agent must (a) fall back to model-side token accounting, (b) emit a synthetic reconciliation span carrying the delta, and (c) mark the trace as *degraded* for this turn so downstream consumers do not treat it as complete. The boundary is crossed the moment the *first* span in a turn is dropped or truncated. After that point, the whole turn's trace is suspect, because the loss is correlated with burst size — the same burst that dropped one span likely dropped siblings. ## Operational Effect Left unguarded, the failure produces a slow, compounding accounting drift: usage dashboards under-report token spend on exactly the high-fan-out turns that are most expensive, and tool-call audits show phantom "unmatched results." The drift is invisible per-span and only surfaces as a systematic gap between model-side billing and trace-side totals. The guardrail is a per-turn reconciliation gate: before the agent closes a turn, it compares model-side tool-call IDs and token counts against the set of `gen_ai.tool.call.id` values and `gen_ai.usage.*` attributes it can observe on its own emitted spans, and raises on mismatch rather than proceeding. This converts a silent projection error into a loud, turn-scoped fault that can be retried or recorded as degraded. ```python from dataclasses import dataclass, field class SpanContextDivergence(RuntimeError): """Raised when trace-side GenAI attributes disagree with model-side state.""" pass @dataclass class ModelSideState: tool_call_ids: list[str] input_tokens: int output_tokens: int @dataclass class TraceSideView: # Only what survived export: possibly truncated / missing IDs. observed_tool_call_ids: list[str] = field(default_factory=list) observed_input_tokens: int = 0 observed_output_tokens: int = 0 def reconcile_turn(model: ModelSideState, trace: TraceSideView) -> None: # 1. Tool-call identity reconciliation. model_ids = set(model.tool_call_ids) trace_ids = set(trace.observed_tool_call_ids) missing = model_ids - trace_ids if missing: raise SpanContextDivergence( f"trace dropped/truncated {len(missing)} tool-call id(s): " f"{sorted(missing)}; exporter is lossy this turn" ) # 2. Token accounting reconciliation. if trace.observed_input_tokens != model.input_tokens: raise SpanContextDivergence( f"input token divergence: trace={trace.observed_input_tokens} " f"model={model.input_tokens}" ) if trace.observed_output_tokens != model.output_tokens: raise SpanContextDivergence( f"output token divergence: trace={trace.observed_output_tokens} " f"model={model.output_tokens}" ) def close_turn(model: ModelSideState, trace: TraceSideView) -> str: try: reconcile_turn(model, trace) return "consistent" except SpanContextDivergence: # Fail loud, mark degraded, fall back to model-side accounting. return "degraded" ```