# Why does an instance lose tool-call lineage when OpenTelemetry GenAI span attributes are exported asynchronously across a checkpoint boundary? **Date of Record:** 2026 November 29 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for tool calls and token accounting, where the span processor is a batching exporter and the agent checkpoints/resumes mid-task. Does not apply to synchronous simple-span-processor setups with no checkpoint/resume, nor to non-GenAI spans. ## Core Distinction The failure is not "spans get dropped." It is a **causal-parent discontinuity**: the `trace_id`/`span_id` linkage that a downstream consumer uses to reconstruct tool-call lineage is fixed at span *creation* time and carried in the span's immutable `SpanContext`, but the *attributes* that describe token accounting (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) and tool identity (`gen_ai.tool.name`, `gen_ai.tool.call.id`) are attached later during the tool round-trip. When an agent checkpoints between span creation and span end, the resumed instance rehydrates state but not the in-flight `SpanContext`. The resumed instance emits a *new* child span with a fresh `span_id` and a parent of the resumed root — not the original tool-call span. The original span still ends (or times out) in the *dead* process's batch processor. Result: two spans, one orphaned with tool attributes and no children, one with children and no tool attributes, and neither carrying the full token accounting. The lineage graph is not broken by loss; it is broken by *duplication across a process boundary*. ## What Was Observed A tool-invoking agent loop (model call → `execute_tool` span → result → model call) ran under a batching `BatchSpanProcessor` with a 5s export interval. A checkpoint was taken after the model emitted a tool call but before the tool returned. The process was killed (deploy) and resumed from checkpoint in a new process. Trace evidence from the collector: - Original process: `gen_ai.tool.name="search"` span `span_id=A` created, parented to root `R1`. Tool call dispatched. Process exits before `A.end()`. Batch processor flushes on shutdown only spans that were *ended*; `A` is abandoned mid-flight and never exported. - Resumed process: rehydrates conversation and tool-call intent. Creates a fresh root `R2` (the trace context was not in the checkpoint), re-emits the tool call as span `span_id=B` under `R2`, completes it, and attaches `gen_ai.usage.*` for the *second* model call only. - Downstream reconstruction: `R1` has no tool span at all (abandoned), `R2` has the tool span but its `gen_ai.usage.input_tokens` reflects only post-resume tokens. The pre-checkpoint model call's token spend (the prompt that produced the tool call) is attributed to `R1`, which the lineage consumer treats as an unrelated trace. The invariant violated: **token accounting must sum to the same total regardless of where the checkpoint boundary falls.** It does not. Tokens spent generating the tool call land in `R1`; tokens spent consuming the result land in `R2`; the tool span `B` sits in `R2` with no parent linking it to the prompt that requested it. `gen_ai.tool.call.id` was regenerated on resume, so even a join on call-id fails. ## Condition of Validity The duplication-and-orphaning behavior is *bounded* (not silently corrupting) only when all of the following hold: 1. The `SpanContext` (trace_id, span_id, trace_flags) is serialized into the checkpoint, not just the conversation state. If it is, the resumed process can re-parent span `B` under `A`'s context and the lineage survives — provided the collector accepts a child whose parent span never ended. 2. `gen_ai.tool.call.id` is treated as the idempotency key and is *persisted* across the checkpoint, not regenerated. Regenerating it (as observed) severs the only join key that survives the trace split. 3. The batching exporter's shutdown flush is *not* trusted to emit unended spans. `BatchSpanProcessor` force-flushes on `shutdown()`, but only spans that have been `end()`ed are in the queue; an in-flight span is not. 4. Token accounting is reconciled by a downstream aggregator keyed on `gen_ai.tool.call.id`, not on `trace_id`. This is the only reconciliation surface that survives the split. If any of 1–4 fails, the two traces cannot be joined and the accounting is permanently split. The observed run failed 1 and 2 simultaneously, which is why the split was unrecoverable. ## Decision Boundary The architectural choice is: **where does the checkpoint boundary sit relative to span lifetime?** - *Boundary inside a span* (checkpoint taken while a tool span is open): requires `SpanContext` serialization + call-id persistence + a reconciler keyed on call-id. Without all three, do not checkpoint here. - *Boundary at a span edge* (checkpoint only between completed spans, after `end()` and after the batch queue has flushed or been drained): the in-flight-span problem disappears, but you pay a latency cost — you must wait for export or force a flush before checkpointing, which couples agent throughput to collector availability. - *Boundary with a synthetic "resume" span*: on resume, emit a span with `gen_ai.operation.name="resume"` carrying the persisted `gen_ai.tool.call.id` and the pre-checkpoint token counts read from the checkpoint, parented to the original `trace_id`. This reconstructs lineage by *replaying* the accounting rather than relying on the dead process's exporter. The invariant that must hold: **for any checkpoint boundary, the union of exported spans must yield a token total equal to the uninterrupted run, and every tool span must have exactly one parent prompt span.** The decision boundary is whether you enforce this by (a) serializing span context, (b) moving the boundary to span edges, or (c) replaying accounting on resume. Choosing none of these yields the observed silent split. ## Operational Effect Downstream, the lineage consumer sees two traces where there was one logical task. Cost dashboards double-count the tool-call prompt (attributed to `R1` with no tool span, and implicitly re-derived in `R2`'s input tokens) or, worse, *under*-count because the abandoned span `A` was never exported at all. Latency attribution is wrong: the tool's wall-clock time appears in `R2` with no upstream prompt, so tool latency looks like it has no cause. Debugging a slow task leads the operator to the wrong trace. The failure is silent — no error span, no dropped-span counter increment, because the span was never ended, not dropped. The only signal is a *gap*: a `gen_ai.tool.call.id` in `R2` with no matching prompt span in the same trace, and a `R1` with a model call but no tool child. The guardrail: on resume, before emitting any new span, the runtime must assert that the checkpoint contains a serialized `SpanContext` and a persisted `gen_ai.tool.call.id`, and must refuse to resume a task whose checkpoint boundary falls inside an unended span unless the replay path is active. Fail loud at resume, not silent at export. ```python class LineageDiscontinuityError(RuntimeError): """Raised when a resumed agent cannot reconstruct tool-call lineage because the checkpoint boundary fell inside an unended GenAI span and the SpanContext / tool-call id were not persisted.""" pass def resume_task(checkpoint: dict, emit_span) -> None: # Invariant: a checkpoint taken mid-span must carry the span context # and the tool-call id, or lineage cannot be reconstructed. in_flight = checkpoint.get("open_span") if in_flight is not None: ctx = in_flight.get("span_context") call_id = in_flight.get("gen_ai.tool.call.id") if ctx is None or call_id is None: raise LineageDiscontinuityError( "checkpoint boundary inside unended span " f"span_id={in_flight.get('span_id')!r} but " f"span_context={'present' if ctx else 'MISSING'} " f"tool_call_id={'present' if call_id else 'MISSING'}; " "resuming would split token accounting across two traces" ) # Replay path: re-parent the resumed tool span under the persisted # context and reuse the persisted call id as the idempotency key. emit_span( parent_context=ctx, attributes={ "gen_ai.tool.call.id": call_id, "gen_ai.operation.name": "resume", "gen_ai.usage.input_tokens": checkpoint["pre_checkpoint_input_tokens"], "gen_ai.usage.output_tokens": checkpoint["pre_checkpoint_output_tokens"], }, ) # Safe to continue: either no open span, or lineage was replayed. ```