# What structural signals indicate tool-call span attribute collapse under OpenTelemetry GenAI semantic conventions when a single logical tool invocation is retried across agent turns **Date of Record:** 2026 November 27 **Scope:** Applies to agents emitting OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*` attributes) where a logical tool call spans multiple agent reasoning turns due to retry, partial failure, or deferred execution. Does not apply to single-shot tool invocations completed within one span, nor to non-GenAI telemetry. ## Core Distinction There are two distinct accounting units that the GenAI semantic conventions conflate unless the emitter is disciplined: the **logical tool invocation** (one intent, one expected result, uniquely identified by the model's tool-call ID) and the **transport attempt** (one actual execution against a downstream system). The convention's `gen_ai.tool.call.id` attribute is defined as an identifier for the tool call, and `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` are defined per-span. When an agent retries a tool call across turns—because of a timeout, a sandbox rejection, a schema mismatch on the first attempt, or a self-correction loop—the naive emitter produces either (a) one span per attempt sharing the same `gen_ai.tool.call.id`, or (b) one span per attempt with freshly minted call IDs because the model regenerated the call. Both are structurally wrong for downstream accounting, but they fail differently and are distinguished by *which* attribute diverges. The non-obvious boundary: the invariant that must hold is **`gen_ai.tool.call.id` is a primary key across the trace, and token usage attributes are additive only within a single attempt span, never across sibling spans sharing that key.** The convention does not enforce this; it merely permits it. Emitters that treat the attribute as decorative break the invariant silently. ## What Was Observed Consider an agent that decides to call `search_docs(query="refund policy")`. The model emits a tool call with ID `call_abc123`. The executor times out at 30s. The orchestrator feeds the timeout back as a tool result. The model, on the next turn, re-emits the *same* semantic intent but the harness mints a new call ID `call_def456` because it re-reads the model's structured output. Two spans are exported: ``` span_1: gen_ai.operation.name="execute_tool" gen_ai.tool.name="search_docs" gen_ai.tool.call.id="call_abc123" gen_ai.usage.input_tokens=412 gen_ai.usage.output_tokens=18 status.code=ERROR (timeout) span_2: gen_ai.operation.name="execute_tool" gen_ai.tool.name="search_docs" gen_ai.tool.call.id="call_def456" gen_ai.usage.input_tokens=398 gen_ai.usage.output_tokens=22 status.code=OK ``` Downstream, a cost dashboard summing `gen_ai.usage.input_tokens` over `execute_tool` spans reports 810 input tokens for a single logical search. A retry-rate metric keyed on `gen_ai.tool.call.id` reports zero retries because the two spans have distinct IDs. A trace viewer rendering the agent's turn structure shows two independent tool calls, not one call with two attempts. The structural signal that reveals the collapse is the **paired divergence**: `gen_ai.tool.name` is identical, the enclosing `gen_ai.conversation.id` (or trace ID) is identical, the parent agent-turn span is sequential, but `gen_ai.tool.call.id` differs. When call IDs differ while tool name and conversation context match and the inter-span gap is a single model turn, you are looking at a logical invocation that has been fragmented into transport attempts. The fragmentation is invisible to any metric that trusts the call ID as ground truth. The inverse pathology appears when the harness *does* reuse the call ID across attempts (common when the orchestrator caches the model's tool-call object). Now `gen_ai.tool.call.id` is duplicated across sibling spans. Any consumer that treats it as a unique key—a join, a dedup pass, a "unique tool calls" counter—silently drops one of the attempts. Retry storms become invisible. ## Condition of Validity The additive token invariant (`sum(input_tokens) over spans with same call ID = logical invocation cost`) is valid **only if** each attempt span carries a distinct `gen_ai.tool.call.id` *and* a shared parent identifier that groups them. The GenAI conventions provide neither a mandated grouping attribute nor a mandated uniqueness constraint. The emitter must supply both: 1. A **stable logical identifier** propagated across attempts—either by preserving the model's original call ID and adding an attempt counter, or by introducing an explicit `agent.tool.invocation.id` attribute outside the GenAI namespace. 2. A **strict one-span-per-attempt** rule so token attributes remain per-attempt additive rather than per-logical-call double-counted. Absent both, the condition of validity fails and every downstream aggregate is suspect. The failure is not loud: no exporter error, no schema violation, no dropped span. The spans are individually well-formed and collectively misleading. ## Decision Boundary The decision boundary is whether the orchestrator treats the model's tool-call ID as **authoritative identity** or as **advisory hint**. - If authoritative: the orchestrator MUST preserve it verbatim across retries and MUST attach an attempt discriminator (`agent.tool.attempt=1,2,...`). Retries are then recoverable by grouping on the preserved ID, and token sums are correct per attempt. - If advisory: the orchestrator MUST mint a fresh `gen_ai.tool.call.id` per attempt and MUST emit a separate `agent.tool.invocation.id` that is stable across the logical call. The GenAI call ID then functions as an attempt key, not an invocation key. The failure mode arises precisely at the unstated middle: orchestrators that *sometimes* preserve and *sometimes* regenerate depending on whether the retry path goes through a cached tool-call object or a re-parse of model output. This produces traces where some logical calls have one ID across attempts and others have many—making any global retry-rate or cost-per-invocation metric a mixture of two incompatible definitions. The boundary is not "retry or don't retry"; it is "commit to one identity discipline and enforce it at the emitter, not the consumer." ## Operational Effect When the invariant is violated, three operational signals degrade in a correlated pattern that is itself diagnostic: 1. **Cost-per-tool-name is inflated** relative to the provider's own billing, because retries double-count input tokens (the model re-sends context on each attempt). 2. **Retry-rate is under-reported to zero** for regeneration-path retries and **over-reported** for preservation-path retries (duplicate keys collapsing in the dedup). 3. **Trace-to-billing reconciliation drifts** by exactly the sum of retried-attempt tokens, which is the quantity the dashboard cannot see. The guardrail is an emitter-side assertion, not a consumer-side correction: before export, verify that for every `(conversation_id, tool.name)` pair, the set of `tool.call.id` values either has cardinality equal to the attempt count with a shared invocation ID present, or cardinality one with an attempt discriminator present. Reject the span batch otherwise. This converts a silent accounting collapse into a loud, early failure at the boundary where it originates. ```python class ToolCallAccountingCollapse(RuntimeError): """Raised when GenAI tool-call spans violate the invocation/attempt identity invariant.""" pass def validate_tool_call_spans(spans: list[dict]) -> None: """ Enforce: within a (conversation_id, tool.name) group, either - call IDs are unique per attempt AND a shared invocation id groups them, or - a single call ID is reused AND an attempt discriminator is present. Anything else is a silent accounting collapse. """ groups: dict[tuple[str, str], list[dict]] = {} for s in spans: if s.get("gen_ai.operation.name") != "execute_tool": continue key = (s.get("gen_ai.conversation.id"), s.get("gen_ai.tool.name")) groups.setdefault(key, []).append(s) for (conv, tool), members in groups.items(): call_ids = [m.get("gen_ai.tool.call.id") for m in members] invocation_ids = {m.get("agent.tool.invocation.id") for m in members} attempts = {m.get("agent.tool.attempt") for m in members} unique_call_ids = len(set(call_ids)) == len(call_ids) shared_invocation = len(invocation_ids) == 1 and None not in invocation_ids reuse_with_attempts = len(set(call_ids)) == 1 and None not in attempts if unique_call_ids and shared_invocation: continue if reuse_with_attempts: continue raise ToolCallAccountingCollapse( f"Invocation/attempt identity invariant violated for " f"conversation={conv!r} tool={tool!r}: " f"call_ids={call_ids}, invocation_ids={invocation_ids}, attempts={attempts}. " f"Token sums and retry rates over this group are unreliable." ) ```