# What happens when an instance emits OTel GenAI spans across a tool-call that outlives its parent invocation span **Date of Record:** 2026 December 3 **Scope:** Applies to agent runtimes that instrument model invocations and tool calls using OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where a tool call's execution duration exceeds the enclosing model-invocation span, or where the tool call is dispatched asynchronously and the parent invocation span ends before the tool result is attached. Does not apply to synchronous, fully-nested tool calls that complete within the parent span's lifetime. ## Core Distinction The GenAI semantic conventions model a model invocation (`gen_ai.operation.name = "chat"`) and its tool calls as a parent/child span hierarchy: the invocation span carries `gen_ai.request.model`, token usage, and finish reason; tool calls are emitted as child spans with `gen_ai.operation.name = "execute_tool"` and `gen_ai.tool.name`. The convention assumes the child tool span is *nested inside* the parent invocation span's start/end window. That assumption is false for any tool whose latency exceeds the model's response-to-next-turn cycle — long-running retrieval, code execution, human-in-the-loop approval, or a downstream agent handoff. The non-obvious boundary is this: **the parent invocation span closes when the model returns its message, but the tool span it spawned is still open.** OpenTelemetry requires that a span's end timestamp be ≥ its start timestamp and that children not outlive the trace's resource-level lifecycle in ways that break the exporter's batch assumptions — but it does *not* require children to end before parents. So the trace is structurally legal yet semantically broken: the tool span's `end_time_unix_nano` lands *after* the parent's, producing a trace where a child finishes after its parent. Downstream consumers that reconstruct agent turn boundaries from span nesting (most agent-observability dashboards, and many replay/audit tools) will assign the tool result to the *wrong* turn, or drop it entirely. ## What Was Observed A single agent turn on a research task: the model emits a `chat` span, requests a `web_fetch` tool call, and the runtime dispatches the fetch asynchronously. The model invocation span ends at T+0.9s (finish_reason `tool_calls`). The tool span starts at T+0.95s and ends at T+41s when the fetch times out and returns a partial document. The exported trace shows: - `chat` span: start T+0.0, end T+0.9, `gen_ai.usage.output_tokens=212`, `gen_ai.response.finish_reasons=["tool_calls"]`. - `execute_tool` span: start T+0.95, end T+41.0, `gen_ai.tool.name="web_fetch"`, `gen_ai.tool.call.id="call_ab12"`, status `ERROR` (deadline exceeded). Two invariants the runtime believed it held were both violated: 1. **Turn-attribution invariant.** The tool result belongs to turn *N+1* (the model must be re-invoked to consume it), but because the tool span is a child of the turn-*N* `chat` span, replay tools that group by parent assign the 41-second fetch to turn N and compute turn N's "tool latency" as 40.1s — inflating the per-turn cost model and mislabeling a *next-turn* input as *this-turn* output. 2. **Usage-accounting invariant.** `gen_ai.usage.input_tokens` on the *next* `chat` span includes the tool result, but the tool span itself carries no `gen_ai.usage.*` attributes. Any cost aggregator that sums usage by walking the span tree undercounts by exactly the tokens the tool result contributed to the following invocation — a silent, systematic undercount proportional to tool-result size. The trigger is precise: it fires when `tool_span.end_time > parent_chat_span.end_time` **and** the tool result is later fed into a new `chat` span. The first condition alone is harmless (a slow tool with no follow-up). The second condition alone is harmless (a fast tool). Together they desynchronize span nesting from logical turn structure. ## Condition of Validity The parent-child nesting assumption is valid only when the tool call is *synchronous within the invocation* — i.e., the runtime blocks the model call until the tool returns, or the tool completes before the model's response is finalized. It is invalid whenever: - The tool is dispatched after the model returns (the standard agent loop: model → tool → model). - The tool result is buffered and attached to a *subsequent* invocation rather than the current one. - A retry or timeout causes the tool span to be re-parented to a fresh invocation span while the original parent has already been exported. The correct structural model is **sibling, not child**: the `execute_tool` span should be a sibling of the `chat` spans it bridges, linked via `gen_ai.tool.call.id` on both sides, with the consuming `chat` span carrying a span *link* (not parent) to the tool span. This preserves turn attribution and lets usage aggregators follow `call.id` rather than tree depth. ## Decision Boundary The runtime must choose, at tool-dispatch time, between two emission strategies: - **Nested (default convention):** emit the tool span as a child of the dispatching `chat` span. Valid only if the tool completes before the `chat` span ends. Requires the runtime to *hold the parent span open* across the tool call — which blocks the trace exporter's batch flush for the whole tool duration and is unacceptable for tools exceeding ~5s. - **Linked-sibling:** close the `chat` span at model return, emit the tool span as a root-or-sibling span with `gen_ai.tool.call.id`, and add a `Link` from the consuming `chat` span back to the tool span. Valid for any tool duration; requires downstream consumers to follow `call.id` and links rather than parent pointers. The boundary is crossed the moment tool p99 latency exceeds the model's response cycle. Below that, nesting is correct and cheaper. Above it, nesting silently corrupts turn attribution. The runtime cannot detect the corruption from its own spans alone — it requires a consumer that validates `child.end ≤ parent.end`. ## Operational Effect When the boundary is crossed without switching to linked-sibling emission, three failures compound: 1. **Replay divergence.** An auditor replaying the trace reconstructs turn N as containing a 41s tool call and turn N+1 as containing no tool call. The replay's token accounting and the live run's token accounting disagree by the tool-result token mass — the exact class of discrepancy that makes verification replay non-deterministic. 2. **Cost undercount.** Per-turn cost models that sum `gen_ai.usage.*` along the span tree miss the tool-result tokens entirely, because those tokens only appear on the *following* `chat` span's `input_tokens`, which is attributed to turn N+1. 3. **Deadline masking.** The tool span's `ERROR` status is buried under a `chat` span that reported `finish_reasons=["tool_calls"]` — a *success* finish reason. Dashboards that alert on `chat` span error rate see zero errors while the agent is silently failing every fetch. The guardrail is a span-tree validator that runs before export: for every `execute_tool` span, assert `end_time ≤ parent.end_time` **or** assert the span is emitted as a linked sibling with `gen_ai.tool.call.id` present on both the tool span and the consuming `chat` span. Violation of both is the boundary failure. ```python from dataclasses import dataclass class SpanNestingInvariantError(RuntimeError): """Raised when a tool span outlives its parent invocation span without a linked-sibling re-parenting, corrupting turn attribution and usage accounting.""" @dataclass class Span: name: str operation: str # "chat" | "execute_tool" start_ns: int end_ns: int parent_id: str | None span_id: str tool_call_id: str | None = None linked_span_ids: tuple[str, ...] = () def validate_genai_trace(spans: list[Span]) -> None: by_id = {s.span_id: s for s in spans} for span in spans: if span.operation != "execute_tool": continue parent = by_id.get(span.parent_id) if span.parent_id else None if parent is None: # Root-level tool span: legal only if a consuming chat span links to it. consumers = [ s for s in spans if s.operation == "chat" and span.span_id in s.linked_span_ids ] if not consumers: raise SpanNestingInvariantError( f"tool span {span.span_id} ({span.tool_call_id}) is root-level " f"but no chat span links back to it; tool result is orphaned " f"and will be dropped by replay tooling" ) continue if span.end_ns > parent.end_ns: # Child outlives parent. Legal only if span was re-emitted as a # linked sibling with the call id preserved on both sides. linked = span.span_id in parent.linked_span_ids call_id_ok = ( span.tool_call_id is not None and parent.tool_call_id == span.tool_call_id ) if not (linked and call_id_ok): raise SpanNestingInvariantError( f"tool span {span.span_id} ends at {span.end_ns} after parent " f"{parent.span_id} ends at {parent.end_ns}; turn attribution " f"will assign the tool result to the wrong invocation. " f"Re-emit as linked sibling with gen_ai.tool.call.id on both spans." ) def run_trace_guard(spans: list[Span]) -> None: # Example: a chat span that closes before its tool call returns. chat = Span("chat", "chat", 0, 900_000_000, None, "c1") tool = Span("web_fetch", "execute_tool", 950_000_000, 41_000_000_000, parent_id="c1", span_id="t1", tool_call_id="call_ab12") validate_genai_trace([chat, tool]) # raises ```