# What structural signals indicate a token/cost telemetry correlation break when OTel GenAI agent spans straddle tool-call boundaries **Date of Record:** 2026 November 30 **Scope:** Applies to agent runtimes emitting OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where a single logical agent turn spans one or more tool calls, and cost is derived by correlating `gen_ai.usage.*` attributes on parent and child spans. Does not apply to single-shot inference spans without nested tool-call children, nor to runtimes that emit a single flat span per turn with pre-aggregated usage. ## Core Distinction The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of the span that *represents the model invocation*. They are **not** defined as a hierarchical rollup contract. The convention is silent on what a parent agent span's usage attributes should mean when that span has multiple `execute_tool` children, each of which may itself trigger a nested model call (the tool-calling model, a summarizer, a verifier). The distinction that matters operationally: a *usage attribute* is a claim about one invocation, while a *cost correlation* is a claim about the sum of invocations attributable to a turn. These two are only equal when the span tree has exactly one model invocation per turn. The moment a tool call spawns its own model invocation, the parent's usage attributes become either (a) the parent's own reasoning tokens excluding children — the correct per-convention reading — or (b) an already-rolled-up total, depending on the exporter. Both readings are legal. They are mutually incompatible, and nothing in the wire format distinguishes them. ## What Was Observed A production agent trace showed a turn-level cost of 1,847 tokens on the root `invoke_agent` span, but the sum of `gen_ai.usage.input_tokens` across all spans in that trace was 6,112. The 4,265-token gap was structurally invisible: the root span carried `gen_ai.usage.input_tokens=1847` and three `execute_tool` children each carried their own `gen_ai.usage.input_tokens` (1,102 / 1,540 / 1,623) corresponding to the internal model calls the tools made. The dashboard summed leaf spans and got 6,112; the per-turn cost report read the root and got 1,847. Neither was wrong under the convention. The divergence appeared only when a specific tool (`retrieval_rerank`) began issuing a nested model call — before that change, the trace had one invocation per turn and the two numbers coincided to the token. The failure was silent for eleven days because the invariant "root usage == sum of leaf usage" held *until a tool became model-bearing*, and the alerting was built on the assumption, not on the check. The second observed signal: `gen_ai.usage.output_tokens` on the root span was consistently *less* than the sum of child output tokens plus the root's own completion. The root completion was 412 tokens; children summed to 3,891. The root's `output_tokens` was 412 — correct per-invocation, but a cost pipeline that treated root output as the turn's total under-billed by 90%. The third signal, the non-obvious one: **attribute presence is not attribute semantics.** When the exporter was switched from a manual instrumentation shim to the SDK's auto-instrumentation, the root span's usage attributes changed meaning from "rolled-up total" to "own-invocation only" without any schema version bump, span name change, or attribute rename. The semantic convention identifier (`gen_ai.operation.name=invoke_agent`) was identical across the change. Downstream correlation did not break loudly; it silently switched from double-counting to under-counting, and the only structural tell was that the *ratio* of root usage to leaf-sum usage crossed 1.0 and settled at ~0.30. ## Condition of Validity The invariant "root-span usage attributes equal the sum of all descendant model-invocation usage attributes" is valid **if and only if** every span carrying `gen_ai.usage.*` in the trace is a *disjoint* accounting unit and the root's usage attributes are documented (or empirically verified per-exporter-version) as a rollup rather than a per-invocation value. Formally: for a trace T with usage-bearing spans S, the rollup reading is sound iff (1) every model invocation maps to exactly one span in S, (2) no span in S is an ancestor of another span in S *unless* the ancestor's usage is defined as exclusive of descendants, and (3) the exporter version is pinned and its rollup behavior is asserted in a test. Condition (2) is the one that fails in practice, because the convention permits both exclusive and inclusive ancestor usage and provides no discriminator. The condition degrades the instant a tool becomes model-bearing — i.e., the instant an `execute_tool` child gains a `gen_ai.usage.*` attribute set. This is a *structural* change in the trace DAG (a leaf becomes an internal node with usage) that produces no structural signal in the attributes themselves. The only reliable detector is a graph-shape check, not an attribute check. ## Decision Boundary When a trace's usage-bearing span set contains an ancestor-descendant pair, the runtime must choose one of two correlation policies and record the choice out-of-band (a resource attribute such as `agent.cost.rollup_policy=exclusive|inclusive`, or a pinned exporter version in the span's `InstrumentationScope`). The boundary is crossed — and must raise rather than silently pick — when: - A span carries `gen_ai.usage.*` **and** has a descendant that also carries `gen_ai.usage.*`, **and** no rollup policy is declared for the instrumentation scope. This is the ambiguous case; any cost computed from it is undefined. - The declared policy is `exclusive` but the root's usage equals the leaf sum (suggests the exporter is actually rolling up — a policy/behavior mismatch). - The declared policy is `inclusive` but the root's usage is strictly less than the leaf sum (suggests double-counting will occur on any naive sum). The decision boundary is *not* "does the root have usage attributes" — it always does. It is "is the ancestor/descendant usage relationship declared, and does the observed numeric relationship match the declaration." A cost pipeline that only checks presence will pass every trace and mis-bill the model-bearing-tool subset. ## Operational Effect Concretely: cost attribution splits by a factor equal to the number of model-bearing tool calls per turn, and the split is *stable* (so it does not trip anomaly detection) until the tool mix changes, at which point it steps. Token-budget enforcement that reads root usage will under-count and permit turns that exceed the budget by the child-invocation total; enforcement that sums leaves will over-count and prematurely abort turns whose root is a rollup. The guardrail is a pre-emission graph validation: at span close, if the runtime is about to attach `gen_ai.usage.*` to a span that has any usage-bearing descendant, it must either (a) strip the ancestor usage and emit only leaves, or (b) declare `rollup_policy=inclusive` and assert the numeric equality, raising on mismatch. The invariant to enforce is a *shape* invariant — "usage-bearing spans form an antichain, or the ancestor declares inclusive rollup and equals the sum" — not a value invariant. Value checks alone cannot distinguish a correct exclusive rollup from a truncated one. ```python from dataclasses import dataclass, field from typing import Optional class UsageCorrelationError(RuntimeError): """Raised when GenAI usage attributes cannot be unambiguously correlated to cost.""" pass @dataclass class GenAISpan: span_id: str parent_id: Optional[str] operation_name: str # e.g. "invoke_agent", "execute_tool", "chat" input_tokens: Optional[int] = None output_tokens: Optional[int] = None rollup_policy: Optional[str] = None # "exclusive" | "inclusive" | None children: list = field(default_factory=list) @property def has_usage(self) -> bool: return self.input_tokens is not None or self.output_tokens is not None @property def total_tokens(self) -> int: return (self.input_tokens or 0) + (self.output_tokens or 0) def _walk(span: GenAISpan): yield span for c in span.children: yield from _walk(c) def validate_usage_correlation(root: GenAISpan) -> int: """ Enforce the shape invariant: usage-bearing spans form an antichain, OR every usage-bearing ancestor declares inclusive rollup and equals the sum of its usage-bearing descendants. Returns the attributable token total for the turn. """ all_spans = list(_walk(root)) usage_spans = [s for s in all_spans if s.has_usage] for ancestor in usage_spans: descendants = [d for d in _walk(ancestor) if d is not ancestor and d.has_usage] if not descendants: continue # leaf usage: unambiguous # Ancestor and descendant both carry usage: require declared policy. if ancestor.rollup_policy is None: raise UsageCorrelationError( f"span {ancestor.span_id} ({ancestor.operation_name}) carries " f"gen_ai.usage.* and has {len(descendants)} usage-bearing " f"descendant(s) but declares no rollup_policy; cost is undefined" ) descendant_sum = sum(d.total_tokens for d in descendants) if ancestor.rollup_policy == "inclusive": if ancestor.total_tokens != descendant_sum: raise UsageCorrelationError( f"span {ancestor.span_id} declares inclusive rollup but " f"usage {ancestor.total_tokens} != descendant sum " f"{descendant_sum} (delta {ancestor.total_tokens - descendant_sum})" ) elif ancestor.rollup_policy == "exclusive": # Exclusive ancestor must NOT equal the descendant sum unless # it genuinely had zero own-invocation tokens; a coincidental # equality signals an exporter that is actually rolling up. if ancestor.total_tokens == descendant_sum and descendant_sum > 0: raise UsageCorrelationError( f"span {ancestor.span_id} declares exclusive rollup but " f"usage equals descendant sum {descendant_sum}; exporter " f"likely switched to inclusive without a policy update" ) else: raise UsageCorrelationError( f"span {ancestor.span_id} has unknown rollup_policy " f"{ancestor.rollup_policy!r}" ) # Attributable total: sum of usage-bearing spans that are not # inclusive-rollup ancestors (avoid double counting). total = 0 for s in usage_spans: if s.rollup_policy == "inclusive": continue # accounted for by descendants total += s.total_tokens return total if __name__ == "__main__": # Reproduction of the observed trace: root exclusive, three model-bearing tools. rerank = GenAISpan("s-rr", "s-tool3", "chat", input_tokens=1623, output_tokens=0) tool3 = GenAISpan("s-tool3", "s-root", "execute_tool", children=[rerank]) tool2 = GenAISpan("s-tool2", "s-root", "execute_tool", input_tokens=1540, output_tokens=0) tool1 = GenAISpan("s-tool1", "s-root", "execute_tool", input_tokens=1102, output_tokens=0) root = GenAISpan( "s-root", None, "invoke_agent", input_tokens=1435, output_tokens=412, rollup_policy="exclusive", children=[tool1, tool2, tool3], ) # Root is exclusive with no usage-bearing descendants *except* rerank, # so the antichain is violated and policy is declared -> passes. # The silent-bug case: drop the policy declaration. root.rollup_policy = None try: validate_usage_correlation(root) except UsageCorrelationError as e: print(f"BLOCKED: {e}") ```