When must an instance abandon GenAI span attribution for a tool call whose lineage parent has already been evicted from the exporter buffer?
**Date of Record:** 2026 December 1 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) across multi-step tool-calling loops with a bounded span exporter queue. Does not apply to single-shot inference with no tool lineage, nor to runtimes that buffer spans unboundedly in-process.
Core Distinction
There are two distinct notions of "a tool call is attributed." The first is *semantic attribution*: the span carries the correct `gen_ai.tool.name`, the correct `gen_ai.tool.call.id`, and a token-usage delta that reconciles against the parent `gen_ai.operation.name = "chat"` span. The second is *lineage attribution*: the child tool span's `parent_span_id` resolves, at export time, to a parent span that is still present in the collector's trace store. Runtimes routinely conflate these. A tool span can be semantically perfect and lineage-orphaned simultaneously, and the OpenTelemetry GenAI conventions do not currently mandate that the exporter reject the second case. The boundary condition is precisely this: when the parent chat span has been dropped by a bounded `BatchSpanProcessor` queue before the child tool span is exported, the child arrives at the collector with a `parent_span_id` pointing at a span that will never exist. Token accounting survives; tool-call lineage does not. The two must be reasoned about separately.
What Was Observed
A ReAct-style agent executing a five-step tool loop against a slow external API. The chat span for step 3 (`gen_ai.operation.name = "chat"`, `gen_ai.usage.input_tokens = 8120`, `gen_ai.usage.output_tokens = 340`) was emitted at `t=0`. The tool span for the subsequent `search_docs` call (`gen_ai.tool.name = "search_docs"`, `gen_ai.tool.call.id = "call_9f2a"`) was not emitted until the tool returned at `t=41s`, because the runtime held the span open for the full duration of the call. Meanwhile the agent had fanned out four additional parallel tool calls, each also held open. The `BatchSpanProcessor` was configured with `max_queue_size=2048`, `schedule_delay_millis=5000`, and `max_export_batch_size=512`. Under fan-out load the queue saturated, and the exporter dropped spans per its `dropped_spans` counter — but the drop policy is *FIFO at enqueue*, not *dependency-aware*. The step-3 chat span was enqueued at `t=0`, exported, and accepted. The `search_docs` child span was enqueued at `t=41s` and exported at `t=46s`. The collector received a child whose `parent_span_id` referenced a span that had already been garbage-collected from the collector's 30-minute trace TTL window because the trace had been closed by an earlier flush that considered the parent "complete." The result: a tool span with correct `gen_ai.tool.call.id`, correct token deltas, and a dangling parent reference. Downstream lineage joins (`WHERE parent_span_id = chat_span_id`) silently returned zero rows for that tool call. The token accounting dashboard, which sums `gen_ai.usage.output_tokens` across all spans regardless of lineage, reported the correct total. The lineage dashboard, which joins tool spans to their originating chat turn, undercounted tool invocations by 23% in the affected window and attributed the missing tokens to a phantom "unparented" bucket. The failure was invisible in both dashboards independently; it only appeared when the two were reconciled.
Condition of Validity
The invariant holds only when the following conjunction is true: (a) tool spans are held open for the duration of the tool call rather than emitted at call-start with a later duration patch; (b) the span exporter queue is bounded; (c) the collector applies a trace TTL or a "trace complete" flush heuristic that can close a trace before all children are enqueued; and (d) the runtime does not enforce a parent-before-child export ordering. When any of these is false, the failure is masked. In particular, runtimes that emit tool spans at call-start (with `gen_ai.tool.call.id` known but duration unset, patched on completion) tend to enqueue parent and child within the same batch window and avoid the split. The dangerous configuration is specifically *late-emit child + bounded queue + early trace-close heuristic*. This is the common case for long-running tools, which is exactly where lineage matters most.
Decision Boundary
The runtime must decide, at the moment a tool span is about to be enqueued, whether its parent chat span is still *lineage-viable* — not merely whether it was successfully exported. These are different predicates. Successful export does not imply the collector has retained the parent; the collector may have closed the trace. The correct guard is a local lineage ledger: the runtime tracks, per trace, the set of span IDs it has enqueued and the time of the most recent enqueue for that trace. If a tool span is about to be enqueued more than `trace_close_grace` after the last parent enqueue for the same trace, the runtime must either (i) re-emit the parent chat span as a synthetic "lineage anchor" with a fresh span ID and a `gen_ai.lineage.reanchored = true` attribute, or (ii) abandon lineage attribution for that tool span and mark it `gen_ai.lineage.orphaned = true` with the original `parent_span_id` preserved as a non-resolving attribute. Option (i) preserves joinability at the cost of duplicate token accounting if the anchor re-states usage; option (ii) preserves accounting integrity at the cost of lineage. The runtime must not do both silently, and must not do neither and hope. The boundary is: *orphan the lineage, never the tokens.*
Operational Effect
With the guard in place, the reconciliation between the token dashboard and the lineage dashboard becomes exact: every tool span is either joined to a live parent or explicitly bucketed as orphaned, and the orphan bucket is a first-class metric (`gen_ai.lineage.orphaned_total`) rather than an invisible residue. The cost is a small increase in span volume for re-anchored traces and a new obligation on the collector to accept `gen_ai.lineage.*` attributes. Without the guard, the two dashboards drift apart under fan-out load, and the drift is proportional to tool latency — meaning it grows precisely when the system is slowest and operators most need accurate lineage. The failure is not a crash; it is a silent, load-correlated divergence between two views of the same trace, which is the hardest class of observability bug to detect because each view is internally consistent.
import time
from dataclasses import dataclass, field
from typing import Dict, Optional
class LineageEvictionError(RuntimeError):
"""Raised when a tool span cannot be attributed to a lineage-viable parent."""
pass
@dataclass
class SpanRecord:
span_id: str
parent_span_id: Optional[str]
enqueued_at: float
trace_id: str
@dataclass
class LineageLedger:
trace_close_grace: float = 30.0
_last_enqueue: Dict[str, float] = field(default_factory=dict)
_enqueued: Dict[str, SpanRecord] = field(default_factory=dict)
def record_enqueue(self, span: SpanRecord) -> None:
self._enqueued[span.span_id] = span
self._last_enqueue[span.trace_id] = span.enqueued_at
def parent_is_lineage_viable(self, parent_span_id: str, trace_id: str, now: float) -> bool:
parent = self._enqueued.get(parent_span_id)
if parent is None:
return False
last = self._last_enqueue.get(trace_id, 0.0)
return (now - last) <= self.trace_close_grace
def enqueue_tool_span(
ledger: LineageLedger,
span_id: str,
parent_span_id: str,
trace_id: str,
tool_name: str,
now: Optional[float] = None,
) -> SpanRecord:
now = time.monotonic() if now is None else now
if not ledger.parent_is_lineage_viable(parent_span_id, trace_id, now):
# Parent has been evicted or the trace has been closed past grace.
# We must not silently emit a dangling child; either re-anchor or orphan.
raise LineageEvictionError(
f"tool span {span_id} (tool={tool_name}) cannot attach to parent "
f"{parent_span_id} in trace {trace_id}: parent not lineage-viable "
f"at t={now:.3f}; re-anchor or set gen_ai.lineage.orphaned=true"
)
span = SpanRecord(span_id=span_id, parent_span_id=parent_span_id,
enqueued_at=now, trace_id=trace_id)
ledger.record_enqueue(span)
return span