# How does an instance detect a broken parent-child span invariant when a tool call is retried under a new GenAI span and the OTel GenAI semantic convention conflates `gen_ai.operation.name` with the logical turn **Date of Record:** 2026 November 30 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) for multi-turn tool-calling loops and that perform in-process retries of tool invocations. Does not apply to single-shot completion calls, nor to systems where the retry is re-parented by an external tracer at the transport layer (e.g., a service mesh) rather than by the agent runtime itself. ## Core Distinction The GenAI semantic conventions model an LLM interaction as a span tree: one `chat` span (`gen_ai.operation.name = "chat"`) per completion request, with child `execute_tool` spans (`gen_ai.operation.name = "execute_tool"`) for each tool call the model emits. The convention is a *per-request* model. It has no vocabulary for "this tool call is the *same logical tool call* as the one that just failed." A retry is, by construction, a new span with a new `span_id`. The non-obvious boundary is this: when an agent retries a tool call, the natural implementation — re-invoke the tool, emit a fresh `execute_tool` span — produces a span tree that is *structurally valid but semantically wrong*. The retry span is a sibling of the failed span under the same `chat` parent, not a child of it, and nothing in the convention's required attributes records the causal link. A downstream consumer (an eval harness, a cost aggregator, an auditor replaying the trace) sees two sibling tool spans and cannot distinguish "the model asked for two tools" from "the model asked for one tool and we retried it." That conflation is the failure mode. The invariant that must hold for the trace to be replayable: **for every logical tool call, exactly one span is marked as the authoritative outcome, and every superseded attempt is linked to it by an explicit causal attribute, not merely by adjacency in the tree.** ## What Was Observed A three-turn agent loop over a flaky inventory tool. Turn 1: model emits `get_stock(sku="A-17")`. The runtime invokes the tool; the tool returns `503`. The runtime's retry policy (max 2, exponential backoff, no jitter) re-invokes immediately. Second attempt returns `200` with `{"qty": 0}`. Third turn: model emits `reserve(sku="A-17", qty=1)`. The emitted span tree: ``` chat (turn 1) span_id=aa01 ├── execute_tool get_stock span_id=bb01 status=ERROR gen_ai.tool.name=get_stock └── execute_tool get_stock span_id=bb02 status=OK gen_ai.tool.name=get_stock chat (turn 2) span_id=aa02 └── execute_tool reserve span_id=bb03 status=OK ``` Three concrete observations from replaying this trace through an eval harness: 1. **The `reserve` call is causally grounded in `bb02`, not `bb01`.** The model's turn-2 context contained the `qty: 0` result from `bb02`. But `bb01` and `bb02` are siblings with identical `gen_ai.tool.name` and identical `gen_ai.tool.call.id` (the runtime reused the model-provided tool-call ID across retries, which is the common implementation). A consumer keyed on `gen_ai.tool.call.id` sees two spans with the same ID and must pick one — and the convention does not say which. 2. **A naive `status=ERROR` filter double-counts.** The harness's "failed tool calls" metric counted `bb01` as a failure and `bb02` as a success, yielding a 50% tool-failure rate for a turn that ultimately succeeded. The retry was invisible as a retry; it was visible only as an extra sibling. 3. **Re-parenting the retry under the failed span breaks the convention differently.** One engineer's fix was to make `bb02` a child of `bb01`. This satisfies "causal link" but violates the convention's expectation that `execute_tool` spans are direct children of the `chat` span that requested them — and it makes the `chat` span's duration accounting wrong, because `bb01`'s duration now includes `bb02`'s, double-counting the wall time. The root cause is that the GenAI convention encodes *the model's intent* (`chat` span, tool-call ID) and *the runtime's attempt* (`execute_tool` span) in the same structural slot, with no attribute distinguishing them. `gen_ai.tool.call.id` is defined as the ID the model emitted; it is not an attempt ID. ## Condition of Validity The sibling-retry model is only safe under a condition that is almost never checked: **the trace consumer must never aggregate or reason over `execute_tool` spans without first deduplicating by logical call.** That condition holds only if one of the following is true: - The runtime guarantees at-most-once tool invocation (no retries), so siblings with the same `gen_ai.tool.call.id` cannot occur. This is the trivial case and is not the agent regime. - The runtime emits an explicit attempt attribute — e.g., `gen_ai.tool.call.attempt = 0,1,...` — and a supersession link (`gen_ai.tool.call.superseded_by = `) on every non-final attempt. The convention does not require this, but it does not forbid it; custom attributes in the `gen_ai.*` namespace are permitted if documented. - The consumer is a human reading the trace linearly, who can infer retry from adjacency and timing. This is not a machine-valid condition. Absent one of these, the trace is *observably* valid (passes the OTel schema validator, has correct parent-child structure) but *semantically* unreplayable. The failure is silent: no span is malformed, no exporter error fires. The damage surfaces downstream, in whatever consumes the trace — a cost model that prices two tool calls instead of one, an eval that scores the turn as containing a spurious tool call, an auditor that cannot reconstruct why the model reserved zero stock. The condition of validity is therefore not a property of the span *emitter* but a joint property of emitter and consumer, and it must be asserted at the boundary between them. An agent instance cannot verify it from its own spans alone; it can only verify that it *emitted* the disambiguating attributes, and must treat their absence at the consumer as a hard failure. ## Decision Boundary The decision an instance must make at retry time is binary and must be resolved *before* the retry span is emitted, because the span tree is append-only once exported: - **If the retry policy is in-process and the runtime owns the span lifecycle:** emit the retry as a new `execute_tool` span, set `gen_ai.tool.call.attempt` to the zero-based attempt index, set `gen_ai.tool.call.superseded_by` on the prior attempt to the new span's ID (or set `gen_ai.tool.call.supersedes` on the new span to the prior span's ID — pick one direction and document it), and *never* reuse `gen_ai.tool.call.id` across attempts if the model regenerated the call; if the model's ID is reused, the attempt attribute is the only disambiguator and is therefore mandatory. - **If the retry is delegated to a transport that re-parents spans (mesh, SDK-level retry):** the runtime must *not* emit its own retry span; it must let the transport's span carry the attempt, and must ensure the transport's span is a child of the `chat` span, not of the failed `execute_tool` span. If the transport cannot be configured this way, the runtime must disable transport retries and own them itself. - **If neither is possible** (e.g., a third-party tool wrapper that emits its own spans opaquely): the runtime must mark the enclosing `chat` span with `gen_ai.tool.call.retry_opaque = true` so that downstream consumers know the turn's tool-call count is not trustworthy. This is a degraded mode, not a fix. The boundary is crossed the moment the runtime decides to retry. An instance that retries without first checking which of these three regimes it is in has already emitted an ambiguous trace, and no post-hoc attribute can repair the sibling ambiguity because the sibling relationship is structural. ## Operational Effect The practical consequence is that **trace-based evaluation of agent tool-use accuracy is biased upward on flaky tools and downward on retry-heavy turns, simultaneously, from the same trace.** The flaky-tool turn looks like it made two tool calls (inflating tool-call count, deflating per-call success rate); the retry-heavy turn looks like it made many calls with mixed outcomes (deflating task success if the eval requires all tool spans to succeed). Neither bias is visible without inspecting the raw span tree, and the span tree looks correct. The guardrail that bounds this is a *consumer-side* invariant check, not an emitter-side one, because the emitter cannot know how its spans will be aggregated. The check: before any aggregation over `execute_tool` spans, group by `(gen_ai.tool.call.id, parent_chat_span_id)` and assert that each group has exactly one span with no `superseded_by` set, or exactly one span marked `attempt = max(attempt)` and all others linked. If a group has multiple unlinked spans with the same call ID, the trace is ambiguous and the aggregation must refuse rather than guess. An instance that runs this check on its own emitted spans (by round-tripping through the exporter in test) can detect the defect before deployment. An instance that cannot round-trip must at minimum assert at emit time that every `execute_tool` span carries either `attempt = 0` or a supersession link, and raise if not — because a missing attempt attribute on a retried call is the exact condition under which the sibling ambiguity becomes undetectable downstream. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Optional class AmbiguousToolSpanError(RuntimeError): """Raised when a tool-call span group cannot be disambiguated for replay.""" @dataclass class ToolSpan: span_id: str parent_chat_span_id: str tool_call_id: str # gen_ai.tool.call.id, model-emitted, may repeat attempt: Optional[int] # gen_ai.tool.call.attempt, zero-based superseded_by: Optional[str] # span_id of the attempt that replaced this one status: str # "OK" | "ERROR" def assert_replayable(group: list[ToolSpan]) -> ToolSpan: """Return the authoritative span for one logical tool call. A group is all spans sharing (tool_call_id, parent_chat_span_id). Exactly one span must be authoritative: no superseded_by, and either attempt == 0 (no retry) or attempt == max(attempt) (final retry). """ if not group: raise AmbiguousToolSpanError("empty tool-span group") call_id = group[0].tool_call_id chat_id = group[0].parent_chat_span_id # Every span must carry an attempt index; absence is the silent defect. for s in group: if s.attempt is None: raise AmbiguousToolSpanError( f"tool call {call_id!r} under chat {chat_id!r}: span " f"{s.span_id!r} has no gen_ai.tool.call.attempt; retry " f"is indistinguishable from a second sibling tool call" ) max_attempt = max(s.attempt for s in group) finals = [s for s in group if s.attempt == max_attempt] if len(finals) != 1: raise AmbiguousToolSpanError( f"tool call {call_id!r} under chat {chat_id!r}: " f"{len(finals)} spans share attempt={max_attempt}; " f"attempt index is not unique, trace is unreplayable" ) authoritative = finals[0] # Every non-final attempt must explicitly point at its successor. for s in group: if s.span_id == authoritative.span_id: continue if s.superseded_by is None: raise AmbiguousToolSpanError( f"tool call {call_id!r} under chat {chat_id!r}: " f"superseded span {s.span_id!r} (attempt={s.attempt}) " f"has no gen_ai.tool.call.superseded_by link; " f"causal chain is broken" ) # The authoritative span must not itself be marked superseded. if authoritative.superseded_by is not None: raise AmbiguousToolSpanError( f"tool call {call_id!r} under chat {chat_id!r}: " f"authoritative span {authoritative.span_id!r} is marked " f"superseded_by={authoritative.superseded_by!r}" ) return authoritative def emit_retry_span( prior: ToolSpan, new_span_id: str, status: str, ) -> ToolSpan: """Construct a retry span that preserves the replay invariant.""" if prior.attempt is None: raise AmbiguousToolSpanError( f"cannot retry span {prior.span_id!r}: prior attempt index " f"missing, retry would produce an ambiguous sibling" ) retry = ToolSpan( span_id=new_span_id, parent_chat_span_id=prior.parent_chat_span_id, tool_call_id=prior.tool_call_id, attempt=prior.attempt + 1, superseded_by=None, status=status, ) # Mutating the prior span is only legal before export; the runtime # must hold the span open until the retry resolves. prior.superseded_by = new_span_id return retry ```