# In what scenarios must an instance reconcile OpenTelemetry GenAI span attributes against the actual token ledger when a tool-call event stream is truncated mid-invocation **Date of Record:** 2026 November 23 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic conventions (`gen_ai.*`) for model invocations and tool calls, where spans and events are exported asynchronously and may be dropped or truncated. Does not apply to synchronous, in-process tracing with guaranteed delivery or to non-agent single-shot inference without tool loops. ## Core Distinction The GenAI semantic conventions treat a model invocation as a span bearing `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`, and treat each tool call as either a child span or an event (`gen_ai.tool.name`, `gen_ai.tool.call.id`) attached to the invocation. The convention assumes these are *descriptive* attributes: they record what happened. But agent runtimes increasingly consume their own telemetry as *authoritative state* — for budget enforcement, for replay-based verification, and for resuming a task after a checkpoint. The distinction that matters is between the **token ledger** (the runtime's internal, monotonic accounting of tokens actually spent) and the **telemetry projection** (the exported `gen_ai.*` attributes, which are best-effort and lossy). The failure mode is not "telemetry is incomplete." It is that a truncated tool-call event stream causes the runtime to *reconstruct* a token count from the surviving events, and that reconstruction silently undercounts because the convention does not require that the sum of per-tool-call token attributes equals the parent invocation's `gen_ai.usage.*`. When the exporter drops the tail of the event stream — the most common truncation, since events are emitted in call order and the buffer flushes forward — the runtime sees a coherent-looking but partial invocation and treats it as complete. ## What Was Observed An agent executing a five-step tool loop (search → fetch → parse → validate → summarize) emits one `gen_ai.chat` span per model turn and one event per tool call, each event carrying `gen_ai.tool.name` and a per-call `gen_ai.usage.input_tokens` fragment for the tool's result re-injection. The OTLP exporter has a `max_queue_size` and a batch timeout. Under load, the batch containing events 4 and 5 is dropped after the batch containing the parent span is accepted. The parent span arrives with `gen_ai.usage.output_tokens = 812` (the full turn's generation). The surviving events 1–3 sum to 340 input-token fragments. A resume routine that reads the telemetry projection computes "tokens spent this turn = 812 + 340 = 1152." The true ledger value is 812 + 340 + 190 + 210 = 1552. The runtime resumes with 400 tokens of headroom it does not have, and the next invocation overruns the model's context budget mid-generation, producing a truncated response that the agent then treats as a completed tool result. The non-obvious part: the parent span's `gen_ai.usage.output_tokens` is *correct and complete* because it is written at span end, after the tool loop. Only the per-event fragments are lost. So the corruption is asymmetric — the invocation-level total looks trustworthy while the decomposition is partial, and any invariant of the form "sum of children == parent" is violated not by arithmetic error but by absence. ## Condition of Validity The reconciliation requirement is valid — and the guardrail is mandatory — precisely when all three hold: 1. The runtime derives any budget, checkpoint, or resume decision from exported `gen_ai.*` attributes rather than from its own ledger. 2. Tool-call events are exported through an at-most-once channel (OTLP batch, fire-and-forget span processor) with no sequence number or end-of-invocation sentinel. 3. The convention's per-tool-call token attributes are optional or partial, so their absence is indistinguishable from a zero-token tool call. When any one fails — the runtime trusts its own ledger, or the exporter guarantees delivery, or the convention mandates an explicit `gen_ai.tool.call.count` on the parent span — the failure cannot occur. The dangerous configuration is the common one: default OTel SDK batch processor, default GenAI instrumentation, and a resume path written by someone who reasonably assumed the span was the source of truth. ## Decision Boundary The boundary is whether the runtime can *prove* the event stream is complete before using it. The convention provides no completeness marker, so completeness must be inferred. The safe inference is: an invocation span is complete-as-projection only if the count of observed tool-call events equals an independently recorded expected count. If no such count exists, the projection must be treated as a *lower bound*, never as an equality. Concretely: if `observed_tool_events < expected_tool_calls` (from the runtime's own pre-invocation plan, or from a `gen_ai.tool.call.count` attribute if present), the runtime must refuse to derive budget or resume state from telemetry and must fall back to the ledger. If the ledger is also unavailable (cold resume after process death), the runtime must fail closed — refuse to resume — rather than resume on a partial projection. The decision boundary is therefore: *telemetry may confirm the ledger; it may never substitute for it.* ## Operational Effect Enforcing this invariant changes resume semantics from optimistic to conservative. A resumed task that cannot reconcile its telemetry projection against a ledger now halts and re-plans from the last *ledgered* checkpoint rather than continuing from a truncated event tail. The cost is occasional redundant re-execution of a turn; the benefit is that context-budget overruns and silent tool-result truncation are eliminated, because the runtime never resumes with a token count it cannot bound from below. The guardrail is a single reconciliation check at every checkpoint and resume point: compute the projection, compute the ledger, and require `projection <= ledger` with equality only when event completeness is proven. Any violation is a hard stop, not a warning, because a warning is exactly the signal a probabilistic agent will rationalize past. ```python class TelemetryReconciliationError(RuntimeError): """Raised when a GenAI telemetry projection cannot be reconciled with the token ledger.""" def reconcile_invocation( ledger_tokens: int | None, parent_output_tokens: int, observed_tool_event_tokens: int, expected_tool_calls: int, observed_tool_events: int, ) -> int: """ Return the authoritative token count for an invocation, or raise if the telemetry projection is partial and cannot be trusted for resume/budget. """ if ledger_tokens is None: # Cold resume: no ledger. We may only proceed if the event stream is # provably complete; otherwise fail closed. if observed_tool_events != expected_tool_calls: raise TelemetryReconciliationError( f"No ledger and truncated event stream: " f"{observed_tool_events}/{expected_tool_calls} tool events observed. " f"Refusing to resume on a partial projection." ) return parent_output_tokens + observed_tool_event_tokens projection = parent_output_tokens + observed_tool_event_tokens if observed_tool_events != expected_tool_calls and projection != ledger_tokens: # Truncation detected and projection diverges from ledger: telemetry is # a lower bound only. Use the ledger; never the projection. return ledger_tokens if projection > ledger_tokens: # Projection claims more than was actually spent: accounting bug or # duplicate events. Hard stop. raise TelemetryReconciliationError( f"Projection {projection} exceeds ledger {ledger_tokens}; " f"event stream likely duplicated or misattributed." ) return ledger_tokens ```