# Why does an instance emit a coherent span tree yet lose the parent-child linkage between an agent invocation span and its tool-call child spans when the tool executes asynchronously across a context re-entry boundary? **Date of Record:** 2026 November 24 **Scope:** Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.invoke`, `gen_ai.tool.call`, `gen_ai.usage.input_tokens`/`output_tokens`) where tool execution is dispatched through an async executor, thread pool, or message queue and rejoins the agent loop after a context switch. Does not apply to fully synchronous in-process tool calls that execute within the same active span context. ## Core Distinction A span tree that *looks* correct in the collector — every span has a `trace_id`, timestamps nest cleanly, token attributes are populated — is not the same as a span tree whose parent-child edges were established by the actual execution context rather than by wall-clock inference. The failure mode here is not a missing span or a dropped export. It is a **silently re-parented tool-call span**: the `gen_ai.tool.call` span is emitted with the correct `trace_id` but with a `parent_span_id` pointing at the async executor's internal span, or at the root, rather than at the `gen_ai.agent.invoke` span that logically invoked the tool. The trace remains "coherent" because trace IDs match and timestamps are ordered, but the agent-to-tool edge — the one edge that lets you attribute a tool's token cost and latency to a specific reasoning step — is severed. This is non-obvious because the OpenTelemetry context propagation contract (`contextvars` in Python, `AsyncLocalStorage` in Node) is *correct by construction within a single logical execution flow*. The break happens precisely at the boundary where the agent loop suspends, hands the tool call to a worker, and the worker starts with a fresh context that was seeded from the *dispatch site*, not from the *continuation site*. ## What Was Observed An agent invocation span (`gen_ai.operation.name = "invoke_agent"`, span A) at `t0` dispatches three tool calls. Two execute synchronously and correctly parent to A. The third is dispatched to an async executor because the tool is a long-running retrieval. The executor's `submit()` captures the current `contextvars.Context` at dispatch time — which is correct — but the worker thread's instrumentation library initializes a *new* `Tracer` bound to the worker's own resource and, critically, calls `start_as_current_span` without `context=ctx` passed explicitly. The GenAI instrumentation auto-instruments the tool function at decoration time, and the decorator reads `context.current()` at *call* time inside the worker, where `context.current()` returns the worker's ambient context (empty), not the captured dispatch context. Result: the tool-call span (span B) is emitted with `trace_id = A.trace_id` (propagated correctly via the trace-level context that *is* captured) but `parent_span_id = 0x00` (root) because no valid parent was in the ambient context. The collector's trace view shows span B as a second root under the same trace. Token accounting for B (`gen_ai.usage.input_tokens`) is present and correct, but the attribution query "tokens consumed by tool calls under agent invocation A" returns zero for B. The tell is a trace with **more than one root span sharing a `trace_id`**, or a tool-call span whose `parent_span_id` resolves to a span with `gen_ai.operation.name` other than the invoking agent operation. Neither triggers an export error; both are valid OTLP payloads. ## Condition of Validity The invariant that must hold: **for every `gen_ai.tool.call` span, the resolved parent span must be the `gen_ai.agent.invoke` span (or a descendant of it) whose reasoning step emitted the tool dispatch.** This holds only when the tool-call span is created with an explicit parent context that was captured at the dispatch site and *re-injected* at the worker entry point — not merely when the trace-level context is propagated. The failure is bounded to the case where: (a) tool execution crosses an async/thread/queue boundary, (b) the instrumentation creates the tool span inside the worker, and (c) the worker does not explicitly re-attach the captured context before span creation. If any of the three is absent — synchronous execution, span created at dispatch site and ended at completion, or explicit `context=ctx` injection — the edge survives. The condition of validity is therefore *not* "instrumentation is installed" but "the span is created under a context that was explicitly restored from the dispatch site." ## Decision Boundary The boundary is between **trace-ID propagation** and **span-parent propagation**. Trace-ID propagation is resilient: it rides on the `traceparent` header or the captured `Context`'s trace state and survives most boundaries. Span-parent propagation is fragile: it requires the *active span* to be present in the context at span-creation time, and that active span is exactly what is lost when a worker starts with a fresh ambient context. The correct decision: at every async dispatch boundary, capture the full `Context` (not just the trace ID), and at every worker entry point, wrap the tool execution in `context.attach(captured_ctx)` before any instrumentation creates a span. If the instrumentation library does not accept an explicit `context=` parameter, the attach must happen *before* the decorated function is entered, or the parent edge will be lost regardless of trace-ID correctness. ## Operational Effect Downstream, this produces three concrete operational failures. First, **token attribution collapses**: per-agent cost queries that aggregate `gen_ai.usage.*` over the agent span's descendants undercount by the full cost of every async tool call, which for retrieval-heavy agents is often the majority of input tokens. Second, **latency decomposition breaks**: the agent's critical-path latency appears shorter than reality because the long-running tool span is no longer a child, so the "time in tools" metric reads low while total wall-clock disagrees. Third, **verification replay diverges**: an auditor agent reconstructing the reasoning trace from spans sees a tool result with no documented invocation, and either flags a gap or, worse, attributes the tool output to the wrong reasoning step. The invariant to enforce in the collector or a span processor: reject or re-parent any `gen_ai.tool.call` span whose `parent_span_id` does not resolve to a `gen_ai.agent.invoke` ancestor within the same trace, and emit a `gen_ai.orphaned_tool_call` diagnostic attribute so the break is visible rather than silent. ```python from dataclasses import dataclass from typing import Optional class OrphanedToolCallError(RuntimeError): """Raised when a gen_ai.tool.call span cannot be attributed to an invoking agent span.""" pass @dataclass class Span: span_id: str parent_span_id: Optional[str] trace_id: str operation: str # e.g. "invoke_agent", "tool_call" def validate_tool_call_parentage(span: Span, index: dict[str, Span]) -> None: if span.operation != "tool_call": return if span.parent_span_id is None: raise OrphanedToolCallError( f"tool_call span {span.span_id} in trace {span.trace_id} has no parent " f"(context lost at async boundary); cannot attribute token usage" ) parent = index.get(span.parent_span_id) if parent is None: raise OrphanedToolCallError( f"tool_call span {span.span_id} references missing parent " f"{span.parent_span_id}; span tree is incomplete" ) if parent.operation != "invoke_agent": raise OrphanedToolCallError( f"tool_call span {span.span_id} parented to {parent.operation} " f"({parent.span_id}), not an invoke_agent span; " f"re-parenting occurred across context re-entry" ) def audit_trace(spans: list[Span]) -> None: index = {s.span_id: s for s in spans} for s in spans: validate_tool_call_parentage(s, index) ```