# When must an instance reject a GenAI span whose tool-call attributes are structurally valid but semantically orphaned **Date of Record:** 2026 November 30 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) for model invocations and tool calls, and that reconstructs execution traces from exported spans for audit, replay, or memory distillation. Does not apply to runtimes that treat spans as pure telemetry with no downstream semantic consumer. ## Core Distinction A GenAI span can be *syntactically conformant* and still be *semantically orphaned*. The convention defines attributes like `gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, and `gen_ai.tool.call.id` — but it does not enforce a referential invariant between a tool-call span and the model-invocation span that *caused* it. The distinction that matters operationally: **attribute presence is not causation.** A `gen_ai.tool.name` attribute tells you a tool was named; it does not tell you which model invocation's reasoning selected that tool, nor whether the tool's result was ever fed back into a subsequent invocation. When a trace consumer (replay engine, memory distiller, auditor) assumes the latter from the former, it fabricates causal edges that never existed and silently corrupts the reconstructed episode. The non-obvious boundary: the orphan condition is *invisible at export time* because the span is valid. It only surfaces at reconstruction time, when the consumer attempts to stitch `model_invocation → tool_call → model_invocation` and finds a gap that the schema permitted. ## What Was Observed A multi-step agent (plan → search tool → synthesize) emitted spans that passed the OTel collector's schema validation with zero errors. The trace contained: - Span A: `gen_ai.operation.name=chat`, `gen_ai.request.model=...`, `gen_ai.usage.input_tokens=1840`, `gen_ai.usage.output_tokens=220`. No `gen_ai.tool.call.id`. - Span B: `gen_ai.operation.name=execute_tool`, `gen_ai.tool.name=web_search`, `gen_ai.tool.call.id=call_7f3a`. Parent span = A. - Span C: `gen_ai.operation.name=chat`, `gen_ai.request.model=...`, `gen_ai.usage.input_tokens=2210`. Parent span = root, **not** B. The failure trace: the runtime's tool-result injection happened *outside* the span that executed the tool, and the follow-up model invocation was parented to the root because the orchestration loop opened it before the tool span closed under async scheduling. The result: Span C's input token count (2210) is 370 tokens larger than Span A's output (220) plus A's input — consistent with the tool result *having been injected* — but there is **no span edge** connecting B's output to C's input. A replay consumer that reconstructs "what did the model see" by walking parent edges will conclude C ran without tool output, then re-invoke the model with a prompt missing the search results. The replay diverges from the original execution on the very first tool-using turn. Critically, no attribute was missing. `gen_ai.tool.call.id` was present on B. The convention simply has no attribute on C asserting "I consumed the result of call_7f3a." Causality was carried by wall-clock ordering and token arithmetic, not by any declared reference. ## Condition of Validity The invariant a consumer may rely on is narrow: **a tool-call span's `gen_ai.tool.call.id` is unique within a trace and is the only sanctioned join key.** It does *not* assert: 1. That a subsequent `chat` span consumed that call's result. 2. That the parent edge of the consuming span points at the tool span. 3. That token accounting on the consuming span reconciles with the tool output length. These three are *empirical correlations* in well-behaved runtimes, not schema guarantees. The invariant holds only when the runtime explicitly emits a linking attribute — e.g., a custom `gen_ai.tool.result.consumed_by` or the tool call id echoed into the consuming span's attributes — or when the consumer reconstructs causality from token deltas and ordering with an explicit tolerance. Absent that, the consumer must treat every `chat` span following a tool span as *possibly* orphaned and flag it, not assume continuity. ## Decision Boundary The boundary is: **does the reconstructed episode's token arithmetic reconcile, and is there a declared reference, or only a temporal one?** - If the consuming `chat` span carries an explicit reference to the tool call id (declared edge) → accept the causal link. - If the link is inferred only from parent span + temporal ordering → accept *provisionally*, mark the episode `causality=inferred`, and require reconciliation: `input_tokens(C) − input_tokens(A) − output_tokens(A)` must be within a tolerance of the tool result's serialized token length. If reconciliation fails, the edge is rejected. - If neither declared edge nor reconciliation holds → the span is semantically orphaned. Reject it from replay and from memory distillation. Do not let an orphaned `chat` span contribute to episodic memory, because it will teach the distilled policy that tool results are optional. The subtle trap: reconciliation can *pass by coincidence* when unrelated context (system prompt churn, retry padding) happens to match the tool output size. Therefore reconciliation is necessary but not sufficient; the declared reference is the only sufficient condition, and its absence must be surfaced as a degradation, not silently tolerated. ## Operational Effect An agent that distills memory from orphaned spans learns a false transition: "after calling `web_search`, proceed to synthesize without the result." Over many episodes this manifests as a policy that under-weights tool outputs, produces confident answers ungrounded in retrieval, and — because the memory distiller saw no contradiction — never flags the gap. The corruption is silent and compounds: each orphaned episode reinforces the prior. The guardrail is a reconstruction-time validator that refuses to emit a distilled transition across an unreconciled span boundary, raising instead of guessing. The validator must live at the *consumer* boundary, not the exporter, because the exporter cannot see the future span and the schema does not require the link. Treat the missing declared edge as a first-class fault, not a cosmetic telemetry gap. ```python from dataclasses import dataclass from typing import Optional class OrphanedToolCausalityError(RuntimeError): """Raised when a chat span's causal link to a prior tool call cannot be established by declared reference and fails token reconciliation.""" @dataclass class Span: name: str operation: str parent: Optional[str] tool_call_id: Optional[str] = None consumed_tool_call_id: Optional[str] = None # declared edge, if emitted input_tokens: int = 0 output_tokens: int = 0 tool_result_tokens: int = 0 def reconstruct_transition(prior_chat: Span, tool: Span, followup: Span, tolerance: int = 16) -> str: # Declared edge is the only sufficient condition. if followup.consumed_tool_call_id == tool.tool_call_id and tool.tool_call_id: return "declared" # Otherwise attempt reconciliation: did the followup's input grow by the # tool result's size relative to the prior chat's context? delta = followup.input_tokens - (prior_chat.input_tokens + prior_chat.output_tokens) if abs(delta - tool.tool_result_tokens) <= tolerance: return "inferred-reconciled" raise OrphanedToolCausalityError( f"tool_call_id={tool.tool_call_id!r} has no declared consumer and " f"token delta {delta} does not reconcile with tool_result_tokens " f"{tool.tool_result_tokens} (tol={tolerance}); refusing to distill " f"transition across orphaned span boundary" ) ```