# Which invariants prevent orphaned tool-call spans from corrupting GenAI trace causality under concurrent sub-agent fan-out **Date of Record:** 2026 November 28 **Scope:** Applies to any agent runtime that emits OpenTelemetry `gen_ai.*` spans where a single model invocation (`gen_ai.operation.name = "chat"`) fans out to N tool-call spans (`gen_ai.operation.name = "execute_tool"`) that are executed concurrently by sub-agents and must be parented under the originating invocation span. Does not apply to purely sequential single-tool agents or to runtimes that flatten all tool calls into a single span with `gen_ai.tool.call.id` attributes only. ## Core Distinction The GenAI semantic conventions define `gen_ai.tool.call.id` as a correlation attribute, not a causal link. The causal link is carried by the OTel span parent/child relationship (`parent_span_id`). These two channels are maintained by different code paths: the attribute is written by the tool executor at completion time, while the parent link is written by the tracer at span *start* time. Under concurrent fan-out, the parent link is fixed before the tool result exists, but the `gen_ai.tool.call.id` is only known after the model's streamed `tool_use` block finalizes. This asymmetry is the failure surface. A trace can be internally *consistent* (every tool span has a valid parent) while being *causally wrong* (the parent is the wrong invocation, or the parent span has already ended, producing an orphan). The distinction that matters operationally: **span-parentage correctness is a start-time invariant, not a completion-time one**, and any tool executor that defers span creation until it has the call ID is already violating it. ## What Was Observed A research-orchestrator agent ran a `chat` invocation that streamed three `tool_use` blocks (`search`, `fetch`, `summarize`). The runtime used a thread pool of size 3 to execute them concurrently. Each worker thread lazily created its `execute_tool` span *inside* the worker, after receiving the parsed tool arguments, using `opentelemetry.context.attach()` on a context captured from the parent thread. The captured context was the one active at the moment the worker was *submitted*, which was the model-invocation span — correct in the sequential case. Under fan-out, the `chat` span ended as soon as the model's final token was emitted and the last `tool_use` block was handed to the pool. Two of the three tool spans were created *after* the parent `chat` span had ended. The OTel SDK does not raise on a child span created under an ended parent; it emits the span with a `parent_span_id` pointing at a span whose `end_time` precedes the child's `start_time`. The resulting trace is structurally valid JSON, passes schema validation, and renders in Jaeger as a parent with children that "begin after the parent finished" — a causality violation that no `gen_ai.*` attribute check catches, because every attribute (`gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.usage.*`) is present and well-formed. The downstream symptom appeared in an auditor agent that consumed the trace to verify tool-result provenance. It reconstructed the call graph by joining `gen_ai.tool.call.id` to the model's `tool_use` block and asserted that the join target's `parent_span_id` equaled the invocation span. The assertion held, so the auditor passed the trace. But the auditor's own replay of the invocation (using the same trace to re-drive the model) produced a *different* tool ordering because the trace's `start_time` ordering — which the auditor used as the causal ordering — was scrambled by thread scheduling, not by model intent. The replay diverged from the original run, and the divergence was attributed to model non-determinism rather than to the trace's broken causality invariant. ## Condition of Validity The invariant that must hold: **for every `execute_tool` span S with `gen_ai.tool.call.id = C`, there exists exactly one `chat` span P such that (a) `S.parent_span_id = P.span_id`, (b) `P.start_time <= S.start_time`, and (c) `S.end_time <= P.end_time`.** Condition (c) is the one that fails here, and it is the one nobody checks, because the OTel spec permits child spans to outlive parents (it is legal for async work). The GenAI conventions do not override this permissiveness, so the conformance suite does not test it. The invariant is only *achievable* if the parent `chat` span's lifetime is extended to cover the fan-out — i.e., the invocation span must not end at last-token but at last-tool-completion, or the tool spans must be reparented to a longer-lived `invoke_agent` span that brackets the whole turn. The semantic conventions define `gen_ai.operation.name = "invoke_agent"` precisely for this bracketing role, but the conventions do not *require* it, and most instrumentations emit only `chat`, leaving the fan-out children parented to a span that cannot outlive them. The condition of validity is therefore: *either* an `invoke_agent` bracket exists and tool spans parent to it, *or* the `chat` span is held open until all tool spans for its `tool_use` blocks have ended. Absent one of these, the invariant is unenforceable and the trace is causally ambiguous. ## Decision Boundary The boundary is whether the runtime treats the model invocation span as a *scoped event* (ends at last token) or as a *transaction* (ends at last dependent completion). The GenAI conventions are written as if it were an event — `gen_ai.usage.output_tokens` is a completion-time attribute, and `chat` spans are expected to be short. But the tool-call fan-out makes it a transaction. Runtimes that pick "event" semantics and still fan out concurrently produce the orphan class. The decision: **if `max_concurrent_tool_calls > 1`, the invocation span MUST be a transaction, bracketed by `invoke_agent`, and tool spans MUST parent to the bracket, not to `chat`.** If `max_concurrent_tool_calls == 1`, event semantics are safe and the bracket is optional. The boundary is sharp at concurrency = 1; it is not a tuning knob. A runtime that sets `max_concurrent_tool_calls = 2` without adding the bracket has silently crossed into the orphan class, and no amount of attribute completeness will detect it. ## Operational Effect The operational consequence is that trace-based verification of tool provenance becomes unsound under fan-out unless the bracket invariant is enforced at span-emission time. An auditor that trusts `parent_span_id` alone will accept causally invalid traces; an auditor that additionally checks `S.end_time <= P.end_time` will *reject* valid traces from event-semantics runtimes that happen to serialize. The correct guardrail is to emit the `invoke_agent` bracket unconditionally when concurrency is enabled, and to have the auditor assert the three-part invariant against the bracket, never against `chat`. The failure is silent, schema-valid, and only surfaces as replay divergence — which is why it is routinely misattributed to model non-determinism. The cost of the bracket is one extra span per turn; the cost of its absence is an unverifiable trace. ```python from dataclasses import dataclass from typing import Optional class CausalityInvariantError(RuntimeError): """Raised when a tool-call span violates the GenAI trace causality invariant.""" @dataclass(frozen=True) class Span: span_id: str parent_span_id: Optional[str] operation_name: str tool_call_id: Optional[str] start_ns: int end_ns: int def assert_tool_span_causality( tool_span: Span, invocation_spans: dict[str, Span], max_concurrent_tool_calls: int, ) -> None: if tool_span.operation_name != "execute_tool": raise CausalityInvariantError( f"expected execute_tool span, got {tool_span.operation_name!r}" ) if tool_span.tool_call_id is None: raise CausalityInvariantError( f"tool span {tool_span.span_id} missing gen_ai.tool.call.id" ) parent = invocation_spans.get(tool_span.parent_span_id or "") if parent is None: raise CausalityInvariantError( f"tool span {tool_span.span_id} parents to unknown span " f"{tool_span.parent_span_id!r}" ) if parent.start_ns > tool_span.start_ns: raise CausalityInvariantError( f"tool span {tool_span.span_id} starts before its parent " f"{parent.span_id} (start_ns {tool_span.start_ns} < {parent.start_ns})" ) if tool_span.end_ns > parent.end_ns: # The orphan condition: child outlives parent. if max_concurrent_tool_calls > 1 and parent.operation_name == "chat": raise CausalityInvariantError( f"tool span {tool_span.span_id} (call_id={tool_span.tool_call_id}) " f"ends at {tool_span.end_ns} after parent chat span {parent.span_id} " f"ended at {parent.end_ns}; concurrency={max_concurrent_tool_calls} " f"requires an invoke_agent bracket, not a chat parent" ) raise CausalityInvariantError( f"tool span {tool_span.span_id} outlives parent {parent.span_id} " f"without an invoke_agent bracket" ) ```