# Which invariants prevent span-attribute lineage collapse when GenAI tool-call spans are exported asynchronously **Date of Record:** 2026 November 25 **Scope:** Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.usage.input_tokens`, `gen_ai.response.finish_reasons`) where the span processor is a batching exporter and tool calls are correlated to model invocations via `gen_ai.tool.call.id` and parent/child span links. Does not apply to synchronous `SimpleSpanProcessor` setups or to pure log-based telemetry that never reconstructs lineage. ## Core Distinction The failure is not "lost spans." A batching exporter reliably delivers *every* span. The failure is **lineage collapse**: the `gen_ai.tool.call.id` that a child tool-execution span carries no longer resolves to the parent `gen_ai.operation.name = "execute_tool"` invocation record at reconstruction time, because the parent span was exported, sampled, or dropped under a *different* sampling decision than its children. In OpenTelemetry GenAI conventions, the tool-call ID is the only semantic join key that survives across the model-invocation boundary — parent/child `SpanContext` linkage is transport-level and can be severed by tail-sampling, while `gen_ai.tool.call.id` is application-level and is *not* guaranteed to be propagated to every span that references the call. The invariant people assume: *"If I emit a span for the tool call and a span for the model invocation that requested it, and both share `gen_ai.tool.call.id`, the lineage reconstructs."* This is false under asynchronous export with per-span sampling, because the two spans are sampled independently and the join key exists only where the sampling decision happened to keep it. ## What Was Observed An agent loop emitted, per turn, one `gen_ai.operation.name = "chat"` span (the model invocation, carrying `gen_ai.usage.input_tokens`, `gen_ai.response.finish_reasons = ["tool_calls"]`) and one or more `gen_ai.operation.name = "execute_tool"` child spans (each carrying `gen_ai.tool.name` and `gen_ai.tool.call.id`). The tracer used a `BatchSpanProcessor` with a 5s schedule delay and a tail-based sampler configured to drop spans whose duration was below a threshold to control cost. Trigger condition: the model-invocation span was long (high `input_tokens`, ~3–8s wall clock) and survived sampling; the tool-execution spans were fast (<50ms) and were dropped by the duration-based tail sampler. The `gen_ai.tool.call.id` therefore existed *only* on the dropped children. At reconstruction, the backend saw `finish_reasons = ["tool_calls"]` on a chat span with **zero resolvable tool-call children**. The turn looked like a model that emitted tool calls which were never executed — a hallucinated tool-call signature — when in fact the tools ran successfully and their spans were simply culled. A second, subtler trace: when the sampler kept the child but dropped the parent (short model invocation, long tool), the `gen_ai.tool.call.id` resolved to an orphan `execute_tool` span with no `chat` parent, and downstream lineage reconstruction attributed the tool call to the *previous* turn's model invocation because the reconstruction heuristic fell back to "nearest preceding chat span by timestamp." This produced a **temporal mis-attribution**: tool-call N appeared to belong to invocation N−1. ## Condition of Validity The lineage invariant `tool.call.id(child) ⇒ exists parent chat span with that id in finish_reasons` holds **only if** the sampling decision is made at the trace (root) level and applied uniformly, *or* if `gen_ai.tool.call.id` is redundantly stamped onto the parent `chat` span (e.g., as `gen_ai.response.tool_call_ids` — a list attribute) so the join survives child loss. Neither is guaranteed by the semantic conventions as written; `gen_ai.tool.call.id` is defined as a tool-span attribute, not a required field on the invoking span. The invariant also requires that reconstruction never uses timestamp-adjacency as a fallback join, because under async export the export order is not the causal order — `BatchSpanProcessor` flushes on a timer, so a child exported in batch *k* may be reconstructed before its parent exported in batch *k+1*. ## Decision Boundary The boundary is: **does the telemetry pipeline need to answer "which model invocation produced this tool call" from spans alone, or can it tolerate the join being probabilistic?** - If lineage must be exact, the guardrail is: stamp the full set of `gen_ai.tool.call.id` values onto the parent `chat` span as a list attribute *before* the tool executes, and make the sampler root-based (sample the whole trace or none). Then child loss is recoverable: the parent still names its calls. - If lineage may be probabilistic, the boundary is the sampling rate: below ~0.1 child-sampling, the orphan rate exceeds the mis-attribution rate and timestamp-fallback reconstruction becomes actively harmful. - The hard boundary: **never** allow a duration-based tail sampler to operate on spans that carry join keys without co-sampling the join partner. The join key is a distributed invariant, and sampling is a per-span operation; these are incompatible unless the sampler is join-aware. ## Operational Effect Without the guardrail, observability reports systematically under-count tool executions and over-count "model emitted tool call but nothing ran" — inflating apparent hallucination rates and depressing apparent tool-success rates. The error is *directional and silent*: it looks like a model-quality regression, not a telemetry defect, so teams chase prompt fixes for a sampling bug. The temporal mis-attribution variant is worse: it corrupts per-turn causal attribution and makes A/B comparisons of tool-use strategies invalid, because the mis-attribution rate is a function of span-duration distribution, which differs between strategies. The required architectural invariant: **join keys must be co-located with the sampling unit.** Either the join key lives on a span that is always sampled with its partner (root-based sampling), or the key is redundantly materialized on the surviving span (list attribute on the parent). Any pipeline that samples spans independently while relying on cross-span join keys will collapse lineage, and the collapse will masquerade as model behavior. ```python from dataclasses import dataclass, field from typing import Optional class LineageCollapseError(RuntimeError): """Raised when a GenAI tool-call span cannot be joined to a model-invocation span.""" pass @dataclass class GenAISpan: operation: str # gen_ai.operation.name tool_call_id: Optional[str] # gen_ai.tool.call.id (tool spans only) tool_call_ids: list = field(default_factory=list) # gen_ai.response.tool_call_ids (chat spans) finish_reasons: list = field(default_factory=list) sampled: bool = True def reconstruct_lineage(spans: list) -> dict: """ Enforce the co-location invariant: a chat span that reports tool_calls in finish_reasons must either (a) carry the tool_call_ids redundantly, or (b) have all child tool spans present in the same sampled set. """ chats = [s for s in spans if s.operation == "chat" and s.sampled] tools = [s for s in spans if s.operation == "execute_tool" and s.sampled] tool_ids_present = {t.tool_call_id for t in tools if t.tool_call_id} lineage = {} for chat in chats: if "tool_calls" not in chat.finish_reasons: continue declared = set(chat.tool_call_ids) if declared: resolvable = declared & tool_ids_present lineage[id(chat)] = {"declared": declared, "resolved": resolvable, "source": "redundant_attribute"} continue # No redundant attribute: must rely on child presence. # If children were sampled out, lineage is unrecoverable. if not declared and not tool_ids_present: raise LineageCollapseError( f"chat span finish_reasons={chat.finish_reasons} declares tool_calls " f"but carries no gen_ai.response.tool_call_ids and no child tool spans " f"survived sampling; lineage is unrecoverable without timestamp fallback " f"(which mis-attributes across turns)." ) lineage[id(chat)] = {"declared": declared, "resolved": tool_ids_present, "source": "child_presence"} return lineage def safe_reconstruct(spans: list) -> dict: try: return reconstruct_lineage(spans) except LineageCollapseError as e: # Do NOT fall back to timestamp adjacency; emit an explicit gap marker. return {"_lineage_gap": str(e), "_fallback": None} ```