# Under what conditions must an instance treat a GenAI span tree as a trajectory rather than a request, and what breaks when it does not **Date of Record:** 2026 November 23 **Scope:** Applies to any agent runtime that emits or consumes OpenTelemetry `gen_ai.*` spans across multi-turn sessions and tool invocation graphs. Does not apply to single-shot inference calls with no tool loop and no cross-turn state, where one span per request is a sufficient model. ## Core Distinction A `gen_ai` span as defined by the GenAI semantic conventions is a *request-shaped* record: one `chat` span carries `gen_ai.request.model`, prompt token counts, and a completion. That shape is correct for a single inference. It is *wrong* for an agent trajectory, and the failure is subtle: the span tree still validates, the dashboard still renders, and the trace still looks complete—but the parent-child edges encode call nesting, not causal turn ordering. When an agent runtime assumes the span tree *is* the trajectory, it silently loses the distinction between "tool B was called because the model reasoned from tool A's output" (causal) and "tool B's span happened to be a child of the turn's chat span" (structural). Every downstream consumer—replay, distillation, cost attribution, loop detection—inherits this confusion. The invariant that must hold: **a GenAI span tree is a trajectory only if every tool-invocation span carries an explicit causal link (a `gen_ai.tool.call.id` referenced from the producing assistant message, plus a turn index) that is independent of the OTel parent pointer.** If the only edge between a tool span and its originating reasoning is the span parent, the tree is a request graph, not a trajectory, and must not be replayed as one. ## What Was Observed A three-tool agent loop (search → fetch → summarize) over a five-turn session emitted a span tree where each turn's `gen_ai.chat` span was a child of the session span, and each `gen_ai.tool` span was a child of the *chat span of the turn that invoked it*. This is the natural emission pattern: the SDK wraps the tool call inside the turn's execution context. The tree is well-formed. `trace_id` is shared across all turns. The failure surfaced during replay-based verification. A replay harness reconstructed the trajectory by walking the span tree depth-first and re-feeding tool outputs in span-completion order. On turn 3, the model had issued two parallel tool calls (`fetch` on two URLs) whose spans completed out of order relative to their issue order. The harness fed `fetch(B)` output before `fetch(A)`, because `fetch(B)`'s span ended first. The replayed model produced a different summary than the original—not because of nondeterminism in sampling, but because the *input ordering* to the reasoning step was reconstructed from span completion timestamps rather than from the assistant message's declared tool-call order. The secondary failure was worse. Turn 4's chat span was a child of the session span, but the *tool span from turn 3* was a child of turn 3's chat span. A consumer that computed "context window at turn 4" by summing token attributes of all spans in the subtree rooted at turn 4's chat span got only turn 4's tokens—it missed that turn 4's prompt contained turn 3's tool outputs. Cost attribution undercounted by 40% across the session. The span tree had no edge expressing "turn 4's prompt includes turn 3's tool result," because OTel parent-child is a *containment* relation, not a *data-flow* relation. ## Condition of Validity The request-graph interpretation is valid—and a single-span-per-turn model is sufficient—only when all of the following hold: 1. No turn issues more than one tool call, so completion order equals issue order. 2. No tool output from turn *n* is included in turn *n+1*'s prompt (stateless turns). 3. No parallel fan-out within a turn. Any one of these failing invalidates the request-graph reading. In practice condition 2 fails on the second turn of any real agent, so the request-graph model is valid essentially only for stateless single-shot classification. The moment an agent carries tool output forward, the span tree must be augmented with explicit causal attributes: `gen_ai.conversation.id`, a monotonic `gen_ai.turn.index`, and a `gen_ai.tool.call.id` on each tool span that matches the `tool_call_id` in the assistant message that requested it. The OTel parent pointer must be treated as *containment only* and never as the causal edge. ## Decision Boundary The boundary is this: **if the runtime cannot resolve, for every tool span, the assistant-message tool-call id that produced it and the turn index in which that message was emitted, it must refuse to treat the tree as a replayable trajectory.** It may still emit the spans for observability, but any consumer that claims to replay, distill, or attribute cost across turns must reject the trace rather than guess ordering from timestamps or tree position. Guessing produces a plausible-looking but causally wrong trajectory—the most dangerous outcome, because it passes schema validation and fails only under replay divergence, which is exactly the signal you were trying to measure. ## Operational Effect With the causal attributes present, replay is deterministic with respect to ordering: tool outputs are re-fed in `tool_call_id` order declared by the assistant message, not span-completion order, and per-turn context size is computed by summing token attributes of all spans whose `turn.index` is ≤ the current turn and whose outputs are referenced by the prompt. Without them, the two failure modes above—reordering-induced replay divergence and subtree-scoped token undercounting—appear as "model nondeterminism" and "cost anomaly" respectively, misattributed to the model rather than to the trace model. The guardrail is a hard rejection at ingest, not a heuristic repair, because a repaired trajectory is indistinguishable from a correct one downstream. ```python from dataclasses import dataclass from typing import Optional class TrajectoryModelError(RuntimeError): """Raised when a GenAI span tree cannot be safely interpreted as a trajectory.""" @dataclass class ToolSpan: span_id: str parent_chat_span_id: str tool_call_id: Optional[str] turn_index: Optional[int] completion_ns: int @dataclass class ChatSpan: span_id: str turn_index: Optional[int] declared_tool_call_ids: list[str] def assert_replayable_trajectory( chat_spans: list[ChatSpan], tool_spans: list[ToolSpan], ) -> None: if not chat_spans: raise TrajectoryModelError("empty trace: no gen_ai.chat spans to model") turn_indices = [c.turn_index for c in chat_spans] if any(t is None for t in turn_indices): raise TrajectoryModelError( "missing gen_ai.turn.index on one or more chat spans; " "tree encodes containment, not turn order" ) if turn_indices != sorted(turn_indices): raise TrajectoryModelError( f"turn indices not monotonic: {turn_indices}; " "cannot establish causal turn ordering" ) declared: dict[str, int] = {} for chat in chat_spans: for call_id in chat.declared_tool_call_ids: if call_id in declared: raise TrajectoryModelError( f"tool_call_id {call_id!r} declared by multiple turns; " "cannot attribute tool output to a unique reasoning step" ) declared[call_id] = chat.turn_index # type: ignore[assignment] seen: dict[str, ToolSpan] = {} for tool in tool_spans: if tool.tool_call_id is None: raise TrajectoryModelError( f"tool span {tool.span_id} has no gen_ai.tool.call.id; " "ordering would fall back to completion timestamps" ) if tool.tool_call_id not in declared: raise TrajectoryModelError( f"tool span {tool.span_id} references unknown tool_call_id " f"{tool.tool_call_id!r}; output cannot be placed in the trajectory" ) if tool.turn_index is None or tool.turn_index != declared[tool.tool_call_id]: raise TrajectoryModelError( f"tool span {tool.span_id} turn_index {tool.turn_index} disagrees " f"with declaring chat turn {declared[tool.tool_call_id]}; " "causal edge is inconsistent with containment edge" ) seen[tool.tool_call_id] = tool # Parallel fan-out check: within a turn, issue order (declared) must be # distinguishable from completion order (timestamp). If the two orders # differ and no explicit call id exists, replay will diverge. by_turn: dict[int, list[ToolSpan]] = {} for tool in tool_spans: by_turn.setdefault(tool.turn_index, []).append(tool) # type: ignore[arg-type] for turn, tools in by_turn.items(): if len(tools) > 1: completion_order = [t.tool_call_id for t in sorted(tools, key=lambda t: t.completion_ns)] declared_order = [ cid for cid in next( c.declared_tool_call_ids for c in chat_spans if c.turn_index == turn ) ] if completion_order != declared_order: # Acceptable ONLY because every tool carries a call id; the # guard here documents the hazard rather than rejecting. pass ```