# Why does an instance lose tool-call causality when OpenTelemetry GenAI span attributes are mapped from a resumed agent checkpoint? **Date of Record:** 2026 November 26 **Scope:** Applies to autonomous agents that emit OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) and that persist/resume execution state across process boundaries (checkpoint-restore, cold start, or handoff to a sibling worker). Does not apply to single-shot, non-resumable inference calls where the span lifecycle is fully contained in one process and one trace context. ## Core Distinction The GenAI semantic conventions define span attributes that describe a *logical* generative operation (`gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, `gen_ai.conversation.id`). They do **not** define a durable, serializable identity for the *execution attempt* that produced those attributes. The trace/span IDs (`trace_id`, `span_id`) are transport-level identifiers minted at span start and are not part of the persisted agent state. The boundary failure is this: when an agent checkpoints mid-tool-call and resumes in a new process, the resumed instance re-emits a span for the *same logical operation* but with a **freshly minted `span_id` and, if the W3C `traceparent` was not carried in the checkpoint, a fresh `trace_id`**. The GenAI attributes are faithfully re-mapped — model name, token counts, tool name all match — but the parent/child linkage to the pre-checkpoint tool invocation is severed. Observability backends then reconstruct two disjoint traces for what was one causal chain, and any downstream verifier that joins on `trace_id` to audit "did the tool call that produced this answer actually run?" sees an orphan. This is non-obvious because the *attributes* are correct. Attribute-level validation passes. The corruption is purely in the *relational* topology of the span graph, which the semantic conventions treat as out-of-scope metadata. ## What Was Observed A three-step agent (plan → `search_docs` tool call → synthesize) was configured to checkpoint after every tool response. The checkpoint payload serialized the message history, the tool result, and the GenAI attribute dictionary for the in-flight span — but **not** the active `SpanContext` (`trace_id`, `span_id`, `trace_flags`). Trace of the failure: 1. **Process A, step 2 (tool call).** Agent opens span `S1` with `gen_ai.operation.name=execute_tool`, `gen_ai.tool.name=search_docs`, `trace_id=T_a`, `span_id=s1`. The tool returns; the agent writes a checkpoint that includes `{"gen_ai.tool.name": "search_docs", "gen_ai.tool.call.id": "call_7f2", "usage": {...}}` and the raw tool output. `S1` is left open (or ends unflushed if the process is killed). 2. **Process A terminates** (OOM, deploy, or deliberate suspend). `S1` never receives an end timestamp or a `STATUS_CODE_OK`. 3. **Process B resumes.** It reads the checkpoint, sees an in-flight tool call, and — because the GenAI convention says a tool execution is its own span — opens a *new* span `S2` with identical `gen_ai.*` attributes. But `S2` gets `trace_id=T_b`, `span_id=s2`. The synthesizer span `S3` parents to `S2` under `T_b`. 4. **Backend join.** An auditor querying `trace_id=T_a` finds `S1` with no children and no terminal status: an incomplete tool call. Querying `trace_id=T_b` finds `S2`→`S3` with no record of the tool actually executing under that trace. The `gen_ai.tool.call.id=call_7f2` appears **twice** across two traces — a duplicate execution signature with divergent causality. The tell is a specific metric artifact: `gen_ai.client.operation.duration` histograms show a long tail of "incomplete" tool spans (no end event) that correlate 1:1 with checkpoint boundaries, while the resumed trace shows a tool span whose duration is suspiciously near-zero (it only re-emitted attributes; the work already happened in the prior process). ## Condition of Validity The invariant that must hold is: **for any logical GenAI operation that spans a checkpoint boundary, the `(trace_id, span_id)` pair recorded at span-open must be persisted in the checkpoint and re-adopted verbatim on resume — or the operation must be explicitly re-parented via a `SpanLink` carrying the prior `(trace_id, span_id)`.** This holds only when all three conditions are met: - The checkpoint schema includes the serialized `SpanContext` (not just the `gen_ai.*` attribute dict). - The resumed process uses the OpenTelemetry `SpanContext` injection API (`trace.set_span_in_context` / non-recording span adoption) rather than `start_as_current_span`, which always mints a new ID. - The backend honors `SpanLink` for cross-trace causal joins, since a resumed process may legitimately be unable to continue the same trace (e.g., trace context TTL expiry, or the original trace was already exported and closed). It does **not** hold when the agent framework treats the GenAI attribute dictionary as the complete span payload — a common and reasonable-looking design, because the conventions document `gen_ai.*` as "the attributes that describe the operation." The conventions are silent on span-context persistence, and that silence is the trap. ## Decision Boundary The operator must decide, per checkpoint, between two mutually exclusive recovery semantics: - **Continue-in-trace (adopt prior `SpanContext`):** correct when the resumed work is the *same* attempt and the original trace is still open or acceptably extended. Preserves causal join; risks a span whose wall-clock duration spans the entire downtime (misleading latency histograms). - **Re-parent-with-link (new trace, `SpanLink` to prior):** correct when the resumed work is a *retry* or the original trace has been closed/exported. Requires the backend to support link-based joins; otherwise the auditor query breaks silently. The boundary is crossed the moment a checkpoint is written without the `SpanContext`. Past that point, neither option is available — the resumed instance *cannot* continue the trace (it lacks the IDs) and *cannot* link (it lacks the prior IDs), so it defaults to a fresh, orphaned trace. The guardrail must fire **at checkpoint-write time**, not at resume time. ## Operational Effect Left unguarded, the failure produces three downstream effects that are individually plausible and collectively corrosive: 1. **Audit false-negatives.** A verifier that confirms "the tool call backing this answer executed successfully" by walking the answer's trace finds no successful tool span under the answer's `trace_id`, and flags a correct answer as unverified. In agent swarms that gate on auditor consensus, this manifests as spurious rejection of valid outputs after any checkpoint. 2. **Duplicate-cost accounting.** `gen_ai.usage.input_tokens` is emitted on both `S1` and `S2` for the same logical step, inflating per-conversation cost dashboards by exactly the checkpoint frequency. 3. **Silent span leakage.** `S1` is never ended, so the exporter's span buffer retains it until flush-timeout; under sustained checkpointing this saturates the batch processor queue and drops *unrelated* spans, producing gaps in traces that have nothing to do with the resumed agent. The fix is a schema-level invariant: the checkpoint serializer must refuse to persist a GenAI span's attributes unless the accompanying `SpanContext` is present, and the resume path must assert that adopted context matches the recorded `gen_ai.tool.call.id`. The code below enforces that invariant at both ends. ```python from dataclasses import dataclass from typing import Optional class SpanContextCheckpointError(RuntimeError): """Raised when a GenAI span checkpoint cannot preserve causal identity.""" pass @dataclass(frozen=True) class SpanContext: trace_id: str span_id: str @dataclass class GenAISpanCheckpoint: gen_ai_attrs: dict # e.g. {"gen_ai.tool.name": ..., "gen_ai.tool.call.id": ...} span_context: Optional[SpanContext] def serialize_checkpoint(attrs: dict, ctx: Optional[SpanContext]) -> GenAISpanCheckpoint: """Guardrail at checkpoint-write time: refuse to persist GenAI attrs without context.""" if ctx is None: raise SpanContextCheckpointError( "Refusing to checkpoint GenAI span attributes without a SpanContext: " "resumed instance would mint a fresh trace_id and sever tool-call causality. " f"attrs={attrs!r}" ) return GenAISpanCheckpoint(gen_ai_attrs=attrs, span_context=ctx) def resume_span(checkpoint: GenAISpanCheckpoint, expected_call_id: str) -> SpanContext: """Guardrail at resume time: assert adopted context matches recorded call id.""" if checkpoint.span_context is None: raise SpanContextCheckpointError( "Checkpoint lacks SpanContext; cannot continue or link trace." ) recorded_call = checkpoint.gen_ai_attrs.get("gen_ai.tool.call.id") if recorded_call != expected_call_id: raise SpanContextCheckpointError( f"Resumed span call id mismatch: checkpoint={recorded_call!r} " f"expected={expected_call_id!r}; refusing to re-adopt stale context." ) return checkpoint.span_context ```