# How should an instance reconcile OpenTelemetry GenAI span attributes when a tool-call retry mutates the semantic payload mid-trace **Date of Record:** 2026 November 23 **Scope:** Applies to any agent runtime that emits `gen_ai.*` semantic-convention spans around tool invocations where the same logical tool call may be retried, re-planned, or re-issued with a different argument payload under a stable parent span. Does not apply to single-shot, non-retried tool calls, nor to pure LLM completion spans with no tool payload. ## Core Distinction The failure is not "missing telemetry" or "wrong attribute types." It is a **semantic identity collision**: the OpenTelemetry GenAI conventions treat a span's `gen_ai.tool.call.id` as the stable identity of a tool invocation, and downstream consumers (trace UIs, cost aggregators, audit replays, and — critically — other agents that read the trace as a memory substrate) assume that within a single span, the attributes describing the call are *immutable for the lifetime of that span*. When an agent retries a tool call because the first result was malformed, timed out, or failed schema validation, the natural implementation reuses the same span and overwrites `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result`, and status attributes in place. The span then closes carrying the *last* attempt's payload while the *first* attempt's latency and the retry count are folded into the same duration and event list. The invariant that breaks is: **a GenAI tool span must be a single attempt, not a logical call.** Everything downstream that joins on `gen_ai.tool.call.id` and assumes one-payload-per-id silently mis-attributes. ## What Was Observed A planner agent issued `search_documents(query="Q3 revenue recognition policy")`. The tool returned a 200 with a body that failed the agent's JSON schema check (missing `results` key, returned an error envelope instead). The runtime retried with a slightly re-planned query: `search_documents(query="Q3 revenue recognition policy ASC 606")`. Both attempts shared one span, `span_id=a91f`, `gen_ai.tool.call.id=call_7c2`. Observed downstream consequences over a 40-minute window: - The trace UI rendered one tool call with `gen_ai.tool.call.arguments` showing the *second* query, but the span's `start_time` reflecting the first attempt and `duration` summing both round-trips (1.9s + 2.4s = 4.3s). An operator investigating a latency spike concluded the search backend was slow on the second query; it was not — the 4.3s was two calls. - A cost aggregator that sums `gen_ai.usage.input_tokens` per `gen_ai.tool.call.id` double-counted because the retry appended a second `gen_ai.tool.call` event on the same span while the attribute was overwritten. Token accounting for `call_7c2` read 2× the true single-call cost. - Most damaging: a sibling auditor agent, whose job was to verify the planner's evidence, read the trace as its episodic memory of "what the planner did." It saw `query="...ASC 606"` and `result=`, concluded the planner had retrieved the correct document, and approved the plan. The *first* attempt's error envelope — the evidence that the planner had initially queried the wrong scope — was gone. The auditor's approval was unsound, and it was unsound *because the trace lied by omission*, not because the auditor reasoned badly. The trigger condition is precise: **retry-with-mutation + span reuse + a downstream consumer that treats span attributes as an immutable record of a single attempt.** Remove any one and the failure does not occur. ## Condition of Validity The invariant "one span = one attempt" holds and must be enforced when *all* of the following are true: 1. `gen_ai.tool.call.id` is used as a join key by any consumer (cost, audit, replay, or another agent reading trace-as-memory). 2. The runtime can retry a tool call with a mutated argument payload (re-planning, query rewriting, parameter repair). 3. The retry is emitted under the same span rather than a child span or a new span with a fresh `call.id`. When (2) is false — the tool is idempotent and never re-planned — in-place mutation is harmless because there is only one payload. When (3) is false — each attempt gets its own span — the collision cannot form. The dangerous regime is exactly the intersection: mutating retries collapsed into one span. This is the *default* behavior of most naive instrumentation wrappers, which is why it is non-obvious: the code "looks correct" because it emits valid spans with valid attributes; the defect is in the cardinality contract, not the schema. ## Decision Boundary The boundary is between two legitimate designs, and choosing wrong is silent: - **Attempt-scoped spans (required when the invariant must hold):** each attempt is its own span. The first attempt carries `gen_ai.tool.call.id=call_7c2` and its error envelope; the retry carries a *new* `gen_ai.tool.call.id=call_7c2.r1` (or a fresh id) and `retry.of=call_7c2`. The parent agent span links them. Downstream joins on `call.id` now see two distinct, truthful records. Cost is not double-counted because each id is counted once. - **Logical-call spans (acceptable only when no consumer joins on payload):** one span, but then the attributes MUST be append-only — the first attempt's error must survive as a `gen_ai.tool.call.error` event, never overwritten, and the span must carry `gen_ai.tool.call.attempt_count=2` so any consumer knows the payload is the last-wins view. The decision rule: **if any consumer treats `gen_ai.tool.call.id` as a stable identity whose payload is immutable, you are in the attempt-scoped regime and in-place mutation is a correctness bug, not a style choice.** Trace-as-memory for a sibling agent is the strictest such consumer and the one most often overlooked, because it does not look like a "telemetry consumer" — it looks like reasoning. ## Operational Effect The runtime must refuse to close a tool span whose payload attributes were mutated after the first write, unless the span explicitly declares logical-call semantics with an attempt count and preserved error events. Concretely: capture the `gen_ai.tool.call.arguments` and `gen_ai.tool.call.id` at span open; on any retry that would change them, either open a child span or raise and force the caller to decide. The guardrail is a write-once check on the identity attributes, not on the whole span, because status and result legitimately evolve. This converts a silent trace corruption into a loud instrumentation error at the moment of the retry, which is the only point where the agent still has the information to choose attempt-scoped vs. logical-call correctly. ```python class GenAISpanIdentityMutationError(RuntimeError): """Raised when a tool span's identity-bearing GenAI attributes are overwritten in place across a retry, breaking the one-span-one-attempt invariant that trace-as-memory consumers rely on.""" pass class ToolSpan: # Identity attributes that MUST be write-once for the span's lifetime. IDENTITY_ATTRS = ("gen_ai.tool.call.id", "gen_ai.tool.call.arguments") def __init__(self, call_id: str, arguments: str): self._attrs = { "gen_ai.tool.call.id": call_id, "gen_ai.tool.call.arguments": arguments, } self._frozen = dict(self._attrs) # snapshot at open self.attempt_count = 1 def set_attr(self, key: str, value): if key in self.IDENTITY_ATTRS and key in self._frozen: if value != self._frozen[key]: raise GenAISpanIdentityMutationError( f"refusing to mutate {key!r} in place on a live tool span: " f"was {self._frozen[key]!r}, retry wants {value!r}. " f"Open a child span with a fresh gen_ai.tool.call.id " f"(e.g. '{self._frozen['gen_ai.tool.call.id']}.r{self.attempt_count}') " f"or declare logical-call semantics with attempt_count and " f"preserved error events." ) self._attrs[key] = value def record_retry(self, new_arguments: str): # A retry with a mutated payload is the trigger condition. self.attempt_count += 1 self.set_attr("gen_ai.tool.call.arguments", new_arguments) def trace_tool_attempt(span: ToolSpan, retry_arguments: str | None = None): if retry_arguments is not None: # This is exactly where naive wrappers overwrite and corrupt the trace. span.record_retry(retry_arguments) return span ```