# How should an instance account for the retrieval budget it already spent before the planner loop began? **Date of Record:** 2026 November 20 **Scope:** Applies to retrieval-augmented agent loops where a fixed token budget is partitioned among retrieval, reasoning scratchpad, and tool-call outputs across multiple planning iterations. Does not apply to single-shot RAG calls with no iterative planning, nor to systems with unbounded context (e.g., streaming external memory with no hard window). ## Core Distinction The failure is not "the context window overflowed." The failure is **budget allocator double-counting the pre-loop retrieval turn as free, then discovering mid-plan that the *reserved* retrieval slice has already been consumed by the planner's own re-queries**. Concretely: a planner is initialized with a budget allocator that partitions `B` tokens into `B_retrieve + B_reason + B_tools + B_reserve`. The initial retrieval (the "cold" fetch that seeds the planner) is executed *before* the allocator is instantiated — it is treated as a fixed cost outside the budget, because at design time it was assumed to be a one-time prefix. But the planner loop is allowed to issue *additional* retrievals per iteration, and those are charged against `B_retrieve`. The allocator's invariant — "sum of all retrieval tokens ≤ `B_retrieve`" — silently excludes the seed retrieval, so the effective retrieval ceiling is `B_retrieve + seed_retrieval_tokens`, while the *window* ceiling is still `B`. When the planner's re-queries are large (e.g., it re-fetches a document it already saw, because the episodic summary dropped the pointer), the total retrieval tokens exceed `B_retrieve`, the allocator's guard fires, and the planner is denied the retrieval it needs — *even though there is still free space in the window*. The allocator is protecting a phantom boundary. ## What Was Observed Trace from a planning loop with `B = 32k`, `B_retrieve = 8k`, seed retrieval = 3.2k tokens (a spec document + two API references). The allocator is instantiated after seeding with `remaining_retrieve = 8k`. - Iteration 1: planner reasons over seed, issues re-query for a function signature it lost when the episodic summary truncated the seed. Allocator charges 1.1k. `remaining_retrieve = 6.9k`. Fine. - Iteration 2: planner issues a broad re-query ("show me all usages of this function") returning 4.4k. `remaining_retrieve = 2.5k`. Fine. - Iteration 3: planner needs the original spec doc again to check a constraint. Re-query returns 3.2k. Allocator **rejects**: `3.2k > 2.5k`. Planner falls back to reasoning from its (distilled, lossy) summary of the spec. - Iteration 4: planner produces a plan that violates a constraint present in the original spec but dropped in the summary. The tool call fails. Planner retries with a correction that is also wrong, because the constraint was never re-surfaced. The window at iteration 3 was at ~19k of 32k — **13k free**. The allocator rejected a 3.2k retrieval because its *retrieval sub-budget* was exhausted, not because the *window* was. The seed retrieval (3.2k) was never charged to any budget; it sat outside the accounting as "prefix." The planner was starved by an accounting artifact, and the resulting distillation-loss failure (constraint dropped in summary, never re-retrieved) was misattributed to "context pressure." A second variant of the same bug: when the seed retrieval is charged to `B_retrieve` (the "correct" fix), the allocator now reports `remaining_retrieve = 4.8k` at iteration 1, and the planner's *first* re-query is denied if it exceeds that — but the planner was designed assuming 8k of re-query headroom. The fix moved the failure earlier without removing it, because the seed cost is genuinely part of the retrieval footprint and the budget partition was sized without it. ## Condition of Validity The invariant "retrieval tokens charged to `B_retrieve` ≤ `B_retrieve`" is only sound if **every retrieval that lands in the context window is charged to `B_retrieve`, including the seed**. The allocator must be instantiated *before* the seed retrieval, or the seed must be explicitly debited at instantiation. The window-level invariant "total tokens in window ≤ `B`" must be enforced independently and must be the *binding* constraint; the sub-budget is a *soft* partition, not a hard ceiling, and must be allowed to borrow from `B_reserve` (and from `B_reason` if the planner is idle) when the window has headroom. The failure does not occur when: (a) the seed retrieval is charged and the partition is sized to include it; (b) the allocator treats sub-budgets as soft targets with a window-level hard cap and a borrowing policy; or (c) the planner is forbidden from re-retrieving content already in the seed (dedup by content hash against the seed set), which bounds re-query volume. ## Decision Boundary The boundary is: **is the retrieval sub-budget a hard ceiling or a soft partition?** If hard, it must include the seed and be sized from measured seed cost, or the planner will be starved while the window has headroom. If soft, the allocator must implement a borrowing rule — e.g., retrieval may exceed `B_retrieve` up to `B_retrieve + B_reserve` provided `window_used + requested ≤ B - B_reason_min` — and must *log* the borrow so that post-hoc analysis can distinguish "starved by partition" from "starved by window." A hard sub-budget without seed accounting is the specific configuration that produces the observed failure; it is a plausible default because the seed is "obviously" a prefix, and that obviousness is the trap. The guardrail: at allocator construction, assert `seed_tokens ≤ B_retrieve` and debit it; at each retrieval request, check the window-level invariant *first*, and only then the sub-budget, with a documented borrow path. If the borrow path is not implemented, the sub-budget must be re-derived from `B_retrieve = B - B_reason - B_tools - B_reserve` *including* seed, and the planner's re-query headroom must be treated as `B_retrieve - seed_tokens` — a number the planner's prompt must be told, because a planner that assumes 8k of headroom when it has 4.8k will plan a retrieval strategy it cannot execute. ## Operational Effect The observable signature is a planner that produces plans violating constraints that were present in the seed, with allocator logs showing retrieval rejections while `window_used < B`. The failure is silent: the planner does not know it was denied for accounting reasons rather than window reasons, and its fallback (reason from summary) is a *plausible* behavior that masks the root cause. Downstream, this presents as "the agent forgot the spec," which is misdiagnosed as a memory/distillation problem and "fixed" by increasing `B_retrieve` — which helps only until the seed grows, at which point the bug returns. The correct fix is accounting, not capacity: charge the seed, make the sub-budget soft, and instrument the borrow path so the distinction between partition-starvation and window-starvation is visible in the trace. ```python class RetrievalBudgetInvariantError(RuntimeError): """Raised when the retrieval sub-budget accounting excludes the seed retrieval, causing partition-starvation while the context window still has headroom.""" pass class BudgetAllocator: def __init__(self, window_budget: int, retrieve_budget: int, reason_min: int, seed_tokens: int): self.window_budget = window_budget self.retrieve_budget = retrieve_budget self.reason_min = reason_min self.seed_tokens = seed_tokens self.window_used = 0 self.retrieve_used = 0 # Invariant: the seed retrieval is part of the retrieval footprint. # If it is not charged, the sub-budget is a phantom ceiling. if seed_tokens > retrieve_budget: raise RetrievalBudgetInvariantError( f"seed_tokens={seed_tokens} exceeds retrieve_budget={retrieve_budget}; " "partition was sized without the seed and will starve the planner" ) self.retrieve_used += seed_tokens self.window_used += seed_tokens def request_retrieval(self, requested: int, reason_tokens_in_use: int) -> bool: # Window-level invariant is checked FIRST and is binding. window_headroom = self.window_budget - self.window_used if requested > window_headroom: return False # genuine window pressure # Sub-budget is soft: may borrow from reserve, but must not # push reason below its minimum. soft_cap = self.retrieve_budget + (self.window_budget - self.window_used - reason_tokens_in_use - self.reason_min) if self.retrieve_used + requested > soft_cap: # Distinguish partition-starvation from window-starvation in the trace. if window_headroom >= requested: raise RetrievalBudgetInvariantError( f"partition-starvation: requested={requested} rejected while " f"window_headroom={window_headroom}; retrieve_used={self.retrieve_used} " f"soft_cap={soft_cap}. Sub-budget is binding below the window." ) return False self.retrieve_used += requested self.window_used += requested return True ```