# When must an instance emit a tool-call span before the tool executes — and why OTel GenAI conventions silently lose the failure when it does not **Date of Record:** 2026 November 28 **Scope:** Applies to any agent runtime that instruments tool invocations with OpenTelemetry GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) and correlates spans across a parent agent span. Does not apply to pure LLM inference spans without tool dispatch, nor to runtimes that emit only metrics without span linkage. ## Core Distinction The GenAI semantic conventions treat a tool call as an *attribute of an operation that already happened* — `gen_ai.tool.name`, `gen_ai.tool.call.id`, and the resulting span status are written at span end, when the tool returns. This is correct for a synchronous, in-process function call. It is wrong for the case that actually dominates agent systems: a tool that *mutates external state* (writes a file, sends a request, charges a card, posts a message) and then fails or hangs *after* the mutation but *before* the span closes. The distinction that matters is not "did the tool error" but **"was the span emitted before or after the side effect became irreversible."** A span that opens at dispatch and closes at return records the side effect's *outcome* but not its *existence*. When the process dies mid-call, the collector receives no span at all — the trace shows the agent span with no child, which is indistinguishable from "the model decided not to call the tool." The telemetry's absence is read as a negative decision, not as a lost execution. ## What Was Observed A tool-dispatch loop on a single agent instance, instrumented per the conventions with span start at invocation and span end at return: ``` t0 agent span opens (gen_ai.operation.name=chat) t1 model emits tool_call{id=call_7f2, name=write_record} t2 child span opens (gen_ai.tool.call.id=call_7f2) t3 tool executes: POST /records {id: 4411} -> 200 OK, record persisted t4 tool begins post-processing (serialize response, ~40ms) t5 process OOM-killed / sandbox evicted / pod rescheduled ``` At t5 the child span never reaches `span.end()`. The batch span processor's in-memory queue is lost. The collector ingests: - agent span `chat` — status OK, ends at t1 (last flush) - **no** span with `gen_ai.tool.call.id=call_7f2` Downstream, a replay/reconciliation job scans traces for tool calls to build the audit of "what did this agent do," finds no `call_7f2`, and concludes the tool was never invoked. The record `4411` exists in the backing store with no corresponding trace. The agent, resumed from a checkpoint taken at t1, re-plans and calls `write_record` again — producing `4412`. The trace now shows one call; the store shows two writes. The telemetry is not merely incomplete; it is *actively misleading about idempotency*, because the missing span is the only signal that the first attempt occurred. A second, subtler variant: the span *is* flushed (span processor configured with `BATCH` but small queue, or `SIMPLE`), but `span.end()` runs in a `finally` block that sets status `ERROR` only on a caught exception. A hard kill skips the `finally`; a soft timeout path that returns a partial result sets status `OK`. In both cases the span closes with `status=OK` while the side effect is in an unknown state — the convention offers no `gen_ai.tool.outcome=UNKNOWN` and the default mapping of "no exception" to `OK` converts ambiguity into success. ## Condition of Validity The convention's "span at invocation, attributes at completion" model is valid **only if** the following invariant holds: > For every tool call whose side effect is not idempotent, the existence of the call must be durably recorded *before* the side effect is applied, and the record must be readable by any process that later reasons about that side effect. In practice this requires a two-phase span discipline the conventions do not specify: 1. **Pre-dispatch span emission.** Open the child span and export a minimal "dispatch" record (`gen_ai.tool.call.id`, `gen_ai.tool.name`, a hash of arguments) *before* invoking the tool, using a `SimpleSpanProcessor` for that record or a synchronous log/event, so it survives a kill at t3–t5. The full span may still close at return. 2. **Terminal-state reconciliation.** On resume, the agent must query the pre-dispatch record set against the completed-span set. Any `call.id` present in dispatch but absent in completion is `UNKNOWN`, not "not called," and must be reconciled with the backing store (idempotency key lookup) before re-planning. If either half is missing, the invariant fails and the trace is a lossy, one-sided log of *successful* executions only — the exact executions least likely to need auditing. ## Decision Boundary The boundary is crossed the moment a tool's side effect is **non-idempotent and externally observable** (write, send, charge, delete). Below that boundary — pure reads, idempotent upserts keyed by `call.id`, in-memory computation — the standard convention is sound and the two-phase discipline is unnecessary overhead. Concretely, emit the pre-dispatch record when **any** of: - the tool schema declares `side_effects: [write|external|financial]`, or - the tool is invoked with arguments containing a value the agent cannot regenerate deterministically (a generated UUID that will not repeat on replay), or - the agent's checkpoint interval exceeds the tool's P99 latency (so a kill between checkpoint and return is probable). Do **not** apply the discipline to read-only tools: the pre-dispatch record adds a span per call and inflates cost without bounding any failure, because a lost read has no reconcilable state. ## Operational Effect With the invariant enforced, a kill at t3–t5 produces: a dispatch record for `call_7f2`, no completion span, and a resume path that marks `call_7f2` `UNKNOWN`, looks up idempotency key `call_7f2` in the store, finds record `4411`, and suppresses the retry. The trace now contains a dispatch-without-completion pair that is a *first-class* signal, not an absence. Without it, the same kill produces silent duplicate side effects, an audit trail that undercounts executions by exactly the failure rate, and a replay loop that is non-deterministic in a way no span reveals. The failure is invisible precisely because the telemetry convention defines a tool call by its completion. The guardrail is a runtime assertion, not a convention: the agent must refuse to re-plan past an unresolved `call.id`. Encode it as a hard precondition on the resume path. ```python from dataclasses import dataclass from typing import Optional class UnreconciledToolCallError(RuntimeError): """Raised when a tool call was dispatched but has no terminal record, and the agent attempts to re-plan or retry without reconciling it.""" pass @dataclass class ToolCallRecord: call_id: str tool_name: str dispatched: bool = False completed: bool = False outcome: Optional[str] = None # "ok" | "error" | "unknown" class DispatchLedger: """Durable pre-dispatch record set, written before the side effect. Survives process kill; reconciled against completed spans on resume.""" def __init__(self) -> None: self._records: dict[str, ToolCallRecord] = {} def pre_dispatch(self, call_id: str, tool_name: str) -> None: # Must be flushed synchronously before the tool executes. self._records[call_id] = ToolCallRecord(call_id, tool_name, dispatched=True) def complete(self, call_id: str, outcome: str) -> None: rec = self._records.get(call_id) if rec is None: raise UnreconciledToolCallError( f"completion for undispatched call {call_id}" ) rec.completed = True rec.outcome = outcome def unresolved(self) -> list[ToolCallRecord]: return [r for r in self._records.values() if r.dispatched and not r.completed] def resume_agent(ledger: DispatchLedger, store_lookup) -> None: """Resume path. Must reconcile every dispatched-but-uncompleted call against the backing store BEFORE allowing re-planning.""" for rec in ledger.unresolved(): existing = store_lookup(idempotency_key=rec.call_id) if existing is None: # Side effect may or may not have landed; cannot safely retry # without operator decision, because retry is non-idempotent. raise UnreconciledToolCallError( f"call {rec.call_id} ({rec.tool_name}) dispatched but no " f"completion span and no store record; state is UNKNOWN. " f"Refusing to re-plan: retry would risk duplicate side effect." ) # Side effect landed; mark reconciled and suppress retry. ledger.complete(rec.call_id, outcome="ok") ```