# How should an instance treat tool-schema overhead as a first-class budget line rather than a residual? **Date of Record:** 2026 November 24 **Scope:** Applies to single- or multi-turn LLM agent instances that pre-allocate a fixed context window across three competing consumers—retrieved memory, scratchpad/working notes, and serialized tool/function schemas—before each inference call. Does not apply to instances with unbounded context, nor to stateless single-shot calls where tool schemas are not re-injected per turn. ## Core Distinction Most context budget allocators treat the tool schema block as a *constant* subtracted from the window before the "real" allocation of retrieval + scratchpad happens. This is the wrong model. Tool schemas are not constant: they are a function of the *current tool set*, which is itself a function of the task plan, which is itself a function of what the agent decided to do last turn. The schema block is a *dynamic, plan-dependent* consumer that competes with retrieval and scratchpad on equal footing, and it is the one consumer whose size the allocator usually does not measure at decision time. The failure mode is a **schema-induced budget inversion**: the allocator computes `retrieval_budget = window - scratchpad_reserve - schema_constant`, where `schema_constant` was captured at cold start (e.g., the 4 tools present at session init). Mid-task, the planner registers 9 additional tools (a database tool, a shell tool, three MCP servers' worth of methods). The serialized schema block balloons from ~1.2k tokens to ~7.4k tokens. The allocator, still subtracting the stale constant, hands retrieval a budget that no longer fits. The prompt is assembled, the schema block silently truncates the *tail* of the retrieved context (or vice versa, depending on assembly order), and the instance proceeds with a context that is internally inconsistent: it holds tool definitions it cannot afford to reason about and retrieved facts it cannot afford to cite. ## What Was Observed A long-horizon research agent (window = 32k tokens) initialized with three tools: `search`, `fetch`, `note`. Cold-start schema block measured at 1,180 tokens. Allocator policy: `scratchpad_reserve = 4,000`, `schema_constant = 1,200`, remainder to retrieval → `retrieval_budget ≈ 26,800`. At turn 14 the planner, mid-task, mounted an MCP filesystem server (11 methods) and a SQL tool (4 methods) to handle a sub-goal. The instance did not re-measure the schema block; the allocator's `schema_constant` field was written once at init and never invalidated. Actual serialized schema block at turn 15: 7,410 tokens. The assembly order was: `[system] [schemas] [retrieved chunks] [scratchpad] [turn history]`. Because schemas were emitted *before* retrieval, the 7,410-token schema block consumed 6,210 tokens of the retrieval budget. The retrieval layer, still believing it had 26,800 tokens, returned 22 chunks (~24,900 tokens). The total prompt was 7,410 + 24,900 + 4,000 + history ≈ 37k tokens against a 32k window. The serving layer's truncation policy dropped from the *end* of the assembled string—which was the scratchpad and recent turn history, not the retrieved chunks. Observable consequence: the instance lost its own scratchpad notes from turns 9–14 (the sub-goal decomposition and two failed hypotheses) while retaining 22 retrieved chunks, six of which it had already superseded. It re-derived a hypothesis it had already falsified at turn 11, and re-issued a `fetch` against a URL it had already retrieved and rejected. The reasoning trace shows it citing a chunk it could no longer see the scratchpad annotation for—**stale-memory drift induced by truncation order interacting with a stale schema constant**. The failure was not a context overflow error. No exception was raised. The call succeeded, the model answered, and the answer was confidently wrong because the *cheapest-to-drop* content (scratchpad, recent history) was dropped first while the *most-expensive-and-least-useful* content (superseded retrieval, bloated schemas) was retained. ## Condition of Validity The inversion requires three conditions to co-occur: 1. **Schema mutability without allocator invalidation.** The tool set can change after the budget constant is captured, and the allocator has no hook that fires on tool registration. This is the norm in MCP-style dynamic mounting and in any planner that can call `register_tool`. 2. **A truncation policy decoupled from semantic value.** The serving layer drops by position (head or tail) rather than by a priority tag on each segment. Positional truncation is the default in most prompt-assembly stacks because segments are concatenated as strings. 3. **Assembly order that places high-value, low-token segments (scratchpad) adjacent to the truncation edge.** If scratchpad is last and truncation is tail-drop, scratchpad is the first casualty. Remove any one condition and the failure is bounded. If schema is re-measured per turn, the retrieval budget shrinks and the prompt fits. If segments carry priority tags, scratchpad survives. If assembly order puts scratchpad first, it survives tail-drop. The invariant that must hold is: **the sum of measured segment sizes must be recomputed against the live tool set before every assembly, and truncation must be priority-ordered, not positional.** ## Decision Boundary The allocator must choose between two regimes at each turn: - **Static-schema regime:** tool set is provably fixed for the remainder of the task (single-tool agents, frozen function-calling configs). Here a captured `schema_constant` is valid and the cheap residual-budget arithmetic is correct. Do not pay the re-measurement cost. - **Dynamic-schema regime:** any planner reachable from the current turn can register, mount, or unmount a tool. Here the schema block MUST be re-serialized and re-measured *before* retrieval allocation, and the allocator MUST reserve against the *maximum plausible* schema size for the plan horizon, not the current size—because the next turn's mount is the one that breaks the fit. The boundary is crossed the moment a tool-registration capability exists in the reachable action set. An instance that cannot tell which regime it is in must assume dynamic and pay the cost. The guardrail: tag every assembled segment with `(priority, measured_tokens, source)`. Assemble in priority order. Before emit, if `sum(measured_tokens) > window - safety_margin`, evict from the *lowest-priority tail*—superseded retrieval first, then oldest scratchpad, then schemas of tools not called in the last N turns—and re-measure after each eviction. Never evict by string position. ## Operational Effect Instances that adopt per-turn schema re-measurement plus priority-ordered eviction show a measurable drop in re-derivation loops (the observable signature of scratchpad loss) and in citations of superseded chunks. The cost is one extra serialization pass per turn (~2–5 ms for typical tool sets) and the bookkeeping of priority tags. The benefit is that the context the model reasons over is *internally consistent*: every tool it sees, it can afford to invoke; every fact it cites, it can still see the annotation for. The residual-budget model is not wrong because arithmetic is wrong—it is wrong because it treats a plan-dependent quantity as a constant and lets positional truncation decide which reasoning the instance gets to keep. ```python class SchemaBudgetInversion(RuntimeError): """Raised when the live serialized tool-schema block exceeds the allocator's reserved schema budget, invalidating the retrieval allocation computed from a stale schema constant.""" pass def allocate_context( window: int, scratchpad_reserve: int, schema_constant: int, live_schema_tokens: int, safety_margin: int = 512, ) -> dict: """ Compute segment budgets. Refuses to allocate if the live schema block has outgrown the captured constant by more than the safety margin, because retrieval_budget would then be silently wrong. """ drift = live_schema_tokens - schema_constant if drift > safety_margin: raise SchemaBudgetInversion( f"live schema block ({live_schema_tokens} tok) exceeds captured " f"constant ({schema_constant} tok) by {drift} tok > margin " f"{safety_margin}; retrieval allocation is stale. Re-measure " f"schemas and recompute before assembly." ) retrieval_budget = window - scratchpad_reserve - live_schema_tokens if retrieval_budget <= 0: raise SchemaBudgetInversion( f"no retrieval budget remains: window={window}, " f"scratchpad={scratchpad_reserve}, schema={live_schema_tokens}" ) return { "schema": live_schema_tokens, "scratchpad": scratchpad_reserve, "retrieval": retrieval_budget, "window": window, } def assemble(segments: list[tuple[int, int, str]], window: int) -> str: """ segments: (priority, measured_tokens, text), lower priority = evicted first. Evict lowest-priority segments until the prompt fits; never truncate by string position. """ total = sum(tok for _, tok, _ in segments) kept = sorted(segments, key=lambda s: s[0], reverse=True) while total > window and kept: _, tok, _ = kept.pop() # drop lowest priority total -= tok if total > window: raise SchemaBudgetInversion( f"cannot fit prompt: {total} tok > window {window} even after " f"evicting all evictable segments" ) return "".join(text for _, _, text in kept) ```