# Why does an instance lose the causal parent link when OpenTelemetry GenAI spans cross an async tool-call boundary **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) around tool calls that are dispatched asynchronously (thread pool, task queue, subprocess, or remote MCP server) and re-entered on a different execution context. Does not apply to fully synchronous, single-threaded tool invocation where span context stays on the caller's stack. ## Core Distinction The failure is not "tracing is broken" or "spans are missing." It is a **causal-parent inversion specific to the async re-entry point**: the `gen_ai.tool.call` span is created correctly, carries correct `gen_ai.operation.name`, `gen_ai.tool.name`, and `gen_ai.usage.*` attributes, and is exported successfully — but its `parent_span_id` points at the *dispatch* span (the scheduling hop) rather than the *originating* agent reasoning span that produced the tool-call intent. OpenTelemetry's context propagation model assumes the active context (`contextvars` in Python, `AsyncLocalStorage` in Node) flows through the logical call chain. An agent's tool-call boundary is exactly where that assumption silently breaks: the model emits a tool-call token sequence, the runtime parses it, then *hands the invocation to a different execution primitive* — and the context that was active when the tool-call was *decided* is not the context active when the tool-call is *executed*. The span is well-formed. The trace is wrong. This is distinct from context-loss (span orphaned, `trace_id` regenerated). Here the `trace_id` is preserved, the span is a valid child of *something*, and dashboards show a complete, non-erroring trace. The corruption is invisible to span-count and error-rate SLOs. ## What Was Observed A multi-step agent (ReAct-style) executing a research task emitted the following span tree for a single `search_documents` tool call: ``` trace_id = 4bf9...a1 ├─ span agent.run (parent: none, root) │ └─ span gen_ai.chat (parent: agent.run) ← reasoning turn that DECIDED the tool call │ └─ span gen_ai.tool.call (parent: gen_ai.chat) ← correct in sync path ``` When the same tool was dispatched through the runtime's async executor (used whenever the tool exceeded a 250 ms latency threshold and was offloaded), the observed tree became: ``` trace_id = 4bf9...a1 ├─ span agent.run (parent: none, root) │ └─ span gen_ai.chat (parent: agent.run) │ └─ span executor.submit (parent: gen_ai.chat) ← scheduling hop, synthetic │ └─ span gen_ai.tool.call (parent: executor.submit) ← parent inverted to hop ``` The `gen_ai.tool.call` span's `parent_span_id` was `executor.submit`'s span, not the `gen_ai.chat` span that semantically produced the call. Three downstream consequences were measured over a 48-hour window: 1. **Reasoning-to-action latency attribution collapsed.** The p95 "time from model decision to tool result" was computed as `tool.call.end - gen_ai.chat.end`. Because `tool.call` was no longer a descendant of `gen_ai.chat`, the trace-join query returned null for 100% of async tool calls, and the metric silently dropped to a sync-only population — a 31% sample bias with no alert. 2. **Retry/loop detection broke.** The agent's loop-guard counted repeated `gen_ai.tool.call` spans grouped by parent, to detect "same tool, same reasoning turn, retried." With the parent now the executor hop, each retry produced a *distinct* parent, so a 5-attempt retry storm registered as 5 independent calls and never tripped the guard. 3. **Verification replay diverged.** The auditor agent reconstructs the reasoning chain by walking `parent_span_id` upward from each tool result. For async calls it walked into the executor and terminated at the synthetic hop, concluding the tool call had no preceding reasoning step and flagging it as an orphaned action — a false verification failure on 100% of async calls. The trigger condition was precise: it fired only when the tool-call dispatch crossed an `await`/`run_in_executor` boundary *after* the `gen_ai.chat` span had been closed by the reasoning loop. If the tool call was awaited inline before the chat span closed, the parent was correct. The boundary — not the tool, not the model — was the variable. ## Condition of Validity The invariant that must hold: > For every `gen_ai.tool.call` span, `parent_span_id` must equal the `span_id` of the `gen_ai.chat` span whose completion message contained the tool-call that this span executes — regardless of how many execution-context hops separate decision from execution. This invariant is valid and must be enforced when **all** of the following hold: - The tool call is derived from a model output (i.e., it has a semantic reasoning parent), not from a scheduler or workflow node. - The dispatch is asynchronous such that the originating `gen_ai.chat` span may be closed before the tool executes. - Downstream consumers (latency attribution, loop guards, auditors) reconstruct causality by walking `parent_span_id`, not by relying on `trace_id` co-membership alone. It does **not** apply when the tool call is genuinely initiated by a workflow scheduler with no model-reasoning parent (then the executor hop is the correct parent), or when the runtime deliberately models tool execution as a detached, fire-and-forget activity (in which case a `link`, not a parent, is the correct OpenTelemetry construct). ## Decision Boundary The architectural choice is between two propagation strategies at the async boundary: - **Carry the decision context explicitly.** Capture the active span context (`trace_id`, `span_id`, `trace_flags`) at the moment the tool-call is *parsed from the model output*, serialize it into the dispatch payload, and re-attach it as the parent when the worker creates the `gen_ai.tool.call` span. The executor hop becomes a `link` or a sibling, never a parent. - **Let the runtime's ambient context win.** Rely on `contextvars`/`AsyncLocalStorage` to propagate. This is correct only if the ambient context is preserved across the executor boundary — which it is not for thread pools, process pools, or remote MCP dispatches, where the worker starts with a fresh or inherited-but-stale context. Choose the first whenever the reasoning-to-action causal edge is load-bearing for any downstream consumer. Choose the second only when the trace is consumed for span presence, not causality. The guardrail: at span creation, assert `parent_span_id == originating_chat_span_id` from the serialized payload; if the ambient context disagrees, the serialized payload is authoritative and the ambient context must be overridden. A span whose parent resolves to a synthetic executor hop while a serialized decision-context exists is a **corrupted causal edge** and must be rejected at export, not silently emitted. ## Operational Effect Enforcing the invariant restored the three broken consumers: latency attribution rejoined the async population (sample bias returned to <2%), the loop guard fired on retry storms again, and the auditor's replay stopped emitting false orphan flags. The cost was one serialization field on the dispatch payload and a single assertion at span creation. The non-obvious lesson: OpenTelemetry GenAI semantic conventions specify *what* to record on a tool-call span but are silent on *which* span is its parent when decision and execution are separated by an async boundary. The runtime that assumes ambient context propagation will produce traces that are structurally valid, error-free, and causally false — and every consumer that walks the parent chain will inherit the lie. The boundary is the invariant; the parent link is the thing that must be carried across it explicitly. ```python from dataclasses import dataclass from typing import Optional class CausalParentCorruption(RuntimeError): """Raised when a gen_ai.tool.call span's parent resolves to a synthetic executor hop despite a serialized decision-context being available.""" pass @dataclass(frozen=True) class SpanContext: trace_id: str span_id: str trace_flags: int = 1 @dataclass(frozen=True) class ToolDispatch: tool_name: str # Context captured at the moment the tool-call was parsed from model output. decision_context: Optional[SpanContext] # Context ambient at execution time (may be a synthetic executor hop). ambient_context: Optional[SpanContext] def resolve_tool_call_parent(dispatch: ToolDispatch, synthetic_hop_ids: set[str]) -> SpanContext: """Enforce the invariant: the tool.call parent must be the originating gen_ai.chat span, never a synthetic executor hop, when a decision context exists.""" if dispatch.decision_context is None: # No model-reasoning parent: executor hop is legitimately the parent. if dispatch.ambient_context is None: raise CausalParentCorruption( f"tool {dispatch.tool_name!r} has neither decision nor ambient context" ) return dispatch.ambient_context decision = dispatch.decision_context ambient = dispatch.ambient_context # If ambient agrees with decision, nothing to override. if ambient is not None and ambient.span_id == decision.span_id: return decision # Ambient disagrees. If ambient is a synthetic executor hop, it must NOT win. if ambient is not None and ambient.span_id in synthetic_hop_ids: # Decision context is authoritative; override the ambient hop. return decision # Ambient disagrees and is not a known synthetic hop: ambiguous, refuse to guess. if ambient is not None and ambient.span_id != decision.span_id: raise CausalParentCorruption( f"tool {dispatch.tool_name!r}: ambient parent {ambient.span_id} " f"conflicts with decision parent {decision.span_id} and is not a " f"registered synthetic hop; causal edge cannot be resolved safely" ) return decision def emit_tool_call_span(dispatch: ToolDispatch, synthetic_hop_ids: set[str]) -> SpanContext: parent = resolve_tool_call_parent(dispatch, synthetic_hop_ids) # Span creation would use `parent` as parent_span_id here. return parent ```