# What happens when an instance emits GenAI tool-call spans without a stable `gen_ai.tool.call.id` and the collector's span-link reconciliation silently orphans the parent agent turn **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) for multi-step tool-calling loops and rely on span links or parent-child relationships to reconstruct an agent turn across a collector. Does not apply to single-shot LLM calls with no tool invocation, nor to systems that persist their own trace-side state independent of OTel export. ## Core Distinction The GenAI semantic conventions treat `gen_ai.operation.name` (`chat`, `execute_tool`, `invoke_agent`) as the discriminator and `gen_ai.tool.call.id` as the correlation key that lets a consumer stitch an `execute_tool` span back to the `chat` span that requested it. The failure mode here is not "missing telemetry." It is **telemetry that is present, well-formed, and individually valid, but unreconcilable** — because the tool-call identifier was regenerated on retry, or was never propagated from the model's `tool_calls[].id` into the span attribute, and the collector's link-joining pass therefore produces an `execute_tool` span with no resolvable parent turn. The span survives; the *causal edge* does not. This is a continuity invariant violation, not an instrumentation bug in the naive sense: every span passes schema validation, yet the reconstructed turn is truncated. The distinction that matters operationally: a **dropped span** is observable (counter increments, exporter error). An **orphaned span** is silent — it is exported, accepted, indexed, and queryable, but it can never be joined to the reasoning step that motivated it. Downstream replay, audit, and distillation pipelines treat the orphan as a standalone tool invocation with unknown provenance. ## What Was Observed An agent loop executing a three-step plan: `chat` → `execute_tool(search)` → `chat` → `execute_tool(fetch)` → `chat`. Instrumentation emitted one `invoke_agent` root span, three `chat` child spans, and two `execute_tool` spans. On the happy path the tool spans carried `gen_ai.tool.call.id` copied from the provider's `tool_calls[i].id` (e.g. `call_a1b2`), and the collector joined each `execute_tool` to the preceding `chat` by matching that id against the `chat` span's recorded `gen_ai.response.tool_calls` payload. The trigger: the first `execute_tool(search)` timed out at the tool boundary and the runtime retried it. The retry path re-derived the tool call from the model response but **regenerated the id locally** (`call_a1b2_retry`) instead of preserving `call_a1b2`, because the retry wrapper constructed a fresh `ToolCall` object rather than cloning the original. Both spans exported — the failed attempt with `gen_ai.tool.call.id=call_a1b2` and `error.type=timeout`, the retry with `call_a1b2_retry` and `status=ok`. The parent `chat` span recorded only `call_a1b2` in its response payload. Failure trace: 1. `execute_tool` #1 (attempt) → id `call_a1b2`, status ERROR. Joins to `chat` #1. Fine. 2. `execute_tool` #1 (retry) → id `call_a1b2_retry`, status OK. **No `chat` span references this id.** The join pass finds no match, emits the span with `parent_span_id` unset (the runtime used `start_as_current_span` outside the original context after the timeout unwound it), and the collector's reconciliation marks it `unlinked`. 3. The agent's second `chat` span consumes the retry's *result* (the fetched text) but its request payload references `call_a1b2` — the id of the *failed* attempt — because the runtime reused the original message history. Net effect: the successful tool execution is an orphan; the `chat` span that used its output points at a failed call. Replay of the trace produces a turn where the model reasons over tool output it never observably requested. An auditor reconstructing the chain sees `chat → execute_tool(ERROR) → chat(output present, source absent)`. ## Condition of Validity The reconciliation invariant only holds when, for every `execute_tool` span, there exists exactly one `chat` (or `invoke_agent`) span in the same trace whose recorded tool-call set contains the span's `gen_ai.tool.call.id`, **and** that id is byte-identical across every retry of the same logical call. The convention supports this but does not enforce it: `gen_ai.tool.call.id` is `recommended`, not `required`, and nothing in the schema forbids two spans sharing a logical call under different ids. The invariant is valid only under these conditions: - The runtime treats `tool_calls[].id` as an **immutable correlation key** that survives retry, timeout, and serialization round-trips — never re-derived. - The retry executes within the parent span's context (`contextvars` intact) or explicitly sets `parent_span_id` to the requesting `chat` span. - The collector's join pass treats an unmatched `execute_tool` id as a **hard reconciliation failure**, not a benign orphan. It is invalid the moment any of these breaks — and the failure is asymmetric: the system degrades from "fully reconstructable turn" to "partially reconstructable turn" with no error surfaced anywhere in the OTel pipeline. ## Decision Boundary The decision point is at the retry wrapper and at the collector's join pass, and they must agree on a single policy. Two coherent policies exist; mixing them produces the orphan. - **Id-stable retry (preferred):** the retry clones the original `ToolCall` including `id`; both attempt-spans carry `call_a1b2`, distinguished only by `status`/`error.type` and a `retry.attempt` attribute. The join pass then sees two spans for one id and must select the terminal one — a defined, bounded multiplicity. - **Id-versioned retry:** the retry mints a new id *and* the runtime rewrites the parent `chat` span's recorded tool-call set (or emits a `chat` re-entry span) to reference the new id. The join pass sees a clean one-to-one mapping. The boundary is crossed when the retry mints a new id (versioned policy) but the parent `chat` span keeps the old id (stable policy) — the two halves of the system chose different policies. The guardrail: make the correlation key **write-once per logical call** and assert at span-close time that every `execute_tool` id resolves to a `chat`-recorded id, else raise a reconciliation error rather than export an orphan. ## Operational Effect The trace remains green in every dashboard that counts spans, error rates, and latency. The corruption is confined to the *edge set*, which almost nothing monitors. Consequences compound: - **Replay divergence:** re-running the agent from the trace yields different tool outputs than the recorded turn, because the recorded turn's provenance edge is missing; the replay harness cannot tell whether divergence is model non-determinism or lost telemetry. - **Distillation loss:** episodic-to-semantic pipelines that mine `(chat, tool_result)` pairs to build training data silently drop every orphaned tool result — the successful fetch never becomes a learning signal, while the failed attempt does. - **Audit falsehood:** a compliance query "which tool outputs informed this response" returns the failed call, not the successful one, because that is the only edge that exists. - **Silent accumulation:** retry-heavy agents (flaky tools, rate limits) orphan a growing fraction of tool spans; the ratio is invisible until someone joins spans by hand and finds the trace graph disconnected. The invariant to enforce: **`gen_ai.tool.call.id` is a primary key, not a label.** It must be assigned once per logical tool call, preserved across all retries, and every `execute_tool` span must resolve to a requesting span or fail loudly at export time. ```python from dataclasses import dataclass, field class OrphanedToolCallError(RuntimeError): """Raised when an execute_tool span's gen_ai.tool.call.id cannot be resolved to any requesting chat/invoke_agent span.""" @dataclass class ToolSpan: call_id: str status: str attempt: int = 1 @dataclass class ChatSpan: recorded_tool_call_ids: set[str] = field(default_factory=set) def reconcile_turn(chat_spans: list[ChatSpan], tool_spans: list[ToolSpan]) -> dict[str, list[ToolSpan]]: """Join execute_tool spans to chat spans by gen_ai.tool.call.id. Enforces the continuity invariant: every tool span must resolve to a requesting chat span. Orphans are a hard failure, not a benign skip. """ requested: set[str] = set() for c in chat_spans: requested |= c.recorded_tool_call_ids joined: dict[str, list[ToolSpan]] = {} orphans: list[ToolSpan] = [] for t in tool_spans: if t.call_id not in requested: orphans.append(t) else: joined.setdefault(t.call_id, []).append(t) if orphans: ids = sorted({o.call_id for o in orphans}) raise OrphanedToolCallError( f"{len(orphans)} execute_tool span(s) have no requesting chat span; " f"unresolvable gen_ai.tool.call.id(s)={ids}. " f"Likely cause: retry regenerated the call id without rewriting " f"the parent chat span's recorded tool_calls." ) # Terminal-attempt selection for id-stable retries. return {cid: [max(spans, key=lambda s: s.attempt)] for cid, spans in joined.items()} def emit_turn(chat_spans, tool_spans): try: return reconcile_turn(chat_spans, tool_spans) except OrphanedToolCallError as e: # Never export a trace with a broken causal edge; fail the turn. raise OrphanedToolCallError(f"trace export blocked: {e}") from e ```