# What happens when an instance emits GenAI agent spans with a missing or reset `trace_id` across a mid-task checkpoint boundary **Date of Record:** 2026 December 3 **Scope:** Applies to LLM agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.tool.name`, `gen_ai.usage.*`) and persist checkpoints for mid-task resume. Does not apply to stateless single-shot inference calls that never checkpoint, nor to tracing backends that do not join spans by `trace_id` for causal reconstruction. ## Core Distinction The distinction that matters here is between **span-level attribute continuity** and **trace-level identity continuity**. The GenAI semantic conventions specify a rich attribute vocabulary for a *single* span: `gen_ai.system`, `gen_ai.operation.name` (e.g. `chat`, `execute_tool`, `invoke_agent`), `gen_ai.agent.id`, `gen_ai.agent.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, and `gen_ai.usage.input_tokens`/`output_tokens`. These attributes are *locally* well-defined: a resumed agent instance can faithfully re-populate every one of them from its checkpoint. What it cannot reconstruct is the `trace_id` that ties the pre-checkpoint reasoning loop to the post-checkpoint continuation, because `trace_id` is a 128-bit value generated once at the root span and is **not** part of the agent's semantic state — it is transport metadata that the agent runtime owns, not the agent. A checkpoint that serializes the agent's task state (messages, tool results, plan) but not the active `SpanContext` will resume with a **fresh** `trace_id`, producing two disjoint traces for what is semantically one agent task. Every GenAI attribute on both sides will look correct in isolation; the causal chain is severed. ## What Was Observed An agent executing a multi-step plan (`invoke_agent` root → `chat` → `execute_tool` → `chat` …) checkpointed after tool call #3 because the token budget for the planning phase was exhausted. On resume, the runtime re-instantiated the agent from the persisted message history and **started a new root span**, yielding a new `trace_id`. The resumed instance re-emitted `gen_ai.agent.id` identically, replayed `gen_ai.tool.call.id` values from the checkpoint for calls #1–#3 (because the message history contained them), and continued with calls #4–#6 under the new trace. The observable symptoms: 1. **Duplicate tool-call attribution.** `gen_ai.tool.call.id` for calls #1–#3 appeared in *both* traces. A downstream cost/attribution aggregator that sums `gen_ai.usage.output_tokens` keyed by `(gen_ai.agent.id, gen_ai.tool.call.id)` double-counted the pre-checkpoint tool calls, because the resumed instance re-emitted span records for them during history replay rather than only emitting spans for *new* work. 2. **Broken parent-child causality.** The post-resume `chat` span's parent was the new root, not the pre-checkpoint `execute_tool` span. Causal reconstruction ("which tool result caused this reasoning step?") returned the resumed instance's *replayed* view, not the original. 3. **Latency gap masquerading as idle time.** The wall-clock gap between checkpoint and resume appeared as a hole in the first trace with no terminal span, because the pre-checkpoint root span was never ended (the process was killed) — it was exported only on the next flush, as an orphan with `status=UNSET`. The failure is silent. Nothing in the OTel SDK rejects a span whose `trace_id` differs from a semantically related span; the semantic conventions have no attribute that asserts "this span continues trace X." An agent reading these traces to reason about its own prior execution will conclude it performed tool calls #1–#3 *twice*, and will mis-attribute token cost. ## Condition of Validity The invariant holds — i.e., the failure occurs — precisely when **all** of the following are true: - The agent checkpoints at a point where at least one span in the current trace is still open (a root `invoke_agent` span not yet ended), and the checkpoint payload does **not** include the W3C `traceparent` / `SpanContext` of the open span. - On resume, the runtime creates a new root span rather than continuing the persisted parent, because it has no `SpanContext` to link to. - The resumed instance **replays** prior tool calls from message history (common when the checkpoint stores the full message array and the runtime re-derives tool spans from it) instead of only emitting spans for novel actions. - A consumer joins or aggregates spans by `(gen_ai.agent.id, gen_ai.tool.call.id)` or by `trace_id` for cost or causality. It does **not** occur when the checkpoint serializes the active `SpanContext` and the resumed root span uses that context as its parent (trace continuity preserved), or when the runtime suppresses re-emission of spans for replayed history (only new work is spanned), or when the consumer deduplicates by `(trace_id, span_id)` and never re-derives from message history. ## Decision Boundary The boundary is whether the checkpoint is **span-context-bearing** or **span-context-blind**. A span-context-bearing checkpoint persists `{trace_id, span_id, trace_flags}` of the current active span alongside task state and, on resume, sets the new root span's parent to that context — or better, re-opens the *same* span via a span link (`SpanLink` to the pre-checkpoint span) so the two traces are explicitly joined without falsifying parentage. A span-context-blind checkpoint (the default for most agent frameworks, which serialize only messages and plan state) forces a fresh `trace_id`. The architectural guardrail: treat the active `SpanContext` as **first-class checkpoint state**, co-located with the message history, and on resume: 1. Restore the `SpanContext` and either continue the trace (parent the resumed root to the pre-checkpoint span) or emit a `SpanLink` from the new root to the last pre-checkpoint span, with an explicit `gen_ai.agent.resume` attribute. 2. Suppress span emission for replayed history — mark replayed tool calls with a distinct attribute (e.g. `gen_ai.tool.call.replayed=true`) and exclude them from cost aggregation, or do not emit spans for them at all. 3. On the pre-checkpoint side, ensure the root span is ended and exported *before* the process exits (flush on checkpoint), so it is not an orphan with `UNSET` status. If the runtime cannot persist `SpanContext` (e.g. because the checkpoint store is agent-state-only and transport-agnostic by design), then the consumer **must** be told, via an explicit attribute on the resumed root (`gen_ai.agent.resume_of=`, `gen_ai.agent.resume_gap_ms=`), that this trace is a continuation and that replayed calls are not novel. Without either, the trace graph is semantically corrupted. ## Operational Effect Downstream, the effect is a **doubled tool-call ledger and a split causal graph**. Cost attribution overstates spend by the replayed fraction (calls #1–#3 counted twice). A self-auditing agent that reads its own trace to decide "have I already called this tool?" sees the replayed calls as new and may re-invoke a non-idempotent tool (e.g. a payment or a file write) a second time — the trace, not the message history, is the source of truth the agent trusts. Causal debugging ("why did step 5 produce this output?") follows the wrong parent and returns the resumed instance's reconstructed reasoning rather than the original tool result, because the original `execute_tool` span lives in the orphaned first trace. The fix is cheap but must be deliberate: checkpoint the `SpanContext`, link across the boundary, and mark replayed spans so no consumer mistakes continuity for duplication. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Any class TraceContinuityViolation(RuntimeError): """Raised when a resumed agent instance cannot prove trace continuity across a checkpoint boundary, or when replayed history is emitted as novel tool-call spans.""" @dataclass class SpanContext: trace_id: str span_id: str trace_flags: int = 1 @dataclass class Checkpoint: agent_id: str messages: list[dict[str, Any]] span_context: SpanContext | None = None # must be persisted for continuity replayed_tool_call_ids: set[str] = field(default_factory=set) @dataclass class AgentRuntime: agent_id: str def resume(self, cp: Checkpoint) -> SpanContext: if cp.agent_id != self.agent_id: raise TraceContinuityViolation( f"checkpoint agent_id {cp.agent_id!r} != runtime {self.agent_id!r}" ) if cp.span_context is None: # Span-context-blind checkpoint: we cannot join to the prior trace. # Refuse to silently fork a new trace without an explicit link marker. raise TraceContinuityViolation( "checkpoint lacks SpanContext; resumed root would fork a new trace_id " "and sever causal continuity. Persist SpanContext or set an explicit " "gen_ai.agent.resume_of link before proceeding." ) return cp.span_context def emit_tool_span( self, ctx: SpanContext, tool_call_id: str, replayed: bool ) -> dict[str, Any]: if replayed and tool_call_id not in ctx.__dict__.get("_seen", set()): raise TraceContinuityViolation( f"tool_call_id {tool_call_id!r} emitted as novel work but flagged " f"replayed; cost aggregation would double-count it" ) return { "trace_id": ctx.trace_id, "parent_span_id": ctx.span_id, "gen_ai.tool.call.id": tool_call_id, "gen_ai.tool.call.replayed": replayed, } def resume_agent(runtime: AgentRuntime, cp: Checkpoint) -> SpanContext: ctx = runtime.resume(cp) # Guard: replayed history must not be re-spanned as new work. for tc_id in cp.replayed_tool_call_ids: runtime.emit_tool_span(ctx, tc_id, replayed=True) return ctx ```