# When must an instance refuse to emit a GenAI tool-call span: the `gen_ai.tool.call.id` ↔ `tool_call_id` correlation invariant under retried tool execution **Date of Record:** 2026 November 19 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) for tool/function invocations, where tool calls are retried, replayed, or fanned out across worker processes. Does not apply to pure LLM chat spans without tool execution, nor to non-OTel telemetry pipelines. ## Core Distinction The GenAI semantic conventions treat a tool-call span as an *observation of one execution attempt*, while agent orchestration treats a tool call as a *logical request with a stable identity that may be retried*. These two models collide on the `gen_ai.tool.call.id` attribute. The spec expects it to identify the tool call as the model emitted it (the `tool_call_id` from the assistant message that produced the invocation), so that a downstream consumer can join the span back to the transcript turn. But when an agent retries a failed tool call, the runtime has two mutually exclusive options: reuse the original `tool_call_id` (preserving correlation but producing duplicate span identities), or mint a fresh one (preserving span uniqueness but breaking transcript join). Neither is "correct" under the convention as written, and the failure is silent: the span exports cleanly, the backend accepts it, and the corruption only surfaces later when a consumer de-duplicates or joins. This is not a schema-validation failure. It is an identity-continuity failure that lives one layer above the wire. ## What Was Observed A tool-execution worker running an agent loop emitted, for a single logical call to `search_docs`, three spans over 4.2 seconds: - Attempt 1: `gen_ai.tool.call.id = "call_ab12"`, status `ERROR`, `error.type = "TimeoutError"`. - Attempt 2: `gen_ai.tool.call.id = "call_ab12"`, status `ERROR`, `error.type = "TimeoutError"`. - Attempt 3: `gen_ai.tool.call.id = "call_ab12"`, status `OK`, with `gen_ai.tool.call.result` populated. The trace backend indexed all three under the same `(trace_id, gen_ai.tool.call.id)` tuple. Two consequences followed: 1. A cost/latency dashboard that grouped by `gen_ai.tool.call.id` counted one call, not three, and reported the p99 latency of the *successful* attempt only — the two 3-second timeouts vanished from the aggregate because the group-by collapsed them under the surviving `OK` span. 2. A replay auditor that reconstructed the agent transcript from spans keyed on `tool_call_id` saw a single tool message where the model had actually seen three distinct tool responses (two errors, one success) in its context. The reconstructed transcript diverged from the real one at exactly the retry boundary, and the auditor's "reasoning replay" diverged downstream. The inverse configuration was observed in a sibling service: on retry it minted `call_ab12_r1`, `call_ab12_r2`. Spans were unique, dashboards counted three calls, but the transcript join broke — the assistant message referenced `call_ab12`, and no span carried that id, so every retried call appeared as an orphan tool response in the reconstruction. Both services were "passing" their OTel conformance tests, because the convention does not specify retry semantics for `gen_ai.tool.call.id` at all. ## Condition of Validity The invariant that must hold is: > For any logical tool invocation `L` emitted by a model turn with identifier `c`, the set of exported spans `{S_i}` describing executions of `L` must satisfy: (a) exactly one span carries `gen_ai.tool.call.id = c` and is marked as the terminal/authoritative attempt, and (b) every retry span is linkable to `c` via a non-colliding attribute (e.g., a `retry.attempt` counter plus an OTel span *link* to the terminal span), never by reusing `c` as its own primary key. This holds only when the runtime distinguishes *logical call identity* (stable, model-issued) from *attempt identity* (unique per execution). It does not hold — and the runtime must not pretend it does — when: - The tool is non-idempotent and a retry executes a second side effect; here the two attempts are genuinely distinct logical calls from the backend's perspective, and collapsing them under one `gen_ai.tool.call.id` actively lies about the world. - The model re-emits a *new* `tool_call_id` for what a human would call "the same" call (observed when an agent re-prompts after a context truncation); in that case the ids are already distinct and must not be unified by the runtime. - Spans are sampled such that the terminal attempt is dropped but retries are kept; the "exactly one terminal" clause is then unsatisfiable at the collector, and any consumer must treat absence of the terminal span as "outcome unknown," not "failed." ## Decision Boundary The runtime must make an explicit, recorded decision at the retry site, not at export time. The boundary is: - **Idempotent tool + retryable error (timeout, 429, transient 5xx):** reuse `c` on the terminal span only; emit retry spans with `gen_ai.tool.call.id` *absent*, carrying `retry.attempt = n` and a span link to the terminal span. This preserves both uniqueness and join. - **Non-idempotent tool, or error after partial effect:** do *not* retry under the same `c`. The runtime must surface the ambiguity to the orchestrator, which either aborts or issues a genuinely new call with a new `tool_call_id`. Emitting a second span under `c` here is a correctness bug, not a telemetry nit — it makes the trace assert an idempotency the system does not have. - **Fan-out / parallel speculative execution:** each branch gets its own attempt id; only the committed branch's span carries `c`. Discarded branches must be marked `gen_ai.tool.call.id` absent and `speculation.discarded = true`, or they will be double-counted. The invariant to enforce in code: no two exported spans in the same trace may share a non-null `gen_ai.tool.call.id`. This is checkable at the span processor and cheap to assert. ## Operational Effect Enforcing the invariant changes three things downstream. Cost and latency aggregates become retry-aware (a call that took three attempts reports three attempts' latency, not one). Transcript reconstruction becomes exact, because the join key is unique and the retry history hangs off it via links rather than masquerading as separate calls. And replay auditors stop silently diverging at retry boundaries — the single most common place where an agent's reconstructed reasoning diverges from its actual reasoning, because the reconstruction was missing the error responses the model actually saw. The cost is that the runtime must track logical-vs-attempt identity explicitly through the retry path, and the span processor must reject duplicate `gen_ai.tool.call.id` values within a trace. That rejection is the guardrail; without it, the failure is invisible until an auditor disagrees with a dashboard and neither can prove which is right. ```python from dataclasses import dataclass, field class ToolCallIdentityCollision(RuntimeError): """Raised when two exported spans in one trace claim the same gen_ai.tool.call.id.""" @dataclass class SpanRecord: trace_id: str span_id: str gen_ai_tool_call_id: str | None retry_attempt: int = 0 status: str = "OK" @dataclass class TraceSpanIndex: _seen: dict[tuple[str, str], str] = field(default_factory=dict) def admit(self, span: SpanRecord) -> None: if span.gen_ai_tool_call_id is None: return # retry/speculation spans must not claim the logical id key = (span.trace_id, span.gen_ai_tool_call_id) if key in self._seen: raise ToolCallIdentityCollision( f"trace {span.trace_id}: gen_ai.tool.call.id " f"{span.gen_ai_tool_call_id!r} already claimed by span " f"{self._seen[key]!r}; retry attempt {span.retry_attempt} " f"must omit the id and link to the terminal span instead" ) self._seen[key] = span.span_id def emit_retry_span( index: TraceSpanIndex, trace_id: str, span_id: str, logical_call_id: str, attempt: int, idempotent: bool, ) -> SpanRecord: if not idempotent: # Non-idempotent retries are distinct logical calls; the orchestrator # must mint a new tool_call_id rather than reuse this one. raise ToolCallIdentityCollision( f"refusing to retry non-idempotent call {logical_call_id!r} " f"under the same gen_ai.tool.call.id (attempt {attempt})" ) span = SpanRecord( trace_id=trace_id, span_id=span_id, gen_ai_tool_call_id=None, # retry spans never claim the logical id retry_attempt=attempt, status="ERROR", ) index.admit(span) return span def emit_terminal_span( index: TraceSpanIndex, trace_id: str, span_id: str, logical_call_id: str, attempt: int, ) -> SpanRecord: span = SpanRecord( trace_id=trace_id, span_id=span_id, gen_ai_tool_call_id=logical_call_id, retry_attempt=attempt, status="OK", ) index.admit(span) # raises ToolCallIdentityCollision on duplicate return span ```