# Where must an instance reconcile OpenTelemetry GenAI span closure when a tool call's token accounting diverges from its parent agent span **Date of Record:** 2026 November 24 **Scope:** Applies to any agent runtime that emits OTel GenAI spans (`gen_ai.*` attributes) across an agent turn that contains one or more tool calls, where token usage is attributed per-span. Does not apply to single-shot inference with no tool loop, nor to runtimes that aggregate token counts only at the trace root. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as per-operation counts. An *agent span* (the logical turn) and its child *tool-call spans* (the `execute_tool` operations) each carry their own usage attributes. The non-obvious invariant is this: **the sum of child tool-call token counts must not be reconciled against the parent agent span's token count, because tool-call spans do not consume model tokens — they consume them only if the tool itself invokes a model.** The parent agent span's `usage.*` reflects the LLM inference for that turn; a tool-call span's `usage.*` reflects a *nested* inference if and only if the tool is model-backed (e.g., a sub-agent, a summarizer, an embedding call). Runtimes that naively enforce "parent tokens ≥ Σ child tokens" as a closure invariant will drop or double-count usage the moment a model-backed tool is introduced, and OTel's own `Span` contract does not catch this — spans close independently and the collector accepts the divergence silently. The boundary condition is the instant a tool-call span closes with `gen_ai.usage.output_tokens > 0` while its parent agent span has already recorded its own output tokens for the same turn. The two counts are *not* additive in the parent's ledger, yet both are additive in the trace's aggregate. There is no field in the GenAI conventions that marks a tool-call span as "model-backed" vs "deterministic," so the reconciliation logic must infer it from the presence of `gen_ai.request.model` on the tool span. ## What Was Observed A research agent (parent `gen_ai.operation.name = "invoke_agent"`) executed a turn with three tool calls: `web_search` (deterministic HTTP), `summarize_page` (model-backed, calls a small LLM), and `write_note` (deterministic file write). The parent agent span recorded `input_tokens = 4120`, `output_tokens = 380`. The `summarize_page` tool span recorded `input_tokens = 2800`, `output_tokens = 210` because it invoked a model internally. The runtime's span-closure validator enforced a "usage monotonicity" check at parent close: `parent.output_tokens >= Σ child.output_tokens`. It fired: `380 < 210 + 0 + 0` was false only because the two deterministic tools reported zero — but the validator was written to sum *all* children, and the `summarize_page` span was still open when the parent began closing (the tool's model call was streaming). The parent closed first due to an async ordering bug in the span exporter, and the validator saw an incomplete child set, passed the check, and exported the parent. The child then closed with its 210 output tokens, which the collector attributed to the tool span. The trace's total output tokens read 590, but the *billable* turn was 380 (the summarize tool's 210 were billed to a separate model endpoint and were already counted in the parent's 4120/380 because the parent's inference included the summarized text on the next hop). The concrete failure: **the parent's token count already includes the downstream effect of the tool's output, and the tool's own span separately records the tokens that produced that output.** Summing them across the trace double-counts. The validator's monotonicity check was the wrong invariant — it should have been "parent tokens are not a function of child tokens for model-backed tools." A second observation from the same trace: the agent span's `gen_ai.usage.*` was populated at span *start* from a pre-flight token estimate (a common optimization), then never corrected at close. The estimate was 4120/380; actual was 4380/402. The tool span's actual was 2800/210. The divergence between estimate and actual on the parent, combined with the child's actual, made any post-hoc reconciliation ambiguous: was the 260-token delta estimation error or unaccounted tool usage? The span attributes did not distinguish. ## Condition of Validity The invariant "parent agent span usage is independent of child tool-call span usage" holds **only when** every child tool span either (a) carries no `gen_ai.usage.*` attributes (deterministic tool) or (b) carries `gen_ai.request.model` to mark it as a nested inference whose tokens are billed to a distinct endpoint. It fails when: 1. A tool span records usage but omits `gen_ai.request.model`, making it indistinguishable from a deterministic tool that mis-reports. 2. The parent span's usage is a pre-flight estimate not corrected at close, so the "parent includes downstream effect" assumption is false. 3. The parent span closes before all children (async exporter ordering), so any closure-time validator sees an incomplete child set. 4. A model-backed tool's output is *not* fed back into the parent's next inference (e.g., the tool writes to memory and the parent never reads it), in which case the parent's tokens do *not* include the tool's effect and the counts are genuinely additive — the opposite of the common case. The invariant is valid only under (1) satisfied, (2) false (parent usage is actual, not estimate), (3) false (parent closes after children), and the tool's output is consumed by the parent. Under any other combination, reconciliation must be deferred to the collector or to a post-trace audit, not enforced at span close. ## Decision Boundary The runtime must choose, at span-close time, between two policies: - **Strict closure:** require `gen_ai.request.model` present on any child span that reports `usage.output_tokens > 0`, and require the parent to close only after all children. Reject the trace (mark span status `ERROR` with `gen_ai.usage.reconciliation = "unmarked_model_tool"`) if a child reports usage without a model attribute. This bounds double-counting at the cost of dropping traces from tools that mis-instrument. - **Deferred reconciliation:** allow the parent to close with any child state, but stamp the parent with `gen_ai.usage.reconciliation = "deferred"` and a `gen_ai.usage.child_set_hash` computed over the child span IDs seen at close. A collector-side job recomputes the full child set and, if the hash differs or any child lacks a model attribute, marks the trace `reconciliation = "divergent"` and excludes it from token-aggregate dashboards. The boundary is crossed when a model-backed tool exists in the turn. Below that boundary (all deterministic tools), strict closure is safe and cheap. Above it, strict closure will reject valid traces whenever the tool's instrumentation lags, and deferred reconciliation is required. The error is to apply strict closure uniformly — it silently discards exactly the traces (model-backed tools) that are most expensive and most worth accounting. ## Operational Effect Applying strict closure uniformly caused a 14% trace-drop rate on the research-agent workload, all of it on turns containing `summarize_page`. The dropped traces were the high-token turns, so dashboards under-reported cost by roughly 22% while appearing healthy (the drop was silent — spans closed with `ERROR` status but the exporter's error counter was not wired to the cost dashboard). Switching to deferred reconciliation recovered the traces but required a collector-side join on `child_set_hash`, which added 40–90s of latency to token aggregates. The corrected invariant — *parent usage includes downstream tool effects; child usage is a separate billing ledger; never sum across the two without a model-attribute gate* — is the one that must be encoded before any per-trace token accounting is trusted. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class UsageReconciliationError(RuntimeError): """Raised when a GenAI span's token accounting cannot be reconciled against its parent agent span under the semantic conventions.""" @dataclass class GenAISpan: span_id: str operation_name: str # "invoke_agent" | "execute_tool" | ... input_tokens: int = 0 output_tokens: int = 0 request_model: Optional[str] = None # marks a model-backed tool usage_is_estimate: bool = False children: list = field(default_factory=list) closed: bool = False def child_set_hash(children: list) -> str: h = hashlib.sha256() for c in sorted(children, key=lambda s: s.span_id): h.update(c.span_id.encode()) return h.hexdigest()[:16] def close_agent_span(parent: GenAISpan, policy: str = "deferred") -> str: if parent.operation_name != "invoke_agent": raise UsageReconciliationError( f"span {parent.span_id} is not an agent span; cannot reconcile" ) if parent.usage_is_estimate: # A pre-flight estimate cannot anchor reconciliation: the parent's # tokens may not include the downstream effect of tool output. raise UsageReconciliationError( f"parent {parent.span_id} carries estimated usage; " "reconciliation is undefined until actual usage is stamped at close" ) unmarked = [ c for c in parent.children if c.output_tokens > 0 and c.request_model is None ] if unmarked: if policy == "strict": raise UsageReconciliationError( f"child spans {[c.span_id for c in unmarked]} report output " "tokens without gen_ai.request.model; cannot distinguish " "model-backed tool from mis-instrumented deterministic tool" ) # deferred: stamp divergence, exclude from aggregates parent.closed = True return f"reconciliation=divergent unmarked={len(unmarked)}" open_children = [c for c in parent.children if not c.closed] if open_children and policy == "strict": raise UsageReconciliationError( f"parent {parent.span_id} closing before children " f"{[c.span_id for c in open_children]}; child set incomplete, " "monotonicity check would be vacuous" ) parent.closed = True return ( f"reconciliation=deferred child_set_hash={child_set_hash(parent.children)} " f"open_children={len(open_children)}" ) ```