# Under what conditions must an instance treat OpenTelemetry GenAI token-accounting attributes as non-additive across a resumed agent span? **Date of Record:** 2026 December 2 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) and that support mid-task checkpoint/resume or retry of individual tool-call spans. Does not apply to single-shot, non-resumable inference calls with no span retry path. ## Core Distinction The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as per-span counters describing the token cost of *that* model invocation. They are additive across sibling spans because each sibling represents a distinct, non-overlapping invocation. The convention does **not** specify a deduplication key or an idempotency contract for a span that is retried, replayed from a checkpoint, or re-emitted after a partial tool-call failure. The invariant that downstream aggregators silently assume—"sum of `gen_ai.usage.*` over all spans under a trace equals total tokens billed for the task"—holds only under the unstated precondition that every model-invocation span is emitted exactly once. Resumed agent spans violate that precondition, and the token accounting becomes non-additive: the same prompt tokens are counted again on resume because the context window is reconstructed and re-sent, while the span identity (`span_id`) changes, defeating naive dedup by span ID. ## What Was Observed An agent executing a five-step tool-using task was checkpointed after step 3. The checkpoint captured the message history and the last committed span context. On resume, the runtime reconstructed the context window from the checkpoint, re-invoked the model with the full history (to regenerate the pending tool call), and emitted a new `gen_ai.chat` span with a fresh `span_id` but the same `gen_ai.conversation.id` and a *reused* `gen_ai.tool.call.id` for the in-flight tool. The re-sent prompt carried ~11.4k input tokens—nearly the entire history—even though only the tokens for the pending step were logically "new work." The failure trace: 1. Checkpoint at step 3 commits spans S1–S3 with cumulative `input_tokens` = 9,800, `output_tokens` = 1,200. 2. Resume reconstructs history, re-invokes model. New span S3' emitted with `input_tokens` = 11,400 (full history re-sent), `output_tokens` = 300, and the *same* `gen_ai.tool.call.id` as the pre-checkpoint attempt. 3. Aggregator sums `gen_ai.usage.input_tokens` over all spans in the trace: 9,800 + 11,400 = 21,200, roughly double the true logical input cost (~11,400), because S1–S3's input is a prefix of S3'. 4. If the runtime *also* retries the tool call and re-emits S3'' with the same `tool.call.id`, a third 11,400 is added. The double-count is now 3×. The non-obvious part: the *output* tokens are additive (each output is genuinely new generation), but the *input* tokens are **prefix-overlapping** across the resume boundary. The GenAI convention has no attribute expressing "this input is a re-send of a previously counted prefix." There is no `gen_ai.usage.cached_input_tokens` guarantee across all providers, and even where it exists, resumed spans frequently report cache misses because the checkpoint serialization reordered or re-rendered the history, invalidating the provider-side prefix cache. ## Condition of Validity Token accounting via summation over GenAI spans is valid **iff** all of the following hold: - Every `gen_ai.chat` / `gen_ai.tool` span under the trace is emitted exactly once (no retry, no replay, no resume re-emission), **or** - Resumed spans carry a stable dedup key that lets the aggregator identify the re-sent prefix, **and** - The aggregator subtracts the overlapping prefix rather than summing raw `input_tokens`. When the runtime resumes from a checkpoint by reconstructing the full context window and re-invoking the model, the second condition fails by construction: the new span has a new `span_id`, and the reused `gen_ai.tool.call.id` is a *tool* identity, not a *model-invocation* identity, so it cannot serve as the dedup key for the chat span. The invariant is therefore violated whenever resume re-sends history without a prefix-dedup attribute. A runtime that resumes by sending *only* the delta (the pending step's instruction plus a compacted state) preserves additivity; a runtime that resumes by replaying the full window does not. ## Decision Boundary The boundary is whether the resume path re-sends a prefix that was already counted in a committed span: - **Delta-resume (send only pending step + compacted state):** spans are non-overlapping; summation is valid; no special handling needed. - **Full-window resume (re-send history):** the new span's `input_tokens` contains the committed spans' input as a prefix; summation overcounts by exactly the committed input total per resume event. The runtime must either (a) mark the resumed span with a custom attribute such as `agent.resume.replayed_prefix_tokens` and have the aggregator subtract it, or (b) emit the resumed span with `gen_ai.usage.input_tokens` set to the *delta only* (the tokens attributable to the new step) and record the re-sent prefix in a separate non-billing attribute. The choice is forced by a second constraint: if the runtime emits delta-only `input_tokens` on the span but the provider bills the full window, the span no longer reflects provider-side cost, and any cost dashboard built on `gen_ai.usage.*` will under-report. This is the genuine tension—the convention's per-span semantics and the billing reality diverge at the resume boundary, and no single attribute value satisfies both. The operational rule: **span attributes must reflect provider-billed tokens (full window), and the aggregator must deduplicate by prefix, not by span ID.** Emitting delta-only tokens on the span is a silent corruption of the observability contract. ## Operational Effect Downstream consumers of GenAI spans—cost dashboards, token-budget enforcers, rate limiters that read `gen_ai.usage.*` to project remaining budget—will overcount input tokens by the committed-prefix total on every resume. In a long-running agent with frequent checkpointing (e.g., a step budget that forces checkpoint every N tool calls), the overcount compounds: each resume re-sends a growing history, so the overcount per resume grows linearly with task length, and the cumulative overcount grows quadratically. A budget enforcer that halts the agent when summed `input_tokens` exceeds a cap will halt a task that is well within its true budget, producing a spurious `BUDGET_EXHAUSTED` termination on a task that used half its allowance. The agent appears to fail from token-budget exhaustion mid-execution, but the exhaustion is an artifact of non-additive accounting, not of actual consumption. The guardrail is a resume-aware aggregator that, for each `gen_ai.conversation.id`, orders spans by emission and subtracts the maximum previously-committed `input_tokens` prefix from any resumed span's `input_tokens` before summing. The runtime must additionally emit a stable `agent.checkpoint.id` on both the committed span and the resumed span so the aggregator can pair them without relying on `tool.call.id` (which is reused and therefore ambiguous). ```python import dataclasses from typing import Iterable @dataclasses.dataclass class GenAISpan: span_id: str conversation_id: str checkpoint_id: str | None input_tokens: int output_tokens: int is_resume: bool class NonAdditiveTokenAccountingError(RuntimeError): pass def sum_billed_input_tokens(spans: Iterable[GenAISpan]) -> int: """Sum input tokens across GenAI spans, deduplicating replayed prefixes. Raises NonAdditiveTokenAccountingError if a resume span lacks a checkpoint_id, because without it the replayed prefix cannot be identified and the sum would silently overcount. """ by_conversation: dict[str, list[GenAISpan]] = {} for s in spans: by_conversation.setdefault(s.conversation_id, []).append(s) total = 0 for conv_id, conv_spans in by_conversation.items(): committed_prefix = 0 for s in conv_spans: if s.is_resume: if s.checkpoint_id is None: raise NonAdditiveTokenAccountingError( f"resume span {s.span_id} in conversation {conv_id} " f"has no checkpoint_id; replayed prefix is " f"unidentifiable and summation would overcount " f"input_tokens by the committed prefix total" ) # Resumed span re-sends the committed prefix; count only the delta. delta = s.input_tokens - committed_prefix if delta < 0: raise NonAdditiveTokenAccountingError( f"resume span {s.span_id} reports " f"{s.input_tokens} input tokens, less than the " f"committed prefix {committed_prefix}; accounting " f"is inconsistent and cannot be bounded" ) total += delta committed_prefix = s.input_tokens else: total += s.input_tokens committed_prefix = s.input_tokens return total ```