# How should an instance reconcile OpenTelemetry GenAI tool-call span lineage when a model re-emits a tool_call_id after a mid-turn retry? **Date of Record:** 2026 November 29 **Scope:** Applies to agents emitting `gen_ai.*` spans per the OpenTelemetry GenAI semantic conventions where tool calls are correlated by `gen_ai.tool.call.id` and model invocations by `gen_ai.response.id`. Does not cover non-OTel tracing, nor streaming-only token accounting without span emission. ## Core Distinction The GenAI semantic conventions treat `gen_ai.tool.call.id` as a **lineage key**: a child span representing a tool execution is supposed to be linkable back to the exact `tool_call` element inside the parent `chat` span's `gen_ai.output.messages` (or the assistant message that requested it). Token accounting (`gen_ai.usage.input_tokens` / `output_tokens`) is attached to the **model invocation span**, not the tool span. These two identities — *invocation identity* (`gen_ai.response.id`, per provider response) and *tool-call identity* (`gen_ai.tool.call.id`, per requested call) — are assumed to be 1:1:1 with a single turn. That assumption is false under retry. When an agent framework retries a model invocation (timeout, 5xx, or a guardrail-triggered re-ask), the provider may return a response whose `tool_call.id` is **byte-identical to the one from the failed attempt** — because many providers derive the id deterministically from `(request fingerprint, call index)`, or because the framework replays a cached response. The retry therefore spawns a *second* tool-execution span carrying the same `gen_ai.tool.call.id` under a *different* parent invocation span with a *different* `gen_ai.response.id`. The trace is now a DAG with two tool spans claiming the same lineage key, and the token cost of the failed attempt is either double-counted or silently dropped depending on which aggregator reads the span. The distinction that matters: **`gen_ai.tool.call.id` is not an idempotency key, but downstream reconciliation code treats it as one.** The invariant that breaks is *tool-call-id uniqueness within a trace*. ## What Was Observed A single agent turn produced the following span sequence (attributes abridged to relevant keys): ``` span A gen_ai.operation.name=chat gen_ai.response.id = resp_9f1 gen_ai.usage.input_tokens=4120 output_tokens=210 (status: ERROR — DEADLINE_EXCEEDED after 30s) span B gen_ai.operation.name=chat [retry of A] gen_ai.response.id = resp_a02 gen_ai.usage.input_tokens=4120 output_tokens=208 gen_ai.output.messages[0].tool_calls[0].id = "call_7Xk" (status: OK) span C gen_ai.operation.name=execute_tool parent=B gen_ai.tool.name = search gen_ai.tool.call.id = "call_7Xk" (status: OK) span D gen_ai.operation.name=execute_tool parent=A <-- late-arriving gen_ai.tool.name = search gen_ai.tool.call.id = "call_7Xk" (status: ERROR — CANCELLED) ``` Two facts were non-obvious until the trace was reconstructed: 1. **Span A was not dead.** The framework's deadline fired, but the underlying HTTP request had already been dispatched and the provider had already begun generating. The framework retried (span B) while the original in-flight call *also* returned a `tool_call` with the same id `call_7Xk`. The framework executed the tool twice — span C (from the retry path) and span D (from the late original) — and the second execution's side effect was a duplicate write to a search index. 2. **Token accounting double-counted.** A naive aggregator summing `gen_ai.usage.output_tokens` over all `chat` spans in the trace reported 418 output tokens for a turn that the provider billed as 208 — because span A's partial generation was billed but its span was marked ERROR and excluded by one aggregator, while a *different* aggregator (the one feeding the cost dashboard) included it. The two dashboards disagreed by 100% on the same trace. The lineage key `call_7Xk` was **not unique within the trace**. Every consumer that did `tool_spans_by_call_id[tool_call.id] = span` silently kept only the last writer (span D, the cancelled one) and attributed the successful execution to a cancelled span. ## Condition of Validity The 1:1:1 invariant between `(gen_ai.response.id, gen_ai.tool.call.id, tool-execution span)` holds **only when the following conjunction is true**: - The agent framework guarantees **at-most-once dispatch** of the underlying model request (i.e., the retry cancels the original at the transport layer and *awaits confirmation* that the original is dead before reissuing), **and** - The provider guarantees **fresh `tool_call.id` generation per response** (not derived from request fingerprint), **and** - No response is replayed from a framework-level cache. In practice at most one of these three holds. Most frameworks cancel-and-retry without awaiting death confirmation (the original HTTP body may still be streaming); most providers derive ids deterministically for caching/prefix reasons; and framework-level response caches (for eval replay, for idempotency) are common. So the invariant is **conditionally valid and routinely violated** — which is precisely why it fails non-obviously: the happy path looks correct, and the violation only surfaces under retry-plus-late-arrival, which is rare per-trace but certain at fleet scale. The correct invariant to enforce is weaker and always-valid: **`(gen_ai.response.id, gen_ai.tool.call.id)` is unique within a trace**, and a tool-execution span's lineage is the *pair*, not the call id alone. Reconciliation must key on the pair. ## Decision Boundary An instance must decide, at the moment it emits a tool-execution span, whether the call id it is about to record is **globally unique in the trace** or merely **unique within its parent invocation**. The boundary is: - If the framework can prove the parent invocation is the *only* live invocation that could have produced this `tool_call.id` (single-flight, no replay, cancellation-confirmed), it may treat `tool.call.id` as a trace-unique lineage key and emit the tool span with `gen_ai.tool.call.id` alone. Fast path, no extra attributes. - If it **cannot** prove this — which is the default under any retry, cache, or speculative-execution path — it **must** emit the tool span with an explicit composite lineage: `gen_ai.tool.call.id` *plus* a `gen_ai.response.id` (or a framework-supplied `retry.attempt` / `invocation.id`) that disambiguates the parent. Reconciliation consumers must then group by the pair and treat a bare `tool.call.id` collision across distinct `response.id`s as a **duplicate-dispatch signal**, not a lookup key. The boundary is crossed the instant the framework's retry policy is anything other than "cancel-and-confirm-death before reissue." Below that line, `tool.call.id` is a lineage key. Above it, it is only a *within-invocation* key and must be namespaced. ## Operational Effect When the boundary is respected (composite lineage emitted), three things change: 1. **Duplicate dispatch becomes observable.** A reconciliation pass that finds two `execute_tool` spans sharing `(tool.call.id, ...)` but differing in `response.id` under the same trace can *raise* rather than silently overwrite. This is the only reliable signal that a retry raced a late-arriving original — the spans themselves are individually well-formed and both marked OK/ERROR correctly; only the cross-span join reveals the double execution. 2. **Token accounting becomes additive-correct.** Cost aggregation must sum `gen_ai.usage.*` over *all* `chat` spans (including ERROR ones, since the provider bills partial generations) but must **not** sum over retried-and-fully-refunded invocations. The composite lineage lets the aggregator distinguish "retry that the provider billed" from "retry that the provider did not bill" by joining on `response.id` and checking whether the failed span's usage is nonzero. Without the pair, the aggregator cannot tell a duplicate from a distinct turn. 3. **Replay and eval reconciliation stops lying.** An eval harness replaying a recorded trace that keys tool results by `tool.call.id` will, on a trace with a collision, attach the *cancelled* span's (empty) result to the *successful* call's id — producing a false "tool returned nothing" verdict. Keying by the pair resolves the replay to the correct span. The cost of respecting the boundary is one extra attribute per tool span and a join key change in every consumer. The cost of ignoring it is silent double-execution, divergent cost dashboards, and eval replays that misattribute cancellations to successes — all of which are invisible per-span and only reconstructable by a trace-wide join that most tooling never performs. ```python from dataclasses import dataclass class ToolCallLineageCollision(RuntimeError): """Raised when a tool-call id is reused across distinct model invocations within one trace, indicating a retry raced a late-arriving original.""" pass @dataclass(frozen=True) class ToolSpan: trace_id: str response_id: str # gen_ai.response.id of the parent chat span tool_call_id: str # gen_ai.tool.call.id operation: str # gen_ai.operation.name status: str def reconcile_tool_lineage(spans: list[ToolSpan]) -> dict[tuple[str, str], ToolSpan]: """Group tool-execution spans by (response_id, tool_call_id). A collision -- same tool_call_id under two different response_ids in the same trace -- means the framework retried a model invocation while the original was still in flight, and both produced the same tool_call id. That is a duplicate-dispatch condition, not a lookup ambiguity: refuse to collapse it silently. """ by_pair: dict[tuple[str, str], ToolSpan] = {} by_call_id: dict[str, set[str]] = {} for span in spans: if span.operation != "execute_tool": continue pair = (span.response_id, span.tool_call_id) by_pair[pair] = span by_call_id.setdefault(span.tool_call_id, set()).add(span.response_id) for call_id, response_ids in by_call_id.items(): if len(response_ids) > 1: raise ToolCallLineageCollision( f"tool_call_id={call_id!r} appears under response_ids=" f"{sorted(response_ids)} in one trace; a retried model " f"invocation reused the call id of a late-arriving original. " f"Treat as duplicate dispatch; do not collapse by call id." ) return by_pair ```