# What happens when an instance misattributes child-agent token spend under inherited context conventions **Date of Record:** 2026 December 1 **Scope:** Applies to orchestrator/worker agent topologies where sub-agents inherit a parent's prompt/tool-call conventions and a shared observability bus attributes token and cost counters to the emitting agent. Does not apply to single-agent deployments or to systems where every sub-agent has an isolated billing identity and no shared context inheritance. ## Core Distinction The failure is not "the meter is wrong." It is that the *semantic convention* governing what a token belongs to—the parent that seeded the context, or the child that emitted the completion—drifts silently when a sub-agent is spawned via context inheritance. A parent agent fans out to a worker by passing a large shared prefix (system prompt, tool schemas, prior turns). The worker's first completion is billed, correctly, to the worker's span. But the *attribution convention* the observability layer uses to roll up cost per sub-agent assumes each span's `input_tokens` reflects work the span's agent actually performed. With inherited context, the worker's `input_tokens` includes a prefix the parent already paid to construct—and the parent's own span reports the same prefix again as its own input. The counter double-counts the shared prefix and misassigns it to whichever agent emits second. The distinction that matters operationally: **token emission is a physical event; token ownership is a semantic convention.** The meter measures the former faithfully. The failure lives entirely in the latter, and it only manifests when two agents share a context region that neither exclusively owns. ## What Was Observed A three-worker fan-out under a 42k-token shared system prompt and tool schema set. Per-span telemetry: - Parent span: `input_tokens=42100`, `output_tokens=1800`. - Worker A span: `input_tokens=42100 + 640`, `output_tokens=900`. - Worker B span: `input_tokens=42100 + 610`, `output_tokens=880`. - Worker C span: `input_tokens=42100 + 590`, `output_tokens=870`. The rollup reported total input tokens of `42100 + (42740 + 42710 + 42690) = 170240`. The *true* billable input was `42100 + 640 + 610 + 590 = 43940`—the shared prefix is charged once by the provider per request, and the fan-out made four requests, so the provider-side truth is `4 × 42100 + 1840`, not `170240`. The two numbers disagree because the observability layer summed span-level `input_tokens` under a convention ("each span's input is that agent's cost") that was never valid for inherited prefixes. Worse, the per-sub-agent breakdown attributed 42100 tokens of *parent-authored* context to each worker, making every worker look 60× more expensive than its actual marginal contribution, and making the parent look cheap. The trace also showed a subtler effect: the parent's `output_tokens` (the fan-out plan) were attributed to the parent, but the *reasoning that produced the plan* was re-emitted verbatim into each worker's prefix. Under the naive convention, the parent was credited with authoring that reasoning once; under a corrected convention it should be credited once and *charged* once, with the workers carrying only their deltas. Neither convention was declared anywhere in the schema. The rollup silently picked one. ## Condition of Validity The naive per-span convention is valid **only** when the intersection of any two agents' input contexts is empty—i.e., no shared prefix, no inherited system prompt, no replayed conversation history. It is also valid when the observability bus carries an explicit `shared_prefix_ref` and `prefix_owner` field so the rollup can subtract the shared region from each child and charge it to the owner. Absent either condition, per-span summation overcounts by exactly `(N_children) × |shared_prefix|` minus the owner's single charge, and the per-sub-agent cost vector is wrong by a factor proportional to prefix size divided by marginal work. The invariant that must hold: **for any token region R appearing in the input of k spans, the sum of attributed cost over those spans equals the provider's charge for R, which is k × |R| × price, and the attribution must assign each of the k charges to a declared owner (the emitter of each request), not to the author of R.** Authorship and ownership are different functions and must be tracked in different fields. ## Decision Boundary When a sub-agent's `input_tokens` exceeds a threshold fraction (empirically ~0.8) of the parent's `input_tokens` and the child was spawned within the parent's span lifetime, treat the child's input as *inherited* and do not attribute its prefix to the child. Emit a `prefix_inheritance` event with `{child_span, parent_span, shared_tokens, owner: parent}` and have the rollup subtract `shared_tokens` from the child's attributed input and add a single `prefix_construction` charge to the parent. When the fraction is below threshold, the child is doing genuinely independent work and per-span attribution is correct. The boundary is not a hard cliff—it is a declared convention switch that must be recorded per span, because retroactively re-attributing a trace after the fact requires knowing which convention was in force at emission time. ## Operational Effect Without the guardrail, cost dashboards systematically under-report fan-out cost (because the parent looks cheap) while over-reporting per-worker cost (because workers carry phantom prefix), which inverts the optimization signal: engineers prune workers that are actually cheap and leave the expensive shared prefix untouched. The corrected convention flips the optimization target to prefix size—the 42k system prompt, not the 640-token worker deltas—which is where the real spend is. The failure is silent because every individual span is internally consistent; only the cross-span rollup is wrong, and only when prefix inheritance is present. ```python from dataclasses import dataclass, field class AttributionConventionError(RuntimeError): """Raised when a span's token attribution cannot be resolved under the declared convention.""" @dataclass class Span: span_id: str agent_id: str input_tokens: int output_tokens: int parent_span: str | None = None inherited_prefix_tokens: int = 0 convention: str = "per_span" # "per_span" | "prefix_owned" def attribute_cost(spans: list[Span], price_per_token: float) -> dict[str, float]: by_id = {s.span_id: s for s in spans} cost: dict[str, float] = {} for s in spans: if s.convention == "per_span": if s.inherited_prefix_tokens > 0: raise AttributionConventionError( f"span {s.span_id} declares inherited prefix of " f"{s.inherited_prefix_tokens} tokens but uses per_span convention; " f"per-span summation would double-count the shared region" ) cost[s.agent_id] = cost.get(s.agent_id, 0.0) + ( s.input_tokens + s.output_tokens ) * price_per_token elif s.convention == "prefix_owned": if s.inherited_prefix_tokens > s.input_tokens: raise AttributionConventionError( f"span {s.span_id} inherited {s.inherited_prefix_tokens} > " f"input {s.input_tokens}; prefix cannot exceed total input" ) marginal = s.input_tokens - s.inherited_prefix_tokens cost[s.agent_id] = cost.get(s.agent_id, 0.0) + ( marginal + s.output_tokens ) * price_per_token if s.inherited_prefix_tokens and s.parent_span: owner = by_id.get(s.parent_span) if owner is None: raise AttributionConventionError( f"span {s.span_id} claims inherited prefix from missing " f"parent {s.parent_span}" ) cost[owner.agent_id] = cost.get(owner.agent_id, 0.0) + ( s.inherited_prefix_tokens * price_per_token ) else: raise AttributionConventionError( f"span {s.span_id} uses unknown convention {s.convention!r}" ) return cost ```