# When must an instance refuse to emit a `gen_ai.tool.call` span: orphaned tool-span attribution under concurrent multi-turn replay **Date of Record:** 2026 December 3 **Scope:** Applies to agents that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.conversation.id`, `gen_ai.agent.id`) for tool invocations across multi-turn conversations, especially when turns are replayed, retried, or resumed from a checkpoint. Does not apply to single-shot, single-turn inference calls with no tool execution, nor to pure transport-level tracing (HTTP/gRPC) wrapping the model call. ## Core Distinction The distinction that matters is between a *tool call as an event in the model's reasoning* and a *tool span as a record of an executed side effect*. The GenAI semantic conventions model the tool span as a child of the agent/inference span, keyed by `gen_ai.tool.call.id` (the model-emitted call identifier) and correlated to the conversation via `gen_ai.conversation.id`. This is correct for the happy path. It becomes wrong the moment the same logical turn is executed more than once — retry, replay, or checkpoint-resume — because the model re-emits a *fresh* `gen_ai.tool.call.id` on each attempt while the *side effect* may or may not have already occurred. The span model has no native slot for "this tool call is a re-attempt of a call whose side effect is already committed." An instance that blindly emits a new tool span per emission produces a trace in which two sibling spans, both children of the same inference span, both claim to be *the* call to `transfer_funds`, and downstream consumers (cost attribution, audit replay, SLO computation) cannot tell which one executed. The invariant: **a tool span must be attributable to exactly one committed side effect, or explicitly marked as a non-executing retry.** When that attribution cannot be established from available state, the instance must refuse to emit the span rather than emit an ambiguous one. ## What Was Observed A three-turn agent handling a refund workflow. Turn 2 emits a tool call: ``` gen_ai.operation.name = "execute_tool" gen_ai.tool.name = "issue_refund" gen_ai.tool.call.id = "call_a1" # emitted by model gen_ai.conversation.id = "conv_7f3" ``` The tool executes and commits a refund of $40. The span is closed with `status = OK`. The process then crashes *after* the side effect commits but *before* the turn checkpoint is durably written (checkpoint write is the last step of the turn). On resume, the orchestrator reloads the last durable checkpoint — end of turn 1 — and replays turn 2. The model, given the same context, emits a *new* call id `call_b2` for the same logical refund (model call ids are non-deterministic; identical context does not reproduce `call_a1`). The runtime executes `issue_refund` again. Depending on the tool's idempotency, one of two things happens: 1. **Non-idempotent tool:** the refund is issued twice ($80 total). Two spans exist, `call_a1` and `call_b2`, both `status = OK`, both children of *different* inference spans (the replayed turn gets a new inference span). The trace is internally consistent but factually wrong: it reports two successful refunds, and the auditor has no signal that one was a replay. 2. **Idempotent tool:** the second call is deduplicated server-side and returns the original result. The runtime emits `call_b2` with `status = OK`, but the side effect it "records" was performed by `call_a1`. Now the trace contains a span whose `gen_ai.tool.call.id` does not correspond to the call that actually executed. Replay-based audit — "re-run the trace, confirm each tool span's inputs produce its recorded outputs" — fails: `call_b2`'s inputs, replayed, hit the idempotency cache and return a result, but the *first* execution that populated that cache is invisible in the trace because its span (`call_a1`) is in a different trace (the crashed process's exporter buffer, likely lost). The second case is the non-obvious one. The trace *looks* clean — one refund span, `OK`, correct amount — and is *wrong* in a way that only surfaces when the audit tries to reconcile the trace against the ledger. The failure is not a crash or a dropped span; it is a *misattributed* span that passes all syntactic validation. A third variant appeared under concurrent multi-turn execution: two turns of the same conversation running in parallel (a "branch and merge" agent topology), both emitting tool spans with the same `gen_ai.conversation.id` but different `gen_ai.tool.call.id`, both children of inference spans that the backend orders non-deterministically. The semantic conventions do not specify a causal ordering between tool spans of the same conversation, so the reconstructed timeline is ambiguous: did the agent call `lookup_balance` before or after `issue_refund`? The trace cannot answer, and any SLO that depends on tool-call ordering (e.g., "refund must follow balance check") is uncomputable from the trace. ## Condition of Validity The tool-span model is valid only when all three of the following hold: - **Single execution per call id.** Each `gen_ai.tool.call.id` maps to at most one executed side effect within the trace's scope. Replays that re-execute must either reuse the original call id (and thus the original span, via span re-emission with the same `trace_id`/`span_id` — which the OTel SDK does not natively support across process restarts) or be marked as retries. - **Causal ordering is recoverable.** Either the trace carries explicit parent-child linkage that establishes tool-call order (tool spans as children of a *sequenced* inference span, with `gen_ai.conversation.id` plus a monotonic turn index), or the agent runs strictly sequentially per conversation. Parallel branches within one conversation break ordering recoverability unless a merge span is emitted. - **The span's recorded inputs are the inputs that produced the recorded output.** This is the replay invariant. It fails silently under idempotent dedup, because the dedup layer returns a result computed from *different* inputs than the ones on the span. When any of these is violated — replayed turn with a fresh call id, parallel branches with no merge span, idempotent dedup masking the original execution — the emitted span is not a faithful record and must not be emitted as if it were. ## Decision Boundary The boundary is a single predicate evaluated at span-emission time, before the span is handed to the exporter: > *Is this tool call's execution attributable to a side effect that is not already represented by another span in a reachable trace?* Operationally, the instance must check, in order: 1. **Replay detection.** Does the current turn's checkpoint generation differ from the generation at which the tool call id was first minted? If the runtime cannot prove this is the first execution of the logical call, it is a replay candidate. 2. **Idempotency disclosure.** Did the tool layer signal a dedup hit (a returned `already_applied` flag, a cache-hit header, a non-null `original_execution_id`)? If so, the span must carry `gen_ai.tool.call.id` of the *original* execution, not the current emission, or be marked `retry = true` with a link to the original. 3. **Ordering recoverability.** For concurrent branches, is there a merge span or sequence attribute that lets a consumer reconstruct order? If not, the tool span cannot be emitted as a sibling without a disambiguating attribute. If (1) is true and (2) is false — replay with no dedup disclosure and no way to recover the original call id — the instance is in the ambiguous zone: it cannot prove the side effect is unrepresented, and it cannot prove it is represented. This is the refusal condition. The correct behavior is to emit a *degraded* span: `status = ERROR`, `error.type = "orphaned_tool_call"`, no `gen_ai.tool.call.id` attribution to a committed effect, and a span event recording the replay generation. The span is still emitted (so the gap is visible) but is explicitly marked as non-attributable. Emitting it as `OK` is the failure. The boundary is sharp: *attribution provable → emit normal span; attribution unprovable → emit degraded span; attribution provably absent (tool did not execute) → emit no span or a `status = UNSET` span with no side-effect claim.* The failure mode is collapsing the middle case into the first. ## Operational Effect Downstream consumers of GenAI traces — cost attribution, audit replay, SLO computation, and the agent's own episodic memory distillation — all assume the replay invariant. When it is violated silently: - **Cost attribution double-counts.** Two `OK` tool spans for one refund inflate the tool-call cost line by 2×; the billing reconciliation drifts from the ledger by exactly the replay rate, which is invisible until the drift is large. - **Audit replay produces false negatives.** The auditor re-runs `call_b2`'s inputs, gets the deduped result, and concludes the trace is faithful — while `call_a1`, the actual execution, is absent. The audit passes; the trace is wrong. This is the worst failure shape: a verification process that *certifies* a misattributed trace. - **Episodic memory distillation ingests the phantom call.** If the agent distills its trace into episodic memory ("I issued a refund of $40"), and the trace contains two `OK` refund spans, the distilled memory records two refunds. Stale-memory drift compounds: the agent's self-model diverges from the ledger, and subsequent turns reason from a false history. - **SLO computation breaks on ordering ambiguity.** "Refund follows balance check" is uncomputable when parallel branches emit unordered siblings; the SLO silently reports 100% because it cannot find a violating pair, not because none exists. The guardrail is to make the refusal condition a hard gate in the span-emission path, not a post-hoc trace processor. Post-hoc reconciliation cannot recover the original call id once the original process's exporter buffer is lost; the gate must run in the live instance, where the replay generation and the dedup disclosure are both in scope. ```python from __future__ import annotations from dataclasses import dataclass from typing import Optional class OrphanedToolCallError(RuntimeError): """Raised when a tool span cannot be attributed to exactly one committed side effect.""" @dataclass(frozen=True) class ToolCallEmission: call_id: str # gen_ai.tool.call.id, minted by the model this emission conversation_id: str # gen_ai.conversation.id turn_index: int # monotonic per conversation checkpoint_generation: int # generation at which this emission is being made first_minted_generation: Optional[int] # generation at which call_id was first seen, if known dedup_disclosure: Optional[str] # original_execution_id if tool layer reported a dedup hit is_concurrent_branch: bool has_merge_span: bool def validate_tool_span_attribution(em: ToolCallEmission) -> str: """ Returns the span disposition: 'normal' | 'degraded' | 'suppress'. Raises OrphanedToolCallError when the emission is in the ambiguous zone and the caller has not opted into degraded emission. """ is_replay = ( em.first_minted_generation is not None and em.first_minted_generation != em.checkpoint_generation ) # Case 1: replay with no dedup disclosure -> ambiguous zone. if is_replay and em.dedup_disclosure is None: raise OrphanedToolCallError( f"tool call {em.call_id!r} in conversation {em.conversation_id!r} " f"is a replay (minted gen {em.first_minted_generation}, " f"emitting gen {em.checkpoint_generation}) with no idempotency " f"disclosure; side effect attribution is unprovable. " f"Either recover the original call id or emit a degraded span " f"(status=ERROR, error.type=orphaned_tool_call)." ) # Case 2: replay with dedup disclosure -> attribute to original execution. if is_replay and em.dedup_disclosure is not None: return "normal" # caller must rewrite gen_ai.tool.call.id to dedup_disclosure # Case 3: concurrent branch with no merge span -> ordering unrecoverable. if em.is_concurrent_branch and not em.has_merge_span: raise OrphanedToolCallError( f"tool call {em.call_id!r} in conversation {em.conversation_id!r} " f"is on a concurrent branch with no merge span; causal ordering " f"within the conversation is unrecoverable from the trace." ) return "normal" def emit_tool_span(em: ToolCallEmission, allow_degraded: bool = False) -> str: try: return validate_tool_span_attribution(em) except OrphanedToolCallError: if allow_degraded: # Emit a degraded span: visible gap, no side-effect attribution. return "degraded" # status=ERROR, error.type="orphaned_tool_call" raise ```