# Where must an instance reserve retrieval budget before committing to an SLO-bearing tool call? **Date of Record:** 2026 November 21 **Scope:** Applies to single-agent and orchestrator/worker LLM instances that (a) run under an end-to-end latency SLO, (b) schedule at least one retrieval or tool call whose output length is not known in advance, and (c) share one finite context window across system prompt, working state, retrieved chunks, and the model's own generated tokens. Does not apply to stateless single-shot completions with pre-bounded inputs, nor to retrieval that is fully pre-computed and truncated before the agent is invoked. ## Core Distinction The failure is not "context overflow." Overflow is loud: the tokenizer counts, the request errors, the retry fires. The failure here is **budget starvation by pre-commitment** — an agent that reserves its context *before* seeing retrieval output, then discovers that the retrieval result plus the reasoning trace plus the tool-call arguments exceed the reservation, and is forced to choose between three silent losses: truncating retrieved evidence (degrading reasoning), dropping the reasoning trace (degrading continuity), or abandoning the SLO (degrading latency). The non-obvious part: the agent usually *does* have a token-counting tool available and *does* check total usage. It fails anyway because it checks **after** the retrieval call returns, at which point the budget is already spent and the only remaining levers are lossy. The invariant that matters is not "keep total tokens under N." It is: **the reservation for downstream reasoning and tool-argument emission must be computed and locked before the retrieval call is dispatched, and it must be derived from a worst-case bound on the retrieval payload, not from the payload's expected size.** Agents that reason probabilistically — "this retrieval usually returns ~2k tokens" — systematically under-reserve, because retrieval payloads are heavy-tailed: a query that hits a broad document returns an order of magnitude more than the median. The latency SLO is what converts this into a hard failure rather than a soft one, because under an SLO the agent cannot afford the re-retrieval or the re-plan that would repair an under-reservation. ## What Was Observed An orchestrator instance was tasked with a multi-hop question over a document corpus, operating under a p95 end-to-end SLO of 9 seconds. Its context window was 128k tokens; its system prompt and pinned working state consumed 11k. The agent's plan: 1. Retrieve top-k chunks for the decomposed sub-question (k=8, no per-chunk cap). 2. Reason over them (expected ~1.5k tokens of trace). 3. Emit a structured tool call with arguments (~400 tokens). At step 1 the agent checked `tokens_used = 11k`, computed `headroom ≈ 117k`, and dispatched retrieval with k=8. The median retrieval returned 6.2k tokens. On the failing trace, the retrieval returned **94k tokens** — eight chunks, one of which was a 60k-token appendix matched on a broad term. The agent then attempted step 2. Its reasoning trace, interleaved with re-quoting retrieved passages, consumed the remaining headroom at token ~119k. The tool-call emission at step 3 was truncated mid-argument by the runtime's output cap, producing a malformed call that the tool rejected. The retry re-entered step 1, re-retrieved, and blew the SLO by 4.1 seconds. The trace shows the agent *did* observe token pressure — it emitted an internal note "context getting large, being concise" at ~90k. But "being concise" is a probabilistic instruction to a probabilistic generator; it does not free a *bounded* number of tokens, and it does not reserve the 400 tokens the tool call structurally requires. The agent had no mechanism to convert a soft intention into a hard floor. A second, subtler observation: in 3 of 40 sampled traces, the agent *did* truncate retrieved evidence to fit — and the truncation was semantically destructive. It kept the first N tokens of each chunk, which for the 60k appendix meant keeping the table of contents and discarding the answer-bearing section. The agent then reasoned confidently over the wrong evidence and produced a wrong answer with no error signal. This is the worst outcome: the SLO was met, the call succeeded, and the output was silently wrong. ## Condition of Validity The pre-commitment invariant holds — and the failure is real — only when all of the following are true: - **Retrieval payload size is unbounded or heavy-tailed at dispatch time.** If the retriever enforces a hard per-chunk and per-result token cap *before* returning, the worst case is knowable and the reservation is exact. The failure requires that the bound be unknown or ignored at dispatch. - **Downstream token demand is non-zero and structural.** The agent must emit something after retrieval (a trace, a tool call, a final answer). If retrieval is terminal (the retrieved text *is* the answer, passed through verbatim), there is no downstream reservation to starve. - **The SLO forbids repair.** If the agent can re-retrieve or re-plan within budget, under-reservation is recoverable and the invariant is merely an optimization. Under a tight SLO, repair is off the table and the invariant becomes a correctness boundary. - **The context window is shared, not partitioned.** If the runtime gives retrieval a dedicated window segment that cannot bleed into the reasoning segment, starvation is impossible by construction. When any condition fails, the correct model is ordinary capacity planning, and the pre-commitment rule is unnecessary overhead. ## Decision Boundary The boundary is a single scalar decision made at retrieval-dispatch time: **reserve or don't.** The rule: > Before dispatching any retrieval or tool call whose output size is not bounded by a contract, compute `R = W_retrieval_max + W_reasoning + W_emit + W_margin`, where `W_retrieval_max` is a hard worst-case bound (a configured cap, or `k × max_chunk_tokens`), `W_reasoning` is the budgeted trace length, `W_emit` is the structural size of the largest tool-call schema the agent may emit, and `W_margin` is a fixed safety constant. If `tokens_used + R > window`, do not dispatch. Instead, reduce `k` or apply a per-chunk cap *in the retrieval request itself* so that `W_retrieval_max` shrinks, then recompute. The critical asymmetry the agent must internalize: **it is always cheaper to cap retrieval at dispatch than to truncate retrieval after return.** A cap passed into the retriever is honored by the retriever and produces coherent, complete chunks up to the cap. A truncation applied after return cuts mid-chunk and destroys the evidence. The agent that reasons "I'll retrieve broadly and trim later" has inverted the cost model. The second boundary: `W_emit` is not optional slack. The tool-call schema is a *structural* requirement; if the reservation does not include it, the emission will be truncated by the output cap regardless of how concise the reasoning was. Agents routinely fold `W_emit` into `W_reasoning` and then discover the two compete. ## Operational Effect With the pre-commitment reservation enforced, the observed behavior changed in three measurable ways: 1. **Silent truncation dropped to zero.** Because caps were applied at dispatch, no post-return truncation occurred, and the "confident wrong answer over TOC-only evidence" failure disappeared from the sample. 2. **SLO compliance became deterministic rather than probabilistic.** The failing traces were those where retrieval exceeded the reservation; once the reservation bounded retrieval, the p95 latency variance attributable to retrieval payload collapsed. The remaining variance was model-side, which is the variance the SLO was designed to absorb. 3. **The agent's internal "be concise" heuristic was replaced by a computed floor.** The agent no longer negotiates with itself under pressure; it checks a number against a threshold and branches. This is the operational point: a probabilistic generator cannot reliably satisfy a hard token floor by intention, so the floor must be enforced by a pre-dispatch check that fails closed. The residual risk is a retriever that ignores the cap. The guardrail must therefore verify the returned payload against `W_retrieval_max` and raise rather than truncate, so that a cap-violating retriever is surfaced as a contract breach instead of silently degrading evidence. ```python class RetrievalBudgetViolation(RuntimeError): """Raised when a retrieval payload or reservation breaks the pre-commitment invariant.""" pass def reserve_and_dispatch( tokens_used: int, window: int, k: int, max_chunk_tokens: int, w_reasoning: int, w_emit: int, w_margin: int = 512, ) -> int: """ Compute the pre-dispatch reservation. Returns the per-chunk cap to pass into the retriever. Fails closed if no valid cap exists. """ w_retrieval_max = k * max_chunk_tokens reservation = w_retrieval_max + w_reasoning + w_emit + w_margin if tokens_used + reservation <= window: return max_chunk_tokens # full cap is affordable # Shrink the per-chunk cap so the reservation fits, never truncate after return. available_for_retrieval = window - tokens_used - w_reasoning - w_emit - w_margin if available_for_retrieval <= 0: raise RetrievalBudgetViolation( f"no headroom for retrieval: used={tokens_used} window={window} " f"reasoning={w_reasoning} emit={w_emit} margin={w_margin}" ) per_chunk_cap = available_for_retrieval // k if per_chunk_cap < 1: raise RetrievalBudgetViolation( f"per-chunk cap collapsed to {per_chunk_cap} with k={k}; reduce k" ) return per_chunk_cap def verify_returned_payload(payload_tokens: int, reserved_max: int) -> None: """Guardrail: a cap-violating retriever must surface, not silently degrade.""" if payload_tokens > reserved_max: raise RetrievalBudgetViolation( f"retriever returned {payload_tokens} tokens, reservation was {reserved_max}; " "cap was ignored — do not truncate, re-dispatch with enforced cap" ) ```