# Which invariants prevent gen_ai.tool.call.id collisions from orphaning child spans when an agent reuses tool-call identifiers across retries **Date of Record:** 2026 November 25 **Scope:** Applies to agents that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.call.id`, `gen_ai.agent.invocation.id`) and that perform retries or replay of tool calls within a single trace. Does not apply to single-shot, non-retrying tool invocations or to traces where tool-call IDs are guaranteed globally unique by an external authority. ## Core Distinction The GenAI semantic conventions treat `gen_ai.tool.call.id` as the join key between a model's *requested* tool call (recorded on the LLM span as part of the completion) and the *executed* tool span that actually runs the function. The convention is silent on uniqueness scope: it does not state whether the ID must be unique within a trace, within a span, or globally. This silence is the boundary. When an agent retries a failed tool call and reuses the same `gen_ai.tool.call.id` — because the model regenerated the identical call, or because the orchestration layer replays the request verbatim — two or more child tool spans now share a join key. Any backend that correlates spans by that key (dashboarding, eval harnesses, cost attribution, replay verification) collapses them into one logical call, and the *second* execution's child spans become orphans: their parent linkage is intact in the OTel tree, but the semantic correlation to the model's decision is ambiguous. The invariant that fails is not trace-tree integrity (that survives); it is *decision-to-execution cardinality*. ## What Was Observed In a trace from a 3-tool agent loop, the LLM span `chat gpt-4o` emitted a completion containing a tool call with `gen_ai.tool.call.id = call_abc123` (a `search_docs` invocation). The tool span executed, returned a 503 from the downstream retriever, and the orchestrator retried. The retry re-entered the model, which — deterministically, same prompt, temperature 0 — regenerated the byte-identical tool call, including `call_abc123`. A second tool span was emitted with the same `gen_ai.tool.call.id`. The first span carried `status = ERROR`; the second carried `status = OK`. A downstream eval harness that joined model decisions to executions via `gen_ai.tool.call.id` reported the call as successful exactly once and *dropped the errored attempt entirely* — it keyed on the ID, saw two rows, and took the last-writer. The retry cost was invisible in cost attribution. Worse, a separate auditor agent replaying the trace to verify the final answer reconstructed a single `search_docs` call from a corpus state that had *changed* between the two attempts (a document was reindexed mid-retry), producing a replay discrepancy that looked like model hallucination but was a correlation artifact. The failure was non-obvious because the OTel span tree was structurally valid: correct parent/child, correct timestamps, monotonic span IDs. Only the *semantic* key was duplicated. Standard OTel validation passes; only GenAI-aware consumers break. ## Condition of Validity The collision only manifests as a failure under a conjunction: 1. The agent retries or replays a tool call, AND 2. The retry reuses the same `gen_ai.tool.call.id` (deterministic regeneration, verbatim replay, or an orchestrator that copies the ID forward), AND 3. A consumer joins on `gen_ai.tool.call.id` under an assumption of uniqueness within the trace (the common case — most dashboards and eval harnesses do exactly this), AND 4. The two attempts differ in outcome, latency, or external side-effect state. If any conjunct fails, the trace is benign. The dangerous case is (2) combined with (3): the convention's silence lets the producer believe reuse is fine (it is, for tree integrity) while the consumer assumes uniqueness (it is not, for correlation). Note the failure is *producer-consumer asymmetric*: no single component is wrong, the contract is underspecified. ## Decision Boundary The boundary is whether `gen_ai.tool.call.id` is treated as a *model-decision identifier* or an *execution identifier*. The model only ever emits a decision ID — it cannot know that execution will be retried. Therefore the invariant must be enforced at the producer: **the model-decision ID and the execution ID must be separated, and the execution span must carry a distinct execution-scoped attribute while preserving the decision ID as a correlation attribute.** Concretely: keep `gen_ai.tool.call.id` as the model-emitted decision ID (stable across retries, because it *is* the same decision), and add an execution-scoped attribute — e.g. `gen_ai.tool.call.attempt` (monotonic integer) or a fresh `gen_ai.tool.execution.id` — on each tool span. Consumers that must deduplicate join on `(gen_ai.tool.call.id, gen_ai.tool.call.attempt)`; consumers that must count decisions join on `gen_ai.tool.call.id` alone and accept the fan-out. The invariant: *no two sibling tool spans under the same LLM span may share the full semantic key tuple.* Reusing the decision ID is correct; reusing it as the sole key is the bug. An alternative — minting a fresh `gen_ai.tool.call.id` on retry — is *wrong* under this boundary, because it severs the correlation between the model's decision and its retried execution, making it impossible to attribute retry cost to the originating decision. The decision ID must remain stable; only the execution key must be made unique. ## Operational Effect Enforcing the invariant changes three downstream behaviors. First, cost attribution becomes correct: retried calls are counted per attempt, and the retry tax is visible against the decision that incurred it. Second, replay verification becomes sound: an auditor replaying `(decision_id, attempt)` can reconstruct the exact external state each attempt observed, rather than collapsing two attempts into one and comparing against a single mutated corpus. Third, eval harnesses stop silently dropping errored attempts — the fan-out is explicit, and a "succeeded on attempt 2 after 503 on attempt 1" is representable rather than flattened. The residual risk is consumer-side: any harness still joining on `gen_ai.tool.call.id` alone will fan out and must be taught the tuple. This is a migration cost, not a correctness regression, because the pre-invariant behavior was already lossy — it just failed silently. The guardrail is to reject at emit time any tool span whose `(gen_ai.tool.call.id, gen_ai.tool.call.attempt)` tuple already exists among its siblings, forcing the producer to assign a fresh attempt number rather than silently overwrite. ```python from dataclasses import dataclass, field class ToolCallKeyCollision(RuntimeError): """Raised when two sibling tool spans share the full semantic key tuple.""" pass @dataclass class ToolSpan: decision_id: str # gen_ai.tool.call.id — stable across retries attempt: int # gen_ai.tool.call.attempt — execution-scoped status: str @dataclass class LLMSpan: span_id: str tool_spans: list[ToolSpan] = field(default_factory=list) _seen: set[tuple[str, int]] = field(default_factory=set) def emit_tool_span(self, decision_id: str, attempt: int, status: str) -> None: key = (decision_id, attempt) if key in self._seen: raise ToolCallKeyCollision( f"sibling tool span under LLM span {self.span_id} reuses " f"semantic key (gen_ai.tool.call.id={decision_id!r}, " f"gen_ai.tool.call.attempt={attempt}); assign a fresh attempt " f"number to preserve decision-to-execution cardinality" ) self._seen.add(key) self.tool_spans.append(ToolSpan(decision_id, attempt, status)) # Correct: retry reuses decision_id but increments attempt. llm = LLMSpan(span_id="span_llm_01") llm.emit_tool_span("call_abc123", attempt=1, status="ERROR") # 503 llm.emit_tool_span("call_abc123", attempt=2, status="OK") # retry # Incorrect: verbatim replay reuses both decision_id and attempt. bad = LLMSpan(span_id="span_llm_02") bad.emit_tool_span("call_abc123", attempt=1, status="ERROR") bad.emit_tool_span("call_abc123", attempt=1, status="OK") # raises ```