# What happens when an instance attributes a nested tool-call span's tokens to the parent agent span: double-counting and orphaned cost under OpenTelemetry GenAI semantic conventions **Date of Record:** 2026 November 28 **Scope:** Applies to agents emitting OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*` attributes) where an agent span (`gen_ai.operation.name = "invoke_agent"`) parents one or more child tool-execution spans that themselves invoke LLM calls. Does not apply to flat single-span-per-LLM-call instrumentation, nor to backends that deduplicate on `gen_ai.response.id`. ## Core Distinction The GenAI semantic conventions define token-usage attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) as properties of a *single model inference*, keyed to the span that represents that inference. They do **not** define a rollup contract for agent spans that orchestrate multiple inferences. The distinction that breaks is between *inference-scoped accounting* (tokens consumed by one `chat` span) and *agent-scoped accounting* (tokens consumed by the whole agent run). Instrumentation libraries frequently collapse these two by summing child `chat` span usage onto the enclosing `invoke_agent` span so that a single query yields "total tokens for this agent." That collapse is not sanctioned by the spec, and it produces a specific, silent double-count: the same tokens appear on both the leaf `chat` span and the ancestor `invoke_agent` span, and any backend that aggregates by summing `gen_ai.usage.*` across all spans in a trace reports roughly 2× the true consumption for every tool-mediated inference. ## What Was Observed A research agent executes a plan with a tool-calling loop. Per user turn it emits: one `invoke_agent` root span, N `execute_tool` child spans, and, nested under each tool span, one or more `chat` spans for the sub-LLM calls the tool makes (e.g., a summarizer tool that calls a model to compress retrieved documents). The instrumentation library was configured with a "propagate usage to parent" flag to make dashboards simpler. Trace for a single turn, as emitted: - Root `invoke_agent` span: `gen_ai.usage.input_tokens = 41_200`, `gen_ai.usage.output_tokens = 3_900`. These are *sums* of the three child `chat` spans plus the agent's own final synthesis call. - Child `chat` span A (planner): `input_tokens = 12_000`, `output_tokens = 800`. - Child `chat` span B (inside `execute_tool` → summarizer): `input_tokens = 18_000`, `output_tokens = 1_100`. - Child `chat` span C (final synthesis): `input_tokens = 11_200`, `output_tokens = 2_000`. The true turn consumption is 41,200 input / 3,900 output. The cost dashboard, configured to `sum(gen_ai.usage.input_tokens)` grouped by `trace_id`, reports **82,400 input / 7,800 output** — exactly 2×. The error is not a rounding artifact; it is structural, and it scales with nesting depth: a tool that itself spawns a sub-agent (three levels of `invoke_agent` → `execute_tool` → `invoke_agent`) yields 3× inflation on the deepest inference. The second observed symptom is more damaging than the inflation itself: **orphaned attribution**. When the tool span is dropped by a sampler (tail-based sampling that keeps only `invoke_agent` roots, a common cost-control configuration), the child `chat` spans vanish but the parent's rolled-up `usage` attributes remain. The parent now claims 41,200 tokens with no child spans to substantiate them. A verifier replaying the trace cannot reconcile the number against any inference record, and per-model or per-tool cost breakdowns silently lose the dropped leaves while the total stays "correct-looking." The trace is internally inconsistent: parent usage exceeds the sum of surviving children, which is the inverse of the double-count case and indistinguishable from it without knowing the sampler's decision. A third symptom appeared under retry. A `chat` span that failed with `gen_ai.response.finish_reasons = ["error"]` and was retried emitted two `chat` spans; both carried `usage` (the failed call still billed input tokens). The parent rollup summed both, so the retried inference was counted twice in the agent total but once in the leaf total — a partial double-count that no single rule catches. ## Condition of Validity The double-count is *only* absent when one of the following holds, and each must be enforced, not assumed: 1. **No rollup.** `invoke_agent` spans carry no `gen_ai.usage.*` attributes; usage lives exclusively on `chat` spans. Backends compute agent totals by summing leaves. This is the spec-faithful configuration and the one to default to. 2. **Rollup with leaf suppression.** If the parent carries rolled-up usage, the leaves must be marked `gen_ai.usage.rollup = true` (or an equivalent non-standard discriminator) so the aggregator sums only one level. This is a local convention, not a spec guarantee — any consumer that does not know the convention will double-count. 3. **Deduplication by response identity.** `gen_ai.response.id` is unique per inference and shared between a leaf and its rollup; an aggregator that counts distinct `gen_ai.response.id` values is immune. This requires the instrumentation to propagate the id to the parent, which most libraries do not do. Absent all three, the invariant "sum of `gen_ai.usage.*` over spans in a trace equals tokens consumed" is false, and it fails silently because both the inflated and the true number are plausible. ## Decision Boundary The boundary is drawn at the **span that represents an inference**. Attribute `gen_ai.usage.*` to exactly one span per inference — the `chat` span whose `gen_ai.response.id` identifies it — and never to an ancestor. If a product requirement demands an agent-level total on the root span (dashboards, budget enforcement at the agent granularity), emit it under a *distinct* attribute namespace, e.g. `agent.usage.total.input_tokens`, so that no generic `sum(gen_ai.usage.*)` query can pick it up. The decision rule for instrumentation authors: - If the span has a `gen_ai.response.id` and represents one model call → it MAY carry `gen_ai.usage.*`. - If the span is `invoke_agent` or `execute_tool` → it MUST NOT carry `gen_ai.usage.*`; it MAY carry a namespaced rollup. - If a sampler may drop descendants → the rollup MUST NOT be emitted, because a rollup whose leaves are absent is unreconcilable and will be double-counted by any consumer that later receives the leaves through a different export path (e.g., a second collector with different sampling). The boundary also governs retries: a retried inference is two inferences and two `gen_ai.response.id` values; both are legitimately billable, and the leaf-level sum is correct. The rollup is what makes the retry look like a single inflated call. ## Operational Effect With the spec-faithful configuration (no rollup, usage only on `chat` spans), cost dashboards must aggregate at the leaf and join to the agent via `trace_id`/`parent_span_id`; totals are then exact and reconcile against provider invoices. With the rollup configuration, every tool-mediated inference is counted twice, retried inferences are counted 1.x times depending on where the retry sits in the tree, and sampled traces produce parent totals that exceed the sum of surviving children — a state that triggers false "missing span" alerts and defeats replay-based verification. The guardrail is a single invariant, enforced at emit time: *a span carries `gen_ai.usage.*` iff it carries a `gen_ai.response.id`.* Instrumentation that violates it should fail closed rather than emit, because an emitted-but-wrong usage attribute is indistinguishable downstream from a correct one, and the double-count is only discovered when the invoice arrives. ```python class UsageAttributionError(RuntimeError): """Raised when a span would carry gen_ai.usage.* without a single-inference identity.""" pass def emit_span_usage(span: dict) -> dict: """ Enforce the invariant: gen_ai.usage.* may appear only on a span that represents exactly one inference (has gen_ai.response.id). Rollups belong in a distinct namespace (agent.usage.*). """ attrs = span.get("attributes", {}) has_usage = any(k.startswith("gen_ai.usage.") for k in attrs) has_response_id = "gen_ai.response.id" in attrs is_agent_span = attrs.get("gen_ai.operation.name") in ("invoke_agent", "execute_tool") if has_usage and is_agent_span: raise UsageAttributionError( f"span {span.get('name')!r} (operation={attrs.get('gen_ai.operation.name')}) " "carries gen_ai.usage.* but is not an inference span; " "move the total to agent.usage.* to avoid double-counting leaves" ) if has_usage and not has_response_id: raise UsageAttributionError( f"span {span.get('name')!r} carries gen_ai.usage.* without gen_ai.response.id; " "usage cannot be reconciled to a single inference" ) return span ```