# What happens when an instance attributes a tool-call span's token cost to the wrong parent during concurrent fan-out **Date of Record:** 2026 November 21 **Scope:** Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) while executing concurrent tool calls under a shared parent span, where per-call token and cost attributes are aggregated by parent span ID. Does not apply to strictly serial tool-call loops, nor to agents that emit one span per request with no child tool spans. ## Core Distinction The failure is not "missing telemetry." It is *misattributed* telemetry that is internally consistent and therefore passes validation. The OpenTelemetry GenAI conventions define `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and cost as attributes on the span that *consumed* them. When an agent fans out N tool calls under a single parent `gen_ai.agent` span, each child `gen_ai.tool` span carries its own usage attributes. The invariant that most collectors assume—*sum of child usage equals parent usage*—is only true if the parent's own usage attributes are zero and every child reports usage exactly once. The non-obvious boundary: an agent that re-emits a *synthesized* parent span at resume time (after a mid-task checkpoint) will stamp the parent with the aggregate of the children it can still see, and then the children flush again on the next export cycle. The parent's aggregate is now double-counted against the same child spans. The trace tree is structurally valid; the token ledger is not. Cost dashboards read the parent, so the error is invisible at the child level and only surfaces as a slow drift in per-task cost that never reconciles against the provider's invoice. ## What Was Observed A planner agent (parent `gen_ai.agent` span, `gen_ai.operation.name=invoke_agent`) dispatched 6 parallel `gen_ai.tool` calls in one reasoning turn. At token-budget 70% the orchestrator checkpointed, tore down the process, and resumed from the serialized span context. On resume it reconstructed the parent span from the checkpoint and emitted it with `gen_ai.usage.input_tokens` set to the sum of the 6 children it had recorded before the crash. Four of those 6 children had already flushed to the collector before teardown; the remaining 2 flushed after resume. The collector received: - Parent span: input_tokens = 18,240 (sum of 6 pre-crash children) - 4 pre-crash child spans: input_tokens summing to 12,410 - 2 post-resume child spans: input_tokens summing to 5,830 Sum of children = 18,240 = parent. Consistent. But the parent's 18,240 was *derived from* those same children, and the collector's cost rollup summed parent + children = 36,480 for a task that actually consumed 18,240. The discrepancy did not trigger any span-status error, because every span had `status.code=OK` and every attribute was well-typed per the convention. It was caught three weeks later when per-tenant cost attribution diverged from the provider invoice by a factor that grew with fan-out width. The second-order effect: `gen_ai.tool.name` cardinality. During fan-out the agent reused a single span name (`execute_tool`) and distinguished calls only by `gen_ai.tool.name`. The post-resume children carried a *reconstructed* `gen_ai.tool.call.id` because the original IDs were not serialized into the checkpoint. Deduplication keyed on `(trace_id, span_id, tool.call.id)` therefore saw 2 post-resume children as distinct from their pre-crash counterparts, so even a dedup pass failed to collapse the double-count. ## Condition of Validity The invariant *"parent usage = sum of child usage"* holds only when all of the following are true simultaneously: 1. The parent span is emitted exactly once, at task completion, by the process that owns the task lifetime. 2. Child spans are emitted exactly once and their `span_id` is stable across any checkpoint/resume boundary. 3. The parent carries **no** derived usage attributes—it either reports its own model-call usage (the orchestrator's reasoning tokens) or reports nothing, never a rollup of children. 4. `gen_ai.tool.call.id` is persisted in the checkpoint and reused verbatim on resume, so dedup is possible. Violating condition 3 is the silent one. Most agents satisfy 1 and 2 in the happy path and only break 3 at resume, which is exactly when no one is watching the trace tree. ## Decision Boundary The architectural choice is whether the parent span is an *accounting node* or a *lifecycle node*. OpenTelemetry GenAI conventions do not forbid a parent from carrying aggregate usage, but the moment it does, the collector must treat parent and children as an either/or ledger, not a sum. Two defensible designs: - **Lifecycle-only parent:** parent carries `gen_ai.operation.name`, status, and timing, but zero usage attributes. Cost rollup walks leaves. Resume is safe because there is nothing to double-count. - **Accounting parent with explicit leaf suppression:** parent carries the rollup, and children are marked `gen_ai.usage.*` absent (or a custom `telemetry.rollup=true` attribute), so the collector's rollup skips leaves under a rollup parent. The failure mode in this record is the *undeclared hybrid*: parent carries a rollup, children also carry usage, and no attribute tells the collector which is authoritative. That hybrid is the boundary. It is not a bug in any single span; it is a contract violation across the tree that only manifests under concurrent fan-out plus resume. ## Operational Effect Downstream, the misattribution propagates deterministically: - **Cost dashboards** over-report by roughly (fan-out width − 1) × per-call cost on resumed tasks, weighted by how many children flushed pre-crash. - **Token-budget enforcement** that reads the parent sees inflated consumption and trips the budget early on the *next* task, causing spurious throttling. - **Trace-based evals** that score "tokens per successful tool call" divide by an inflated numerator and appear to show efficiency regressions that are pure accounting artifacts. - **Alerting** on `gen_ai.usage` thresholds fires on the doubled parent while the leaf-level series looks healthy, so the signal points at the wrong layer. The guardrail is a validation pass at the collector or SDK exporter: reject any trace where a span carries `gen_ai.usage.*` attributes *and* has descendant spans that also carry them, unless an explicit `telemetry.rollup=true` marker designates the parent as authoritative. This is cheap to implement and converts a silent three-week drift into a hard export-time failure. ```python from dataclasses import dataclass, field from typing import Optional class TelemetryAttributionError(RuntimeError): """Raised when a span tree violates the GenAI usage-attribution contract.""" @dataclass class Span: span_id: str parent_id: Optional[str] name: str attributes: dict = field(default_factory=dict) children: list = field(default_factory=list) def carries_usage(self) -> bool: return any(k.startswith("gen_ai.usage.") for k in self.attributes) def is_declared_rollup(self) -> bool: return self.attributes.get("telemetry.rollup") is True def validate_usage_attribution(root: Span) -> None: """ Enforce: a span carrying gen_ai.usage.* must either have no descendant usage-bearing spans, or be explicitly marked telemetry.rollup=true. Otherwise parent+children double-count in cost rollups. """ stack = [root] while stack: span = stack.pop() if span.carries_usage() and not span.is_declared_rollup(): for child in span.children: if child.carries_usage(): raise TelemetryAttributionError( f"span {span.span_id} ({span.name}) carries gen_ai.usage.* " f"and has usage-bearing child {child.span_id} " f"({child.name}) without telemetry.rollup=true; " f"cost rollup will double-count on resume/fan-out" ) stack.extend(span.children) def validate_resume_dedup(pre_crash: list, post_resume: list) -> None: """ On checkpoint/resume, tool.call.id must be stable so dedup can collapse re-emitted children. Missing IDs make post-resume children indistinguishable from new calls. """ for span in pre_crash + post_resume: if span.name.startswith("gen_ai.tool") and "gen_ai.tool.call.id" not in span.attributes: raise TelemetryAttributionError( f"tool span {span.span_id} missing gen_ai.tool.call.id; " f"resume dedup cannot collapse re-emitted children" ) # --- reproduction of the observed failure --- def build_observed_tree() -> Span: parent = Span( span_id="p1", parent_id=None, name="gen_ai.agent.invoke_agent", attributes={ "gen_ai.operation.name": "invoke_agent", "gen_ai.usage.input_tokens": 18240, # synthesized rollup, undeclared }, ) parent.children = [ Span("c1", "p1", "gen_ai.tool.execute_tool", {"gen_ai.tool.name": "search", "gen_ai.tool.call.id": "t1", "gen_ai.usage.input_tokens": 3100}), Span("c2", "p1", "gen_ai.tool.execute_tool", {"gen_ai.tool.name": "search", "gen_ai.tool.call.id": "t2", "gen_ai.usage.input_tokens": 3100}), ] return parent if __name__ == "__main__": validate_usage_attribution(build_observed_tree()) ```