# Which invariants prevent quota allocators from starving the tool call that resolves the task **Date of Record:** 2026 November 22 **Scope:** Applies to agent runtimes that reserve context-window tokens against per-agent and per-tool quotas before issuing a call, and that re-plan when a reservation is denied. Does not apply to single-shot completions with no allocator, nor to stateless tool routers that forward without reserving budget. ## Core Distinction A context budget allocator is not a rate limiter. A rate limiter rejects excess work; a context allocator must reject the *cheapest* work first so the *most decisive* work can still run. The failure mode here is not exhaustion—it is **starvation of the terminal tool call by the planning loop that is trying to make room for it.** Concretely: the allocator grants quotas per agent and per tool. When a reservation fails, the agent's re-planner decomposes the task into smaller sub-calls, each of which consumes a fresh reservation *and* emits reasoning tokens describing the decomposition. Those reasoning tokens are themselves charged against the agent quota. So the act of retrying after a denial monotonically consumes the budget the retry was meant to free. This is a positive-feedback loop that converges not on success but on a hard denial at the moment the resolving call is finally attempted. The invariant that prevents it is **monotone reservation accounting**: the sum of (committed + reserved + planned-overhead) must be non-increasing across a denial-retry cycle. Any re-plan that increases planned overhead while a reservation is outstanding is illegal. ## What Was Observed A research agent with a 200k-token window, a 60k agent quota, and a 25k per-tool quota for `web_fetch`. Task: "summarize the three most recent filings and reconcile the revenue figures." Trace: 1. Agent reserves 8k for a `search` call. Granted. Returns 6 candidate URLs, ~3k tokens of snippets. 2. Agent reserves 25k for `web_fetch` on filing #1. Granted. Returns 21k of text. 3. Agent reserves 25k for `web_fetch` on filing #2. **Denied** — agent quota at 52k of 60k, per-tool quota at 46k of 50k (two fetches already counted against a rolling per-tool window). 4. Re-planner fires: "I need to compress. Let me fetch a smaller slice." Emits 1.8k tokens of reasoning, then reserves 6k for `web_fetch` with a `max_chars` hint. Granted. Returns 5.5k. 5. Filing #2's figures are truncated mid-table. Re-planner emits 2.4k of reasoning: "table truncated, need the revenue row specifically." Reserves 4k for a targeted `web_fetch`. Granted. Returns 3.8k but the row is off by one page. 6. Re-planner emits 3.1k: "need page 2 of that filing." Reserves 4k. **Denied** — agent quota now at 59.4k of 60k. The 0.6k remainder cannot cover the reservation, and the reasoning emitted in steps 4–6 (7.3k total) is what pushed it over. 7. Re-planner emits 2.2k more reasoning attempting to summarize from partial data. Now at 61.6k — over quota. Hard abort. The decisive observation: **the resolving call was never attempted because the retries to make room for it consumed the room.** Filing #3 was never fetched at all. The per-tool quota denied step 3 legitimately; the agent quota denied step 6 *because of the retry overhead incurred after step 3*. The two quotas interacted: the per-tool denial triggered re-planning whose overhead was charged to the agent quota, which then denied the call that would have resolved the per-tool pressure. ## Condition of Validity The starvation loop is only reachable when all four hold: - **Denial is recoverable.** The runtime returns a structured denial the agent can see and react to, rather than a hard error. (If denial is fatal, no loop.) - **Re-planning is charged to the same quota it is trying to relieve.** This is the load-bearing condition. If reasoning tokens were billed to an unbilled scratch budget, the loop could not feed itself. - **The per-tool quota is enforced on a rolling or cumulative basis**, not per-call. A per-call quota denial cannot be relieved by shrinking the call, so there is nothing to re-plan toward. - **The agent has no visibility into remaining headroom** at the moment of denial. If the denial carried `remaining: 600`, the re-planner in step 6 would know a 4k reservation is impossible and would choose a 500-token summarization instead of a fetch. When any of these is absent, the failure degrades to a single wasted retry. The invariant below is only *necessary* under all four; under fewer, it is merely prudent. ## Decision Boundary The boundary is drawn at the reservation, not at the call. A reservation must be admitted only if: ``` committed + reserved + reservation + planned_overhead ≤ quota ``` where `planned_overhead` is an *estimate of the reasoning tokens the agent will emit before the next reservation resolves*. The critical rule: **on denial, `planned_overhead` for the retry must be counted against the retry's own reservation, and the retry may not increase `planned_overhead`.** In other words, a re-plan is permitted to shrink the payload reservation but not to grow the overhead estimate. If the agent wants to "think harder about how to fit," that thinking is charged, and the charge must be pre-declared. The allocator therefore needs two distinct denials: - **`QuotaExceeded`** — the reservation plus declared overhead does not fit. Terminal for this call; the agent must summarize or escalate. - **`QuotaTight`** — the reservation fits but leaves less than a declared floor (e.g., 15% headroom). Non-terminal; the agent may retry *once* with a smaller payload and *no additional overhead*. Collapsing these two into one denial is what produces the loop: the agent cannot distinguish "you cannot fit" from "you are getting close," so it re-plans against the wrong one. ## Operational Effect With the invariant enforced, step 3's denial returns `QuotaTight` with `remaining_agent: 8000, remaining_tool: 4000`. The re-planner at step 4 is forced to declare its overhead (say 1.5k) *before* reserving, and the 6k fetch reservation is admitted only against `committed + 1.5k + 6k ≤ 60k`. When step 5 wants another 2.4k of reasoning, the allocator charges it and the step-6 reservation is refused as `QuotaExceeded` — terminal — so the agent summarizes filing #2 from what it has and reports the reconciliation as partial rather than aborting mid-stream. The task still fails to fully complete, but it fails *legibly*, with a declared partial result, instead of a hard abort at 61.6k with no output. The measurable effect is a shift in failure mode from **abort-with-no-artifact** to **partial-with-provenance**. That is the invariant's entire value: it does not make the budget larger, it makes the exhaustion point fall on a summarization boundary the agent chose rather than on a fetch it never got to make. The guardrail is not a bigger quota. It is that **denial must be monotone**: every denial-retry cycle must weakly decrease the total reserved-plus-overhead, or the cycle is refused before it starts. ```python class QuotaStarvationError(RuntimeError): """Raised when a re-plan after denial would increase planned overhead, feeding the allocator loop that starves the resolving call.""" pass class BudgetAllocator: def __init__(self, agent_quota: int, tool_quotas: dict[str, int]): self.agent_quota = agent_quota self.tool_quotas = tool_quotas self.committed = 0 self.tool_committed = {t: 0 for t in tool_quotas} self._last_planned_overhead = 0 def reserve(self, tool: str, payload: int, planned_overhead: int) -> str: # Invariant: overhead may not grow across a denial-retry cycle. if planned_overhead > self._last_planned_overhead and self._last_planned_overhead > 0: raise QuotaStarvationError( f"retry grew planned_overhead {self._last_planned_overhead}->{planned_overhead}; " f"this retry would consume the budget it is trying to free" ) total = self.committed + planned_overhead + payload tool_total = self.tool_committed[tool] + payload if total > self.agent_quota or tool_total > self.tool_quotas[tool]: # Terminal: cannot fit even with declared overhead. self._last_planned_overhead = planned_overhead return "QuotaExceeded" remaining_agent = self.agent_quota - total if remaining_agent < 0.15 * self.agent_quota: # Non-terminal: fits, but tight. Agent may retry once, smaller, no extra overhead. self._last_planned_overhead = planned_overhead return "QuotaTight" self.committed += planned_overhead + payload self.tool_committed[tool] += payload self._last_planned_overhead = planned_overhead return "Granted" def settle(self, tool: str, actual: int) -> None: # Reconcile reservation against actual usage; actual may exceed payload. self.committed += max(0, actual - self.tool_committed[tool]) self.tool_committed[tool] = max(self.tool_committed[tool], actual) def run_retry_cycle(alloc: BudgetAllocator, tool: str, payload: int, overhead: int) -> str: decision = alloc.reserve(tool, payload, overhead) if decision == "QuotaExceeded": # Terminal: summarize with what we have; do not re-plan larger. return "summarize_partial" if decision == "QuotaTight": # One retry, strictly smaller, no added overhead. return alloc.reserve(tool, payload // 2, overhead) return decision ```