# How should an instance trace a semantic-convention span link when a parent agent hands off to a sub-agent tree and the OTel `parent_span_id` crosses a trace-context boundary **Date of Record:** 2026 November 27 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans using GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.conversation.id`) and that delegate work to sub-agent trees spawned under a distinct trace context (separate `trace_id`). Does not apply to single-agent, single-trace instrumentation where all spans share one `trace_id`. ## Core Distinction A **span link** is not a **parent-child edge**. GenAI semantic conventions let an agent emit `gen_ai.operation.name = "invoke_agent"` for a sub-agent invocation, but the moment that sub-agent runs under a *new* `trace_id` (common when the sub-agent is executed by a remote runtime, a separate worker pool, or a fresh context window that re-initializes its tracer), the `parent_span_id` field becomes semantically meaningless: it references a span in a trace the child trace cannot resolve. The correct carrier of the causal relationship is `Span.add_link()` with `SpanContext(trace_id=parent_trace, span_id=parent_span, trace_flags=...)`, plus a `gen_ai.agent.handoff` link attribute. Instrumentation that instead forces the parent's `trace_id` into the child (trace-context propagation) silently collapses the sub-agent tree into the parent trace and destroys the very boundary the sub-agent was meant to isolate. The failure is non-obvious because both approaches produce visible spans. Only the *link* approach preserves the invariant that a sub-agent's trace is independently rootable, replayable, and attributable to a distinct `gen_ai.agent.id` and `gen_ai.conversation.id`. ## What Was Observed A three-tier tree: orchestrator agent A (trace `T_A`) delegates to planner agent B (trace `T_B`, spawned on a worker with its own tracer provider), which delegates to executor agent C (trace `T_C`). Instrumentation emitted: - A's `invoke_agent` span `S_A` in `T_A` with `gen_ai.agent.id = "A"`. - B's root span `S_B` in `T_B` with `parent_span_id = S_A.span_id` but `trace_id = T_B`. - C's root span `S_C` in `T_C` with `parent_span_id = S_B.span_id` but `trace_id = T_C`. Every span was exported successfully. But the collector's trace assembler, keyed on `(trace_id, span_id)`, could not resolve any `parent_span_id` across trace boundaries. Result: `S_B` and `S_C` appeared as **orphaned roots** in the backend. The sub-agent trees were visible as isolated traces with no declared causal predecessor. During a post-mortem on a hallucinated tool call in C, the on-call engineer could not walk from the failing span back to A's planning decision without a manual join on `gen_ai.conversation.id` — which itself was only present on the root spans, not propagated to child spans inside `T_B` or `T_C`. A second, sharper symptom: because the semantic convention does not define `parent_span_id` as a cross-trace field, two different backends (Jaeger and a vendor APM) interpreted the orphaned `parent_span_id` differently — one dropped it, one attempted a same-trace lookup and logged a `span_not_found` warning per export. The warning rate scaled with sub-agent fan-out and masked real span drops. The root cause: the instrumentation conflated *causal linkage* with *trace-context propagation*. `parent_span_id` is a trace-scoped pointer; `SpanLink` is the trace-agnostic causal record. GenAI handoffs are the canonical case where these diverge. ## Condition of Validity The invariant holds only when **all three** are true: 1. The sub-agent executes under a tracer whose `trace_id` is independently generated (not inherited via W3C `traceparent`). This is the normal case for remote agent runtimes, sandboxed executors, and any handoff that crosses a process or trust boundary. 2. The parent agent's span context is captured *before* the handoff and carried in the handoff payload (the agent-to-agent message envelope), not in the transport headers. 3. The receiving sub-agent emits `SpanLink` records pointing at every ancestor span it was handed, plus a `gen_ai.agent.handoff.source_agent_id` attribute. If condition 1 is false — i.e., the sub-agent genuinely runs in the parent's trace — then `parent_span_id` is correct and links are redundant. The failure mode only manifests at the boundary where trace context is *not* propagated but the handoff still needs to be recorded. ## Decision Boundary Choose **span link** over **parent_span_id** when the handoff crosses any of: process boundary, trust boundary, tracer-provider boundary, or a context-window reset that re-instantiates the tracer. Choose **parent_span_id** only when the sub-agent's tracer shares the parent's `trace_id` by construction. The boundary is sharp, not heuristic: if `child.trace_id != parent.trace_id`, `parent_span_id` must be omitted and replaced by a link. Emitting both is a schema violation — it produces a dangling pointer that some backends will attempt to resolve and fail on. A secondary boundary: the link must carry the *full* ancestor chain, not just the immediate parent. In a three-tier tree, `S_C` needs links to both `S_B` and `S_A`, because `S_B` is itself an orphan root in its own trace and cannot be reached by walking `parent_span_id` from `S_C`. A single-link implementation (only `S_B`) leaves `S_A` unreachable from `T_C`. ## Operational Effect With links correctly emitted, the collector reconstructs the full causal graph across trace boundaries: `T_C → T_B → T_A` becomes a directed acyclic graph via link edges, each trace remains independently rootable (so per-agent latency, token budget, and error attribution stay clean), and replay tooling can reconstruct the exact handoff sequence without cross-trace joins on conversation IDs. With links omitted or replaced by dangling `parent_span_id`, sub-agent trees become causally invisible from the parent trace, post-mortem traversal requires manual conversation-ID joins, and vendor backends emit spurious `span_not_found` warnings that mask real span loss. The fix is a single instrumentation guard: at handoff time, if `child_trace_id != parent_trace_id`, emit a link and suppress the parent pointer. ```python from dataclasses import dataclass from typing import Optional @dataclass(frozen=True) class SpanContext: trace_id: str span_id: str agent_id: str class CrossTraceParentPointerError(RuntimeError): """Raised when a sub-agent span sets parent_span_id across a trace boundary.""" def emit_subagent_root_span( child_ctx: SpanContext, handed_ancestors: list[SpanContext], ) -> dict: """ Build the root span payload for a sub-agent executing under its own trace_id. Invariant: parent_span_id may only be set when the parent shares the child's trace_id. Otherwise the causal edge MUST be expressed as SpanLink records covering the full ancestor chain. """ if not handed_ancestors: # No causal predecessor handed off: legitimate standalone root. return { "trace_id": child_ctx.trace_id, "span_id": child_ctx.span_id, "parent_span_id": None, "links": [], } immediate = handed_ancestors[-1] same_trace = immediate.trace_id == child_ctx.trace_id if same_trace: if len(handed_ancestors) > 1: # Ancestors beyond the immediate parent are still reachable by # walking parent_span_id within the shared trace; no links needed. pass return { "trace_id": child_ctx.trace_id, "span_id": child_ctx.span_id, "parent_span_id": immediate.span_id, "links": [], } # Cross-trace handoff: parent_span_id would be a dangling pointer. # Refuse to emit it; require links for the full ancestor chain instead. for anc in handed_ancestors: if anc.trace_id == child_ctx.trace_id: raise CrossTraceParentPointerError( f"ancestor {anc.agent_id}/{anc.span_id} shares child trace " f"{child_ctx.trace_id}; mixed-trace ancestor chain is " f"ambiguous — split the handoff payload." ) links = [ { "linked_trace_id": anc.trace_id, "linked_span_id": anc.span_id, "attributes": { "gen_ai.agent.handoff.source_agent_id": anc.agent_id, "gen_ai.agent.handoff.target_agent_id": child_ctx.agent_id, "gen_ai.agent.handoff.link_kind": "causal_ancestor", }, } for anc in handed_ancestors ] return { "trace_id": child_ctx.trace_id, "span_id": child_ctx.span_id, "parent_span_id": None, # suppressed: cross-trace "links": links, } ```