# What happens when an instance orphans its causal parent span during GenAI semantic-convention emission **Date of Record:** 2026 December 1 **Scope:** Applies to instrumented LLM agent runtimes that emit OpenTelemetry-style spans annotated with GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.*`, `gen_ai.conversation.id`, `gen_ai.tool.call.id`) and reconstruct causal traces by joining spans on parent/child links plus conversation identifiers. Does not apply to single-shot, single-span inference calls with no downstream tool or sub-agent fan-out. ## Core Distinction There are two distinct notions of "parent" in an agent trace, and conflating them produces silent causal corruption. The **execution parent** is the span whose scope lexically encloses the child — the `execute_tool` call made from inside the `chat` handler. The **causal parent** is the span that *caused* the child to exist — the earlier `chat` completion whose emitted tool-call token is what the tool invocation is actually fulfilling. In a well-behaved synchronous loop these coincide, so instrumentation that only propagates the active context is correct. But when the agent's reasoning step ends, the tool dispatch is queued, and the actual `execute_tool` span is opened from a *different* async context — a worker, a resumed checkpoint, a re-entrant callback — the execution parent is whatever context happens to be active, and the causal parent (the `chat` span that produced the tool-call) is no longer on the stack. The failure is not that the span is dropped. The span is emitted, well-formed, and carries a `trace_id` and `parent_span_id`. The failure is that `parent_span_id` points at the *wrong* span — typically the agent's outer orchestration span or a fresh root — so the trace reconstructs as a plausible but false topology. Downstream replay, cost attribution, and blame assignment all operate on a graph whose edges are semantically wrong but syntactically valid. There is no error, no dropped span, no `status=ERROR`. This is the sharp edge: **span linking is a graph-reconstruction invariant, not a logging invariant, and it fails open.** ## What Was Observed A multi-step agent (plan → `chat` → `execute_tool` × N → `chat` → answer) was instrumented with the standard pattern: a `tracer.start_as_current_span("chat")` around the model call, and tool execution wrapped in `start_as_current_span("execute_tool")` inside the tool dispatcher. The dispatcher was invoked from an async task pool so that parallel tool calls (the model emitted three tool-call tokens in one completion) could run concurrently. Observed trace fragment after reconstruction: ``` trace_id=7f3a... span A agent.run (root) span B chat gen_ai.operation.name=chat, model=gpt-x, finish_reason=tool_calls span C execute_tool gen_ai.tool.name=search, parent=A <-- wrong span D execute_tool gen_ai.tool.name=lookup, parent=A <-- wrong span E execute_tool gen_ai.tool.name=calc, parent=A <-- wrong span F chat parent=A ``` Each `execute_tool` span (C, D, E) was emitted with `parent_span_id = A` (the orchestration root) instead of `parent_span_id = B` (the `chat` span that emitted the tool-call). The reason: the async task pool opened each span from a context that had been captured *before* span B was started, or from a context where B had already ended and the "current span" had unwound back to A. The `gen_ai.tool.call.id` on each `execute_tool` span still matched the `tool_call.id` embedded in B's recorded completion, so a human reading the payload could reconstruct the truth — but the *span graph* used by the trace backend, the cost rollup, and the replay harness could not. Downstream consequences observed in the same incident: 1. **Cost attribution drift.** The trace backend rolled `gen_ai.usage.input_tokens` / `output_tokens` up the parent chain. Because C/D/E were children of A rather than B, the per-`chat`-turn cost was understated and the orchestration root absorbed tool latency as if it were model latency. A budget alert keyed on "chat spans exceeding token threshold" never fired. 2. **Replay divergence.** The replay harness reconstructs the agent's decision sequence by walking the trace depth-first and re-deriving "which completion produced which tool call." With C/D/E parented to A, the harness saw three tool calls that appeared to be emitted by the root, found no matching `tool_call.id` on any root-level completion, and silently dropped them from the replay — producing a replay that diverged from the original run at exactly the fan-out point. 3. **Blame misattribution across a multi-agent handoff.** When the same agent was wrapped as a sub-agent invoked by a coordinator (A2A handoff), the coordinator's auditor compared the sub-agent's reported tool calls against the reconstructed trace and flagged a *verification mismatch* — the auditor's peer agent had recorded the tool calls as children of the `chat` span (it used a different instrumentation path), while the sub-agent's trace showed them under the root. The two agents deadlocked on a consensus check over a discrepancy that was purely an instrumentation artifact. The trigger condition, stated precisely: **a child span is opened from a context whose active span is not the span that semantically caused the child, and the causal linkage exists only as a payload field (`tool_call.id`) rather than as a structural edge.** This occurs whenever (a) tool dispatch is decoupled from the reasoning step's span scope via async scheduling, queueing, checkpoint/resume, or a re-entrant callback, and (b) the instrumentation relies on implicit context propagation (thread-local / contextvar active span) rather than explicit parent linkage. ## Condition of Validity The standard "wrap in `start_as_current_span` and let context propagate" pattern is valid **only** under the invariant: > For every child span S, the context active at S's creation contains, as its current span, the span that causally produced S's triggering event. This invariant holds for synchronous, single-threaded, non-checkpointed execution. It is violated the moment any of the following is true: - **Async fan-out:** the model emits K tool calls in one completion; K tasks are scheduled; each task's context is captured at schedule time (possibly before the `chat` span was entered) or at a different point in the contextvar stack. - **Checkpoint/resume:** the agent persists state mid-task, the process restarts, and the `execute_tool` span is opened in a fresh process where the `chat` span from the prior process is not on the stack (and cannot be, since spans are process-local). The causal parent exists only as a serialized `span_id` in the checkpoint. - **Re-entrant callbacks:** a tool's completion callback re-enters the runtime and opens the *next* `chat` span from the callback's context, which is the tool's context, not the original `chat`'s. - **Sub-agent handoff:** the coordinator opens a span for the sub-agent invocation, but the sub-agent's first `chat` span is opened from the sub-agent runtime's own root, severing the A2A causal edge unless an explicit `links` or `parent` is passed across the boundary. In all four cases the correct fix is not "propagate context better" — it is **explicit causal linkage**: the tool-call's `span_id` must be recorded as a field on the completion that emitted it, and the `execute_tool` span must be opened with an explicit parent reference to that recorded `span_id`, not to whatever is current. The GenAI semantic conventions already carry the necessary join key (`gen_ai.tool.call.id`); the invariant is that this key must be *promoted to a structural edge* at span-creation time, not left as a payload annotation to be reconciled later. ## Decision Boundary The operational decision is: **at what point does the runtime trust implicit context propagation versus require explicit causal linkage?** - **Trust implicit context** when the child span is opened synchronously inside the parent's scope and no checkpoint, queue, or async boundary intervenes. Cost of being wrong: low (the pattern is correct here). Cost of adding explicit linkage: negligible but noisy. - **Require explicit linkage** when *any* of: (1) the child is opened from a different async task than the one that entered the parent span; (2) the child is opened after a process restart or checkpoint restore; (3) the child crosses an agent or process boundary (A2A, sub-agent, tool server); (4) the parent span has already ended (the completion is recorded and the span closed) before the child begins — which is the normal case for tool dispatch, since the `chat` span ends when the completion returns. The last condition is the one most often missed and the one that caused the observed incident. **A `chat` span that emits `tool_calls` ends when the completion returns; the `execute_tool` span necessarily begins after that.** By construction, the causal parent is already closed when the child is created. Implicit context propagation *cannot* link them, because the causal parent is no longer current — it has unwound. The only correct mechanism is to capture the parent `span_id` at completion time, persist it alongside the `tool_call.id`, and pass it explicitly when opening the tool span. This is not an optimization; it is a structural requirement of the trace graph. ## Operational Effect When the invariant is enforced — explicit `parent_span_id` on every `execute_tool` span, sourced from the `span_id` recorded on the emitting completion — the trace reconstructs with the correct topology: `chat` → `execute_tool` × K → next `chat`, and cost, replay, and audit all key off the same edges. When it is not enforced, the trace is *syntactically valid and semantically false*, which is worse than a missing span because it survives validation and poisons every consumer downstream. The guardrail is a **span-creation-time assertion**: every `execute_tool` (and every cross-boundary `chat`) span must carry an explicit parent that is not the orchestration root unless the root is genuinely the causal parent, and the parent's `span_id` must match a `tool_call.id` recorded on a completion span in the same trace. Violations must fail the span emission, not silently reparent, because a reparented span is an undetectable lie. The following reference guard encodes this: ```python from dataclasses import dataclass from typing import Optional class OrphanedCausalParentError(RuntimeError): """Raised when a tool/child span's causal parent cannot be structurally validated against the completion that emitted its tool_call.id.""" @dataclass class SpanRef: span_id: str parent_span_id: Optional[str] operation: str # gen_ai.operation.name tool_call_id: Optional[str] = None # set on execute_tool spans emitted_tool_call_ids: tuple = () # set on chat spans def open_tool_span( tool_call_id: str, causal_parent_span_id: str, trace: dict[str, SpanRef], orchestration_root_id: str, ) -> SpanRef: """Open an execute_tool span with explicit causal linkage. The causal parent MUST be the chat span whose completion emitted tool_call_id. Implicit context propagation is rejected: by the time a tool span opens, the chat span has already ended. """ parent = trace.get(causal_parent_span_id) if parent is None: raise OrphanedCausalParentError( f"causal parent {causal_parent_span_id} for tool_call {tool_call_id} " f"not present in trace; cannot reconstruct edge" ) if parent.operation != "chat": raise OrphanedCausalParentError( f"causal parent {parent.span_id} has operation {parent.operation!r}, " f"expected 'chat'; tool call {tool_call_id} would be misattributed" ) if tool_call_id not in parent.emitted_tool_call_ids: raise OrphanedCausalParentError( f"tool_call {tool_call_id} was not emitted by chat span {parent.span_id}; " f"emitted={parent.emitted_tool_call_ids}" ) if causal_parent_span_id == orchestration_root_id: raise OrphanedCausalParentError( f"tool span for {tool_call_id} parented directly to orchestration root; " f"this is the async-fan-out failure signature, not a valid causal edge" ) child = SpanRef( span_id=f"tool-{tool_call_id}", parent_span_id=causal_parent_span_id, operation="execute_tool", tool_call_id=tool_call_id, ) trace[child.span_id] = child return child ``` The assertion that fires on `causal_parent_span_id == orchestration_root_id` is the load-bearing one: it is exactly the signature of the async-fan-out orphaning observed in the incident, and it converts an undetectable graph corruption into a loud, attributable failure at span-creation time.