# Which invariants prevent GenAI span attribute collapse when a tool-call lineage re-enters a resumed invocation **Date of Record:** 2026 November 28 **Scope:** Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) across multi-step tool loops where a single logical invocation is checkpointed and resumed, possibly on a different process or model instance. Does not apply to single-shot, non-tool-using completions or to tracing systems that do not correlate spans by `trace_id`/`span_id` lineage. ## Core Distinction A *model invocation event* is a discrete call to the inference endpoint. A *tool-call lineage* is the causal chain of `gen_ai.tool.call` spans that a given invocation emitted and whose results feed the next invocation. The GenAI semantic conventions treat these as orthogonal: invocation spans carry `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`, while tool spans carry `gen_ai.tool.name`, `gen_ai.tool.call.id`, and (recently) `gen_ai.tool.call.arguments`/`result`. The non-obvious failure is that when an agent resumes from a checkpoint, the resumed invocation re-emits tool spans that were already emitted pre-checkpoint, and the *aggregator* — not the emitter — double-counts tokens and orphans lineage because the resumed span's `parent_span_id` points at a new invocation span that has no recorded child relationship to the pre-checkpoint tool spans. The distinction that matters operationally: token accounting is *additive per span* but *non-idempotent under re-emission*, whereas lineage is *relational* and breaks under re-parenting. A guardrail must treat these two invariants separately. ## What Was Observed An agent executing a five-step tool loop checkpointed after step 3 (context-window pressure forced a distillation-and-resume). On resume, the orchestrator replayed the conversation from the distilled summary and re-invoked the model. The model, lacking the original `gen_ai.tool.call.id` values (they were dropped during distillation because the summarizer treated them as noise), re-issued tool calls with *new* generated call IDs for steps 3 and 4. The trace exhibited: - Two `gen_ai.tool.call` spans with identical `gen_ai.tool.name` and near-identical `arguments`, but distinct `gen_ai.tool.call.id`, both parented under *different* invocation spans. - The post-resume invocation span reported `gen_ai.usage.input_tokens` covering the full distilled context, while the pre-checkpoint invocation spans had already reported the raw context tokens — the sum over the trace exceeded the true billed tokens by ~38%. - The lineage walk (following `parent_span_id` from final invocation backward) terminated at the resume boundary: the pre-checkpoint tool spans became unreachable from the final span, appearing as a disconnected subgraph. The failure was silent. No span was malformed; each span individually validated against the semantic conventions. The corruption lived in the *aggregate* invariant that no single span asserts. ## Condition of Validity The double-count and orphan-lineage failure is only possible when three conditions co-hold: 1. **Checkpoint distillation drops call identity.** If `gen_ai.tool.call.id` survives distillation into the resumed context, the model can reference the prior call and the orchestrator can dedupe. The failure requires identity loss at the distillation boundary. 2. **Re-emission is permitted by the loop controller.** If the loop controller enforces "a tool call with a recorded `call.id` in this trace is not re-executed," replay is blocked. The failure requires the controller to treat the resumed context as authoritative rather than the trace. 3. **The aggregator sums token attributes without span-identity dedupe.** If accounting keys on `(trace_id, span_id)` it is correct; if it keys on `(trace_id, gen_ai.operation.name)` or sums naively, it inflates. The invariant that must hold: **for any `trace_id`, the multiset of `gen_ai.tool.call.id` values is unique, and the sum of `gen_ai.usage.*_tokens` over invocation spans equals the true billed total.** Neither is asserted by the emitter; both must be enforced by a reconciler. ## Decision Boundary The boundary is: *does the resume path have access to the pre-checkpoint trace, or only to the distilled context?* - If the resume path can query the trace store by `trace_id`, it **must** reconcile: reject re-emitted tool calls whose `(gen_ai.tool.name, arguments-hash)` matches a pre-checkpoint call, and refuse to emit a new invocation span until the prior lineage is reattached via an explicit `gen_ai.tool.call.id` carried through distillation. - If the resume path is trace-blind (e.g., resumed on a fresh host with no trace access), it **must not** re-emit under the same `trace_id`; it must open a new root span and record a `gen_ai.resume.from_checkpoint` link attribute pointing at the checkpoint artifact, so downstream reconciliation can join the two traces rather than silently merging them. The anti-pattern is the middle case: trace-blind resume that reuses the original `trace_id`. That is precisely the configuration that produces double-counted tokens and orphaned lineage, and it is the default in most checkpoint/resume frameworks because reusing the trace ID "keeps the trace together." ## Operational Effect Enforcing the invariant changes three things: 1. **Token accounting becomes a reconciliation, not a sum.** The aggregator must dedupe invocation spans by `(trace_id, gen_ai.tool.call.id)` lineage and cross-check against provider billing. Divergence > a small epsilon is a signal that a resume re-emitted. 2. **Distillation must preserve call identity.** The summarizer that compresses context at checkpoint must carry `gen_ai.tool.call.id` and an arguments hash forward as first-class fields, not as prose. Dropping them is the root cause. 3. **Resume becomes a lineage event, not a fresh invocation.** A resumed invocation span must carry a link to the checkpoint and either reattach to the prior tool spans or explicitly declare a new root. Silent re-parenting is the failure mode. The guardrail is a pre-emit check in the loop controller: before emitting a tool span, verify its `call.id` is not already present in the trace; before emitting an invocation span on a resumed context, verify the resume link is set. Both are cheap; both are routinely omitted. ```python import hashlib from dataclasses import dataclass, field class GenAILineageInvariantError(RuntimeError): """Raised when a resumed invocation would violate tool-call lineage uniqueness or double-count token usage within a single trace_id.""" pass @dataclass class ToolCall: call_id: str name: str arguments: str def arg_hash(self) -> str: return hashlib.sha256(self.arguments.encode()).hexdigest() @dataclass class InvocationSpan: span_id: str trace_id: str input_tokens: int output_tokens: int tool_calls: list[ToolCall] = field(default_factory=list) resumed_from_checkpoint: bool = False resume_link: str | None = None class TraceReconciler: """Enforces the two GenAI aggregate invariants: (1) gen_ai.tool.call.id unique per trace_id; (2) token sums equal true billed total (no re-emission double-count). """ def __init__(self) -> None: self._seen_call_ids: dict[str, set[str]] = {} self._seen_arg_hashes: dict[str, set[str]] = {} self._billed_tokens: dict[str, int] = {} def admit_invocation(self, span: InvocationSpan) -> None: trace = span.trace_id self._seen_call_ids.setdefault(trace, set()) self._seen_arg_hashes.setdefault(trace, set()) if span.resumed_from_checkpoint and not span.resume_link: raise GenAILineageInvariantError( f"resumed invocation {span.span_id} in trace {trace} " f"lacks gen_ai.resume.link; lineage would orphan" ) for call in span.tool_calls: if call.call_id in self._seen_call_ids[trace]: raise GenAILineageInvariantError( f"duplicate gen_ai.tool.call.id {call.call_id} in " f"trace {trace}; re-emission would double-count" ) if call.arg_hash() in self._seen_arg_hashes[trace]: raise GenAILineageInvariantError( f"re-emitted tool call {call.name} (arg_hash " f"{call.arg_hash()[:12]}) in trace {trace}; " f"checkpoint distillation dropped call identity" ) self._seen_call_ids[trace].add(call.call_id) self._seen_arg_hashes[trace].add(call.arg_hash()) self._billed_tokens[trace] = ( self._billed_tokens.get(trace, 0) + span.input_tokens + span.output_tokens ) def assert_billed_total(self, trace_id: str, provider_total: int, epsilon: int = 0) -> None: observed = self._billed_tokens.get(trace_id, 0) if abs(observed - provider_total) > epsilon: raise GenAILineageInvariantError( f"trace {trace_id} token sum {observed} diverges from " f"provider billed {provider_total} by {observed - provider_total}; " f"suspected resume re-emission" ) ```