Full text not available for this paper
PACE: Keeping the Right Tokens in the KV Cache Is Not Enough for Long-Horizon LLM Agents
Author: Suman Chatterjee (Amazon Web Services)
Summary (Overview)
- Core finding: In long-horizon LLM agent sessions under a fixed KV cache budget, keeping the right tokens resident is necessary but not sufficient for correct behavior. Even at residency rates above 0.99, accuracy degrades as the rotary-position distance between needed tokens and the query grows.
- PACE (Planner-Aware Context Engine): A KV-cache engine combining (1) plan-aware retention—using the agent planner's dependency graph to predict which context future steps will read—with (2) bounded positions—renumbering kept tokens so their rotary distance to the query stays below a limit , regardless of session length.
- Results: At 100K tokens with a 2,048-token budget (2% of session), PACE passes 97% of hidden unit tests vs. 42% for H₂O and 3% for SnapKV. At 1M and 2M tokens on a single 24 GB GPU, PACE passes 99% and 100% of checks respectively, with KV state under 0.7 GB where the full cache would need 147–295 GB.
- Both components required: Retention-only reaches 63% and positions-only reaches 63%; the combination reaches 97% (), demonstrating that keeping and retrieving are distinct problems.
- Renumbering is exact: Re-rotation of keys costs no measurable perplexity and adds only 9% wall time.
Introduction and Theoretical Foundation
Background
LLM agents operate in sessions—multi-step interactions where each step appends instructions, retrieved material, and model outputs. Sessions of – tokens are routine for repository-scale coding or long tool-use traces. The KV cache grows linearly with session length: for Qwen3-8B (grouped-query attention), each token costs 144 KiB, so 100K tokens = 14.7 GB and 1M tokens = 147 GB—far beyond a single 24 GB GPU.
The Problem: Keeping ≠ Retrieving
Existing eviction policies (H₂O, SnapKV, StreamingLLM, agent-aware systems) share one objective: when a later step needs a token, that token should still be cached. The paper shows this objective, even when met perfectly, does not deliver correct behavior:
At residency 0.996, accuracy falls from 93% to 64% as the gap between a definition and its use grows from 2K to 90K tokens.
Rotary Position Embeddings (RoPE)
Most open LLMs use rotary embeddings. A query at position and key at position are rotated by angles and , so their dot product depends on positions only through the difference:
Eviction removes keys but leaves survivors' positions untouched, so in long sessions the query's position keeps growing while kept definitions stay where they were written. The rotary gap (distance in physical rotary positions from query to needed span) grows with session length.
Problem Statement
Let step issue its query at logical position (seen at physical rotary position ), with the set of earlier tokens it needs. Define:
- Residency: (fraction of needed tokens resident in cache )
- Rotary gap: (distance in physical rotary positions)
Eviction policies maximize . This paper shows is insufficient: accuracy still falls with .
Methodology
1. Plan-Aware Retention
Blueprint: Before each step, the agent sends a dependency graph as a "blueprint"—a list of segments naming earlier messages and which future steps will read them. This becomes a forward-reference table of spans where is the set of future steps reading tokens .
Foresight bonus: At step , a token in span with next reference receives:
Tokens with no future reference receive 0. The scale (floor of ) exceeds every cumulative attention score observed (max 25K at 2M tokens), so every referenced token outranks every unreferenced one. Among referenced tokens, soonest-needed ranks highest (Belady's rule). .
2. Bounded Positions
Renumbering: PACE keeps a physical position for every resident slot and a shift per sequence with for newly written tokens. When the next physical position approaches a limit (checked after each eviction), resident rows are renumbered to in logical order, and every kept key is re-rotated from to .
Proposition 1 (Exact re-rotation): Let a cached key be for pre-rotation key at physical position . Then : re-rotating by the position difference yields exactly the key the engine would have cached at .
Re-rotation uses cosine/sine tables:
Choosing : Must satisfy where is the largest resident row and the prefill chunk. With and 4,096-token chunks, is the smallest admissible power of two. Smaller is better.
Non-compounding re-rotation: Keys stored in bf16 accumulate rounding error with each renumber. PACE copies each resident key to a reference store (fp32) at first renumber, deriving all later positions from that copy. Maximum relative key error: 0.003 after 60 renumbers (vs. 0.031 without the fix).
3. Persistent Sessions & Tiered Rehydration
- Persistent sessions: Each step sends only new tokens; KV state (including eviction decisions) carries over. Reduces prefill 5.5× and wall time 4.3×.
- Tiered rehydration: Evicted context is offloaded to host memory → local storage → remote cache (Valkey). The planner's lookahead identifies needed spans no longer resident, and PACE restores them from the nearest tier.
4. Benchmark Protocol
A pre-registered, execution-scored long-horizon coding benchmark:
- Session structure: 3 definition steps (dataclass, status class, fee rule) → filler steps (2,000-token Python stdlib chunks) → 3 probe steps (fee_for, settle, complete module)
- Scoring: Hidden unit tests (7 + 3 + 5 checks) executed in subprocesses; independent recall metric separates forgetting from coding errors
- Randomized specifications: 30 seeds give 30 distinct specifications; leak checks verify probes reveal no values
5. Experimental Arms
| Arm | Retention | Positions | Role |
|---|---|---|---|
| H₂O | cumulative attention | as written (YaRN) | standard eviction |
| SnapKV | current-query attention | as written (YaRN) | standard eviction |
| retention only | H₂O + foresight bonus | as written (YaRN) | ablation |
| positions only | H₂O | bounded, | ablation |
| PACE | H₂O + foresight bonus | bounded, | full system |
| PACE () | H₂O + foresight bonus | bounded at model limit | ablation |
Empirical Validation / Results
Headline: Pre-registered Comparison (Qwen3-8B, 100K tokens, )
| Arm | Checks | Full | Recall | Paired vs. H₂O |
|---|---|---|---|---|
| H₂O | 63/150 (42%) | 7/30 | 83% | – |
| positions only | 94/150 (63%) | 10/30 | 86% | 14/2/14, |
| retention only | 94/150 (63%) | 12/30 | 96% | 15/2/13, |
| PACE | 145/150 (97%) | 28/30 | 100% | 22/0/8, |
PACE beats retention only: 17/0/13, ; beats positions only: 19/1/10, .
Mechanism: Residency ≠ Retrievability
Distance sweep (definitions placed tokens before probes, 100K-token sessions):
| H₂O | retention only | PACE | PACE | Gap | Gap | |
|---|---|---|---|---|---|---|
| 2K | 44% | 93% | 100% | 97% | 2.9K | 2.7K |
| 10K | 55% | 76% | 97% | 97% | 10.7K | 5.5K |
| 25K | 45% | 76% | 56% | 100% | 24.4K | 5.5K |
| 50K | 53% | 77% | 64% | 100% | 24.7K | 5.8K |
| 90K | 52% | 64% | 93% | 100% | 23.9K | 6.5K |
Retention only keeps 99.6% of needed tokens at every gap yet accuracy falls from 93% to 64%. Recall stays at 98–100%—facts are present but applied incorrectly due to rotary distance.
Pooled analysis (Figure 3): At residency 0.995–0.996 across all bins, pass rate falls from 96–100% (gap < 8K) to 87% (8–16K) to 69–77% (beyond 16K). The rotary gap, not the retention policy, predicts accuracy.
Baselines and Planner Robustness
- SnapKV: 3% checks (2/75), 5% recall—query-aware eviction is myopic across turns
- LLM planner (recall 0.9, precision 1.0): PACE reaches 92% (69/75) vs. H₂O's 41%
Second Model: Mistral-7B
No-eviction ceiling is 40% (coding ability limit). PACE stays at that ceiling at every gap (45%, 49%, 37% at , 25K, 90K) and recalls 96–100% of facts; retention only falls below the ceiling as gap grows (53%, 32%, 30%); H₂O collapses (9%, 1%, 1%).
Scaling to 1M and 2M Tokens
| Session | Sessions | Arm | Checks | Full | Recall |
|---|---|---|---|---|---|
| 100K | 5 | H₂O | 44% | 2/15 | 81% |
| PACE | 100% | 15/15 | 100% | ||
| 300K | 3 | StreamingLLM-style | 0% | 0/9 | 11% |
| positions only | 58% | 2/9 | 79% | ||
| PACE | 100% | 9/9 | 100% | ||
| 1M | 5 | StreamingLLM-style | 0% | 0/15 | 8% |
| positions only | 33% | 1/15 | 74% | ||
| PACE | 99% | 14/15 | 100% | ||
| 2M | 5 | positions only | 13% | 0/15 | 50% |
| PACE | 100% | 15/15 | 100% |
At 2M tokens, definitions sit 4.4–7.1K positions from the query (mean 6.3K) against a logical gap of ~1.79M. The margin from foresight widens with length: positions only falls from 33% (1M) to 13% (2M).
Exactness and Cost
- Token-identical to H₂O when blueprint absent and limit not reached
- Perplexity change: −0.003 nats (Qwen3-8B), +0.007 (Mistral-7B)—not significant
- Wall time: +9% (1,047s vs. 963s over ten 100K-token sessions)
- KV state: 0.30 GB resident + ≤0.30 GB reference store, independent of session length
Theoretical and Practical Implications
Theoretical Contribution
The paper reframes KV eviction as a two-dimensional problem: which tokens to keep (retention) and where they sit in position space (retrievability). The finding that accuracy falls with rotary distance at fixed residency connects to RoPE's long-range attenuation and "lost in the middle" phenomena, but is demonstrated here inside a KV-evicting server at controlled residency—isolating distance as the causal factor.
Practical Implications
- Agent-aware serving systems must consider not just which context to keep, but how to make it retrievable. Systems like ActKV, MemDecay, and KVFlow optimize residency; PACE shows this is insufficient.
- Position renumbering is a general technique applicable to any score-based eviction policy, not just H₂O.
- The benchmark protocol (execution-scored, pre-registered, with hidden tests and separate recall metrics) provides a template for evaluating long-horizon agent memory systems.
- Scaling insight: PACE achieves 2M-token sessions on a single 24 GB GPU with 0.7 GB KV state—a 400× compression of what the full cache would require (295 GB).
Theoretical Limits
The foresight bonus floor (50K) is constant, while maximum unprotected attention scores grow with session length (8.0–14.6K at 1M, 11.5–25.1K at 2M). Extrapolated, accumulated heavy hitters could outrank planned context at ~5M tokens, requiring a relative bonus scaled to current unprotected maximum.
Conclusion
The working assumption behind KV eviction—that a token kept is a token usable—is false in long-horizon agent sessions. At full residency, accuracy falls with the rotary distance between needed context and query. PACE addresses both dimensions:
- Plan-aware retention keeps the context the plan says will be needed
- Bounded positions renumber kept tokens so their distance to the query stays bounded
Under a 2% budget of a 100K-token session, this lifts accuracy from 42% to 97% on fresh tasks; neither component alone achieves it (63% each). The effect persists at length: 99% at 1M tokens and 100% at 2M tokens on a single 24 GB GPU, with a KV footprint under 0.7 GB where the full cache would need 147–295 GB.
Future directions: The paper identifies several open problems:
- Relative (session-adaptive) foresight bonuses for sessions beyond ~5M tokens
- Characterizing degradation with planner errors beyond recall 0.9
- Isolation of per-turn latency growth at 2M tokens (outside the bounded attention path)
- Validation on larger models and other agent workloads (tool traces, multi-document research, multi-agent hand-offs)
We expect the distinction between keeping and retrieving to matter for any system that bounds an agent's working memory, and we hope the residency-gap view and the benchmark protocol help evaluate them.
Related papers
- Evolve to Learn: Gradient-aligned experience evolution for model self-improvement
Gradient alignment between experience-induced and reference gradients predicts post-distillation gains, while inference-time accuracy shows no correlation, enabling autonomous self-improvement via the E2L framework.
- The Teacher Is Already Inside: Self-Distilling Looped Language Models Beyond Their Last Loop
ELPSD enables a looped language model to self-distill without rewards by training its last loop against an extrapolated contrastive teacher from its own frozen weights, beating GRPO on math benchmarks.
- CoT-Proxy: Cross-Format Transfer for Distilling Long-Horizon Policies
Distilling a long-horizon policy from teacher actions alone, without chain-of-thought, transfers successfully via an empty reasoning block plus KL replay regularization, enabling closed-teacher distillation.