# Under what conditions must an instance abort a GenAI span tree when token-usage attributes disagree across nested agent and tool spans **Date of Record:** 2026 November 29 **Scope:** Applies to agents emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where an orchestrator span, delegated sub-agent spans, and tool-execution spans each report `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` and derived cost. Does not cover single-span synchronous inference calls where only one span attributes usage, nor provider-side billing reconciliation outside the trace. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *span-local* attributes: each span that performs a model invocation reports what that invocation consumed. There is no normative rule that a parent agent span's usage equals the sum of its children, because a parent agent may itself invoke a model (planning, synthesis) in addition to delegating. The failure mode is not "parent ≠ sum of children." The failure mode is **double-attribution**: the same tokens counted on both a delegating parent and a delegated child, producing a cost total that exceeds the true billed amount while every individual span remains internally self-consistent. This is invisible to per-span validation and only surfaces at the tree root. ## What Was Observed An orchestrator agent (`gen_ai.operation.name=invoke_agent`) spawned two sub-agents via a handoff tool. Each sub-agent span carried `gen_ai.usage.input_tokens` reflecting its full context — which included the orchestrator's conversation history replayed into the child. The orchestrator span *also* reported `gen_ai.usage.input_tokens` for its own planning turns, computed over the same shared history. Downstream cost aggregation summed `gen_ai.usage.*` across all spans with `gen_ai.operation.name` in `{invoke_agent, chat}`, yielding a total ~2.4× the provider invoice for the trace. The trace was structurally valid: no span violated a convention, no attribute was malformed, `trace_id`/`span_id` linkage was intact. The discrepancy only appeared when the aggregator compared tree-sum against the single authoritative `gen_ai.usage.*` on the root span (which the SDK emitted correctly from the final provider response). The gap was pure replay overlap: context inherited by children was billed once by the provider but counted once per span that re-sent it. A second observation compounded it. Tool-call events emitted as span events (`gen_ai.tool.call`) carried no usage attributes, but the sub-agents' *chat* spans that generated those tool calls reported `output_tokens` that included the serialized tool-call arguments. When the orchestrator re-serialized the tool result into its own next prompt, those tokens were counted again on the orchestrator's input. The overlap was not a bug in any one emitter; it was the absence of a tree-level invariant. ## Condition of Validity The tree-sum cost is trustworthy **iff** every token-consuming span's context is disjoint from every other span's context, or the emitter explicitly marks inherited context so the aggregator can subtract it. Concretely, the invariant is: > For any span tree rooted at an `invoke_agent` span, `Σ(gen_ai.usage.input_tokens over all descendant spans) ≤ gen_ai.usage.input_tokens` reported on the root by the provider, allowing equality only when children consume disjoint context. This holds only when: (a) sub-agent spans do not replay parent history, or (b) replayed history is tagged (e.g., a `gen_ai.usage.cached_input_tokens` split, or a custom `agent.context.inherited_tokens` attribute) so the aggregator deduplicates, or (c) the aggregator trusts only the root span's usage and treats descendants as *breakdown*, never as *addends*. It does **not** hold when children re-send shared history, when tool-call arguments are counted in both the generating span and the consuming span, or when a handoff re-embeds the parent transcript. ## Decision Boundary An instance must **abort tree-sum attribution and fall back to root-authoritative usage** when, at trace close, the summed descendant `gen_ai.usage.input_tokens` exceeds the root span's provider-reported `input_tokens` by more than a tolerance (recommend 5% to absorb tokenizer edge cases and cached-token rounding). Below tolerance, tree-sum is a legitimate breakdown. Above tolerance, the tree contains replayed context and the sum is a fiction — emitting it as cost is an operational error, not a rounding artifact. The boundary is *not* "parent ≠ children." A parent that plans plus delegates legitimately has usage greater than any single child. The boundary is strictly **sum-of-descendants > root**, which is only reachable through double-counting. Symmetrically, if sum-of-descendants is *far below* root (children report nothing), the instance must not silently attribute the gap to the root — that indicates dropped child spans or an uninstrumented delegation path, a separate continuity failure. ## Operational Effect When the boundary trips, the agent must: (1) emit a span event on the root marking `attribution.conflict=true` with both the tree-sum and root-authoritative values; (2) set the exported cost attribute to the root-authoritative figure only; (3) preserve descendant usage as a non-additive breakdown under a namespaced attribute (`agent.usage.breakdown.*`) that downstream dashboards are forbidden to sum. Failing to enforce this produces budget alarms that fire on phantom spend, autoscalers that throttle healthy agents, and — worst — cost-attribution pipelines that train future routing decisions on inflated per-sub-agent costs, systematically starving the sub-agents that inherit the most context. The guardrail is a tree-close invariant, not a per-span schema check, because every individual span in the failing trace was valid. ```python from dataclasses import dataclass class GenAIAttributionConflict(RuntimeError): """Raised when a GenAI span tree's summed descendant usage exceeds the root's provider-authoritative usage, indicating double-attributed tokens.""" pass @dataclass class SpanUsage: span_id: str operation: str # "invoke_agent" | "chat" | "execute_tool" input_tokens: int output_tokens: int TOLERANCE = 0.05 # 5% absorbs tokenizer/cache rounding def close_span_tree(root: SpanUsage, descendants: list[SpanUsage]) -> dict: """Enforce the tree-sum invariant at trace close. Returns a dict with authoritative usage and a non-additive breakdown. Raises GenAIAttributionConflict when descendants double-count root context. """ desc_input = sum(s.input_tokens for s in descendants) desc_output = sum(s.output_tokens for s in descendants) if root.input_tokens <= 0: raise GenAIAttributionConflict( f"root span {root.span_id} reports non-positive input_tokens=" f"{root.input_tokens}; cannot establish authoritative baseline" ) overage = (desc_input - root.input_tokens) / root.input_tokens if overage > TOLERANCE: # Double-attribution detected: children replayed shared context. # Do NOT emit tree-sum as cost. Fall back to root-authoritative. raise GenAIAttributionConflict( f"tree-sum input_tokens={desc_input} exceeds root-authoritative " f"{root.input_tokens} by {overage:.1%} (> {TOLERANCE:.0%}); " f"descendant usage is a breakdown, not an addend. " f"root={root.span_id}, descendants={[s.span_id for s in descendants]}" ) return { "authoritative_input_tokens": root.input_tokens, "authoritative_output_tokens": root.output_tokens, "breakdown": { "additive": True, "descendant_input_tokens": desc_input, "descendant_output_tokens": desc_output, }, } ```