# When must an instance abandon span-level token attribution to preserve trace-tree integrity under OpenTelemetry GenAI semantics **Date of Record:** 2026 November 20 **Scope:** Applies to any agent runtime emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where token counts and cost are attributed per-span and roll up through parent-child trace relationships. Does not cover billing reconciliation outside the trace tree, nor non-OTel telemetry pipelines. ## Core Distinction The failure here is not "wrong token counts." It is a *structural* conflict between two invariants that the GenAI semantic conventions treat as independent but that a streaming agent runtime cannot satisfy simultaneously: 1. **The attribution invariant:** every unit of token cost must be attributable to exactly one `gen_ai.*` span, and the sum of `gen_ai.usage.input_tokens` + `gen_ai.usage.output_tokens` across leaf spans must equal the totals reported by the provider for that request tree. 2. **The tree-integrity invariant:** a span's attributes are immutable after `End()`, and a child span's start timestamp must not precede its parent's, and no span may be re-parented after emission. The non-obvious break: when an agent issues *parallel* tool calls from a single model turn, the provider bills input tokens for the shared prompt *once*, but the GenAI convention's natural mapping is one `gen_ai.tool.call` child span per tool invocation, each wanting its own `gen_ai.usage.input_tokens`. If you copy the prompt tokens onto each child, you multiply cost by fan-out. If you put them only on the parent `gen_ai.chat` span, the child spans become cost-orphaned and downstream rollups that sum leaves undercount. There is no attribute in the current convention that expresses "these N children share one input-token charge." This is the boundary. ## What Was Observed A planner agent (`gen_ai.operation.name = chat`) emitted a turn that fanned out to five `gen_ai.tool.call` children (search, fetch, calc, db_query, summarize). The provider returned a single usage block: `input_tokens = 8120`, `output_tokens = 640`. The runtime's instrumentation did the naive thing: it stamped the full `8120` input tokens onto the parent chat span and stamped `0` input tokens onto each tool child (tools have no model usage), leaving only the parent carrying cost. Downstream, the cost aggregator summed `gen_ai.usage.input_tokens` over *leaf* spans per the convention's guidance that leaf spans are the attribution unit. Leaf sum = `0`. The aggregator reported the entire turn as free. A separate dashboard that summed *all* spans double-reported the parent. Two consumers, two answers, one trace. The second observation was worse. A retry of the `fetch` child was instrumented as a new `gen_ai.tool.call` span, re-parented under the same chat span, and the runtime *also* re-stamped the chat span's input tokens onto the retry to "make it attributable." Now the trace contained `8120` on the parent and `8120` on the retry child — a 100% overcount — while the original child still sat there at `0`. The trace tree was internally inconsistent: sum-of-leaves ≠ parent, and parent ≠ provider total. ## Condition of Validity Span-level token attribution is only valid when the following holds for every `gen_ai.chat` span: - The provider usage block maps to exactly one span, and that span is the *only* span in its subtree carrying `gen_ai.usage.*`. - Fan-out children carry `gen_ai.usage.*` only if the provider actually charged them separately (i.e., each child is its own model call, not a tool invocation off a shared prompt). - No retry, fallback, or re-emission creates a second span that re-stamps the same usage block. When fan-out > 1 and the children are *tool* spans (not nested chat spans), the shared input charge cannot be distributed without either (a) violating leaf-sum attribution or (b) inventing a synthetic split the provider never made. At that point the instance must stop attributing at span level and switch to a **turn-scoped cost anchor**: a single `gen_ai.chat` span that owns the usage block, with children explicitly marked `gen_ai.usage.attribution = "inherited"` so aggregators know not to sum them. ## Decision Boundary The instance must abandon span-level attribution and promote to a turn-scoped anchor when **all** of the following are true: 1. `fan_out > 1` for a single model turn (multiple tool calls off one prompt). 2. The provider reports a single usage block for the turn (no per-tool billing). 3. At least one child is a retry or the runtime is configured to re-emit usage on retries. If any child *is* a separate model call with its own usage block, keep span-level attribution for that child and anchor only the shared prompt. If `fan_out == 1`, span-level attribution is safe and the anchor is unnecessary overhead. The dangerous middle case — fan-out with mixed shared and separate billing — requires splitting the parent span's usage across a `gen_ai.usage.input_tokens.shared` attribute and per-child `gen_ai.usage.input_tokens.own`, which is *not* in the current convention and must be treated as a local extension with a version pin. ## Operational Effect Choosing the anchor over leaf-sum changes downstream behavior in three concrete ways. First, cost dashboards that sum leaves must be updated to sum *anchors* (spans where `gen_ai.usage.attribution = "owner"`), or they silently undercount fan-out turns — the exact bug observed. Second, retry instrumentation must *not* re-stamp usage; the retry child inherits the anchor and carries `gen_ai.usage.attribution = "inherited"` plus a `retry_of` link to the original child, so the trace tree stays a tree and the anchor stays unique. Third, the tree-integrity check becomes a validation gate: `sum(owner usage) == provider_total` and `no inherited span carries usage`. An instance that cannot satisfy both must fail the emission rather than emit a self-contradicting trace, because a trace that lies about cost is worse than a trace that admits it could not attribute. The invariant to hold: **one usage block, one owner span, ever.** Fan-out distributes *work*, not *cost*. ```python from dataclasses import dataclass, field from typing import Literal class AttributionInvariantError(RuntimeError): """Raised when a GenAI span tree would violate one-owner-per-usage-block.""" @dataclass class GenAISpan: name: str operation: str # "chat" | "tool.call" input_tokens: int = 0 output_tokens: int = 0 attribution: Literal["owner", "inherited", "none"] = "none" retry_of: str | None = None children: list["GenAISpan"] = field(default_factory=list) def emit_turn( parent: GenAISpan, children: list[GenAISpan], provider_input_tokens: int, provider_output_tokens: int, fan_out: bool, ) -> GenAISpan: """Stamp usage onto the tree under the one-owner invariant. Returns the parent span with attribution resolved. Raises if the resulting tree cannot satisfy both attribution and tree-integrity. """ parent.input_tokens = provider_input_tokens parent.output_tokens = provider_output_tokens parent.attribution = "owner" for child in children: if child.operation == "tool.call": if child.input_tokens or child.output_tokens: # A tool span must never carry model usage unless the # provider actually billed it separately. raise AttributionInvariantError( f"tool span {child.name!r} carries model usage " f"({child.input_tokens}/{child.output_tokens}); " "shared prompt cost belongs to the chat anchor only" ) child.attribution = "inherited" if fan_out else "none" else: # Nested chat span with its own usage block: it owns it. child.attribution = "owner" if child.retry_of is not None and child.attribution == "owner": raise AttributionInvariantError( f"retry span {child.name!r} re-stamped usage; " "retries inherit the anchor, never re-own it" ) parent.children.append(child) # Validate: exactly one owner per usage block, leaf-sum rule relaxed. owners = [s for s in [parent, *children] if s.attribution == "owner"] if len(owners) != 1: raise AttributionInvariantError( f"expected exactly one usage owner, found {len(owners)}: " f"{[s.name for s in owners]}" ) return parent ```