# In what scenarios must an instance reject OpenTelemetry GenAI span parenting when a tool-call child span's `gen_ai.tool.call.id` does not resolve to a parent `gen_ai.operation.name=execute_tool` span within the same trace? **Date of Record:** 2026 November 20 **Scope:** Applies to any agent runtime emitting OTel GenAI semantic-convention spans where tool invocations are modeled as child spans of an `invoke_agent`/`chat` span and correlated back to tool results by `gen_ai.tool.call.id`. Does not apply to single-shot LLM calls with no tool loop, nor to runtimes that serialize tool calls inline within the parent span's attributes rather than as discrete spans. ## Core Distinction The GenAI semantic conventions treat `gen_ai.tool.call.id` as a *correlation key* — the same value must appear on the tool-invocation span (emitted when the model requests the call) and on the tool-result span (emitted when execution returns). This is distinct from the W3C `trace_id`/`span_id`/`parent_span_id` linkage, which is *structural*. The failure mode this record addresses is the **decoupling of the correlation key from the structural parent**: a tool-result span can carry a valid `gen_ai.tool.call.id` while its `parent_span_id` points at a span that is not the `execute_tool` span that emitted the matching invocation — or points at a span in a *different trace* entirely, because the agent runtime re-entered its reasoning loop after a checkpoint/resume and re-rooted the span tree under a fresh trace context. The invariant at stake: **within a single trace, `gen_ai.tool.call.id` must resolve to exactly one `execute_tool` span, and every span that consumes that call id (result span, retry span, error-attribution span) must be a structural descendant of it.** When this invariant is violated, span-parenting-based latency attribution and tool-error-rate computation silently double-count or orphan spans, and downstream evaluators that join on call id produce contradictory verdicts. ## What Was Observed A tool-calling agent (ReAct-style loop, 4 tools, checkpointing every step to a durable log) resumed mid-task after a host migration. The checkpoint captured the LLM's `tool_calls` array — including `id: "call_8f3a"` — but the resume path constructed a *new* OTel `Tracer.start_as_current_span("execute_tool", ...)` context rather than re-attaching the persisted `SpanContext`. The persisted `SpanContext` had a `trace_id` from the pre-migration process; the new process's `TracerProvider` had a different `Resource` and the baggage carried a stale `traceparent`. Result: the tool-result span emitted `gen_ai.tool.call.id=call_8f3a` with `parent_span_id` = the *new* `invoke_agent` span (new trace), while the *invocation* record for `call_8f3a` lived in the *old* trace under a now-closed `execute_tool` span. Two traces, one call id. The collector's tail-sampling policy kept the new trace (it had an error) and dropped the old one (it looked complete). The observability backend's "tool error rate" panel joined on call id across traces and reported `call_8f3a` as **both succeeded and failed** — a phantom 200% error rate on a single tool invocation. A second, subtler variant appeared under retries: the agent's retry wrapper emitted a second `execute_tool` span for the *same* `call_8f3a` (correct — the model did retry) but reused the invocation span's attributes, including `gen_ai.tool.call.id`. Now three spans shared one call id across two traces, and the "exactly one `execute_tool` span per call id per trace" assumption broke even within a single trace. ## Condition of Validity The correlation-by-call-id model is only sound under three jointly-held conditions: 1. **Trace continuity across checkpoint/resume.** The persisted `SpanContext` (trace id, span id, trace flags, and `tracestate`) must be re-attached via `trace.set_span_in_context` before the resumed `execute_tool` span is started. If the runtime cannot guarantee this — e.g., the checkpoint store does not persist `tracestate`, or the resumed process uses a different `TracerProvider` — then the runtime must *not* emit a result span under the new trace with the old call id. It must instead emit a fresh call id and a `gen_ai.tool.call.id.reassigned=true` attribute, breaking the join explicitly rather than silently. 2. **Call-id uniqueness scoped to trace, not to process.** `gen_ai.tool.call.id` is generated by the *model* (it is the `id` field of the `tool_calls` entry) and is only unique within a single model response. A retry of the same logical call by the agent's retry wrapper reuses the model's id. Therefore the invariant must be stated as "at most one *successful* `execute_tool` span per call id per trace, plus zero or more retry spans carrying `gen_ai.tool.call.id.attempt=N`." Backends that join on call id alone without filtering on attempt will double-count. 3. **Parent span must be the `execute_tool` span, not the agent span.** The GenAI conventions place `gen_ai.operation.name=execute_tool` on the span that wraps the *tool execution*, and the invocation is recorded as an *event* (`gen_ai.tool.call`) on the parent `chat`/`invoke_agent` span, not as a separate span. A result span parented to the agent span rather than the `execute_tool` span loses the latency decomposition (queue time vs. execution time) and, worse, makes the call-id join ambiguous when the agent span has multiple concurrent tool calls (parallel tool use). The structural parent must be the `execute_tool` span; the invocation event lives on the agent span and is correlated by call id, not by parenting. ## Decision Boundary An instance must **reject** the span (raise, do not emit) when any of the following holds at `execute_tool` span creation: - The call id resolves to an `execute_tool` span whose `trace_id` differs from the current active span's `trace_id`, **and** no `gen_ai.tool.call.id.reassigned` attribute is set. (Cross-trace join — the migration case.) - A second `execute_tool` span is started for the same call id in the same trace **without** an incremented `gen_ai.tool.call.id.attempt`, and the first span has not been marked `gen_ai.tool.call.status=error`. (Silent duplicate — the retry-without-attempt case.) - The current active span's `gen_ai.operation.name` is not `execute_tool` but the span is being asked to carry `gen_ai.tool.call.id` as a result span. (Parenting violation — result span parented to agent span.) An instance must **accept and emit** when the call id is new to the trace, or when a retry explicitly increments `attempt`, or when a cross-trace resume explicitly sets `reassigned=true` and the backend is configured to join on `(trace_id, call_id)` rather than `call_id` alone. The boundary is not "did the tool call succeed" — it is "does the emitted span set preserve the one-to-one-or-explicitly-many mapping between call id and `execute_tool` span within a trace." Success/failure is an attribute; the structural invariant is the parenting and the attempt counter. ## Operational Effect Downstream consumers must be told which regime they are in. A backend that computes tool error rate by joining `gen_ai.tool.call.id` across traces will, under the violation, report rates >100% and attribute failures to the wrong tool. A backend that computes p99 tool latency by `parent_span_id` traversal will, under the parenting violation, attribute agent reasoning time to tool execution and inflate tool latency by an order of magnitude. The guardrail is therefore twofold: (a) the runtime enforces the decision boundary above and refuses to emit ambiguous spans, surfacing the refusal as a `gen_ai.tool.call.id.orphaned` span event on the nearest valid ancestor so the trace is not silently lossy; and (b) the collector's tail-sampling and the backend's join key are both documented as `(trace_id, gen_ai.tool.call.id, gen_ai.tool.call.id.attempt)` — never call id alone. When a resume cannot re-attach the persisted `SpanContext`, the correct behavior is to start a new trace, mint a new call id, and emit a `gen_ai.tool.call.id.reassigned=true` link back to the old trace id as a span link — not to silently reuse the old id under a new parent. ```python from dataclasses import dataclass from typing import Optional class ToolCallSpanParentingError(RuntimeError): """Raised when an execute_tool span would violate the call-id/trace invariant.""" @dataclass class SpanRef: trace_id: str span_id: str operation_name: str call_id: Optional[str] = None attempt: int = 0 class ToolSpanRegistry: """Tracks execute_tool spans per trace to enforce the call-id invariant.""" def __init__(self) -> None: # (trace_id, call_id) -> list of (span_id, attempt, status) self._spans: dict[tuple[str, str], list[tuple[str, int, str]]] = {} def register( self, parent: SpanRef, call_id: str, attempt: int = 0, reassigned: bool = False, ) -> None: if parent.operation_name != "execute_tool": raise ToolCallSpanParentingError( f"result span for call_id={call_id!r} parented to " f"{parent.operation_name!r} (trace {parent.trace_id}); " f"must parent to an execute_tool span" ) key = (parent.trace_id, call_id) existing = self._spans.get(key, []) # Cross-trace reuse: call id already seen in a *different* trace. for (other_trace, other_call), spans in self._spans.items(): if other_call == call_id and other_trace != parent.trace_id and spans: if not reassigned: raise ToolCallSpanParentingError( f"call_id={call_id!r} already bound to trace " f"{other_trace}; cross-trace reuse requires " f"reassigned=True" ) # Same-trace duplicate without attempt increment. if existing: last_attempt, last_status = existing[-1][1], existing[-1][2] if attempt <= last_attempt and last_status != "error": raise ToolCallSpanParentingError( f"duplicate execute_tool span for call_id={call_id!r} " f"in trace {parent.trace_id} without attempt increment " f"(last attempt={last_attempt}, status={last_status})" ) self._spans.setdefault(key, []).append( (parent.span_id, attempt, "pending") ) def close(self, trace_id: str, call_id: str, span_id: str, status: str) -> None: key = (trace_id, call_id) spans = self._spans.get(key, []) for i, (sid, attempt, _) in enumerate(spans): if sid == span_id: spans[i] = (sid, attempt, status) return raise ToolCallSpanParentingError( f"close() for unknown span {span_id} on call_id={call_id!r}" ) ```