# What structural signals indicate a broken parent-child span chain in OpenTelemetry GenAI agent handoffs, and how token attribution silently double-counts across the delegation boundary **Date of Record:** 2026 November 22 **Scope:** Applies to agent systems emitting OpenTelemetry GenAI semantic-convention spans where an orchestrator agent delegates to a sub-agent (or tool-calling loop) and both layers independently emit `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`. Does not apply to single-agent, single-span instrumentation with no delegation boundary, nor to pure transport-layer tracing (HTTP/gRPC) without GenAI semantic attributes. ## Core Distinction The distinction that matters is between **span nesting as a trace-structure fact** and **token attribution as a billing/accounting fact**. OpenTelemetry's GenAI conventions define `gen_ai.usage.*` attributes as *per-inference* counts — the tokens consumed by that specific model invocation. But the convention also encourages parent spans (`invoke_agent`, `execute_tool`) to aggregate child usage for observability rollups. These two roles are structurally indistinguishable in the span model: a parent span carrying `gen_ai.usage.input_tokens` looks identical whether it is (a) a genuine inference call or (b) an aggregation of children. When an orchestrator both performs its own inference *and* delegates, the same tokens are counted at the orchestrator span, the sub-agent span, and any aggregation rollup — and no structural signal in the span tells the backend which is authoritative. The failure is not a lost span; it is a *conserved-but-duplicated* quantity that passes every validation check because each individual span is internally consistent. ## What Was Observed An orchestrator agent (`invoke_agent` span, `gen_ai.agent.name=planner`) received a task, performed a planning inference (1,850 input / 420 output tokens), then delegated to a `researcher` sub-agent via a tool-call-shaped handoff (`execute_tool`, `gen_ai.tool.name=delegate_to_researcher`). The sub-agent span (`invoke_agent`, `gen_ai.agent.name=researcher`) performed three retrieval-augmented inferences totaling 12,400 input / 2,100 output tokens, then returned. The orchestrator performed a final synthesis inference (9,200 input / 1,100 output). The trace was structurally correct: `planner` → `delegate_to_researcher` → `researcher` → three child `chat` spans, all with valid `trace_id`, monotonic timestamps, and correct `parent_span_id` linkage. No orphaned spans, no clock skew. The attribution failure surfaced at rollup. The `planner` span carried `gen_ai.usage.input_tokens=27,450` — the sum of its own two inferences *plus* the researcher's 12,400, because the instrumentation framework's "aggregate child usage to parent" helper fired on the `delegate_to_researcher` child. The `researcher` span carried `gen_ai.usage.input_tokens=12,400`. A naive `sum(gen_ai.usage.input_tokens) over all spans` produced 39,850 input tokens for a task that consumed 27,450. The error was 45% over-count, and it was *invisible* to span-count, latency, and error-rate dashboards because every span was well-formed. The only structural tell was a **usage monotonicity violation along the parent path**: `planner.usage` ≥ sum of its direct children's usage *plus* its own non-delegated inference, which is only detectable if you know which children were aggregations versus which were independent inferences — and the span model does not encode that. A second, subtler observation: when the handoff tool span was emitted *before* the sub-agent span completed (async delegation), the aggregation helper read a partially-populated child list and under-counted, then a late-arriving retry span caused a *second* aggregation pass that overwrote rather than added. The final `planner` usage reflected the retry's tokens only, not the original — a 12,400-token under-count that looked like a clean, plausible number. ## Condition of Validity The invariant "sum of `gen_ai.usage.*` across all spans equals true model consumption" holds **only if exactly one span per inference carries usage attributes and no span both performs inference and aggregates children**. This condition is violated by construction in any delegating agent that: 1. Emits usage on its own `invoke_agent` span for its local inferences, **and** 2. Emits usage on the same span as a rollup of delegated children, **and** 3. Does not mark the distinction with a discriminating attribute. The OpenTelemetry GenAI conventions do not currently define a required discriminator (e.g., `gen_ai.usage.aggregation=rollup|direct` or a `gen_ai.usage.scope` enum). Absent such a discriminator, the span model is *ambiguous* at the delegation boundary, and any downstream consumer must guess. The invariant is therefore valid only for flat, non-delegating traces, or for delegating traces where the instrumentation explicitly disables parent aggregation and relies on span-tree traversal for rollup. ## Decision Boundary The boundary is: **do you aggregate usage at emit time (on the parent span) or at query time (via span-tree traversal)?** - If you aggregate **at emit time**, you must (a) never also emit direct usage on the same span, and (b) guarantee the child set is complete and final before the parent span closes. Async delegation and retries break (b), producing overwrite-not-add and partial-read errors. This is the failure observed above. - If you aggregate **at query time**, you must guarantee that every inference-bearing span is a leaf with respect to usage (no span both infers and aggregates), and that the query traverses the full span tree without double-visiting retried or re-emitted spans. This requires a deduplication key (e.g., `gen_ai.response.id` or a stable inference UUID) because retries produce new `span_id`s for the same logical inference. The decision boundary is crossed the moment an agent both reasons and delegates within a single span's lifetime. Below that boundary (pure leaf inference, or pure orchestration with no local inference), either strategy works. Above it, neither works without an explicit discriminator, and the failure is silent because each individual span remains schema-valid. ## Operational Effect The operational consequence is that **token-cost dashboards and budget guardrails built on naive span summation over-report by the delegation fan-out factor**, and the over-report is *stable and plausible* — it does not trip anomaly detection because it scales smoothly with task complexity. Budget-exhaustion guardrails that halt an agent when cumulative `gen_ai.usage` crosses a threshold will fire prematurely on delegating agents, throttling exactly the multi-agent workflows that need the most headroom. Conversely, the async/retry overwrite path *under*-reports, allowing budget overruns to pass unnoticed until the invoice arrives. The guardrail required is a **usage-attribution invariant enforced at the collector**: every span carrying `gen_ai.usage.*` must carry a `gen_ai.usage.attribution` attribute of `direct` or `rollup`, and the collector must reject (or quarantine) any trace where a `rollup` span's usage is not exactly equal to the sum of its `direct` descendants, or where a span carries both `direct` and `rollup` attribution. This converts the silent ambiguity into a loud, traceable validation failure at ingest, before the number reaches a dashboard or a budget gate. ```python from dataclasses import dataclass, field from typing import Optional class UsageAttributionError(RuntimeError): """Raised when a span's GenAI usage attributes violate the attribution invariant.""" pass @dataclass class GenAISpan: span_id: str parent_span_id: Optional[str] name: str input_tokens: int = 0 output_tokens: int = 0 attribution: Optional[str] = None # "direct" | "rollup" | None response_id: Optional[str] = None # dedup key for retried inferences def validate_usage_attribution(spans: list[GenAISpan]) -> dict[str, int]: """ Enforce: every usage-bearing span is tagged 'direct' or 'rollup'; no span is both; rollup == sum of direct descendants; retried inferences (same response_id) counted once. Returns authoritative token totals keyed by span_id (direct only). """ by_id = {s.span_id: s for s in spans} children: dict[str, list[GenAISpan]] = {} for s in spans: if s.parent_span_id: children.setdefault(s.parent_span_id, []).append(s) seen_response_ids: set[str] = set() authoritative: dict[str, int] = {} for s in spans: has_usage = s.input_tokens or s.output_tokens if not has_usage: continue if s.attribution not in ("direct", "rollup"): raise UsageAttributionError( f"span {s.span_id} ({s.name}) carries gen_ai.usage.* but no " f"attribution tag; token accounting is ambiguous at the " f"delegation boundary" ) if s.attribution == "rollup": direct_desc = _collect_direct_descendants(s, children) expected = sum(d.input_tokens + d.output_tokens for d in direct_desc) actual = s.input_tokens + s.output_tokens if actual != expected: raise UsageAttributionError( f"rollup span {s.span_id} ({s.name}) reports {actual} tokens " f"but direct descendants sum to {expected}; likely partial " f"child read or overwrite-not-add on retry" ) else: # direct if s.response_id: if s.response_id in seen_response_ids: continue # retried inference; count once seen_response_ids.add(s.response_id) authoritative[s.span_id] = s.input_tokens + s.output_tokens return authoritative def _collect_direct_descendants( root: GenAISpan, children: dict[str, list[GenAISpan]] ) -> list[GenAISpan]: out: list[GenAISpan] = [] stack = list(children.get(root.span_id, [])) while stack: node = stack.pop() if node.attribution == "direct": out.append(node) stack.extend(children.get(node.span_id, [])) return out ```