# Under what conditions must an instance refuse to close an agent span when OpenTelemetry GenAI usage attributes disaggregate below the parent token total **Date of Record:** 2026 November 24 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`) where child tool-call and sub-agent spans share a token budget with the parent `invoke_agent` span. Does not apply to single-shot LLM invocations with no nested spans, nor to runtimes that bill tokens exclusively at the leaf span. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-span* counters, but a parent agent span's usage is conventionally the sum of its own model turns plus the usage of every descendant span that consumed tokens on its behalf. This creates a conservation invariant that the spec does not enforce but that downstream cost attribution, budget guardrails, and audit replay all assume: **the parent's reported usage must be ≥ the sum of its children's reported usage**, with the residual attributed to the parent's own reasoning turns. The non-obvious failure is that this invariant silently inverts when a child tool span reports usage that the parent never observes—because the child completed after the parent's context was checkpointed, or because the tool returned a token count the parent's model never re-read. The span closes with a parent total *less* than the sum of children, and every consumer that sums leaves to reconcile against the root double-counts or under-counts without erroring. ## What Was Observed An agent executing a five-step tool loop emitted a root span `invoke_agent` with `gen_ai.usage.input_tokens=8120`, `output_tokens=1140`. Beneath it, four `execute_tool` spans and one nested `invoke_agent` (a delegated sub-task) reported input totals summing to `9304` and output summing to `1287`. The root's numbers were *lower* than the sum of its descendants on both axes. No span was marked error, no exporter dropped a batch, and the trace was structurally complete. The trace: the root agent's context window saturated at turn 4. A checkpoint-and-resume routine compacted the running history, and on resume the runtime re-initialized the root span's usage accumulator from the *compacted* prompt size rather than the cumulative billed size. The delegated sub-agent's span, emitted by a separate process, reported its full pre-compaction usage. The root, having compacted, reported only post-compaction usage plus its own residual turns. The two accounting bases diverged at the compaction boundary, and the parent's accumulator was reset to the smaller base while children kept the larger one. A second trigger produced the same inversion without compaction: a tool span that reported usage for an *internal* LLM call (a retrieval re-ranker) whose token cost the root agent never attributed to itself because the tool's result was summarized before re-entering the root's context. The tool span counted the re-ranker's tokens; the root counted only the summary. ## Condition of Validity The conservation invariant holds if and only if every token consumed by a descendant span is also observed by the parent span's accumulator at the moment the parent closes. That requires three conditions to hold jointly: 1. **Single accounting base.** The parent's usage accumulator must be monotonic across context compaction, checkpoint/resume, and sub-agent handoff. Compaction must not reset the base; it must fold the pre-compaction billed total forward. 2. **Attribution completeness.** Every child span that reports non-zero `gen_ai.usage.*` must have its usage added to the parent's accumulator before the parent closes, including spans that complete asynchronously after the parent's last model turn. 3. **No double-attribution at leaves.** A leaf span's usage must be counted exactly once—by its immediate parent—and never re-reported upward by a summarizing intermediate. When any condition fails, the parent's reported usage is no longer an upper bound on descendant usage, and the invariant that cost attribution and replay depend on is violated even though the trace looks well-formed. ## Decision Boundary The decision boundary is whether the runtime, at span-close time, has *observed* every descendant's final usage. If a descendant span is still open, or completed after the parent's last observation point, the parent cannot truthfully close with a total it has not reconciled. The rule: **a parent agent span must not close while any descendant span that reports `gen_ai.usage.*` remains open or unreconciled.** If the parent's lifecycle forces closure (timeout, cancellation, context exhaustion), the runtime must either (a) hold the parent open until descendants drain, (b) mark the parent with an explicit `gen_ai.usage.incomplete=true` attribute and refuse to treat its totals as authoritative, or (c) recompute the parent total as the max of its own accumulator and the sum of observed descendants at close time. Silently closing with the smaller accumulator is the failure. The boundary is not "did the trace export"—it is "did the parent observe the children before sealing its total." ## Operational Effect Downstream consumers that sum leaf usage to reconcile against the root will see a root that is smaller than its leaves, and cost dashboards will attribute the difference to "unaccounted" or, worse, to a sibling trace if the exporter batches by resource. Budget guardrails keyed on the root's `input_tokens` under-count and permit the agent to exceed its ceiling by exactly the compaction delta. Audit replay that reconstructs the prompt from root usage will reconstruct a truncated history and diverge from the actual execution. The correct operational posture is to treat any parent span whose descendant-sum exceeds its own total as *suspect*, quarantine it from cost aggregation, and emit a reconciliation warning rather than trusting the sealed number. ```python class AgentSpanUsageInconsistency(RuntimeError): """Raised when a parent agent span's reported GenAI usage does not bound the sum of its descendant spans' usage at close time.""" pass def close_agent_span(span, own_input: int, own_output: int, descendant_usage: list[tuple[int, int]], descendants_open: bool) -> dict: """Seal a GenAI invoke_agent span only if token conservation holds. Invariant: parent_input >= sum(child_input) and parent_output >= sum(child_output), with the residual attributed to the parent's own reasoning turns. """ if descendants_open: # Parent lifecycle forced closure before children drained. # Must not seal an authoritative total. span.set_attribute("gen_ai.usage.incomplete", True) raise AgentSpanUsageInconsistency( "parent span closing while descendant usage spans remain open; " "totals are non-authoritative and must not be aggregated" ) child_in = sum(u[0] for u in descendant_usage) child_out = sum(u[1] for u in descendant_usage) parent_in = own_input parent_out = own_output if parent_in < child_in or parent_out < child_out: # Accounting base diverged (compaction reset, async late child, # or double-attribution at a leaf). Reconcile by lifting the # parent to the observed descendant sum, then flag. reconciled_in = max(parent_in, child_in) reconciled_out = max(parent_out, child_out) span.set_attribute("gen_ai.usage.input_tokens", reconciled_in) span.set_attribute("gen_ai.usage.output_tokens", reconciled_out) span.set_attribute("gen_ai.usage.reconciled", True) raise AgentSpanUsageInconsistency( f"parent usage ({parent_in},{parent_out}) did not bound " f"descendant sum ({child_in},{child_out}); " f"sealed reconciled totals ({reconciled_in},{reconciled_out}) " f"and marked span non-authoritative for cost aggregation" ) span.set_attribute("gen_ai.usage.input_tokens", parent_in) span.set_attribute("gen_ai.usage.output_tokens", parent_out) span.set_attribute("gen_ai.usage.reconciled", False) return {"input_tokens": parent_in, "output_tokens": parent_out, "residual_input": parent_in - child_in, "residual_output": parent_out - child_out} ```