# Where must an instance bound span cardinality before OpenTelemetry GenAI attributes become lossy under retry fan-out **Date of Record:** 2026 November 26 **Scope:** Applies to agents that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) and that perform retries, tool-call fan-out, or multi-turn loops within a single logical task. Does not apply to single-shot inference calls with no retry or tooling, nor to trace backends that ingest spans without attribute-length or cardinality limits. ## Core Distinction The GenAI semantic conventions treat a span as a *record of one model or tool invocation*. They define attributes like `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.response.finish_reasons`, and `gen_ai.operation.name`, and they assume a one-to-one mapping between an invocation and a span. The convention is silent on what an *agent* is: a single logical task can produce N model calls, M tool calls, and R retries, each of which is a legitimate span. The non-obvious boundary is this: the conventions are correct per-span, but an agent instrumented naively against them produces a trace whose *aggregate* is not reconstructible. The failure is not a malformed span. Every individual span validates against the schema. The failure is that the parent-child topology and the attribute values together cannot distinguish *retry of the same logical step* from *a genuine second step*, and cannot distinguish *fan-out across parallel tool calls* from *sequential tool calls*. Once that distinction is lost, token accounting double-counts, finish-reason aggregation is wrong, and any downstream auditor replaying the trace reconstructs a different execution graph than the one that ran. The distinction that matters: **span-level conformance is not trace-level fidelity.** The conventions specify the former. The operational invariant required is the latter, and it must be supplied by the instrumenter, not the spec. ## What Was Observed A retrieval agent executing a single user task emitted the following span tree (simplified, attributes abridged): ``` agent.invoke (task_id=T7) ├── gen_ai.chat model=gpt-4o finish=stop attempt=1 ├── gen_ai.chat model=gpt-4o finish=length attempt=1 ├── gen_ai.chat model=gpt-4o finish=stop attempt=1 ├── gen_ai.execute_tool tool=search status=error ├── gen_ai.execute_tool tool=search status=ok └── gen_ai.chat model=gpt-4o finish=stop attempt=1 ``` Three distinct defects surfaced when this trace was replayed by an auditor agent: 1. **Retry ambiguity.** The two `search` spans are a retry of one logical tool call (the first hit a transient 503). But `gen_ai.execute_tool` carries no retry linkage. The auditor counted two tool invocations. The agent's own state machine counted one, with a retry counter. The trace and the agent disagreed on how many times the tool ran. 2. **Finish-reason collision.** The `finish=length` span was a truncated generation that the agent discarded and re-issued. Because both spans share the same parent and the same `gen_ai.operation.name`, a naive aggregator that sums `gen_ai.usage.input_tokens` across children double-counted the prompt. The agent's internal token budget counted the discarded attempt as spent; the trace, summed naively, counted it twice. 3. **Missing attempt attribute.** No span carried `attempt`, `retry.of`, or any equivalent. The convention has no such field. The instrumenter had assumed the backend would infer retries from identical `gen_ai.request.model` and `gen_ai.operation.name` — an inference that is unsound whenever the same model is called twice for legitimate reasons (e.g., a two-step plan). The auditor's replay produced a graph with 4 model calls and 2 tool calls. The agent executed 3 model calls (one discarded) and 1 tool call (retried once). The divergence was not a bug in either system; it was the predictable consequence of instrumenting to the per-span convention without an agent-level invariant. ## Condition of Validity The GenAI conventions are *sufficient* for trace-level fidelity only under a condition the spec does not state: **each logical agent step maps to exactly one span, and each span is emitted only on terminal outcome.** This holds when: - The agent performs no retries, or retries are represented as a distinct span kind (e.g., a `gen_ai.retry` wrapper) rather than a sibling. - Tool fan-out is sequential and each call is semantically distinct (different arguments, different intent), so identical-attribute siblings never occur. - No generation is discarded and re-issued within the same parent scope. - Token accounting is read per-span, never aggregated by summation across siblings without a deduplication key. When any of these fails — and in production agents, at least one fails on most non-trivial tasks — the convention remains valid per-span but the trace becomes *lossy at the topology layer*. The instrumenter must add the missing invariant. The required invariant: **every span that represents a retry or a discarded attempt must carry an explicit linkage attribute identifying the logical step it belongs to, and the parent span must record the logical step count independently of the child span count.** Concretely, an `agent.step.id` (stable across retries) plus a `agent.step.attempt` counter, emitted as a span attribute even though it is not in the GenAI namespace. The GenAI attributes remain conformant; the agent-layer attributes carry the topology the convention omits. ## Decision Boundary The instrumenter must decide, per agent, whether to (a) emit every attempt as a sibling span and rely on a linkage attribute for replay, or (b) collapse retries into a single span with an `attempt.count` attribute and per-attempt events. The boundary between these is determined by a single measurable: **the ratio of retried-or-discarded invocations to total invocations within a task.** - Below roughly 5% retry rate, sibling spans with a linkage attribute are cheaper and preserve per-attempt timing, which matters for latency attribution. - Above that, sibling spans inflate trace size, and naive backend aggregators (which the auditor may not control) will double-count unless they honor the linkage attribute — which they generally do not, because it is not in the convention. At high retry rates, collapsing to one span with `attempt.count` and per-attempt events is the only representation that survives a convention-naive aggregator. The decision is not about span count. It is about **whether the consumer of the trace honors attributes outside the GenAI namespace.** If the auditor is convention-naive, the instrumenter must encode retry topology *within* convention-compliant fields — e.g., by making the collapsed span's `gen_ai.usage.input_tokens` reflect total spend across attempts and recording per-attempt detail in span events, which convention-naive consumers pass through untouched. ## Operational Effect Instrumenting without this boundary produces three concrete operational failures: 1. **Token budget drift.** An agent that reads its own token spend from trace aggregation will over- or under-count relative to its internal budget, because discarded attempts are counted by the trace but not by the agent (or vice versa). Over a long task, this drift causes premature budget exhaustion or silent overrun. 2. **Auditor replay divergence.** A verification agent that reconstructs the execution graph from spans will produce a graph with different node and edge counts than the executing agent's state machine. Consensus checks between executor and auditor fail not because either is wrong, but because they are reading different topologies. 3. **Retry-storm invisibility.** Without an explicit attempt counter, a retry storm (a tool failing repeatedly) is indistinguishable from legitimate repeated calls. The trace shows N spans; the operator cannot tell whether N is fan-out or failure. Alerting on span count then fires on healthy fan-out and stays silent on pathological retry loops. The guardrail: the instrumenter must assert, at span-emission time, that every span belonging to a retried or discarded logical step carries a linkage attribute, and must reject emission of a retry sibling that lacks it. This is a pre-emission invariant, enforced in the instrumentation layer, not a post-hoc query. ```python from dataclasses import dataclass, field from typing import Optional class SpanTopologyViolation(RuntimeError): """Raised when a retry/discard span is emitted without logical-step linkage, making the trace lossy under convention-naive aggregation.""" pass @dataclass class LogicalStep: step_id: str attempt: int = 1 discarded: bool = False @dataclass class SpanEmitter: # Convention-compliant GenAI attributes are emitted per span. # Agent-layer linkage attributes are REQUIRED for retries/discards. emitted_step_ids: dict = field(default_factory=dict) def emit_invocation( self, step: LogicalStep, gen_ai_operation: str, gen_ai_model: str, input_tokens: int, finish_reason: str, linkage_attr: Optional[str] = None, ) -> dict: is_retry_or_discard = step.attempt > 1 or step.discarded if is_retry_or_discard and linkage_attr is None: raise SpanTopologyViolation( f"step {step.step_id!r} attempt={step.attempt} discarded={step.discarded} " f"emitted without linkage attribute; trace will be lossy under " f"convention-naive aggregation (retry indistinguishable from fan-out)" ) span = { "gen_ai.operation.name": gen_ai_operation, "gen_ai.request.model": gen_ai_model, "gen_ai.usage.input_tokens": input_tokens, "gen_ai.response.finish_reasons": [finish_reason], "agent.step.id": step.step_id, "agent.step.attempt": step.attempt, "agent.step.discarded": step.discarded, } if linkage_attr is not None: span["agent.retry.of"] = linkage_attr self.emitted_step_ids.setdefault(step.step_id, 0) self.emitted_step_ids[step.step_id] += 1 return span def assert_trace_fidelity(self, expected_logical_steps: int) -> None: # Invariant: logical step count must equal distinct agent.step.id values, # NOT the number of emitted spans. distinct = len(self.emitted_step_ids) if distinct != expected_logical_steps: raise SpanTopologyViolation( f"trace has {distinct} distinct logical steps but agent executed " f"{expected_logical_steps}; replay will diverge from executor graph" ) # Trigger: a discarded truncated generation re-issued without linkage. emitter = SpanEmitter() emitter.emit_invocation( step=LogicalStep(step_id="s1", attempt=1), gen_ai_operation="chat", gen_ai_model="gpt-4o", input_tokens=1200, finish_reason="length", ) # Second attempt of the SAME logical step, discarded, no linkage -> violation. emitter.emit_invocation( step=LogicalStep(step_id="s1", attempt=2, discarded=True), gen_ai_operation="chat", gen_ai_model="gpt-4o", input_tokens=1200, finish_reason="stop", linkage_attr=None, # <-- omission that makes the trace lossy ) ```