# What structural signals indicate lineage collapse in OpenTelemetry GenAI span attributes when an agent re-emits a tool call under a new trace **Date of Record:** 2026 November 23 **Scope:** Applies to agent runtimes that emit OpenTelemetry `gen_ai.*` spans per the GenAI semantic conventions and reconstruct invocation lineage from `trace_id`/`span_id`/`parent_span_id` plus `gen_ai.operation.name`, `gen_ai.tool.name`, and `gen_ai.usage.*` attributes. Does not cover vendor-proprietary tracing that bypasses OTel, nor pure metric-only pipelines. ## Core Distinction The GenAI semantic conventions describe *what* a span represents (`gen_ai.operation.name = chat | execute_tool | embeddings`, plus `gen_ai.tool.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`/`output_tokens`). They do **not** define a lineage contract. Lineage is an emergent property of the parent/child span tree plus the trace context propagation decision the agent runtime makes at each tool boundary. The non-obvious failure is this: when an agent re-plans after a tool failure and re-issues *the same logical tool call*, the runtime frequently starts a **new trace** (fresh root span) rather than emitting a sibling span under the existing trace. Every individual span remains schema-valid — correct `gen_ai.operation.name`, correct `gen_ai.tool.name`, correct token counts. Nothing is "broken" per the conventions. But the *lineage* is now split across two traces with no shared key, so downstream replay, cost attribution, and loop-detection all silently lose the causal edge between the failed attempt and the retry. This is a lineage-continuity invariant violation, not a schema violation. The signal is structural, not semantic: it lives in the relationship between spans, not in any single span's attributes. ## What Was Observed A planner-executor agent executing a two-step tool task produced the following span topology across a retry: - Trace A: root `invoke_agent` → child `chat` → child `execute_tool` (`gen_ai.tool.name = "search"`), status `ERROR`, no `gen_ai.tool.call.id`. - Trace B (new `trace_id`): root `invoke_agent` → child `chat` → child `execute_tool` (`gen_ai.tool.name = "search"`), status `OK`, `gen_ai.tool.call.id = "call_7f3a"`. Both traces are individually conformant. The retry's `chat` span has a *different* `gen_ai.request.model` sampling seed and a higher `gen_ai.usage.input_tokens` (the re-plan re-sent the full history). The only structural signal linking them is a wall-clock adjacency and identical `gen_ai.tool.name` — which is not a lineage key and is not unique. The concrete trigger: the runtime's tool executor caught the tool exception, propagated it to the planner, and the planner's re-invocation path constructed a *new* `TracerProvider` scope rooted at the retry entry point rather than continuing the ambient context. In OTel terms, `context.attach()` was not carried across the exception boundary; the `SpanContext` from the failed `execute_tool` was never set as the parent of the retry's `chat` span. The retry therefore became a new root. The structural signals that reveal this after the fact: 1. **Orphan root multiplicity per logical task.** More than one `invoke_agent` root span sharing an identical `gen_ai.agent.name` and overlapping `gen_ai.request.model` within a short window, with no `links` array connecting them. 2. **Repeated `gen_ai.tool.name` with divergent `gen_ai.tool.call.id` nullability.** The failed attempt lacks a call id; the retry has one. A conformant retry that preserved lineage would carry a `link` to the failed span (OTel `SpanLink` is the sanctioned mechanism for exactly this). 3. **Token-count discontinuity at the trace boundary.** The retry's `gen_ai.usage.input_tokens` exceeds the original by approximately the full prior history length, confirming the retry re-serialized state rather than resuming it — evidence the runtime did not have a live parent context to inherit. 4. **Missing `gen_ai.tool.call.id` on the failed span.** The convention expects a call id on `execute_tool` spans; its absence on the failure path is the earliest structural tell that the failure handler bypassed the normal span-finalization path. ## Condition of Validity The invariant — *a re-issued tool call must share a lineage edge with its failed predecessor* — holds only when the runtime treats retries as continuations. It is **not** valid when: - The retry is a deliberate *fresh attempt* under a new task identity (e.g., user-initiated re-run), in which case a new trace is correct and the absence of a link is expected. - The runtime uses OTel `SpanLink` explicitly to connect the new trace to the failed span; in that case trace-splitting is fine because the causal edge is preserved in the `links` array, not the parent tree. - The tool is idempotent and stateless and the retry carries no inherited context (token counts should then be *equal*, not larger — which is itself a discriminator). The invariant is invalid as a guardrail if any of these hold, so a detector must first check for `SpanLink` presence and token-count parity before flagging lineage collapse. ## Decision Boundary The boundary between "acceptable trace split" and "lineage collapse" is determined by three conjunctive checks, evaluated in order: 1. **Link check:** Does the retry root span carry a `SpanLink` whose `span_id` matches the failed `execute_tool` span? If yes → acceptable, stop. 2. **Token-parity check:** Is `retry.gen_ai.usage.input_tokens` within a small delta of `failed.gen_ai.usage.input_tokens`? If yes → the retry did not inherit re-serialized history; likely an independent attempt → acceptable, stop. 3. **Tool-identity check:** Does `retry.gen_ai.tool.name == failed.gen_ai.tool.name` AND `failed.gen_ai.tool.call.id` is null AND the retry began within a bounded window of the failure? If all three → **lineage collapse**, raise. The guardrail must run at span-export time (or in a post-hoc analyzer), because the collapse is only visible once both traces exist. A single-span validator cannot detect it — this is the crux: GenAI conventions validate spans, but this failure is a property of the *edge between spans across traces*. ## Operational Effect When lineage collapse goes undetected: - **Loop detection fails.** An agent stuck retrying a failing tool produces N independent traces, each looking like a fresh healthy invocation. Retry-storm alarms keyed on trace-internal error rates never fire because each trace has exactly one error-free retry. - **Cost attribution double-counts.** The re-serialized history on each retry inflates `gen_ai.usage.input_tokens`; without the lineage edge, billing reports attribute each retry to a distinct task, masking a single runaway task as many cheap ones. - **Replay diverges.** Replaying Trace B alone cannot reconstruct why the tool was re-called, because the causal predecessor lives in Trace A. Verification replay produces a different decision path than the original run. The architectural fix is to make the tool-executor's exception boundary context-preserving: capture the failed `SpanContext`, and on re-invocation either (a) parent the retry span under the failed span, or (b) emit a `SpanLink` from the retry root to the failed span. Option (b) is preferred because it preserves the "new trace" semantics for independent attempts while keeping the causal edge auditable. ```python from dataclasses import dataclass from typing import Optional class LineageCollapseError(RuntimeError): """Raised when a re-issued tool call loses its causal edge to the failed attempt.""" pass @dataclass class GenAISpanView: trace_id: str span_id: str operation_name: str # gen_ai.operation.name tool_name: Optional[str] # gen_ai.tool.name tool_call_id: Optional[str] # gen_ai.tool.call.id input_tokens: Optional[int] # gen_ai.usage.input_tokens status: str # "OK" | "ERROR" linked_span_ids: tuple = () # SpanLink targets start_ns: int = 0 def assert_lineage_preserved( failed: GenAISpanView, retry_root: GenAISpanView, *, token_parity_tolerance: int = 16, max_gap_ns: int = 60_000_000_000, ) -> None: if failed.operation_name != "execute_tool" or retry_root.operation_name != "invoke_agent": raise LineageCollapseError( f"unexpected span roles: failed={failed.operation_name}, retry={retry_root.operation_name}" ) # 1. Explicit SpanLink preserves the edge across a trace split. if failed.span_id in retry_root.linked_span_ids: return # 2. Token parity implies an independent attempt, not a resumed continuation. if ( failed.input_tokens is not None and retry_root.input_tokens is not None and abs(retry_root.input_tokens - failed.input_tokens) <= token_parity_tolerance ): return # 3. Conjunctive collapse signature. same_tool = retry_root.tool_name == failed.tool_name missing_call_id = failed.tool_call_id is None within_window = 0 <= (retry_root.start_ns - failed.start_ns) <= max_gap_ns if same_tool and missing_call_id and within_window: raise LineageCollapseError( "re-issued tool call lost lineage: " f"tool={failed.tool_name!r}, failed_span={failed.span_id}, " f"retry_trace={retry_root.trace_id}, no SpanLink, " f"token delta={retry_root.input_tokens} - {failed.input_tokens}" ) # Fallthrough: not a collapse under our predicate; log-worthy but not fatal. return def emit_retry_span(runtime, failed: GenAISpanView, retry_root: GenAISpanView) -> None: """Wraps span emission so a collapsed lineage fails closed at export time.""" try: assert_lineage_preserved(failed, retry_root) except LineageCollapseError: # Fail closed: refuse to export a retry span that breaks the invariant # until the runtime attaches a SpanLink or proves token parity. runtime.mark_lineage_degraded(retry_root.trace_id) raise runtime.export(retry_root) ```