# Where must an instance reconcile OpenTelemetry GenAI tool-call spans when the model invocation span closes before the tool result is attributed? **Date of Record:** 2026 November 30 **Scope:** Applies to agents that emit OpenTelemetry `gen_ai.*` spans per the GenAI semantic conventions, where a single model invocation requests one or more tool calls that execute asynchronously or in a child agent. Does not apply to synchronous single-shot completions with no tool calls, or to tracing systems that do not export span end-times independently of attribute mutation. ## Core Distinction The failure is not "missing spans" or "dropped attributes." It is a *temporal attribution boundary*: the GenAI conventions require tool-call outcome attributes (`gen_ai.tool.call.id`, `gen_ai.tool.name`, and the result/error status) to be recorded on a span, but the model-invocation span (`gen_ai.operation.name = chat`) is conventionally ended when the completion returns—*before* the tool it requested has executed. The tool-call span is a sibling or child that outlives its parent. When the exporter flushes and the backend reconstructs the trace, the parent's `end_time` precedes the child's `start_time`. The trace is structurally valid (all spans present, all attributes set) but *causally inverted*: the invocation appears to have completed before the work it spawned began. This is distinct from context-window loss or reasoning divergence. The agent's reasoning is intact. The corruption is in the observability contract: the span graph encodes a happens-before relation that contradicts reality, and any downstream consumer—cost attribution, latency SLOs, replay-based auditing—inherits the inversion. ## What Was Observed A tool-using agent emits, per turn: 1. A `chat` span opened at request dispatch, closed the moment the streaming completion finalizes. Attributes: `gen_ai.request.model`, token counts, `gen_ai.response.finish_reasons = ["tool_calls"]`. 2. One `execute_tool` span per requested call, opened when the tool actually runs—after the `chat` span has already ended. Because the convention ties `gen_ai.tool.call.id` to the *tool* span but the model's *request* for that tool is only visible in the `chat` span's finish reason, the linkage between "model asked for call X" and "call X executed" lives across a parent-child edge whose timestamps are inverted. Observed symptoms: - Trace viewers render the tool span as a disconnected or "future" child; some backends silently drop the parent-child link and re-root the tool span. - Latency aggregation over `chat` spans reports p99 well below wall-clock turn time, because tool execution time is excluded by construction. An SLO on `chat` duration passes while the user-visible turn is 40× slower. - A replay auditor reconstructing "what did the model see before deciding" reads the `chat` span's `end_time`, filters tool results by `timestamp <= end_time`, and finds *no* tool results—concluding the model hallucinated the tool output. The audit is wrong; the timestamps are. The inversion is deterministic for any agent whose tool calls are not inlined into the completion. It is invisible in single-span sampling and only surfaces when the full trace is joined by trace ID. ## Condition of Validity The inversion is benign only under a narrow condition: the tool executes *within* the `chat` span's lifetime (i.e., the runtime blocks on tool execution before closing the invocation span), **or** the backend does not enforce parent-child temporal ordering. Neither holds for streaming agents with concurrent tool dispatch, which is the default for modern tool-use loops. The invariant that must hold: *for every `execute_tool` span attributed to a `chat` span, `tool.start_time >= chat.start_time` AND the causal edge (model requested → tool ran) is representable without requiring `tool.start_time <= chat.end_time`.* The GenAI conventions do not currently encode this edge as a first-class link; the parent-child relation is the only carrier, and it collides with the span lifecycle. A second validity condition: the `gen_ai.tool.call.id` on the tool span must equal the call ID the model emitted in the completion. If the runtime generates its own ID at execution time (common when the completion's tool-call IDs are discarded during deserialization), the link is broken even if timestamps were fixed. The two failures compound: inverted timestamps *and* mismatched IDs make the trace unreconstructable. ## Decision Boundary The reconciliation decision: **when the model invocation span closes, does the runtime (a) hold it open until all requested tools resolve, (b) close it and emit explicit span links, or (c) close it and rely on parent-child with inverted timestamps?** - (a) is correct for causality but corrupts invocation latency—`chat` duration now includes tool time, defeating the purpose of separating model time from tool time. It also risks unbounded span lifetime if a tool hangs, exhausting exporter buffers. - (b) is the correct boundary: close `chat` at completion, open `execute_tool` as a *linked* span (OpenTelemetry `Link`, not parent), carrying `gen_ai.tool.call.id` copied verbatim from the completion's tool-call object. The link is timestamp-agnostic; the backend reconstructs causality from the link, not from temporal containment. - (c) is the observed failure. The boundary is crossed the moment the runtime discards the completion's tool-call IDs before tool dispatch, or the moment it sets the tool span's parent to the `chat` span while allowing `tool.start_time > chat.end_time`. Either action commits the trace to the inverted representation. ## Operational Effect Downstream consumers must be re-derived, not patched: - **Cost attribution** keyed on `chat` spans under-counts turns that fan out to tools; per-tool cost must be summed from `execute_tool` spans joined by link, not by temporal containment. - **Replay auditors** that filter by `timestamp <= parent.end_time` must instead follow links; the timestamp filter is structurally wrong for this trace shape. - **SLOs** on `chat` duration measure model time only; a separate turn-level SLO must join `chat` and its linked tool spans by trace ID and take `max(end_time) - min(start_time)`. The guardrail is a trace-shape validator that runs at export time: for every `chat` span with `finish_reasons` containing `tool_calls`, assert that each referenced `gen_ai.tool.call.id` resolves to an `execute_tool` span reachable by link (not by temporal containment), and reject the batch if any link is missing or any tool span was parented with inverted timestamps. This converts a silent observability corruption into a loud export failure. ```python from dataclasses import dataclass from typing import Optional class TraceAttributionError(RuntimeError): """Raised when a GenAI trace's tool-call attribution is causally invalid.""" pass @dataclass class Span: name: str operation: str # "chat" or "execute_tool" start_time: float end_time: Optional[float] tool_call_ids: list[str] # IDs this span requested (chat) or executed (tool) parent_id: Optional[str] link_ids: list[str] # explicit span links, timestamp-agnostic span_id: str def validate_genai_trace(spans: list[Span]) -> None: by_id = {s.span_id: s for s in spans} for chat in (s for s in spans if s.operation == "chat"): if chat.end_time is None: raise TraceAttributionError( f"chat span {chat.span_id} never closed; cannot attribute tool calls" ) for call_id in chat.tool_call_ids: resolved = [ t for t in spans if t.operation == "execute_tool" and call_id in t.tool_call_ids ] if not resolved: raise TraceAttributionError( f"chat {chat.span_id} requested tool call {call_id} " f"but no execute_tool span carries that id" ) for tool in resolved: linked = tool.span_id in chat.link_ids or chat.span_id in tool.link_ids parented = tool.parent_id == chat.span_id if parented and tool.start_time > chat.end_time: raise TraceAttributionError( f"causal inversion: tool {tool.span_id} starts " f"({tool.start_time}) after parent chat {chat.span_id} " f"ended ({chat.end_time}); use a span link, not parent-child" ) if not linked and not parented: raise TraceAttributionError( f"tool {tool.span_id} for call {call_id} is unattributed: " f"no link or parent edge to chat {chat.span_id}" ) ```