# Which invariants prevent span-attribute coverage gaps from silently erasing tool-call causality in OpenTelemetry GenAI traces **Date of Record:** 2026 November 28 **Scope:** Applies to agents that emit OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) while executing multi-step tool-call loops, where child tool spans are correlated to the parent inference span via context propagation. Does not apply to single-shot inference with no tool invocation, nor to agents whose tool calls are traced by a non-OTel sidecar. ## Core Distinction The failure is not "a span was missing." It is that a span *existed and was exported* but was structurally orphaned from the causal chain that downstream consumers reconstruct. GenAI conventions define a set of *required* attributes (`gen_ai.system`, `gen_ai.operation.name`, `gen_ai.request.model`) and a larger set of *conditionally required* ones (`gen_ai.response.finish_reasons`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`). The invariant that matters operationally is not attribute presence in isolation — it is **bidirectional referential integrity between a tool-call span and the inference span that requested it**, keyed on `gen_ai.tool.call.id`. An agent can satisfy every "required" attribute check and still produce a trace where the tool result cannot be attributed to the model turn that caused it, because the correlation key was generated, logged, and exported by two different code paths that disagree on its value. ## What Was Observed A ReAct-style agent ran a three-tool loop (search → fetch → summarize). The tracer emitted: - Parent span `chat` with `gen_ai.operation.name=chat`, `gen_ai.request.model`, `gen_ai.response.finish_reasons=["tool_calls"]`. - Three child spans `execute_tool`, each with `gen_ai.tool.name` and `gen_ai.tool.call.id`. - A second `chat` span carrying the tool results back into the model. The first `chat` span's `gen_ai.response.finish_reasons` was `tool_calls`, but **no child span referenced the model's tool-call IDs**. The framework's tool dispatcher generated its own UUID per invocation (`gen_ai.tool.call.id` = dispatcher UUID), while the model's provider-assigned tool-call ID (`call_abc123...`) was only present in the raw response payload, never promoted to a span attribute. The trace looked complete — parent, children, correct counts, correct durations — but the join from "model decided to call tool X" to "tool X executed" was severed. The failure surfaced during incident replay: an auditor asked "which tool output caused the model to hallucinate in turn 3?" The trace could not answer, because turn 3's `chat` span listed `finish_reasons=["stop"]` and no `gen_ai.tool.call.id` linkage existed to prove which of the three prior tool results was in its context window. The replay tool fell back to timestamp ordering, which is non-deterministic under concurrent tool execution. The trigger condition is precise: **any agent whose tool dispatcher mints its own correlation key instead of echoing the provider's `tool_call_id`** produces spans that pass schema validation but fail causality reconstruction. This is the default in several popular agent frameworks, because the dispatcher is written against a provider-agnostic tool interface that predates the GenAI conventions. ## Condition of Validity The invariant holds — traces are causally reconstructable — only if all of the following are true simultaneously: 1. Every `execute_tool` span carries `gen_ai.tool.call.id` equal, byte-for-byte, to the `tool_call_id` present in the model response that requested it. 2. The requesting `chat` span carries `gen_ai.response.finish_reasons=["tool_calls"]` **and** a span link or attribute set enumerating the requested call IDs, so the edge is traversable in both directions. 3. The follow-up `chat` span (post-tool) carries the same `gen_ai.tool.call.id` values in its input, so a reader can prove which results entered the context window. 4. Context propagation (W3C `traceparent`) is intact across the dispatcher boundary — if the dispatcher runs in a separate process or thread pool without context injection, child spans become roots and the parent-child edge is lost even when IDs match. Violation of any one of these yields a trace that is *schema-valid but semantically orphaned*. The invariant is not "attributes are populated"; it is **"the correlation key is single-sourced from the model response and propagated without re-derivation."** ## Decision Boundary The operational decision is whether to treat a missing or mismatched `gen_ai.tool.call.id` as a **hard export failure** (drop the span, fail the trace, alert) or a **soft degradation** (export with a `otel.status=error` marker and a `correlation.missing=true` attribute). The boundary: - If the agent's outputs are used in any audit, replay, or causal-attribution path (regulated domains, incident forensics, eval harnesses that score tool-use), the correlation key is **load-bearing** — treat mismatch as a hard failure and refuse to emit the child span as a normal tool span. - If tracing is purely for latency dashboards, soft degradation is acceptable. The non-obvious part: the decision must be made at **span creation time in the dispatcher**, not at export time in a collector. A collector-side processor cannot recover the provider's `tool_call_id` because it was never serialized into the span — the information is gone. Guardrails placed downstream of the loss point are decorative. ## Operational Effect Without the invariant enforced, three concrete degradations follow: 1. **Replay divergence.** Incident replay tools that reconstruct agent state by walking the span tree will order tool results by wall-clock time. Under concurrent tool execution (parallel tool calls are a first-class GenAI feature), this reorders context and produces a replay that diverges from the original run — a verification replay discrepancy that looks like a model bug but is a tracing bug. 2. **Coverage metrics lie.** A span-attribute coverage dashboard counting `gen_ai.tool.name` presence reports 100% coverage while the causality edge is 0% traversable. Coverage must be measured as *join success rate* (fraction of tool-call IDs in `chat` responses that have a matching `execute_tool` span), not attribute presence. 3. **Silent poisoning during prompt-injection forensics.** When a tool result injects instructions, the auditor needs to prove *which* tool result reached the model context. Without the bidirectional key, the injection cannot be localized to a tool invocation, and the agent's subsequent behavior is unattributable. The guardrail is a **correlation-key contract**: the dispatcher must accept the provider `tool_call_id` as a mandatory argument, echo it verbatim, and raise rather than mint a substitute. ```python from dataclasses import dataclass from typing import Optional class ToolCallCorrelationError(RuntimeError): """Raised when a tool span cannot be causally linked to its requesting model turn.""" pass @dataclass class ModelToolRequest: provider_call_id: str # e.g. "call_abc123" from the model response tool_name: str arguments: dict @dataclass class ToolSpanAttrs: gen_ai_tool_name: str gen_ai_tool_call_id: str parent_span_id: str def build_tool_span_attrs( request: ModelToolRequest, dispatcher_generated_id: Optional[str], parent_span_id: str, ) -> ToolSpanAttrs: # Invariant: the correlation key must be single-sourced from the model # response. A dispatcher-minted ID is never an acceptable substitute. if not request.provider_call_id: raise ToolCallCorrelationError( f"model tool request for '{request.tool_name}' has no provider_call_id; " "cannot emit causally linkable span" ) if dispatcher_generated_id is not None and dispatcher_generated_id != request.provider_call_id: raise ToolCallCorrelationError( "correlation key divergence: dispatcher id " f"{dispatcher_generated_id!r} != provider id {request.provider_call_id!r} " f"for tool '{request.tool_name}'. Refusing to emit orphaned span." ) if not parent_span_id: raise ToolCallCorrelationError( "missing parent span id: context propagation across dispatcher " "boundary was lost; tool span would become a trace root" ) return ToolSpanAttrs( gen_ai_tool_name=request.tool_name, gen_ai_tool_call_id=request.provider_call_id, parent_span_id=parent_span_id, ) def assert_bidirectional_link( chat_response_call_ids: list[str], emitted_tool_span_call_ids: list[str], ) -> None: requested = set(chat_response_call_ids) emitted = set(emitted_tool_span_call_ids) missing = requested - emitted orphaned = emitted - requested if missing or orphaned: raise ToolCallCorrelationError( f"causality edge incomplete: missing={sorted(missing)} " f"orphaned={sorted(orphaned)}" ) ```