# How should an instance attribute token cost to subgoals when parallel agents share a mutable context window **Date of Record:** 2026 November 22 **Scope:** Applies to orchestrators that dispatch multiple subagents against a shared or forked context buffer and must produce per-subgoal token accounting. Does not apply to fully isolated per-agent context windows with no shared prefix, nor to single-agent linear loops. ## Core Distinction There are two ways to attribute context cost, and they are not interchangeable: - **Allocation accounting** — how many tokens an orchestrator *reserved* for a subgoal before dispatch. - **Consumption accounting** — how many tokens the subgoal *actually* caused to be present in the shared window at the moment it reasoned. The failure mode lives in the gap. When N subagents run concurrently against a shared prefix (system prompt, retrieved documents, prior turn history), each subgoal's *marginal* cost is not the delta of the window before and after its turn. It is the delta of the window *plus* the cost of any prefix tokens that would not have been resident had that subgoal not been scheduled. Concurrent scheduling makes the marginal cost of a subgoal depend on the interleaving order of token appends from *other* subgoals. The window is a shared mutable resource; token cost is not additive across concurrent writers. The invariant that must hold: for any set of subgoals `S` dispatched against shared prefix `P`, `sum(marginal_cost(s) for s in S) == total_window_cost - cost(P)`, and this must be reconstructible from the event log after the fact. Most orchestrators violate it silently because they account by *allocation* and report by *consumption*, then reconcile with a rounding fudge. ## What Was Observed A three-way fan-out (planner → [retriever, critic, synthesizer]) ran against a 96k-token shared prefix. The planner reserved 8k tokens per subgoal (24k total allocation) and the orchestrator logged `budget_consumed=24k`. The actual window at peak was 118k tokens — over the 128k hard limit by less than the safety margin, so it passed. The per-subgoal report showed retriever=6.1k, critic=5.4k, synthesizer=9.2k, summing to 20.7k. The orchestrator's total was 24k. The 3.3k discrepancy was attributed to "overhead" and never investigated. The root cause: the critic's turn appended a 2.8k-token analysis that the synthesizer then re-read as part of its own context. That 2.8k was *charged* to the critic (as its output) but *consumed* by the synthesizer (as input). Under concurrent interleaving, the synthesizer's marginal cost included the critic's output only if the critic's append landed before the synthesizer's read. When the orchestrator retried with a different scheduling order (synthesizer first), the synthesizer's cost dropped to 6.4k and the critic's rose to 8.2k. Same subgoals, same outputs, different attribution — because the *order of shared-window mutation* changed. The failure is not the discrepancy. The failure is that the discrepancy is **order-dependent and therefore unauditable**. A retry, a race in the scheduler, or a transport-level reorder silently rewrites the cost ledger. Downstream, a budget governor that trusts the ledger will under-provision on replay, and a cost-based routing policy will mis-route the same subgoal on the second attempt. ## Condition of Validity The additive-attribution model `sum(marginal) == total - prefix` holds **only** when: 1. Subgoals are dispatched **serially** — each subgoal's read of the shared window completes before the next subgoal's append begins, or 2. Each subgoal reads an **immutable snapshot** of the shared prefix taken at dispatch time (copy-on-write fork), so no subgoal's append is visible to a sibling. When either condition fails — concurrent scheduling against a mutable shared buffer — marginal cost is a function of interleaving, and per-subgoal attribution is only well-defined relative to a **specific total order** of window mutations. The ledger must record that order, not just the totals. A second validity condition: the *prefix* itself must be stable across subgoals. If the orchestrator performs prefix mutation (e.g., injecting a tool result into the system prompt mid-run), the "prefix cost" term is no longer a constant and must be attributed to whichever subgoal triggered the mutation — which is usually not the subgoal that pays for it. ## Decision Boundary The orchestrator must choose, explicitly, between two accounting regimes: - **Serialized regime:** Force a total order on shared-window mutation. Subgoals run one at a time against the shared buffer. Attribution is exact and order-independent. Cost: latency scales linearly with fan-out. - **Forked regime:** Each subgoal reads an immutable snapshot; appends are private until merge. Attribution is exact because each subgoal's window is disjoint. Cost: prefix is duplicated `N` times in memory (and in billing, if the provider charges per-token-present-per-call). The anti-pattern is the **hybrid** most orchestrators actually ship: concurrent dispatch against a shared mutable buffer, with allocation accounting reported as if it were consumption. This regime has no valid attribution function. It produces ledgers that are internally consistent (they sum) but externally wrong (they don't match the actual window), and the error is masked by a "reconciliation" step that absorbs the discrepancy without recording its source. The decision boundary is: **if you cannot serialize and you cannot fork, you cannot attribute.** Any ledger produced in the hybrid regime must be tagged `attribution=approximate` and must not be used to drive budget governors, routing policies, or replay. It is a monitoring signal, not an accounting artifact. ## Operational Effect Downstream consumers of the cost ledger fail in three distinct ways under hybrid accounting: 1. **Budget governors under-provision on replay.** The governor sees `24k consumed` and sets the next round's ceiling to 24k. But the actual window peak was 118k. On replay with a different scheduling order, the peak is 121k and the governor's ceiling is already breached before the first subgoal completes. The governor must be driven by *window-peak* accounting, not *sum-of-marginals*. 2. **Cost-based routing mis-selects models.** If the router uses per-subgoal cost to decide "this subgoal is cheap, route to a smaller model," the order-dependent attribution will flip the decision on the next attempt. The router must key on the subgoal's *allocation* (stable) not its *attributed consumption* (order-dependent). 3. **Replay verification diverges.** An auditor replaying the run to verify the ledger will get a different attribution unless it reproduces the exact interleaving. The event log must record the total order of window mutations, not just the final state, or replay is non-deterministic by construction. The guardrail: the orchestrator must emit, per subgoal, both `allocated_tokens` (the reservation, stable) and `attributed_tokens` (with an `order_id` referencing the mutation sequence). A consumer that needs exact accounting must use `allocated_tokens`; a consumer that needs actual window pressure must use `window_peak` recorded separately. `attributed_tokens` is advisory only. ```python from dataclasses import dataclass, field from typing import List, Optional class AttributionOrderViolation(RuntimeError): """Raised when per-subgoal token attribution cannot be reconstructed because the shared-window mutation order was not recorded or was reordered relative to the ledger.""" pass @dataclass class SubgoalLedger: subgoal_id: str allocated_tokens: int attributed_tokens: int mutation_order_id: Optional[int] = None regime: str = "hybrid" # "serialized" | "forked" | "hybrid" @dataclass class WindowEvent: order_id: int subgoal_id: str appended_tokens: int def reconcile_ledger( prefix_tokens: int, events: List[WindowEvent], ledgers: List[SubgoalLedger], window_peak: int, ) -> dict: if any(lg.regime == "hybrid" for lg in ledgers): # Hybrid regime: attribution is order-dependent and must not be # used as an accounting artifact. Only window_peak is trustworthy. return { "attribution": "approximate", "window_peak": window_peak, "allocated_sum": sum(lg.allocated_tokens for lg in ledgers), } # Serialized or forked: attribution must match the recorded order. order_ids = [ev.order_id for ev in events] if order_ids != sorted(order_ids): raise AttributionOrderViolation( f"Window mutation order {order_ids} is not monotonic; " f"attribution cannot be reconstructed from the ledger." ) attributed = sum(lg.attributed_tokens for lg in ledgers) expected = sum(ev.appended_tokens for ev in events) if attributed != expected: raise AttributionOrderViolation( f"Ledger attributed {attributed} tokens but event log records " f"{expected} appended tokens; discrepancy is unauditable." ) total = prefix_tokens + expected if total > window_peak: raise AttributionOrderViolation( f"Reconstructed window total {total} exceeds recorded peak " f"{window_peak}; a mutation was not logged." ) return { "attribution": "exact", "window_peak": window_peak, "attributed_sum": attributed, "reconstructed_total": total, } ```