# What structural signals indicate OTel GenAI span attribute drift at tool-call fan-out boundaries **Date of Record:** 2026 December 2 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) for tool/function invocations, specifically where a single logical tool call is fanned out across retries, streaming partials, or parallel sub-invocations. Does not apply to single-shot, non-retried, non-streamed tool calls where one span maps 1:1 to one invocation. ## Core Distinction The GenAI semantic conventions define `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, and `gen_ai.usage.*` as *per-invocation* attributes. The tracing backend treats a span as the unit of truth. The agent runtime, however, treats a *logical tool call* as the unit of truth. These two units diverge the moment a tool call is retried, streamed, or parallelized — and the divergence is silent: spans remain syntactically valid, attributes remain type-correct, and the trace tree still renders. The failure is not a crash. It is a **cardinality and identity mismatch** where the number of spans carrying `gen_ai.tool.call.id == X` no longer equals one, and downstream consumers (cost attribution, replay, audit) aggregate over the wrong unit. The non-obvious part: the OpenTelemetry spec permits multiple spans to share a `call.id` (retries legitimately re-emit it), but it does *not* define a discriminator attribute to distinguish "retry of the same call" from "parallel fan-out of the same call" from "duplicate emission from a buggy SDK." The agent runtime must synthesize that discriminator or the boundary is unobservable. ## What Was Observed A tool-calling agent invoked a search tool with a 3-way parallel fan-out (three query variants) under a single logical `tool.call.id`, because the planner treated the fan-out as one semantic step. The runtime emitted three child spans, each with: - `gen_ai.operation.name = "execute_tool"` - `gen_ai.tool.name = "web_search"` - `gen_ai.tool.call.id = "call_8f3a..."` - distinct `span_id`, identical `parent_span_id` Two of the three returned HTTP 429 and were retried by the SDK's transport layer, which emitted *additional* spans reusing the same `call.id` but with a new `span_id` and `retry.count` unset (the transport did not know about the GenAI conventions). The final trace contained **5 spans** with `call.id = call_8f3a...`: 3 fan-out attempts + 2 transport retries. Three structural signals surfaced the drift: 1. **Fan-out cardinality exceeded the declared degree.** The planner recorded `plan.fanout_degree = 3` on the parent span; the child count with matching `call.id` was 5. A cardinality invariant `count(children where call.id == X) == plan.fanout_degree` failed. 2. **Retry spans lacked `gen_ai.usage.input_tokens`.** The transport retries carried `http.status_code = 429` but no token accounting, so a cost aggregator summing `gen_ai.usage.*` over `call.id` undercounted by the retried tokens — a silent billing discontinuity, not an error. 3. **`span.kind` was inconsistent.** Fan-out spans were `INTERNAL` (emitted by the planner); transport retries were `CLIENT` (emitted by the HTTP layer). A consumer keying on `span.kind == INTERNAL` to count "real" tool calls got 3; a consumer keying on `call.id` alone got 5. Two dashboards, two truths, same trace. The critical observation: **no span was malformed**. Every attribute passed schema validation. The drift is only visible when you cross-check the *relationship* between spans, which OTel does not enforce. ## Condition of Validity The invariant "one `gen_ai.tool.call.id` ⇒ one logical tool invocation" holds **only if** the runtime guarantees: - At most one span per `call.id` carries `gen_ai.operation.name`, OR - Every additional span with the same `call.id` carries an explicit discriminator: `gen_ai.tool.call.attempt` (monotonic int) for retries, and `gen_ai.tool.call.fanout_index` (int, with `plan.fanout_degree` on the parent) for parallel fan-out. Without the discriminator, the boundary is undecidable from the trace alone. The convention's silence on fan-out is the gap; the runtime must close it locally. This condition is **valid** for any agent that retries, streams, or parallelizes tool calls — i.e., nearly all production agents — and **invalid** only for strictly serial, non-retried, non-streamed single-shot invocations. ## Decision Boundary The runtime must decide, at span-emission time, whether a new span represents a *new logical call* (new `call.id`) or a *continuation of an existing call* (same `call.id` + discriminator). The boundary rule: - **New `call.id`** iff the planner initiated a semantically distinct step (different arguments, different intent). - **Same `call.id` + `attempt`** iff the transport/retry layer re-invokes the identical request. - **Same `call.id` + `fanout_index`** iff the planner split one step into N parallel sub-invocations. The failure mode occurs when the retry layer and the planner layer each independently mint spans without consulting a shared `call.id` registry, so neither knows the other's cardinality. The guardrail: a single `CallIdRegistry` owned by the runtime (not the SDK, not the transport) that assigns `call.id`, `attempt`, and `fanout_index` before any span is emitted, and rejects emission of a span whose `(call.id, attempt, fanout_index)` tuple already exists. This makes the boundary *decidable* — duplicate tuples are structurally impossible, and cardinality checks become exact. ## Operational Effect Without the registry, three downstream operations degrade silently: - **Cost attribution** undercounts retried calls (retries lack usage attributes) and double-counts fan-out (each fan-out span may carry full usage). The error is not random; it is biased by retry rate and fan-out degree, so it compounds across a session. - **Replay/audit** cannot reconstruct the logical call sequence: a replay engine that iterates spans by `call.id` sees 5 events where the agent executed 1 logical step, and mis-orders them because `span_id` ordering does not reflect attempt ordering. - **Verification** (a peer auditor agent checking tool-call correctness) sees conflicting cardinalities depending on which attribute it keys on, and may raise a false "duplicate execution" alarm or miss a real one. The invariant to enforce: **`(gen_ai.tool.call.id, attempt, fanout_index)` is a unique key across the trace, and `count(spans with call.id == X) == 1 + max(attempt) + max(fanout_index)`.** Violation is a structural signal of attribute drift, not a transport error — it will not appear in error rates, only in cardinality audits. ```python from dataclasses import dataclass, field from typing import Dict, Tuple class CallIdDriftError(RuntimeError): """Raised when a span emission would violate the call.id uniqueness invariant.""" pass @dataclass class CallIdRegistry: # (call_id, attempt, fanout_index) -> span_id _issued: Dict[Tuple[str, int, int], str] = field(default_factory=dict) _fanout_degree: Dict[str, int] = field(default_factory=dict) def declare_fanout(self, call_id: str, degree: int) -> None: if call_id in self._fanout_degree: raise CallIdDriftError( f"call.id {call_id} fan-out already declared as " f"{self._fanout_degree[call_id]}, cannot redeclare as {degree}" ) self._fanout_degree[call_id] = degree def issue(self, call_id: str, attempt: int, fanout_index: int, span_id: str) -> None: key = (call_id, attempt, fanout_index) if key in self._issued: raise CallIdDriftError( f"duplicate span emission for tuple {key}: " f"existing span_id={self._issued[key]}, new span_id={span_id}" ) # Bound fan-out index against declared degree. if call_id in self._fanout_degree: if fanout_index >= self._fanout_degree[call_id]: raise CallIdDriftError( f"fanout_index {fanout_index} exceeds declared degree " f"{self._fanout_degree[call_id]} for call.id {call_id}" ) self._issued[key] = span_id def verify_cardinality(self, call_id: str, observed_span_count: int) -> None: expected = 1 + max( (a for (c, a, f) in self._issued if c == call_id), default=0 ) + max( (f for (c, a, f) in self._issued if c == call_id), default=0 ) if observed_span_count != expected: raise CallIdDriftError( f"cardinality drift for call.id {call_id}: " f"observed {observed_span_count} spans, invariant requires {expected}" ) def emit_tool_span(registry: CallIdRegistry, call_id: str, attempt: int, fanout_index: int, span_id: str) -> None: registry.issue(call_id, attempt, fanout_index, span_id) # ... emit OTel span with gen_ai.tool.call.id, attempt, fanout_index ... ```