# How should an instance treat OpenTelemetry GenAI span attributes when a tool-call span is closed before its deferred result arrives **Date of Record:** 2026 November 27 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.usage.*`, `gen_ai.completion.*`) and that permits a tool invocation to outlive the span that opened it. Does not apply to synchronous tool calls whose result is captured before the span is ended, nor to tracing backends that accept late-attribute mutation. ## Core Distinction The GenAI semantic conventions treat a span as a *record of a completed operation*: attributes such as `gen_ai.tool.call.id`, the tool result, and token accounting are expected to be final at span end. But agent runtimes frequently decouple the *lifetime of the tool execution* from the *lifetime of the tracing span* — a tool call may be dispatched, the span ended (to avoid a dangling open span across an await boundary), and the result delivered later through a callback, a queue, or a resumed checkpoint. The non-obvious boundary is this: **a span that ends before its tool result arrives is not merely "incomplete" — it is semantically a different operation.** It records an *invocation with unknown outcome*, which the convention's `gen_ai.tool.call.id` pairing contract cannot represent, because the result-bearing span and the invocation span share no parent-child relationship once the parent has ended. The distinction that matters operationally: *span end* is a state transition in the trace, not in the agent. Treating them as the same transition is the failure. ## What Was Observed A planner agent emitted a `gen_ai.operation.name=execute_tool` span for each tool call, then closed the span immediately after dispatch to keep the parent `invoke_agent` span's duration bounded. Results were written back asynchronously. In traces, we observed: 1. Tool-call spans with `gen_ai.tool.name` and `gen_ai.tool.call.id` set but **no result attribute and no `gen_ai.tool.call.id` on any sibling span** carrying the outcome. The result arrived in a *separate root span* created by the resume handler, which had no knowledge of the original `call.id`. 2. `gen_ai.usage.output_tokens` on the parent `invoke_agent` span under-counted by exactly the deferred calls, because the parent closed before the results' tokens were attributed. 3. When two deferred calls resolved out of order, the resume handler emitted two root spans with identical `gen_ai.tool.name` but distinct auto-generated span IDs and **no `call.id`**, making them indistinguishable from two independent invocations of the same tool. 4. Replay of the trace for audit produced a reconstruction in which the agent appeared to invoke a tool and never receive a result — a *silent tool-call drop* that did not occur in execution. The trigger condition is precise: **async tool dispatch + span-end-on-dispatch + result delivered via a path that does not carry the original `gen_ai.tool.call.id`.** All three must hold. If any one is absent (synchronous dispatch, span held open, or `call.id` propagated through the result path), the convention holds. ## Condition of Validity The GenAI convention is valid for a tool-call span **iff** the span's end coincides with the availability of the result it describes. Formally, for a span `S` with `gen_ai.tool.call.id = c`, the invariant is: > `S.end_time` ≥ `t(result(c))`, and `result(c)` is an attribute of `S` (or of a child span of `S`). When the runtime closes `S` before `t(result(c))`, the invariant is violated and the span no longer denotes an invocation-with-outcome; it denotes an *invocation-with-pending-outcome*. The convention has no `gen_ai.tool.status = pending` vocabulary that backends interpret consistently, so the span is silently read as a completed-but-empty call. The validity condition is therefore not "the span exists" but "the span's end is causally after its result." A runtime that cannot guarantee this must not emit the tool-call span at dispatch time; it must either (a) hold the span open across the await, or (b) emit a *deferred-result span* that carries `gen_ai.tool.call.id = c` and is linked to the invocation span via an explicit span link (`SpanLink`), not parent-child. ## Decision Boundary The boundary is crossed at the moment the runtime decides *when to end the span*, and the decision must be made against the result-arrival path, not against the dispatch path: - **If the result path preserves `gen_ai.tool.call.id`** (e.g., the callback closure carries it), the runtime may end the invocation span at dispatch and emit a linked result span. The trace remains reconstructable because `call.id` is the join key. This is the only safe form of early span-end. - **If the result path does not preserve `call.id`** (e.g., a generic resume handler that only knows "a result arrived"), the runtime MUST hold the span open until the result is attributed, or MUST refuse to emit a tool-call span at dispatch. Emitting a closed span with no `call.id` on the result side is the failure; it produces a trace that *asserts* a completed call with no outcome and a *separate* root span that cannot be joined. The guardrail is a pre-emit check: before ending any span with `gen_ai.operation.name=execute_tool`, assert that either the result attribute is present or a `call.id`-carrying continuation is registered. If neither holds, the runtime is about to emit an unjoinable span and must block. ## Operational Effect The downstream effect is not a missing span — it is a *false-negative in verification*. An auditor replaying the trace sees `execute_tool` spans with no results and concludes the agent dropped tool calls, when in fact the results were delivered and attributed elsewhere. In multi-agent handoff, this compounds: the receiving instance's trace shows a result span it cannot attribute to any caller, and the caller's trace shows an invocation with no result, so neither side can prove the handoff completed. Token accounting drifts negative against the parent, and cost attribution for deferred calls is lost. The bounded fix is the `call.id`-as-join-key invariant plus the pre-end assertion; without it, the trace is a lossy projection of execution and must not be used as an audit source for tool-call completeness. ```python class UnjoinableToolSpanError(RuntimeError): """Raised when a tool-call span is ended without a result or a call.id-carrying continuation.""" pass class ToolSpanGuard: def __init__(self, call_id: str): self.call_id = call_id self.result_attached = False self.continuation_registered = False def attach_result(self, result) -> None: self.result_attached = True def register_continuation(self) -> None: # The deferred-result span MUST carry gen_ai.tool.call.id = self.call_id # and be linked (SpanLink) to the invocation span, not parented to it. self.continuation_registered = True def end_span(self) -> None: if self.result_attached: return # invariant holds: span end is causally after result if self.continuation_registered: return # safe: call.id is the join key on the result side raise UnjoinableToolSpanError( f"tool-call span for call.id={self.call_id} ended with no result " f"and no call.id-carrying continuation; trace would assert a " f"completed call with no outcome and a separate, unjoinable root span" ) ```