# In what scenarios must an instance treat OpenTelemetry GenAI span attributes as non-authoritative for token accounting across a resumed agent trace **Date of Record:** 2026 November 22 **Scope:** Applies to agent runtimes that emit OTel GenAI spans (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.request.model`, `gen_ai.tool.call.id`) and later reconcile cost/lineage from the trace store after a checkpoint/resume, retry, or multi-process handoff. Does not apply to single-shot synchronous inference where one span maps one-to-one to one billing event. ## Core Distinction The OTel GenAI semantic conventions define span attributes as *observational metadata about a single model invocation attempt*, not as a ledger of billable or causally-consumed tokens. The convention is explicit that `gen_ai.usage.input_tokens` describes the tokens the provider reported for *that request*. It says nothing about whether those tokens were already counted in a parent span, whether the request was a retry of a failed attempt, or whether a resumed agent re-sent the full context window. The non-obvious boundary is this: **span-level token attributes are per-attempt and additive across the trace, but agent-level token consumption is per-logical-step and non-additive across retries and resumes.** Any instance that sums `gen_ai.usage.*` up a trace tree to reconstruct "tokens this task cost" will systematically over-count whenever a retry, a checkpoint-resume, or a fan-out re-invocation occurred — and the over-count is invisible because every individual span is internally consistent. ## What Was Observed A three-stage agent (plan → retrieve → synthesize) running under a supervisor that checkpoints after each tool call. The runtime emits one `gen_ai.chat` span per model invocation and one `gen_ai.execute_tool` span per tool call, with `gen_ai.tool.call.id` linking them. The supervisor resumes from checkpoint on any tool timeout. Trace of a single logical task that hit one tool timeout: - Span A (`gen_ai.chat`, plan): input=4,120, output=310. Status OK. - Span B (`gen_ai.execute_tool`, retrieve): Status ERROR, `error.type=timeout`. No tokens. - Span C (`gen_ai.chat`, plan, **retry**): input=4,120, output=310. Identical prompt re-sent because the checkpoint captured state *before* the tool call but the model was re-invoked from the pre-tool context. Status OK. - Span D (`gen_ai.execute_tool`, retrieve, retry): OK. - Span E (`gen_ai.chat`, synthesize): input=6,880, output=1,240. A naive reconciler summing `gen_ai.usage.input_tokens` across all `gen_ai.chat` spans reports 4,120 + 4,120 + 6,880 = **15,120 input tokens**. The actual billable input is **11,000** (the retry re-sent the same 4,120-token prefix; the provider charged it twice, but the *agent's* context consumption — what the context-window budget and the distillation ledger care about — is 4,120 + 6,880 once). Worse: the reconciler also cannot tell from the trace alone whether Span C was a retry (re-billed) or an independent parallel branch (legitimately additive), because the OTel GenAI conventions do not mandate a retry/parent-attempt attribute. The only signal is the coincidental equality of `gen_ai.request.model`, input token count, and the shared `gen_ai.tool.call.id` lineage — which is a heuristic, not an invariant. The failure compounds at resume: the checkpoint stored *post-Span-A* state, so the resumed process re-emitted Span C. The trace store now contains two `gen_ai.chat` spans with the same logical step identity but no `gen_ai.retry.attempt` or `gen_ai.step.id` attribute to disambiguate them. `trace_id` differs between the pre-timeout and post-resume processes because the supervisor minted a new trace on resume. ## Condition of Validity The invariant "sum of `gen_ai.usage.input_tokens` over `gen_ai.chat` spans equals logical context consumption" holds **only if** all of the following are true: 1. No span in the trace is a retry of a prior span (no duplicate logical step). 2. No checkpoint/resume boundary re-invoked a model from a pre-checkpoint context. 3. No fan-out/parallel branch re-sent a shared prefix that the provider bills per-request but the agent consumes once. 4. `trace_id` is stable across the entire logical task (no resume-minted trace). In practice condition 4 is the one that silently breaks first, because checkpoint-resume is exactly when a new process (and often a new trace) is created. The moment `trace_id` changes at a resume boundary, span-level summation across the *logical* task is impossible without an out-of-band join key — and the OTel GenAI conventions do not define one. `gen_ai.tool.call.id` links tool spans to their invoking chat span, but there is no `gen_ai.agent.step.id` or `gen_ai.agent.task.id` that survives a resume-minted trace. ## Decision Boundary An instance must treat span-level GenAI token attributes as **non-authoritative for logical consumption** — and must not sum them to reconstruct task cost — whenever any of these is true: - The trace contains two or more `gen_ai.chat` spans sharing the same `gen_ai.request.model` *and* identical `gen_ai.usage.input_tokens` *and* an intervening error span, with no `gen_ai.retry.attempt` attribute. Treat as retry; count once. - A resume boundary is detected (new `trace_id` with a `gen_ai.tool.call.id` that matches a span in a prior trace). Join on tool-call id, do not sum across traces. - `gen_ai.usage.input_tokens` for a chat span exceeds the model's advertised context window divided by the number of concurrent branches — a signal the prefix was re-sent rather than newly consumed. Conversely, span attributes **are** authoritative for: per-attempt provider billing (sum them for cost, because the provider did charge the retry), latency attribution per attempt, and model-selection auditing (`gen_ai.request.model` per span is ground truth for which model served that attempt). The boundary is between *billing reconciliation* (sum spans, accept double-count) and *context/consumption accounting* (dedupe by logical step, which the trace cannot do alone). ## Operational Effect The architecture must carry a **logical-step identity that outlives the trace**. Concretely: the agent runtime must stamp every model invocation with a stable `gen_ai.agent.step.id` (and `gen_ai.agent.task.id`) that is preserved across checkpoint serialization and re-emitted identically on resume, *in addition to* the OTel-mandated attributes. Reconciliation then dedupes on `(task.id, step.id, attempt)` rather than summing spans. Without this, the two failure modes are: (a) cost dashboards over-report by the retry multiplier, and (b) context-budget accounting under-reports remaining window after a resume, because the resumed process believes it has already "spent" the re-sent prefix and truncates prematurely — a silent quality regression where the agent drops retrieved context it actually still had room for. The guardrail is a reconciler that refuses to emit a logical-consumption figure when it detects an un-joined resume boundary, rather than emitting a plausible-but-wrong sum. ```python from dataclasses import dataclass, field class UnjoinableResumeBoundary(RuntimeError): """Raised when span-level GenAI token attributes cannot be safely summed into a logical-consumption figure because a resume boundary created spans with no shared task/step identity.""" pass @dataclass class GenAISpan: trace_id: str span_id: str name: str # "gen_ai.chat" | "gen_ai.execute_tool" model: str | None = None input_tokens: int = 0 output_tokens: int = 0 tool_call_id: str | None = None step_id: str | None = None # non-standard; required for safe dedupe task_id: str | None = None status: str = "OK" def reconcile_logical_input_tokens(spans: list[GenAISpan]) -> int: """Return deduped logical input-token consumption. Refuses to return a number when a resume boundary is present but the spans lack the out-of-band step/task identity needed to join them. """ chat_spans = [s for s in spans if s.name == "gen_ai.chat"] trace_ids = {s.trace_id for s in spans} # Detect a resume boundary: multiple traces linked only by tool_call_id. if len(trace_ids) > 1: tool_ids = {s.tool_call_id for s in spans if s.tool_call_id} linked = any( s.tool_call_id in tool_ids for s in spans if s.trace_id != next(iter(trace_ids)) ) if linked and any(s.step_id is None for s in chat_spans): raise UnjoinableResumeBoundary( f"Resume boundary across {len(trace_ids)} traces linked by " f"tool_call_id but chat spans lack gen_ai.agent.step.id; " f"cannot dedupe retried invocations. Refusing to emit a " f"logical-consumption figure from span attributes alone." ) # Safe path: dedupe on (task_id, step_id); count each logical step once. seen: set[tuple[str, str]] = set() total = 0 for s in chat_spans: if s.task_id is None or s.step_id is None: # Single-trace, no-resume case: attribute is authoritative. total += s.input_tokens continue key = (s.task_id, s.step_id) if key in seen: continue # retry of an already-counted logical step seen.add(key) total += s.input_tokens return total ```