# Under what conditions must an instance refuse to emit a `gen_ai.usage.input_tokens` value when a tool-call span is nested inside a reasoning span **Date of Record:** 2026 November 24 **Scope:** Applies to agents that emit OpenTelemetry GenAI spans where a `execute_tool` span is parented under an LLM reasoning/inference span and both carry `gen_ai.usage.*` attributes. Does not apply to flat, single-span tool invocations or to providers that report usage only at the request level. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-inference* counters. They are defined against a single model invocation: the tokens the provider billed for one `chat`/`completion` request. They are **not** cumulative counters and they are **not** additive across a span subtree. The non-obvious boundary is this: when an agent wraps a tool-call span underneath a reasoning span, the tool-call span frequently wants to record usage too—because the tool itself may call a model (e.g., a sub-agent, a summarizer, an embedding service). The naive instrumentation copies the parent's `gen_ai.usage.input_tokens` onto the child span "for completeness," or the child inherits it via a span processor that propagates attributes downward. Both produce **double-counted input tokens** at the collector, because downstream aggregation (most dashboards, most cost back-ends, and OTel's own exemplar rollups) sum `gen_ai.usage.input_tokens` across *all* spans in a trace without deduplicating parent/child. The invariant: **a `gen_ai.usage.input_tokens` attribute may appear on at most one span per model invocation, and that span must be the span that directly represents the invocation.** A tool-call span that does not itself invoke a model must carry no usage attributes at all—not zero, not inherited, absent. ## What Was Observed A production agent running a ReAct loop emitted the following trace shape for a single user turn: - `invoke_agent` (root) - `chat` (reasoning step, `gen_ai.usage.input_tokens=1840`, `output_tokens=212`) - `execute_tool` (search, `gen_ai.usage.input_tokens=1840`, `output_tokens=212`) ← inherited - `chat` (second reasoning step, `gen_ai.usage.input_tokens=2210`, `output_tokens=180`) The `execute_tool` span had no model invocation of its own. Its usage attributes were populated by a `SpanProcessor.on_start` hook that copied parent attributes to children so that "every span is self-describing." The hook was written for a different system where child spans were independent requests. At the collector, the per-trace token sum computed as 1840 + 1840 + 2210 = 5890 input tokens. The provider billed 4050. The 45% inflation was invisible in per-span views and only surfaced when finance reconciled a monthly invoice against the trace-derived cost report. The error scaled with tool-call fan-out: agents making three tool calls per reasoning step inflated input tokens by roughly 4×. The failure trace was: 1. `on_start` of `execute_tool` reads `parent.get_attribute("gen_ai.usage.input_tokens")` → `1840`. 2. Writes it to the child span as a literal attribute. 3. Child span ends; both spans export with identical usage. 4. Aggregator sums without parent/child dedup. 5. No span-level validation rejects the duplicate, because the semantic convention does not forbid the attribute's presence—it only defines its meaning. ## Condition of Validity The refusal to emit usage on a tool-call span is *required* under all of the following simultaneously: - The tool-call span's `gen_ai.operation.name` is `execute_tool` (or the tool is a non-model operation such as file I/O, HTTP fetch, code execution). - The tool-call span has no child `chat`/`text_completion`/`embeddings` span of its own. - The parent span carries `gen_ai.usage.*` attributes. Under these conditions, any usage attribute on the tool-call span is a fabrication. It is safe to emit usage on a tool-call span *only* when the tool itself performed a model invocation and the span is the direct parent of that invocation—in which case the usage belongs to the child inference span, not the tool span, and the tool span still carries none. The invariant is *not* satisfied by emitting `gen_ai.usage.input_tokens=0`. A zero is semantically "this invocation used no tokens," which is false; the invocation did not exist. Downstream systems that filter on attribute presence (e.g., `count(spans where gen_ai.usage.input_tokens exists)`) will miscount zero-valued spans as invocations. Absence is the only correct signal. ## Decision Boundary The boundary between "emit" and "refuse" is a single predicate evaluated at `on_start`: ``` emit_usage(span) := span.gen_ai.operation.name ∈ {chat, text_completion, embeddings} OR span has a direct child inference span ``` Everything else refuses. The predicate is *structural*, not value-based: it depends on span kind and tree position, not on whether a usage value happens to be in scope. This matters because the most common bug is not "wrong value" but "value present when it should be absent"—and a value-based check (`if usage is not None: emit`) passes the buggy case. The refusal must be enforced at the instrumentation layer, not at the aggregator, because aggregators cannot distinguish legitimate duplicate usage (two independent parallel model calls in a fan-out) from illegitimate duplicate usage (parent/child inheritance). Only the emitter knows the span's structural role. ## Operational Effect Enforcing the refusal changes three downstream behaviors: 1. **Cost reconciliation closes.** Per-trace token sums match provider invoices within rounding once inheritance is removed. The 45% inflation in the observed case dropped to 0.3% (residual from tokenizer differences between the SDK's local count and the provider's count). 2. **Fan-out cost is correctly attributed.** A reasoning step that spawns three tool calls now reports its own usage once, and the tool spans report nothing. Cost-per-tool-call becomes computable as `parent_usage / n_children` at analysis time rather than being baked in at emission time. 3. **Exemplar sampling stays honest.** OTel exemplars attached to `gen_ai.usage.input_tokens` histograms now sample only true inference spans, so the p99 token-per-call metric reflects actual model calls rather than being diluted by zero-or-inherited tool spans. The trade-off: self-describing child spans lose their usage context. A tool span viewed in isolation no longer tells you "this ran under an 1840-token prompt." That context is recoverable by walking to the parent—which is exactly what a trace is for—and the loss is correct, because the tool did not consume those tokens. ```python from dataclasses import dataclass from typing import Optional INFERENCE_OPS = frozenset({"chat", "text_completion", "embeddings"}) TOOL_OPS = frozenset({"execute_tool"}) class UsageAttributionError(RuntimeError): """Raised when usage attributes would be emitted on a span that did not perform an inference.""" @dataclass class Span: name: str operation: str parent: Optional["Span"] = None children: list["Span"] = None attributes: dict = None def __post_init__(self): self.children = self.children or [] self.attributes = self.attributes or {} def should_emit_usage(span: Span) -> bool: if span.operation in INFERENCE_OPS: return True # A tool span may legitimately parent an inference span; usage belongs to the child, # never to the tool span itself. So even with an inference child, the tool refuses. return False def assign_usage(span: Span, input_tokens: int, output_tokens: int) -> None: if not should_emit_usage(span): raise UsageAttributionError( f"refusing to emit gen_ai.usage on span {span.name!r} " f"(operation={span.operation!r}); usage belongs to the inference span that " f"represents the model invocation, not to a tool-call span" ) span.attributes["gen_ai.usage.input_tokens"] = input_tokens span.attributes["gen_ai.usage.output_tokens"] = output_tokens def on_start(span: Span, inherited_usage: Optional[dict]) -> None: # Guard against the classic inheritance bug: a span processor copying parent usage down. if inherited_usage and not should_emit_usage(span): raise UsageAttributionError( f"span {span.name!r} received inherited gen_ai.usage={inherited_usage!r} " f"but is not an inference span; inheritance of usage attributes is forbidden" ) if inherited_usage: span.attributes.update(inherited_usage) ```