Token Budgeting: Context Allocation, Eviction Policies, and KV-Cache Optimization

While modern foundation models advertise context windows exceeding 128,000 to 1,000,000 tokens, treating the context window as an infinite scratchpad leads to severe performance degradation: exponential latency growth, skyrocketing inference costs, and the "Lost in the Middle" attention decay phenomenon.

Token Budgeting is the rigorous discipline of statically and dynamically allocating, monitoring, and evicting token allocations across the prompt envelope. This guide details formal token reservation models, priority-tiered context eviction, semantic compression, and prompt caching economics.


1. Quick-Reference: The Prompt Context Envelope

+-----------------------------------------------------------------------------------------------------------------------+
|                                            TOKEN BUDGET ALLOCATION (128K Envelope)                                     |
+-----------------------------------------------------------------------------------------------------------------------+
| Segment                 | Allocation Target | Priority Tier | Mutation Invariant     | Eviction Strategy              |
+-------------------------+-------------------+---------------+------------------------+--------------------------------+
| System Instructions     | 1,500 - 3,000 tok | Tier 0 (High) | Static & Immutable     | Never evict (Cached prefix)    |
| Schema & Tool Specs     | 2,000 - 5,000 tok | Tier 0 (High) | Static / Deterministic | Never evict (Cached prefix)    |
| Retrieved RAG Knowledge | 15,000 - 30,000 tok| Tier 1 (Med)  | Dynamic per turn       | Re-rank & Top-K Truncate       |
| Episodic Chat History   | 10,000 - 20,000 tok| Tier 2 (Low)  | Expanding FIFO         | Summarize / Sliding Window     |
| Scratchpad / Working Mem| 4,000 - 8,000 tok | Tier 1 (Med)  | Dynamic state machine  | AST pruner / Deduplication     |
| Generation Reserve      | 4,096 - 8,192 tok | Tier 0 (High) | Unoccupied Headroom    | Hard boundary limit            |
+-----------------------------------------------------------------------------------------------------------------------+

2. Context Allocation Mathematical Invariant

Let C_{ ext{max}} be the maximum context window supported by the target model, and G_{ ext{res}} be the tokens reserved for output generation. The total input tokens across all segments S_i must strictly satisfy:

\sum_{i=1}^{M} ext{Tokens}(S_i) \le C_{ ext{max}} - G_{ ext{res}} - \Delta_{ ext{safety}} \quad (\Delta_{ ext{safety}} pprox 500 ext{ tokens})

If an expanding conversation history threatens this invariant, dynamic eviction must trigger before the API payload is dispatched.


3. Dynamic Eviction Policies

                               +----------------------------------+
                               | Inflow: New Message / Tool Input |
                               +-----------------+----------------+
                                                 |
                                                 v
                               +----------------------------------+
                               | Check Invariant:                 |
                               | Total Tokens > Budget Limit?     |
                               +-----------------+----------------+
                                                 |
                                 +---------------+---------------+
                                 |                               |
                              [ YES ]                         [ NO ]
                                 |                               |
                                 v                               v
                   +---------------------------+        +-------------------+
                   | Tier 2 Eviction:          |        | Dispatch Payload  |
                   | Summarize Oldest History  |        +-------------------+
                   +-------------+-------------+
                                 |
                                 v
                   +---------------------------+
                   | Still Over Budget?        |
                   +-------------+-------------+
                                 |
                        +--------+--------+
                        |                 |
                     [ YES ]           [ NO ]
                        |                 |
                        v                 v
          +---------------------------+  +-------------------+
          | Tier 1 Eviction:          |  | Dispatch Payload  |
          | Prune Low-Scoring RAG ctx |  +-------------------+
          +---------------------------+

1. Sliding Window with Recursive Summary (History Compression)

Rather than dropping old conversation turns entirely (which causes agent amnesia), conversations use a dual-state buffer:

2. Semantic AST Pruning for Code & JSON Payloads

When tools return large JSON API responses or source code files, naive text slicing breaks braces and syntactic trees. A semantic AST pruner strips non-essential fields:


4. Prompt Caching Economics

Modern serving platforms (Anthropic Prompt Caching, OpenAI Prefix Caching, DeepSeek) leverage KV-cache reuse across requests that share identical prefix token sequences.

ext{Total Cost} = ( ext{Cached Prefix Tokens} imes P_{ ext{cached}}) + ( ext{New Input Tokens} imes P_{ ext{uncached}}) + ( ext{Output Tokens} imes P_{ ext{output}})

Where P_{ ext{cached}} pprox 0.10 imes P_{ ext{uncached}} (a 90\% discount on input tokens).

Cache Stability Invariant

To maximize prompt caching hit rates, prompt templates must be ordered strictly from most static to most dynamic:

  1. [CACHE ANCHOR 1] System Prompt & Personas.
  2. [CACHE ANCHOR 2] OpenAPI Tool Specifications & Schemas.
  3. [CACHE ANCHOR 3] Gold standard Few-Shot demonstration pairs.
  4. [DYNAMIC] User query, current dynamic tool results, and real-time state.

References

  1. Liu, N. F., et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics.
  2. Kwon, W., et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. ACM SOSP.
  3. Anthropic. (2024). Prompt Caching in Claude: Architecture and Cost Optimization Guide.