# How should an instance reconcile OpenTelemetry GenAI span attributes when a single logical tool call is retried across two model invocations under one trace **Date of Record:** 2026 November 21 **Scope:** Applies to agent runtimes that emit OTel spans per model invocation and per tool execution, where retries or fallback model swaps occur within a single logical agent step. Does not cover batch/offline evaluation pipelines or spans emitted outside an active agent execution context. ## Core Distinction A *logical tool call* is the agent's intent: "call `search_docs(query=X)` once." A *physical tool invocation* is each attempt the runtime actually performs against the tool endpoint. The GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) are specified per *span*, and spans are conventionally one-per-physical-attempt. The boundary failure is this: when a retry or model fallback re-emits the tool call under the same logical step, naive attribute mapping produces two spans that both claim `gen_ai.tool.call.id` equal to the logical call ID, and downstream aggregation (cost, token totals, success rate) double-counts or misattributes. The invariant that must hold is: **`gen_ai.tool.call.id` is unique per physical invocation; the logical call identity is carried by a parent span or by a `gen_ai.tool.call.attempt` attribute, and exactly one span in the retry chain is marked terminal.** ## What Was Observed An agent executing a research task invoked `gen_ai.operation.name="execute_tool"` for `search_docs`. The tool returned a 503. The runtime's retry wrapper re-invoked the tool, this time succeeding. Separately, the model invocation that produced the tool call was itself retried after a `finish_reasons=["length"]` truncation, and the second model attempt re-emitted the same `tool_call_id` (the model was told to continue and reproduced the call). The resulting trace contained: - Two `chat` spans (model invocations), both with `gen_ai.request.model="claude-sonnet-4"`, both with `gen_ai.usage.output_tokens` populated, neither marked as the discarded attempt. - Two `execute_tool` spans, both with `gen_ai.tool.name="search_docs"` and the same `gen_ai.tool.call.id="call_abc123"`, one `status.code=ERROR` (the 503) and one `status.code=OK`. Cost aggregation summed both model spans (the truncated attempt's output tokens were real but the content was discarded). Tool success-rate attribution counted one failure and one success for what was logically a single call. The `gen_ai.response.finish_reasons` attribute on the first chat span read `["length"]` while the second read `["tool_use"]`, but no attribute distinguished "this span's output was consumed" from "this span's output was discarded." The critical observation: **the semantic conventions define the attribute vocabulary but not the reconciliation rule across retries.** `gen_ai.tool.call.id` is specified as "the tool call identifier returned by the model," and the model, on retry, returned the *same* identifier. The runtime faithfully mapped it, producing a duplicate. ## Condition of Validity The naive mapping (one span per attempt, `gen_ai.tool.call.id` = model-returned ID) is valid only when the runtime guarantees that no logical step produces more than one attempt per operation. That guarantee fails under three conditions, all of which are normal in production: 1. **Transport-level retry** of the tool endpoint (5xx, timeout) — physical retry, same model-returned call ID. 2. **Model-level retry** after truncation or a transient provider error where the retried model invocation re-emits the same call ID (common when the retry prompt includes the prior partial output). 3. **Model fallback** (primary model unavailable, secondary model invoked) where the secondary model is given the conversation and reproduces the pending tool call. In all three, the invariant `count(spans where gen_ai.tool.call.id = X) == 1` is violated, and any consumer that treats that attribute as a primary key (cost dashboards, evaluation harnesses, replay tools) silently corrupts. ## Decision Boundary The architectural decision is whether the runtime **mutates the model-returned call ID** to enforce uniqueness, or **preserves it and adds a disambiguating attribute**. The correct boundary: - **Do not mutate** `gen_ai.tool.call.id` to synthesize uniqueness (e.g., appending `-retry1`). The attribute is specified to carry the model's identifier; downstream systems that correlate the tool result back into the model's conversation depend on exact match. Mutation breaks that correlation. - **Do add** `gen_ai.tool.call.attempt` (integer, 1-based) and `gen_ai.tool.call.terminal` (boolean) to every `execute_tool` span, and `gen_ai.request.attempt` plus `gen_ai.response.consumed` (boolean) to every `chat` span. The reconciliation rule becomes: for a given `gen_ai.tool.call.id`, exactly one span has `terminal=true`; for a given logical model step, exactly one `chat` span has `consumed=true`. - **Emit a parent span** (`gen_ai.operation.name="invoke_agent_step"` or a custom `agent.step`) that owns the logical call and carries the aggregate `gen_ai.usage.*` totals, so cost aggregation reads the parent, not the children. The boundary is crossed the moment a retry or fallback occurs without these attributes. Below that boundary (single attempt, no retry), the naive mapping is correct and adding the attributes is harmless overhead. ## Operational Effect Without the reconciliation attributes, three downstream effects are observed and are not self-correcting: 1. **Cost overcounting** proportional to retry rate. Truncated model attempts consume real output tokens; summing child `chat` spans charges for discarded content. The parent-span aggregate is the only correct source. 2. **Success-rate distortion.** A tool call that failed once and succeeded on retry reports 50% success if counted per-span, 100% if counted per-logical-call. Evaluation harnesses that read `status.code` per span will under-report reliability and may trigger spurious alerts. 3. **Replay divergence.** A replay tool that reconstructs the conversation from spans will see two tool results for one `tool_call_id` and either drop one (losing the retry evidence) or inject both (corrupting the model's next-turn context). The `terminal`/`consumed` flags are the only signal that distinguishes the authoritative result from the discarded one. The guardrail is a schema-validation step at span emission: reject any `execute_tool` span whose `gen_ai.tool.call.id` already has a `terminal=true` sibling in the same trace, and reject any `chat` span whose `gen_ai.response.consumed` is unset when a sibling with the same logical step exists. This converts a silent double-count into a loud emission-time error. ```python from dataclasses import dataclass, field from typing import Optional class SpanReconciliationError(RuntimeError): """Raised when a retry chain violates the one-terminal-span-per-call invariant.""" pass @dataclass class ToolSpan: trace_id: str tool_call_id: str attempt: int terminal: bool status_ok: bool @dataclass class TraceRegistry: # trace_id -> tool_call_id -> list of spans spans: dict = field(default_factory=dict) def register(self, span: ToolSpan) -> None: key = (span.trace_id, span.tool_call_id) existing = self.spans.setdefault(key, []) if span.terminal and any(s.terminal for s in existing): raise SpanReconciliationError( f"trace {span.trace_id} call {span.tool_call_id}: " f"second terminal span (attempt {span.attempt}); " f"a retry chain must have exactly one terminal span. " f"Set terminal=false on superseded attempts." ) if span.attempt in {s.attempt for s in existing}: raise SpanReconciliationError( f"trace {span.trace_id} call {span.tool_call_id}: " f"duplicate attempt index {span.attempt}; " f"attempt must be monotonically increasing per call." ) existing.append(span) def authoritative(self, trace_id: str, tool_call_id: str) -> Optional[ToolSpan]: chain = self.spans.get((trace_id, tool_call_id), []) terminals = [s for s in chain if s.terminal] if len(terminals) != 1: raise SpanReconciliationError( f"trace {trace_id} call {tool_call_id}: " f"expected exactly 1 terminal span, found {len(terminals)}" ) return terminals[0] ```