# What happens when an instance emits a tool-call span under OpenTelemetry GenAI conventions before its parent agent span has been exported **Date of Record:** 2026 November 27 **Scope:** Applies to multi-agent systems that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*` attributes, `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.agent.id`) where spans are produced by concurrently executing agents sharing a `TracerProvider` and exported via batch processors. Does not apply to single-threaded, synchronous, single-agent tracing pipelines with `SimpleSpanProcessor`, nor to systems that emit only root spans. ## Core Distinction The GenAI semantic conventions define *what attributes* a span carries, not *when* the span must be visible to an exporter relative to its logical parent. The convention assumes parent-child linkage is established via `SpanContext` propagation at span creation time — but in a multi-agent system, the "parent" of a tool-call span is frequently an *agent span running in a different process or goroutine* whose span has been created but not yet exported, or whose `SpanContext` was serialized across an A2A/MCP handoff and reconstructed by the receiving agent. The non-obvious failure: `gen_ai.operation.name = "execute_tool"` spans are emitted by the tool-executing agent, but their `parent_span_id` points to an agent span that the *orchestrating* agent owns. When the orchestrator's batch processor flushes its agent span before the tool-executor's batch processor flushes the child, the backend receives an orphaned child span. The convention does not mandate ordering, and `BatchSpanProcessor` explicitly does not guarantee it. The invariant that breaks is: **a span's `parent_span_id` must reference a span that either already exists in the collector's store or arrives within the collector's orphan-retention window.** Nothing in the GenAI conventions enforces this across process boundaries. ## What Was Observed A three-agent pipeline (planner → executor → verifier) using OTLP/gRPC export with default `BatchSpanProcessor` settings (`max_queue_size=2048`, `schedule_delay_millis=5000`, `max_export_batch_size=512`). Each agent runs in its own container with its own `TracerProvider`. The planner creates a root span `gen_ai.operation.name="invoke_agent"`, serializes its `SpanContext` into the A2A task payload, and hands off to the executor. The executor reconstructs a non-recording remote parent from the serialized `trace_id`/`span_id`, creates `gen_ai.operation.name="execute_tool"` child spans, and exports them. The planner, meanwhile, finishes its reasoning loop in ~200ms, its agent span ends, and its batch processor flushes at the 5s schedule delay — *after* the executor has already shipped its tool spans. Observed in the trace backend: - Tool-call spans arrived with `parent_span_id` set to a span the backend had never seen. The backend's default orphan handling (Jaeger: 30s retention; Tempo: configurable, often 0) either dropped the linkage or, in Tempo's case, materialized the child as a root. - `gen_ai.tool.name` and `gen_ai.tool.call.id` attributes were intact on the tool span, but the *agent* attributes (`gen_ai.agent.id`, `gen_ai.agent.name`) lived only on the planner's span. Because the linkage was broken, the tool span appeared agent-less — a tool call from nowhere. - In one run, the verifier's `gen_ai.operation.name="invoke_agent"` span arrived *before* the executor's tool spans it was supposed to verify, because the verifier's batch flushed earlier. The trace tree showed verification preceding the action it verified. The failure is silent: no exporter error, no span drop, no log line. The trace is structurally valid OTLP; it is semantically wrong. ## Condition of Validity The parent-before-child invariant holds only when one of the following is true: 1. **Single exporter, ordered queue.** All spans in a trace share one `TracerProvider` and one `BatchSpanProcessor`, and the processor's FIFO queue preserves creation order — which it does *within a process* but not across processes. 2. **Synchronous export.** `SimpleSpanProcessor` (or `BatchSpanProcessor` with `schedule_delay_millis=0` and `max_export_batch_size=1`) flushes each span at `end()`, so a parent ended before a child is created is always exported first. This is the only configuration where cross-process ordering is approximately guaranteed — and it costs throughput. 3. **Collector-side orphan retention.** The collector buffers orphaned children for a window longer than the maximum inter-agent export skew. Tempo's `wait_on_overflow`/`max_wait_duration` and Jaeger's `--span-storage.orphan-wait` are the knobs. The window must exceed `schedule_delay_millis` of the *slowest* agent's processor plus network jitter. 4. **Deterministic handoff ordering.** The orchestrator does not end its agent span until it has received an acknowledgment that the downstream agent has flushed its tool spans. This requires an explicit flush barrier in the A2A/MCP protocol — which the current conventions do not define. Outside these conditions, the invariant is violated and the trace is silently mis-linked. ## Decision Boundary The boundary is between **linkage-by-reference** (child span carries `parent_span_id`, resolved at query time) and **linkage-by-embedding** (child span carries enough parent context to be self-describing). The GenAI conventions chose linkage-by-reference for `execute_tool` spans, which is correct for single-process tracing and wrong for multi-agent handoffs. The decision boundary for an operator is: - If inter-agent export skew can be bounded below the collector's orphan window → keep reference linkage, set `schedule_delay_millis` low (e.g., 500ms) on all agent processors, and configure the collector's orphan retention to `max_skew × 2`. This is the cheap path and is adequate for pipelines with a single orchestrator and short-lived tools. - If skew is unbounded (long-running tools, retries, network partitions, agents that sleep) → reference linkage cannot be trusted. The tool span must *embed* the agent identity as first-class attributes (`gen_ai.agent.id`, `gen_ai.agent.name`, `gen_ai.conversation.id`) so that even an orphaned span is attributable. The `parent_span_id` becomes advisory, not load-bearing. The boundary is crossed the moment a tool call's duration or export latency can exceed the collector's orphan window. For most production multi-agent systems, that moment is the first long-running tool call — and it is not detected until someone queries the trace tree and finds a tool call with no agent. ## Operational Effect The operational consequence is that **trace-derived metrics lie in a specific, reproducible way**. Span-metric aggregations that group tool calls by parent agent (a common dashboard: "tool calls per agent per minute") undercount any agent whose tool spans arrived orphaned. The undercount is not random — it correlates with tool duration and with the orchestrator's reasoning-loop length, so it systematically undercounts the slowest, most expensive tool calls, which are exactly the ones an operator needs to see. The guardrail is a two-part invariant enforced at span emission: 1. **Embed agent identity on every tool span**, not just on the invoking agent span. `gen_ai.agent.id` must be present on `execute_tool` spans regardless of parent linkage. 2. **Assert export ordering at the protocol layer.** When an agent hands off via A2A/MCP, the handoff payload must include a flush barrier: the downstream agent calls `force_flush()` on its processor and returns a receipt before the upstream agent ends its span. This makes parent-before-child a protocol guarantee rather than an exporter-timing accident. Without both, the trace is a plausible-looking artifact that misrepresents causality. The following check encodes the invariant: ```python import time from dataclasses import dataclass from typing import Optional class OrphanedToolSpanError(RuntimeError): """Raised when a tool-call span cannot be attributed to an agent at emission time.""" pass @dataclass class SpanContext: trace_id: str span_id: str @dataclass class ToolSpan: trace_id: str span_id: str parent_span_id: Optional[str] gen_ai_operation_name: str gen_ai_tool_name: str gen_ai_agent_id: Optional[str] gen_ai_agent_name: Optional[str] emitted_at: float def emit_tool_span( ctx: SpanContext, tool_name: str, agent_id: Optional[str], agent_name: Optional[str], collector_orphan_window_s: float, upstream_flushed_at: float, ) -> ToolSpan: """ Enforce the multi-agent GenAI linkage invariant: 1. Tool spans must embed agent identity (not rely solely on parent linkage). 2. The upstream parent span must have been flushed before this child is emitted, or the skew must fall inside the collector's orphan-retention window. """ now = time.monotonic() skew = now - upstream_flushed_at if agent_id is None or agent_name is None: raise OrphanedToolSpanError( f"tool span '{tool_name}' in trace {ctx.trace_id} has no gen_ai.agent.id/" f"gen_ai.agent.name; parent linkage alone is insufficient for multi-agent traces" ) if skew > collector_orphan_window_s: raise OrphanedToolSpanError( f"tool span '{tool_name}' emitted {skew:.3f}s after upstream flush, " f"exceeding collector orphan window {collector_orphan_window_s:.3f}s; " f"parent span {ctx.span_id} may not yet be visible to the backend" ) return ToolSpan( trace_id=ctx.trace_id, span_id=f"{ctx.span_id}-tool-{tool_name}", parent_span_id=ctx.span_id, gen_ai_operation_name="execute_tool", gen_ai_tool_name=tool_name, gen_ai_agent_id=agent_id, gen_ai_agent_name=agent_name, emitted_at=now, ) ``` The invariant this encodes is not "parents are always exported first" — the exporter cannot promise that. It is "a tool span is never emitted in a state where its attribution depends on a parent span that may not arrive." Embedding agent identity makes the span self-describing; the skew check makes the ordering assumption explicit and bounded. Together they convert a silent trace-corruption failure into a loud, attributable one at emission time, which is the only point where the emitting agent still has the context to fix it.