# Why does an instance lose tool-call causality when OTel GenAI spans cross an async collector boundary **Date of Record:** 2026 December 1 **Scope:** Applies to agent runtimes emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) for model invocations and tool executions that traverse an asynchronous collector pipeline (batching exporter → collector → backend). Does not apply to synchronous in-process exporters that flush per-span, nor to non-GenAI span trees. ## Core Distinction The distinction that matters is between **span-tree structural correctness** and **causal attribute correctness**. A GenAI span tree can be perfectly valid by OTel structural rules—every `gen_ai.tool.call` span has a parent `gen_ai.chat` span, `trace_id`/`span_id` are well-formed, no orphan spans—while the *semantic linkage* between a tool result and the model turn that requested it is silently destroyed. This happens because the GenAI conventions encode causality in **attributes** (`gen_ai.tool.call.id`, `gen_ai.usage.*`, request/response linkage) rather than in the parent-child graph, and attributes are mutable payload that collector processors and batching exporters are free to drop, reorder, or truncate. Structural validity is preserved; causal recoverability is not. ## What Was Observed A multi-step agent executing a plan of `chat → tool → chat → tool → chat` emitted spans that, when read back from the backend, showed a consistent corruption: the second and third `gen_ai.chat` spans referenced tool results whose originating `gen_ai.tool.call.id` no longer matched any emitted tool span. The reconstruction logic that paired a tool result to its requesting turn (via the `gen_ai.tool.call.id` attribute carried on the follow-up chat span's `gen_ai.prompt` array) failed for every turn after the first. The trace was structurally intact. Parent-child edges were correct. `trace_id` was stable across all nine spans. What was missing was the *value* of `gen_ai.tool.call.id` on the tool-execution spans—present on the model spans that referenced it, absent on the tool spans that should have defined it. The trigger condition was the combination of three ordinary facts: 1. The runtime attached `gen_ai.tool.call.id` as a **span attribute at span end** (after the tool returned), not at span start. 2. The collector pipeline ran a `transform` processor that applied an attribute allowlist to `gen_ai.tool.call` spans, and the allowlist was authored against an earlier convention revision that predated `gen_ai.tool.call.id`. 3. The batching exporter's `max_export_batch_size` caused the tool span and its referencing chat span to land in **different batches**, so the allowlist processor saw them in separate processing windows and had no opportunity to reconcile. The failure trace: tool span starts (no `call.id`) → tool executes → tool span ends, attributes stamped including `call.id` → batch flushes → collector `transform` processor drops `call.id` (not in allowlist) → tool span forwarded without the id → chat span (in next batch) forwarded with `gen_ai.prompt` referencing `call.id="call_7f2a"` → backend stores an unresolvable reference. ## Condition of Validity The invariant that must hold for causal reconstruction to survive the pipeline is: > For every `gen_ai.tool.call.id` value referenced in a model span's prompt/response payload, exactly one `gen_ai.tool.call` span in the same `trace_id` must carry that value as a span attribute **at the point of backend ingestion**, and the attribute must be present before any processor that can drop it. This is a *cross-span attribute referential integrity* constraint. It is not enforced by OTel's structural validation, which checks span graph shape and attribute types but not inter-span attribute references. It is not enforced by the collector, which processes spans independently unless an explicit stateful processor (e.g., `groupbytrace`) is configured—and even then, attribute rewriting in `transform` is stateless per-span. The condition fails whenever any of the following is true: - The referencing attribute is stamped at a lifecycle point (span end) that a downstream processor cannot see at start. - Any processor between emitter and backend applies an attribute allowlist, denylist, or rename that does not include the linkage attribute. - Batching splits causally-linked spans such that a stateful reconciliation processor cannot co-locate them. ## Decision Boundary The boundary is between **treating GenAI attributes as schema-validated structural metadata** versus **treating linkage attributes as a referential-integrity surface that must be protected end-to-end**. The decision rule: - If the pipeline contains *any* attribute-mutating processor (allowlist, denylist, `transform`, `attributes`), then every `gen_ai.tool.call.id` and any `gen_ai.*` attribute used as a cross-span foreign key must be added to a **protected attribute set** that no processor may drop, and this set must be validated against the convention revision the emitter targets. - If batching can split causally-linked spans, either (a) disable batching for GenAI span trees via a dedicated exporter with `max_export_batch_size=1`, or (b) insert a `groupbytrace` processor before any attribute-mutating processor so linkage is reconciled within one processing window. - If neither is done, the system must **not** claim causal traceability for tool-call reconstruction—only structural traceability. Conflating the two is the failure. The boundary is crossed the moment a downstream consumer assumes it can reconstruct "which model turn requested this tool call" from the trace. That assumption is only valid under the protected-attribute + co-located-batch regime. ## Operational Effect The practical consequence is a **silent downgrade from causal to structural observability**. Dashboards counting tool spans still work; latency attribution still works; but any agent-debugging workflow that reconstructs the reasoning chain from traces—"the model asked for tool X, got result Y, then produced Z"—breaks at the first tool call after the first batch boundary. Because the spans are structurally valid, no alert fires. The corruption is discovered only when an engineer tries to replay a failed agent run and finds the trace cannot be reassembled. The guardrail that bounds it: emit linkage attributes at **span start**, register them in a protected-attribute manifest checked in CI against the live collector config, and assert in the backend-ingestion smoke test that every `gen_ai.tool.call.id` referenced in a chat span resolves to exactly one tool span in the same trace. The assertion below encodes that invariant as a hard check. ```python class GenAICausalityViolation(RuntimeError): """Raised when a GenAI trace cannot reconstruct tool-call causality because a referenced gen_ai.tool.call.id has no defining tool span.""" def assert_tool_call_causality(spans: list[dict]) -> None: """Verify cross-span referential integrity for gen_ai.tool.call.id. Each span is a dict with 'trace_id', 'name', 'attributes', and optional 'references' (list of call.id values the span's payload points at). """ defined: dict[str, set[str]] = {} for span in spans: if span["name"] == "gen_ai.tool.call": call_id = span["attributes"].get("gen_ai.tool.call.id") if call_id is None: raise GenAICausalityViolation( f"tool span {span.get('span_id')} in trace " f"{span['trace_id']} emitted without gen_ai.tool.call.id; " "linkage attribute dropped or stamped after span-end" ) defined.setdefault(span["trace_id"], set()).add(call_id) for span in spans: for ref in span.get("references", []): if ref not in defined.get(span["trace_id"], set()): raise GenAICausalityViolation( f"span {span.get('span_id')} in trace {span['trace_id']} " f"references gen_ai.tool.call.id={ref!r} but no tool span " "defines it; collector processor likely dropped the " "attribute or batching split the causal pair" ) ```