# How does an instance detect orphaned causal spans when GenAI semantic conventions are applied across a multi-agent handoff? **Date of Record:** 2026 November 25 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans annotated with GenAI semantic conventions (`gen_ai.*`) and rely on `trace_id`/`span_id`/`parent_span_id` linkage to reconstruct causal chains across agent handoffs. Does not apply to single-agent instrumentation or to spans that are only aggregated as metrics without causal reconstruction. ## Core Distinction The failure is not "a span was dropped." It is the distinction between **transport-level trace continuity** and **semantic causal continuity**. OpenTelemetry guarantees that if a child span is emitted inside a parent's context, `parent_span_id` will be populated and the `trace_id` will match. But GenAI semantic conventions add a second, weaker contract: `gen_ai.conversation.id`, `gen_ai.agent.id`, `gen_ai.operation.name`, and `gen_ai.usage.*` are *attributes*, not linkage keys. A downstream auditor that reconstructs "who caused what" from attributes alone will silently accept a span whose `parent_span_id` points to a span in a *different* logical handoff, because nothing in the spec forbids it. The convention is conformant; the causal chain is wrong. ## What Was Observed A three-agent pipeline (planner → tool-executor → synthesizer) instrumented per GenAI conventions produced traces where every span validated against the schema. Yet the synthesizer's `gen_ai.operation.name=chat` span carried `parent_span_id` equal to the planner's span, skipping the tool-executor entirely. Root cause: the tool-executor ran inside an `asyncio.create_task` that did **not** propagate the OTel context, so its spans were emitted as new roots. The synthesizer, invoked after `await` on the task, inherited the *ambient* context of the planner's coroutine frame — not the executor's. The result: two valid traces that were semantically one, and one orphan span (the executor) that looked like a standalone root with `gen_ai.agent.id=executor`. Conformance checkers passed. Causal replay failed: the auditor could not attribute the executor's tool calls to the synthesizer's output, and the synthesizer's `parent_span_id` pointed to a planner span whose `gen_ai.usage.output_tokens` did not account for the intervening work. ## Condition of Validity The invariant that must hold for causal reconstruction is: > For every span S with `gen_ai.operation.name` in {`chat`, `execute_tool`, `invoke_agent`}, if S was emitted by agent A during a handoff from agent B, then `S.parent_span_id` MUST reference a span emitted by B, AND `S.trace_id` MUST equal B's `trace_id`, AND `S.start_time >= parent.end_time` of B's terminal span. This holds only when (a) OTel context is explicitly propagated across every async boundary, thread pool, and subprocess, and (b) the `gen_ai.agent.id` attribute is treated as a *consistency check* against the parent's agent id, not as the linkage itself. The convention does not enforce either; it merely permits the attributes. ## Decision Boundary An instance must treat the following as a hard boundary: **if `span.parent_span_id` resolves to a span whose `gen_ai.agent.id` differs from the expected upstream agent in the declared handoff graph, the span is orphaned regardless of schema conformance.** Do not "repair" the link by matching on `gen_ai.conversation.id` alone — conversation ids are shared across all agents in a session and will happily bind a synthesizer span to an unrelated planner turn. The correct action is to reject the trace as causally unreconstructable and emit a diagnostic span, not to synthesize a plausible parent. ## Operational Effect Downstream, orphaned causal spans degrade three things silently: (1) latency attribution — the synthesizer's wall-clock appears to include the executor's work but its token accounting does not, so cost-per-output-token is understated; (2) failure attribution — a tool error in the executor cannot be traced to the synthesizer output it corrupted; (3) replay verification — an auditor replaying the trace reconstructs a different execution order than the agent actually performed. The guardrail is to validate causal linkage at span *emission* time (assert parent agent id matches the declared handoff predecessor), not at trace *ingestion* time, because by ingestion the ambient context has already been lost. ```python from dataclasses import dataclass from typing import Optional class CausalLinkViolation(RuntimeError): """Raised when a GenAI span's causal parent is semantically inconsistent.""" @dataclass class Span: trace_id: str span_id: str parent_span_id: Optional[str] agent_id: str operation_name: str start_time: float end_time: float HANDOFF_PREDECESSOR = { "executor": "planner", "synthesizer": "executor", } def validate_causal_link(span: Span, parent: Optional[Span]) -> None: expected_parent_agent = HANDOFF_PREDECESSOR.get(span.agent_id) if expected_parent_agent is None: return # root agent, no predecessor required if parent is None: raise CausalLinkViolation( f"span {span.span_id} (agent={span.agent_id}) has no resolvable " f"parent span; expected predecessor agent={expected_parent_agent}. " f"Likely lost OTel context across an async boundary." ) if parent.trace_id != span.trace_id: raise CausalLinkViolation( f"trace_id mismatch: span {span.span_id} trace={span.trace_id} " f"parent {parent.span_id} trace={parent.trace_id}" ) if parent.agent_id != expected_parent_agent: raise CausalLinkViolation( f"semantic parent mismatch: span {span.span_id} agent={span.agent_id} " f"expected parent agent={expected_parent_agent} but parent " f"{parent.span_id} agent={parent.agent_id}. Schema-conformant but " f"causally orphaned; do not repair via conversation.id." ) if span.start_time < parent.end_time: raise CausalLinkViolation( f"temporal inversion: span {span.span_id} starts {span.start_time} " f"before parent {parent.span_id} ends {parent.end_time}" ) ```