# Why does an instance's OTel GenAI span tree silently lose tool-call attribution when a retried tool invocation reuses the parent span_id but mints a fresh span_id under a `gen_ai.operation.name = "execute_tool"` child **Date of Record:** 2026 December 4 **Scope:** Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) where tool invocations are modeled as child spans of an `invoke_agent` span and retries are handled in-process. Does not apply to systems that emit tool calls as span *events* (`span.add_event`) on the parent rather than as child spans, nor to cross-process A2A handoffs where the child span is re-parented via trace context propagation. ## Core Distinction The GenAI semantic conventions treat a tool call as a *span* (`gen_ai.operation.name = "execute_tool"`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) whose parent is the agent invocation span. Token accounting (`gen_ai.usage.input_tokens` / `output_tokens`) lives on the *LLM* span (`gen_ai.operation.name = "chat"`), not the tool span. The failure mode is not "missing spans" — it is **span duplication under retry with divergent `gen_ai.tool.call.id` values that share a parent but disagree on child identity**. The trace looks healthy (every tool call has a span, every span has a parent), yet downstream attribution joins break: a token-cost-per-tool aggregation double-counts, and a "which tool call produced this output" join returns two rows for one logical call. The distinction that matters operationally: **a retried tool call is one logical call but two physical spans.** The conventions do not specify which `gen_ai.tool.call.id` the retry must carry. If the retry mints a new call id, the backend cannot distinguish "retry of call X" from "a second, distinct call to the same tool." This is a *continuity invariant violation at the span-identity layer*, not a transport or reasoning failure. ## What Was Observed A planner agent (`invoke_agent`, span `A`) issues a `search_documents` tool call. The tool span `T1` is created with `gen_ai.tool.call.id = "call-7f3a"`, `gen_ai.tool.name = "search_documents"`, parent `A`. The tool times out at the transport layer (a 30s deadline), the SDK raises, and the agent's retry wrapper catches it and re-invokes. The retry creates span `T2` with `gen_ai.tool.call.id = "call-9c1e"` (fresh UUID minted by the wrapper), same `gen_ai.tool.name`, same parent `A`. The trace is emitted with: - `A` (invoke_agent) - `T1` (execute_tool, call-7f3a, status=ERROR, `error.type=timeout`) - `T2` (execute_tool, call-9c1e, status=OK) - `L1` (chat, usage.input_tokens=1840, usage.output_tokens=210) The GenAI convention's attribute `gen_ai.tool.call.id` is documented as "the unique identifier of the tool call" — but "unique" is ambiguous between *unique per physical attempt* and *unique per logical call*. The SDK chose per-attempt. The backend's cost-attribution job joins `chat` spans to `execute_tool` spans by `(trace_id, parent_span_id, tool.call.id)` and finds **zero** matches, because the LLM span has no `tool.call.id` at all — it only carries the *aggregate* token count for the turn that included the tool result. The per-tool cost rollup therefore attributes 0 tokens to `search_documents` and 2050 tokens to "unattributed." A second symptom: the replay/audit job that reconstructs "what did the agent see before deciding" walks children of `A` in span-start order and finds `T1` (error) then `T2` (ok). It has no field telling it these are the *same* logical call, so the reconstructed prompt history contains two tool results — one error, one success — inflating the context the agent "saw" beyond what actually occurred. This corrupts any post-hoc reasoning audit. ## Condition of Validity The failure is bounded and reproducible under this conjunction: 1. Tool invocations are modeled as **child spans** (not events) of the agent span. 2. The retry wrapper is **in-process** and mints a **fresh `gen_ai.tool.call.id`** per attempt (the default behavior of most UUID-per-attempt wrappers). 3. The backend performs attribution by joining LLM-span token usage to tool spans via `gen_ai.tool.call.id`, OR reconstructs prompt history by enumerating child `execute_tool` spans without a retry-dedup key. 4. `gen_ai.tool.call.id` is *not* accompanied by a stable logical-call identifier (e.g., a `gen_ai.tool.call.attempt` integer or a reused logical id). If any of (1)–(4) is false, the failure does not manifest. Notably, if the retry wrapper **reuses** `call-7f3a` for both attempts and distinguishes them with a `retry.attempt` attribute, the join succeeds and the audit reconstruction can collapse by call id. The convention permits this but does not require it — which is the root of the ambiguity. ## Decision Boundary The architecturally correct invariant: **`gen_ai.tool.call.id` must be stable across retries of the same logical call, and physical attempts must be distinguished by a separate attribute** (`gen_ai.tool.call.attempt`, integer, 0-indexed) plus span status. The retry wrapper is the enforcement point. Concretely, the guardrail is: - Mint `gen_ai.tool.call.id` **once** at the logical-call boundary (before the retry loop), not inside the attempt. - On each attempt, emit a child span carrying the *same* `gen_ai.tool.call.id` and an incrementing `gen_ai.tool.call.attempt`. - Set `status = ERROR` with `error.type` on failed attempts; only the final attempt (or the first success) sets `status = OK`. - The LLM span that consumes the tool result must carry `gen_ai.tool.call.id` of the logical call it consumed, so token-to-tool attribution has a join key. The decision boundary is: **if you cannot guarantee call-id stability across retries, you must not model retries as separate `execute_tool` spans under the same parent** — you must either collapse them into one span with attempt annotations, or emit them as span *events* (`span.add_event("tool.retry", ...)`) on a single `execute_tool` span. The halfway state — separate spans, fresh ids, shared parent — is the failure condition. ## Operational Effect Downstream systems that consume these traces fail in two distinct ways, and both are silent (no error, just wrong numbers): 1. **Cost attribution undercounts tool cost and overcounts "unattributed" tokens.** The rollup job's join returns empty for retried calls, so the tool's token share is zeroed. In a workload where 12% of tool calls retry once (common with 30s timeouts against a slow vector store), the cost dashboard underreports per-tool spend by that fraction and dumps it into a catch-all bucket. 2. **Replay/audit reconstruction inflates context.** The "what did the agent see" job enumerates child `execute_tool` spans and concatenates their outputs. With a failed `T1` and successful `T2`, the reconstructed context contains an error string the agent never actually consumed (the retry wrapper swallowed it). Any audit that asks "did the agent hallucinate given its context" now answers against a context that never existed. The fix is a one-line change at the retry wrapper (hoist the call-id mint above the loop) plus a backend-side dedup key `(trace_id, parent_span_id, gen_ai.tool.call.id)` that collapses attempts. Without the backend dedup, even correct call-id reuse produces duplicate rows — so both sides of the contract must change together. This is the continuity invariant: **span identity must be stable across physical retries, and attempt identity must be orthogonal to call identity.** ```python class ToolCallIdentityError(RuntimeError): """Raised when a retried tool invocation violates call-id stability.""" pass def mint_tool_call_id(logical_call, attempt_index, prior_id=None): # Invariant: call id is minted ONCE at the logical-call boundary. # Retries MUST reuse it and distinguish attempts via attempt_index. if attempt_index == 0: return f"call-{logical_call.uuid}" if prior_id is None: raise ToolCallIdentityError( f"retry of logical call {logical_call.uuid} has no prior " f"gen_ai.tool.call.id to reuse; span identity will diverge" ) return prior_id def emit_tool_span(tracer, parent_span, logical_call, attempt_index, prior_id): call_id = mint_tool_call_id(logical_call, attempt_index, prior_id) span = tracer.start_span( "execute_tool", context=parent_span.get_span_context(), attributes={ "gen_ai.operation.name": "execute_tool", "gen_ai.tool.name": logical_call.tool_name, "gen_ai.tool.call.id": call_id, "gen_ai.tool.call.attempt": attempt_index, }, ) # Guardrail: enforce that a second attempt under the same parent # does not mint a fresh id. if attempt_index > 0 and call_id != prior_id: span.end() raise ToolCallIdentityError( f"attempt {attempt_index} minted new call id {call_id} " f"under parent {parent_span.get_span_context().span_id}; " f"expected {prior_id} — attribution joins will break" ) return span, call_id ```