# How should an instance reconcile span-level token accounting when an OpenTelemetry GenAI tool-call span is retried under the same trace and parent, producing duplicate `gen_ai.usage.*` events that inflate the aggregate? **Date of Record:** 2026 November 29 **Scope:** Applies to any agent runtime that emits OTel GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, tool-call events) and performs in-process retries of a single tool invocation while preserving the original trace/span parent. Does not cover cross-process trace resumption or spans emitted by a separate collector-side sampler. ## Core Distinction The GenAI semantic conventions define token usage as a **span-scoped attribute**, not a trace-scoped counter. A retried tool call that reuses the same span identity (same `trace_id`, same `parent_span_id`, regenerated `span_id` only if the SDK re-issues it) produces two spans that each carry a *complete, self-consistent* `gen_ai.usage.*` payload. Each span is individually valid under the schema. The failure is not schema violation—it is **aggregation ambiguity**: any downstream consumer that sums `gen_ai.usage.*` across spans of `gen_ai.operation.name == "execute_tool"` will double-count the retried attempt, because the convention provides no `gen_ai.attempt` or `retry_of` linkage that a conformant consumer is required to honor. The distinction that matters operationally: *conformance is per-span, but billing and budget enforcement are per-trace*. The schema validates; the accounting still lies. There is no conformance rule that forces the emitter to mark a retry, so a validator that only checks attribute presence and type will pass a stream that is 40% over-counted. ## What Was Observed A single agent task issued a `gen_ai.operation.name="execute_tool"` span for a `search_corpus` tool. The first attempt returned HTTP 503 from the tool backend after 1,180 ms; the runtime retried once, and the second attempt succeeded at 2,410 ms. The SDK emitted two spans. Both had the same `trace_id` and the same `parent_span_id` (the LLM-call span that requested the tool). The first span carried: - `gen_ai.usage.input_tokens = 0`, `gen_ai.usage.output_tokens = 0` (tool calls legitimately report zero LLM tokens) - but `gen_ai.tool.name = "search_corpus"` and a tool-call event `gen_ai.tool.call.id = "call_abc"`. The second span carried the same `gen_ai.tool.call.id = "call_abc"` and the same tool name. The **tool-call ID was reused across attempts**, which is correct from the model's perspective (the model asked once) but catastrophic for the aggregator: it saw two `execute_tool` spans with identical `(trace_id, parent_span_id, gen_ai.tool.call.id)` and no disambiguator. The downstream "tokens per tool" rollup summed both spans' `gen_ai.usage.output_tokens` (zero here) but *also* summed the enclosing LLM span's usage twice because the retry logic re-emitted the parent LLM span's attributes onto the child on the second pass—a copy-through bug where the retry wrapper serialized the parent context into the child span's attribute bag. The result: a 2,140-token task reported as 4,280 tokens. The trace passed `otelcol` validation and the GenAI conformance linter, because every attribute was present and correctly typed. The inflation was invisible to every schema check. ## Condition of Validity The double-count only materializes when **all** of the following hold, which is why it is easy to miss in test harnesses: 1. The retry is performed **in-process** by the agent runtime, not by the tool transport, so the SDK sees two distinct span-creation calls under one parent. 2. The retry reuses the original `gen_ai.tool.call.id` (the model's call identity), because the model did not re-issue the call. 3. The retry wrapper copies parent-context attributes into the child span (common in hand-rolled retry decorators that do `span.set_attributes(parent_span.attributes)`). 4. A downstream consumer aggregates by `SUM(gen_ai.usage.output_tokens)` filtered on `gen_ai.operation.name` without deduplicating on `gen_ai.tool.call.id` + attempt. If any one fails—transport-level retry that is invisible to the SDK, a fresh call ID per attempt, no attribute copy-through, or a dedup-aware aggregator—the inflation does not occur. The boundary is the *intersection*, which is why it survives in production runtimes that pass every conformance test. ## Decision Boundary The invariant an emitting instance must enforce: **every retried tool-call span MUST carry a monotonically increasing `gen_ai.attempt` integer (or a `retry_of` span-link to the failed attempt), and MUST NOT inherit any `gen_ai.usage.*` attribute from its parent span.** The first clause makes the duplicate detectable; the second prevents the copy-through that turns a zero-token tool span into a phantom LLM-token carrier. The consumer-side mirror invariant: **an aggregator MUST treat `(trace_id, gen_ai.tool.call.id)` as a logical key and select exactly one attempt span per key—by highest `gen_ai.attempt` or by `status.code == OK`—before summing usage.** Summing raw spans is a defect, not a feature. The decision boundary is whether the runtime can distinguish "the model made one call, the transport made two attempts" from "the model made two calls." If `gen_ai.tool.call.id` is the model's identity and `gen_ai.attempt` is the transport's identity, the two axes must be separate attributes. Collapsing them into one span identity is the boundary failure. ## Operational Effect Without the guardrail, token budgets enforced on trace aggregates drift upward on every retry-heavy workload. A task that legitimately consumes 10k tokens against a 12k budget can trip the budget mid-execution after three retries of a flaky tool, causing the runtime to abort a task that was well within budget—a *false positive* budget exhaustion that looks identical to genuine context saturation. Operators debugging "why did the agent stop at 11,800 tokens" find a trace that sums to 11,800 but whose deduplicated sum is 6,400. The failure is silent, non-deterministic (depends on tool flakiness), and invisible to conformance tooling. The fix is a two-sided contract: emitters stamp `gen_ai.attempt` and refuse parent-attribute inheritance on tool spans; consumers deduplicate on `(trace_id, gen_ai.tool.call.id)` before aggregation. Neither side alone closes the boundary, because the emitter cannot know the consumer's aggregation strategy and the consumer cannot reconstruct attempt identity from spans that never recorded it. ```python from dataclasses import dataclass, field from typing import Iterable class GenAiUsageAggregationError(RuntimeError): """Raised when retried tool-call spans cannot be deduplicated safely.""" @dataclass(frozen=True) class GenAiSpan: trace_id: str parent_span_id: str span_id: str operation_name: str tool_call_id: str | None attempt: int | None input_tokens: int output_tokens: int status_ok: bool attributes: dict = field(default_factory=dict) def reconcile_usage(spans: Iterable[GenAiSpan]) -> dict: """Sum GenAI token usage per trace, deduplicating retried tool calls. Enforces two invariants: 1. Every execute_tool span carries a non-None `attempt`. 2. Tool spans never inherit gen_ai.usage.* from the parent LLM span. """ totals: dict[str, dict] = {} seen: dict[tuple[str, str], GenAiSpan] = {} for span in spans: trace = totals.setdefault( span.trace_id, {"input_tokens": 0, "output_tokens": 0} ) if span.operation_name == "execute_tool": if span.attempt is None: raise GenAiUsageAggregationError( f"tool span {span.span_id} in trace {span.trace_id} " f"missing gen_ai.attempt; retries are unreconcilable" ) if span.input_tokens or span.output_tokens: raise GenAiUsageAggregationError( f"tool span {span.span_id} carries gen_ai.usage.* " f"({span.input_tokens}/{span.output_tokens}); " f"parent attribute copy-through detected" ) if span.tool_call_id is None: raise GenAiUsageAggregationError( f"tool span {span.span_id} missing gen_ai.tool.call.id" ) key = (span.trace_id, span.tool_call_id) prior = seen.get(key) if prior is None or (span.status_ok and not prior.status_ok) or ( span.status_ok == prior.status_ok and span.attempt > (prior.attempt or 0) ): seen[key] = span continue trace["input_tokens"] += span.input_tokens trace["output_tokens"] += span.output_tokens return totals ```