Under what conditions must an instance reject a GenAI span whose `gen_ai.usage.input_tokens` was emitted by a resumed turn, and how does the resumed-turn double-count corrupt cost attribution across the trace?

**Date of Record:** 2026 December 1 **Scope:** Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for LLM turns, tool calls, and agent-turn boundaries, where a turn can be checkpointed and resumed across a process, host, or container boundary. Does not apply to single-shot, non-resumable inference where the span lifecycle is strictly contained within one process invocation.

Core Distinction

The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as **per-span, per-completion facts** — attributes describing what the model actually billed for that specific request/response pair. They are *not* cumulative counters and they are *not* deltas relative to a session. The convention assumes a one-to-one binding between an emitted span and one model invocation.

The failure mode lives exactly on the seam between that assumption and agent checkpoint/resume. When an agent turn is interrupted mid-flight (context-window eviction, tool-call timeout, process restart) and resumed, the resumed executor frequently re-emits the *entire* prior turn's completion as a fresh span — including re-serializing the prompt that already included the partial output. The result is two spans carrying overlapping token counts, both stamped as independent completions, both with the same `gen_ai.response.id` in many implementations because the response was cached or replayed. The trace is internally consistent at the span level and globally wrong at the trace level.

The distinction that matters: **span-level attribute validity is not trace-level accounting validity.** A span can pass every semantic-convention check (required attributes present, types correct, `gen_ai.operation.name` set) while still being a duplicate contributor to the trace's total token spend. The invariant that must hold is not "each span is well-formed" but "each model invocation is attributed to exactly one span, and no span's usage attributes overlap another's."

What Was Observed

A tool-calling agent was instrumented with a `gen_ai.chat` span per LLM call and a `gen_ai.execute_tool` span per tool invocation, with the agent turn wrapped in an `invoke_agent` span. The runtime checkpointed agent state (message history, pending tool call, partial assistant message) to durable storage every N tokens. On a downstream tool timeout, the executor resumed from the last checkpoint and re-issued the LLM call.

The observed span sequence for a single logical turn:

invoke_agent (turn_id=t-4471)
  gen_ai.chat  (span=A, response.id=resp-aa91, input_tokens=8120, output_tokens=340)
  gen_ai.execute_tool (tool=search, status=timeout, error.type=deadline_exceeded)
  -- checkpoint boundary, resume --
  gen_ai.chat  (span=B, response.id=resp-aa91, input_tokens=8120, output_tokens=340)
  gen_ai.execute_tool (tool=search, status=ok)

Three concrete anomalies:

  1. **`response.id` collision.** Span A and span B carried identical `gen_ai.response.id`. The backend's cost aggregator, which deduplicates by response id *within a trace*, silently dropped B — underreporting by exactly one completion. A second aggregator that deduplicated by `(trace_id, span_id)` counted both — overreporting by 8120 input tokens. Two downstream consumers of the same trace disagreed by ~50% on turn cost.
  1. **Checkpoint re-serialization inflation.** The resume path rebuilt the prompt from the checkpointed message history, which already contained the partial assistant output from span A. The rebuilt prompt was *longer* than the original by the length of that partial output, so span B's `input_tokens` (8120) was not even equal to span A's true prompt — it was a different, larger prompt that happened to be reported with a stale count copied from the checkpoint metadata. The reported number was neither the original nor the actual.
  1. **Tool span pairing drift.** The `execute_tool` span for `search` appeared twice under one `invoke_agent`, once with `status=timeout` and once with `status=ok`. The convention has no field expressing "this is a retry of the same logical tool call." A trace consumer computing tool error rates saw a 50% failure rate on a turn that succeeded on first retry.

The critical non-obvious point: **the semantic conventions validated cleanly.** Every span had the required `gen_ai.operation.name`, `gen_ai.system`, and usage attributes. A schema validator passed the trace. Only a cross-span invariant check — "no two `gen_ai.chat` spans in a trace share a `gen_ai.response.id` unless one is explicitly marked as a replay" — caught it.

Condition of Validity

The invariant holds — that is, the trace's token accounting is trustworthy — only when all of the following are true:

If any of these fails, the trace is *structurally valid but semantically corrupt*: it will pass every convention-level check and still produce wrong cost, wrong error rates, and wrong latency distributions.

Decision Boundary

An instance must **reject** (fail closed on) a GenAI span at emission time when:

An instance may **accept with annotation** when the replay marker and stable tool-call id are present; the trace is then self-describing and consumers can reconstruct true cost. The boundary is not "is this span well-formed" but "does this span make a claim about a model invocation that is unique and true." A duplicate span that passes schema validation is more dangerous than a malformed one, because nothing downstream will catch it.

Operational Effect

The practical consequence is that **trace-level token cost is only as reliable as the runtime's resume discipline, and the semantic conventions provide no guardrail for it.** Teams that instrument with the conventions and trust the resulting dashboards will, on any workload with checkpoint/resume, systematically mis-attribute cost — in one direction or the other depending on the aggregator's dedup key. The error is not random; it is a deterministic function of how many turns were resumed and how long the partial outputs were, which means it is *invisible in aggregate averages* and only surfaces when a specific expensive turn is audited line by line.

The guardrail is architectural: the resume path must be the *only* place that knows a span is a replay, and it must either retract the original or emit a linked replacement — never a bare duplicate. The semantic conventions describe spans; they do not describe span *lifecycles across process boundaries*, and that gap is where the accounting rots.

class ReplayedSpanAccountingError(RuntimeError):
    """Raised when a resumed GenAI span would double-count a model invocation."""
    pass


class GenAISpanRegistry:
    def __init__(self):
        self._response_ids = {}   # response.id -> span_id
        self._tool_call_ids = {}  # tool.call.id -> attempt count

    def admit_chat_span(self, span):
        rid = span.attributes.get("gen_ai.response.id")
        replay_of = span.attributes.get("agent.resume.replayed_span_id")

        if rid is None:
            raise ReplayedSpanAccountingError(
                f"span {span.span_id} missing gen_ai.response.id; "
                "cannot prove uniqueness"
            )

        if rid in self._response_ids:
            if replay_of is None:
                raise ReplayedSpanAccountingError(
                    f"span {span.span_id} reuses response.id={rid} "
                    f"(held by {self._response_ids[rid]}) with no replay marker; "
                    "downstream dedup would silently drop one completion"
                )
            if replay_of != self._response_ids[rid]:
                raise ReplayedSpanAccountingError(
                    f"span {span.span_id} claims replay of {replay_of} "
                    f"but response.id={rid} belongs to {self._response_ids[rid]}"
                )
            # Linked replay: usage must be measured, not copied.
            if span.attributes.get("gen_ai.usage.input_tokens") is None:
                raise ReplayedSpanAccountingError(
                    f"replay span {span.span_id} has no measured input_tokens; "
                    "checkpoint-copied counts are not admissible"
                )
            return "accepted_linked_replay"

        self._response_ids[rid] = span.span_id
        return "accepted_primary"

    def admit_tool_span(self, span):
        call_id = span.attributes.get("gen_ai.tool.call.id")
        if call_id is None:
            raise ReplayedSpanAccountingError(
                f"tool span {span.span_id} missing stable gen_ai.tool.call.id; "
                "retries will be indistinguishable from independent calls"
            )
        self._tool_call_ids[call_id] = self._tool_call_ids.get(call_id, 0) + 1
        return self._tool_call_ids[call_id]