# In what scenarios must an instance treat OTel GenAI `gen_ai.tool.call.id` as non-injective across a retried tool invocation, and refuse to merge spans by that key alone **Date of Record:** 2026 December 3 **Scope:** Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for tool calls, where the exporter, collector, or a downstream trace-assembly step keys on `gen_ai.tool.call.id` to correlate a tool request span with its response span, or to deduplicate retried attempts. Does not apply to single-shot, non-retried tool calls with a provider-assigned unique ID, nor to spans correlated purely by `trace_id` + `span_id` parentage. ## Core Distinction The GenAI semantic conventions define `gen_ai.tool.call.id` as the identifier of a tool call, and the common operational assumption is that it is a *key*: unique per logical invocation, stable across the request/response span pair, and thus safe to use for join and dedup. That assumption conflates two different things — **logical invocation identity** (one intent to call a tool) and **transport attempt identity** (one wire request). When an agent retries a tool call — because of a timeout, a 429, a truncated stream, or a reasoning-loop re-plan — most SDKs and provider gateways reuse the *same* `gen_ai.tool.call.id` on every attempt, because the ID is minted at the logical-call layer and passed down verbatim. The span attributes then become **non-injective**: one `call.id` maps to N request spans and N response spans, and any consumer that treats it as a primary key will silently collapse or mis-pair them. The distinction that matters: `gen_ai.tool.call.id` is an *intent* identifier, not an *attempt* identifier. Attempt identity lives in `span_id` (and, if emitted, an attempt counter). Merging on `call.id` is valid only when N=1. ## What Was Observed A tool-executing agent issued a call to a search tool. Attempt 1's request span was emitted, the provider gateway timed out at 30s (agent-side deadline), the agent's retry policy fired, and attempt 2 was issued with the *identical* `gen_ai.tool.call.id` — the ID had been generated once at the planning step and threaded through the retry wrapper. Attempt 2 succeeded. The trace contained: - request span A: `gen_ai.tool.call.id = "call_7f3a"`, `gen_ai.tool.name = "search"`, status `ERROR` (deadline exceeded), `span_id = 0x1a` - request span B: `gen_ai.tool.call.id = "call_7f3a"`, `gen_ai.tool.name = "search"`, status `OK`, `span_id = 0x2b` - response span A': `gen_ai.tool.call.id = "call_7f3a"`, `gen_ai.tool.result` absent (timeout), `span_id = 0x1c`, parent `0x1a` - response span B': `gen_ai.tool.call.id = "call_7f3a"`, `gen_ai.tool.result = `, `span_id = 0x2c`, parent `0x2b` A downstream trace-assembly job that joined request→response on `gen_ai.tool.call.id` produced two cross-pairs: it matched A (error) with B' (success payload) and B (ok) with A' (empty). The reconstructed "tool call" record showed a successful call with an error status and a payload attached to the failed attempt. The agent's own audit log, which replayed from these assembled records, concluded the tool had returned a payload *before* the deadline — a false negative on the timeout, which suppressed a circuit-breaker trip on the next cycle. The failure is not the retry. The retry is correct. The failure is the **join key**: `call.id` was non-injective (1→2) and the assembler assumed injectivity. ## Condition of Validity Keying or deduplicating GenAI tool-call spans by `gen_ai.tool.call.id` alone is valid **iff** the following invariant holds for the trace under assembly: ``` ∀ id ∈ {gen_ai.tool.call.id} : |{spans with that id, role=request}| == 1 ``` Equivalently: no retry, no hedging, no speculative parallel execution, and no re-plan reuse of the same call ID. In practice this holds only when the agent's retry/hedge layer mints a **fresh** `gen_ai.tool.call.id` per attempt (or emits an explicit attempt attribute). When the ID is minted at planning time and reused, the invariant is violated and the join is undefined. The correct composite key is `(trace_id, gen_ai.tool.call.id, span_id)` for request/response pairing via parentage, or `(gen_ai.tool.call.id, attempt_index)` if an attempt attribute is emitted. `call.id` alone is a *grouping* key, not a *join* key, whenever retries are possible. ## Decision Boundary An instance must refuse to merge or dedup on `gen_ai.tool.call.id` alone and must escalate to the composite key when **any** of the following is true: 1. More than one request span shares a `gen_ai.tool.call.id` within a `trace_id` (the non-injectivity trigger — detect by counting, do not assume). 2. The agent's retry policy is configured with `max_attempts > 1` and the ID is minted upstream of the retry wrapper. 3. The tool-call layer performs hedging or speculative parallel fan-out (two attempts in flight for one intent). 4. A response span's `parent_span_id` disagrees with the request span that shares its `call.id` (parentage is authoritative; the ID is not). Below that boundary — single attempt, provider-minted unique ID, parentage intact — the simple join is safe and the composite key is unnecessary overhead. The boundary is *attempt multiplicity*, not tool type or provider. ## Operational Effect When the invariant is enforced, the observable consequences are: (a) timeout/error attempts remain distinct from success attempts, so circuit breakers and replay audits see the true attempt history; (b) the assembled "logical tool call" is a *set* of attempts with an explicit resolution rule (last-success-wins, first-terminal-wins), not a single fused record; (c) trace-assembly cost rises marginally (composite key) but correctness is restored. When the invariant is *not* enforced, the failure is silent: no exception is raised at join time, the cross-pair produces a plausible-looking record, and the corruption surfaces only later as a wrong circuit-breaker decision or a replay discrepancy. The guardrail is therefore a *pre-join assertion* on attempt multiplicity, not a post-hoc check. ```python from __future__ import annotations from dataclasses import dataclass from typing import Iterable class NonInjectiveCallIdError(RuntimeError): """Raised when gen_ai.tool.call.id maps to more than one request span within a trace, making a call.id-only join undefined.""" @dataclass(frozen=True) class ToolSpan: trace_id: str span_id: str parent_span_id: str | None call_id: str role: str # "request" | "response" status: str # "OK" | "ERROR" def assert_injective_call_ids(spans: Iterable[ToolSpan]) -> None: """Enforce the invariant: within each trace, each call.id has exactly one request span. Violation => composite key required; refuse to join. """ requests_by_key: dict[tuple[str, str], list[ToolSpan]] = {} for s in spans: if s.role != "request": continue requests_by_key.setdefault((s.trace_id, s.call_id), []).append(s) for (trace_id, call_id), reqs in requests_by_key.items(): if len(reqs) > 1: span_ids = sorted(r.span_id for r in reqs) raise NonInjectiveCallIdError( f"gen_ai.tool.call.id={call_id!r} in trace_id={trace_id!r} " f"maps to {len(reqs)} request spans {span_ids}; " f"call.id-only join is undefined. Use composite key " f"(trace_id, call_id, span_id) or (call_id, attempt_index)." ) def join_request_response( spans: Iterable[ToolSpan], ) -> dict[str, list[tuple[ToolSpan, ToolSpan | None]]]: """Join request->response by parentage (span_id), never by call.id alone. Returns {call_id: [(request, response_or_None), ...]} preserving attempts. """ spans = list(spans) assert_injective_call_ids(spans) # fail fast on non-injectivity by_span = {s.span_id: s for s in spans} out: dict[str, list[tuple[ToolSpan, ToolSpan | None]]] = {} for s in spans: if s.role != "request": continue # parentage is authoritative; call.id is only a grouping label resp = next( (r for r in spans if r.role == "response" and r.parent_span_id == s.span_id), None, ) out.setdefault(s.call_id, []).append((s, resp)) return out ```