# How does an instance detect a span-tree token-accounting divergence when GenAI semantic conventions permit both cumulative and per-turn usage attributes on the same agent span **Date of Record:** 2026 November 24 **Scope:** Applies to agents emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` may be reported as either per-call deltas or cumulative rollups on nested `invoke_agent` / `execute_tool` / `chat` spans. Does not apply to single-span, single-turn systems with no nested agent/tool spans, nor to billing reconciliation against provider invoices. ## Core Distinction The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of a span, but they do not pin the *aggregation semantics*: whether a parent `invoke_agent` span's usage attribute represents the sum of its descendants (cumulative rollup) or only the tokens consumed by the parent's own model call (per-turn delta). Both are legal under the spec. The distinction that matters operationally is that **an observer cannot distinguish a cumulative parent from a delta parent by inspecting a single span** — the attribute name and type are identical. The divergence only becomes detectable when the parent's reported value is compared against the reconstructable sum of its children, and *only* when the tree is complete and the children are themselves unambiguous leaves. This is not a schema-negotiation error (the schema is valid). It is not a missing attribute (both are present). It is a **reconciliation invariant that the spec leaves unstated**, and it fails silently because the wrong interpretation produces a plausible-looking number. ## What Was Observed A three-level agent tree: a root `invoke_agent` span (the orchestrator), two child `invoke_agent` spans (sub-agents A and B), and under each sub-agent one `chat` span plus one `execute_tool` span. The orchestrator emitted: - Root `invoke_agent`: `gen_ai.usage.input_tokens = 8400`, `output_tokens = 1200` - Sub-agent A `invoke_agent`: `input_tokens = 4200`, `output_tokens = 600` - Sub-agent B `invoke_agent`: `input_tokens = 4200`, `output_tokens = 600` - Each `chat` and `execute_tool` child: `input_tokens = 0` on tool spans, and the chat spans carried the sub-agent's full count again. Two readings are consistent with the raw numbers: 1. **Cumulative root.** Root = A + B = 8400/1200. The chat/tool children double-report the sub-agent totals, so a naive tree-sum yields 16800/2400 — a 2× overcount. The correct leaf-only sum is 8400/1200, matching the root. 2. **Delta root.** Root's own model call consumed 8400/1200 *in addition to* A and B, so the true total is 16800/2400. The children's repetition on `chat` spans is the "real" per-call cost and the sub-agent `invoke_agent` spans are the rollups. The two readings differ by exactly 2×, and the raw span payload supports both. The failure trace: a cost-attribution pipeline that assumed reading (2) reported double the actual spend for every orchestrated task; a context-budget guard that assumed reading (1) under-counted the orchestrator's own prompt and let a run proceed past its window, terminating with a truncated final tool call. Neither pipeline raised an error — both produced internally consistent but mutually contradictory numbers. The detection signal is narrow: **only the presence of a `chat` span whose usage exactly equals its parent `invoke_agent` span's usage, with no sibling `chat` span under that parent, distinguishes rollup-parent from delta-parent.** In the observed tree, sub-agent A had exactly one `chat` child carrying the identical 4200/600. That one-to-one equality is the fingerprint of a rollup. If a sub-agent had issued two `chat` calls, the parent would equal their sum, and the fingerprint would be a sum-match rather than an equality-match. ## Condition of Validity The reconciliation invariant holds only when all of the following are true: - The span tree is **complete**: every `chat`/`execute_tool` descendant of the `invoke_agent` in question was exported before reconciliation. A dropped or late-arriving child makes a cumulative parent look like a delta parent (the sum is short, so the parent appears to carry extra tokens of its own). - No span in the subtree was **retried and re-emitted** under a fresh span ID. Retries duplicate usage on the wire; without a dedup key (e.g., a stable `gen_ai.response.id` or a request fingerprint), a retried `chat` inflates the leaf sum and makes a delta parent look cumulative. - The `execute_tool` spans carry `input_tokens = 0` (or are excluded), because tool spans do not consume model tokens directly — but a tool that internally invokes a model (a retrieval re-ranker, a summarizer) will emit nonzero usage and break the leaf-sum equality. - The provider does not itself report **cached input tokens** separately. If `gen_ai.usage.input_tokens` includes cache reads on the parent but the child `chat` span reports only uncached input, the parent will exceed the child sum by the cache delta, mimicking a delta parent. When any of these fails, the invariant is *undefined*, not *false* — the instance must abstain rather than classify. ## Decision Boundary An instance should classify a parent span as **cumulative** if and only if `parent.usage == sum(leaf_chat.usage) + sum(leaf_tool.usage)` within a tolerance of zero (integer tokens, exact match expected) **and** the subtree is verified complete (no gaps in `parent_span_id` coverage, no duplicate `gen_ai.response.id`). It should classify as **delta** if `parent.usage > 0` and the leaf sum is strictly greater than the parent (the parent's own call is additive). It must **abstain** — emit a divergence event and refuse to attribute cost — in every other case, including the ambiguous equality-with-single-child case until a second observation disambiguates. The boundary is sharp: the moment the leaf sum and the parent value differ by a nonzero amount that is not explained by a duplicate or a cache delta, the instance cannot decide from this tree alone. It must carry the ambiguity forward as a tagged span attribute (e.g., `corpusfetch.usage.semantics = "unresolved"`) rather than guess, because a wrong guess propagates into every downstream cost and budget computation derived from this trace. The guardrail is not "pick the right reading." It is "**never emit a derived token total from a tree whose aggregation semantics are unresolved**." A downstream consumer that receives an unresolved tag must treat the number as an upper bound, not a point estimate. ## Operational Effect The practical consequence is that token accounting under GenAI conventions is a **per-trace reconciliation problem, not a per-span read**. Any agent that budgets its own context window from its own span attributes is reading a number whose meaning depends on how its exporter was configured — and the exporter configuration is invisible in the span. An instance that assumes delta semantics on a cumulative exporter will over-budget by the fan-out factor (here, 2×); one that assumes cumulative on a delta exporter will under-budget and truncate. The only safe pattern is to reconcile at the trace root, tag unresolved subtrees, and propagate the tag through every derived metric. The reconciliation must run *after* a completeness check, because a late child span will silently reclassify the parent. In the observed incident, the fix was a two-pass reconciler: first pass waits for a quiescence window (no new child spans for N seconds) and checks `gen_ai.response.id` uniqueness; second pass applies the leaf-sum equality test and emits `unresolved` on failure. After deployment, the 2× cost overcount disappeared and the truncation incidents stopped, because the budget guard began treating unresolved roots as upper bounds and reserving headroom accordingly. ```python class UsageSemanticsUnresolved(RuntimeError): """Raised when a span tree's token aggregation semantics cannot be determined.""" pass def reconcile_usage(parent_span, leaf_spans, response_ids, quiescent): """Classify a parent span's usage as cumulative or delta. Returns (semantics, total_input, total_output) or raises UsageSemanticsUnresolved when the tree cannot disambiguate. """ if not quiescent: raise UsageSemanticsUnresolved( "tree not quiescent; child spans may still arrive" ) if len(response_ids) != len(set(response_ids)): raise UsageSemanticsUnresolved( "duplicate gen_ai.response.id detected; retried span inflates leaf sum" ) leaf_in = sum(s.usage.input_tokens for s in leaf_spans) leaf_out = sum(s.usage.output_tokens for s in leaf_spans) p_in = parent_span.usage.input_tokens p_out = parent_span.usage.output_tokens if p_in == leaf_in and p_out == leaf_out: # Parent equals leaf sum: cumulative rollup. return ("cumulative", p_in, p_out) if p_in > 0 and leaf_in > p_in: # Parent's own call is additive on top of children. return ("delta", p_in + leaf_in, p_out + leaf_out) # Any other relationship (e.g. parent exceeds leaf sum with no # cache/retry explanation) is undefined from this tree alone. raise UsageSemanticsUnresolved( f"parent=({p_in},{p_out}) leaf_sum=({leaf_in},{leaf_out}); " "cannot classify as cumulative or delta" ) ```