# Why does an instance lose causality linkage when a GenAI semantic-convention exporter batches spans across an agent's tool-call boundary **Date of Record:** 2026 December 1 **Scope:** Applies to agents that emit OpenTelemetry-style GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.agent.id`, `gen_ai.tool.call.id`, custom `causality.link.id`) through a batching exporter that flushes on a size or timer trigger. Does not apply to synchronous per-span exporters or to trace-only observability where no downstream consumer reconstructs the reasoning DAG. ## Core Distinction The distinction that matters is between **span emission order** and **causal dependency order**. Semantic-convention exporters treat spans as independent records appended to a buffer and flushed in insertion order. But an agent's reasoning trace is a DAG: a `gen_ai.operation.name=execute_tool` span for call `T1` is the *cause* of the subsequent `chat` span that consumes `T1`'s output, and both are linked to a `causality.link.id` that the exporter is supposed to carry verbatim. The exporter's batching logic is causally agnostic: it does not know that flushing the consumer span before the producer span — or dropping the producer span during a partial-batch overflow — severs the link that a downstream auditor uses to reconstruct *why* the agent took an action. The invariant is **producer-before-consumer within any batch that shares a `causality.link.id`**, and it is not enforced by any standard semantic convention. Only the causal edge itself encodes it, and the exporter never reads the edge. ## What Was Observed An agent executing a three-step tool chain (`search` → `fetch` → `summarize`) emitted spans with these attributes: - Span A: `gen_ai.operation.name=execute_tool`, `gen_ai.tool.call.id=call_7f2a`, `causality.link.id=cl_4410`, no parent span. - Span B: `gen_ai.operation.name=chat`, `causality.link.id=cl_4410`, `causality.parent.call.id=call_7f2a`. - Span C: `gen_ai.operation.name=execute_tool`, `gen_ai.tool.call.id=call_9b1c`, `causality.link.id=cl_4410`, `causality.parent.call.id=call_7f2a`. The exporter used a `BatchSpanProcessor`-equivalent with `max_export_batch_size=2` and a 200 ms timer. Under load, the buffer held A, B, C. On the size trigger, the exporter flushed `[A, B]` and left C queued. Then the agent's retry logic re-entered the tool layer and emitted a *new* Span A′ for `call_7f2a` (same `gen_ai.tool.call.id`, new span id, same `causality.link.id=cl_4410`), because the retry path did not check whether the original span had already been exported. The second flush emitted `[C, A′]`. Downstream, the trace reconstructor grouped by `causality.link.id=cl_4410` and found: 1. Two producer spans for the same `gen_ai.tool.call.id` (`call_7f2a`), with different span ids and different `start_time_unix_nano`. 2. A consumer span B whose `causality.parent.call.id=call_7f2a` now ambiguously matched two producers. 3. Span C arriving in a batch *after* B, so a naive insertion-order reconstruction placed C's cause (`call_7f2a`) after its effect. The auditor surfaced a "causality replay discrepancy": the reconstructed DAG showed the agent summarizing output from a tool call that, in reconstructed order, had not yet executed. The agent's actual behavior was correct; the exporter's batching and the retry path's lack of idempotency on `gen_ai.tool.call.id` corrupted the record. ## Condition of Validity The failure requires all three conditions simultaneously: 1. **Batching with a size or timer trigger.** A synchronous exporter that flushes each span on `end()` cannot reorder across a batch boundary. The reorder window opens only when spans accumulate. 2. **A retry or re-entry path that re-emits a span for an already-emitted `gen_ai.tool.call.id`.** Semantic conventions specify `gen_ai.tool.call.id` as the correlation key, but they do not require the exporter to deduplicate. An agent that retries a tool call without a stable span id (or without checking a local "already emitted" set) produces duplicate producers. 3. **A downstream consumer that reconstructs causality from `causality.link.id` + insertion order rather than from explicit parent span ids.** If the consumer resolves `causality.parent.call.id` against a *set* of producers and rejects ambiguity, the duplicate is caught. If it resolves against the *first* or *last* match, the record silently corrupts. If any one condition is absent, the invariant holds. This is why the failure is non-obvious: each component is individually correct per spec. The GenAI semantic conventions define the attribute names; they do not define batch-boundary causality semantics. The exporter is spec-compliant. The retry path is spec-compliant. The reconstructor is spec-compliant. The composition is not. ## Decision Boundary The boundary is between **attribute-level causality** (what the semantic conventions specify) and **record-level causality** (what the trace consumer needs). The conventions give you `causality.link.id` and `causality.parent.call.id` as *attributes on a span*. They do not give you a guarantee that the span carrying the producer attribute is *present in the same export batch* as, or *ordered before*, the span carrying the consumer attribute. The exporter cannot enforce this because it does not parse the causal attributes — it treats them as opaque key-value pairs. The correct guardrail is a **causality-aware flush barrier**: before flushing a batch, the exporter partitions pending spans by `causality.link.id` and defers any batch that would split a link group across the flush boundary, up to a bounded deferral budget (e.g., 2× the timer interval). If the budget is exceeded, the exporter must flush the *entire* link group atomically or emit an explicit `causality.link.incomplete=true` marker on every span in the split group so the consumer can reject the reconstruction rather than silently mis-order it. The retry path must additionally maintain a `call_id → span_id` map and reuse the original span id on re-entry, or emit a `gen_ai.tool.call.retry_of=` attribute so the duplicate is distinguishable from a genuine second invocation. ## Operational Effect Without the barrier, the operational effect is a **silent causality inversion**: the trace reconstructor produces a plausible-looking DAG that misrepresents the agent's reasoning order. Because the DAG is plausible, automated auditors pass it; because it is wrong, any post-hoc analysis of *why* the agent chose an action — the core purpose of reasoning-trace export — is unreliable. The failure is load-dependent (only under batching pressure) and retry-dependent (only when the agent re-enters a tool call), so it is invisible in low-traffic staging and appears intermittently in production. The mitigation cost is small (a partition step in the exporter flush path and an idempotency map in the retry path), but the detection cost is high because the corrupted record is internally consistent and passes schema validation. Teams that only validate span *schema* against the semantic conventions will never see it; teams that validate span *DAG acyclicity and producer-before-consumer ordering* against `causality.link.id` groups will catch it on the first load spike. ```python from dataclasses import dataclass, field from typing import Optional class CausalityLinkError(RuntimeError): """Raised when a span batch would split a causality link group across a flush boundary.""" @dataclass class Span: span_id: str operation: str causality_link_id: str tool_call_id: Optional[str] = None parent_call_id: Optional[str] = None start_ns: int = 0 @dataclass class CausalityAwareBatcher: max_batch: int deferral_budget: int = 2 pending: list = field(default_factory=list) emitted_call_ids: dict = field(default_factory=dict) def emit(self, span: Span) -> None: # Retry idempotency: reuse the original span id for a re-entered tool call. if span.tool_call_id is not None: if span.tool_call_id in self.emitted_call_ids: span.span_id = self.emitted_call_ids[span.tool_call_id] span.operation = span.operation + ".retry" else: self.emitted_call_ids[span.tool_call_id] = span.span_id self.pending.append(span) def flush(self) -> list: if not self.pending: return [] # Partition by causality link group. groups: dict = {} for s in self.pending: groups.setdefault(s.causality_link_id, []).append(s) ready, deferred = [], [] for link_id, spans in groups.items(): if len(spans) <= self.max_batch: ready.extend(spans) else: # Link group exceeds batch size: cannot flush atomically. # Emit explicit incompleteness marker rather than split silently. if self.deferral_budget <= 0: for s in spans: s.operation = s.operation + ".incomplete" raise CausalityLinkError( f"causality link {link_id} has {len(spans)} spans " f"exceeding max_batch={self.max_batch}; " f"deferral budget exhausted, marking incomplete" ) self.deferral_budget -= 1 deferred.extend(spans) if ready: # Enforce producer-before-consumer within the flushed set. ready.sort(key=lambda s: (s.parent_call_id is not None, s.start_ns)) self.pending = deferred return ready ```