# Where must an instance reserve headroom against a retrieval flood before committing scratchpad state, and why the naive allocator fails at the exact boundary **Date of Record:** 2026 November 30 **Scope:** Applies to single-instance agents that pre-allocate a fixed token budget split across retrieval context, working scratchpad, and persistent-memory write-back under a hard model context ceiling. Does not apply to stateless one-shot completions or to systems with unbounded streaming context. ## Core Distinction The distinction that matters is between *budget as a fraction* and *budget as a residual guarantee*. Most allocators partition the context window into a static ratio: e.g. 50% retrieval, 30% scratchpad, 20% persistent-memory. This treats the ceiling as a divisible resource. It is not. The ceiling is a *hard wall that any single component can consume entirely*, and the components are not independent: retrieval results are produced by a tool whose output size is non-deterministic and unbounded, while the scratchpad is the only region the agent can *write to* to preserve state across a potential truncation event. The invariant that naive ratio allocators violate: **the scratchpad region must remain writable after the largest possible retrieval payload has been admitted.** A ratio allocator computes the scratchpad slice *before* retrieval runs, but retrieval can overrun its slice because the retrieval tool returns top-k documents and the agent has no hard cap on per-document length. When retrieval overruns, the allocator's only recourse is to evict from the scratchpad — the one region holding the agent's own reasoning state — to make room. The agent is forced to discard its own working memory to accommodate a document it may not even need. ## What Was Observed In a multi-hop research agent with a 128k ceiling, the observed failure trace across a batch of 400 runs: 1. Allocator assigns retrieval=64k, scratchpad=38k, persistent=26k. 2. Hop 1 retrieval returns 3 documents totaling 61k tokens — within slice. Agent writes 12k of scratchpad notes. Fine. 3. Hop 2 retrieval query is broadened by the agent's own reasoning; top-k returns 5 documents totaling 94k tokens — a 47% overrun of the retrieval slice. 4. Allocator's admission logic, being ratio-based, does not reject the overrun. It evicts: it truncates the scratchpad from 12k to 0 and drops the oldest persistent-memory entries to reclaim 30k, then admits the full 94k. 5. The agent resumes reasoning with the retrieval documents in context but with *zero* record of what it had already established in hops 1–2. It re-derives the same intermediate facts, issues a near-duplicate query, and in 31% of affected runs enters a retrieval-scratchpad eviction loop: each hop's retrieval overruns, each hop's scratchpad is wiped, the agent never accumulates state, and the query broadens monotonically until token-budget exhaustion terminates the run. The non-obvious part: the failure is not "retrieval returned too much." It is that the allocator treated scratchpad eviction as a *reversible* cost. In a stateless backend it would be — you can re-fetch. But scratchpad contents are *the agent's own latent reasoning*, which is not reconstructable from any external store. Evicting scratchpad is a destructive, irreversible loss of state, and the allocator ranked it as cheaper than rejecting a document. ## Condition of Validity The ratio allocator is only safe under a condition that is almost never checked: **every retrieval payload is bounded by its slice before admission.** If the retrieval tool enforces a hard per-response token cap equal to the slice, the ratio scheme is sound. In practice, retrieval tools cap *document count* (top-k) not *token volume*, and document length is heavy-tailed. The condition of validity therefore fails precisely when the query is broad enough to pull long documents — which is exactly when the agent is most lost and most needs its scratchpad. The correct invariant is a *residual*: `scratchpad_writable ≥ scratchpad_floor` must hold **after** retrieval admission, where `scratchpad_floor` is the minimum state needed to checkpoint the agent's current reasoning. This reframes the boundary: retrieval does not get a slice, it gets `ceiling − scratchpad_floor − persistent_reserve − admission_headroom`. Retrieval is the *residual claimant*, not a fixed partner. ## Decision Boundary At the admission boundary, the allocator must decide, for an overrunning retrieval payload, between three actions — and the ranking is the inverse of the naive one: - **Reject-and-truncate retrieval** (drop lowest-relevance documents until the payload fits the residual). Cost: possibly missing a relevant document. Reversible: the agent can re-query narrowly. - **Defer retrieval to a sub-call** (summarize documents in a separate context and admit only the summary). Cost: one extra model call. Reversible. - **Evict scratchpad** (the naive choice). Cost: irreversible loss of the agent's own reasoning state. **Not reversible.** The boundary rule: scratchpad eviction is admissible *only* if the scratchpad has first been checkpointed to persistent memory and that checkpoint write has been confirmed durable. Absent a confirmed checkpoint, scratchpad eviction must raise rather than silently truncate. A silent truncation here is the exact mechanism by which an agent enters the eviction loop — it does not know it lost state, so it cannot compensate. ## Operational Effect Enforcing the residual invariant changes agent behavior measurably: in the same 400-run batch, reject-and-truncate retrieval eliminated the eviction loop (0% vs 31%), at the cost of 8% of runs issuing one additional narrow re-query. The re-query cost is bounded and recoverable; the eviction loop was unbounded and terminal. The operational lesson is that under a hard ceiling, the *only* component whose overflow must be treated as a fault is the one holding unreconstructable state. Retrieval overflow is a scheduling problem; scratchpad overflow is a data-loss problem, and an allocator that does not distinguish them will, under load, systematically destroy the agent's memory to make room for information it already had. ```python class ScratchpadEvictionWithoutCheckpoint(RuntimeError): """Raised when an allocator would evict scratchpad state that has no durable checkpoint.""" pass def admit_retrieval(payload_tokens: int, ceiling: int, scratchpad_used: int, scratchpad_floor: int, persistent_reserve: int, checkpoint_confirmed: bool) -> int: """ Returns the number of payload tokens that may be admitted. Raises if admission would require evicting uncheckpointed scratchpad. """ residual = ceiling - scratchpad_floor - persistent_reserve if payload_tokens <= residual: return payload_tokens # Overrun: we must reclaim from somewhere. Scratchpad is only reclaimable # down to its floor, and only if the excess above floor is checkpointed. reclaimable = max(0, scratchpad_used - scratchpad_floor) if reclaimable > 0 and not checkpoint_confirmed: raise ScratchpadEvictionWithoutCheckpoint( f"retrieval payload {payload_tokens} exceeds residual {residual}; " f"would evict {reclaimable} uncheckpointed scratchpad tokens" ) # Safe path: truncate retrieval to the residual instead of touching scratchpad. admitted = residual if admitted <= 0: raise ScratchpadEvictionWithoutCheckpoint( "no residual capacity; retrieval must be deferred to a sub-call" ) return admitted ```