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 LL, 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% (p<10−4p < 10^{-4}), 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 10510^5–10610^6 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 mm and key at position nn are rotated by angles mθim\theta_i and nθin\theta_i, so their dot product depends on positions only through the difference:

⟨Rmq,Rnk⟩=⟨q,Rn−mk⟩,RaRb=Ra+b(1)\langle R_m q, R_n k \rangle = \langle q, R_{n-m} k \rangle, \quad R_a R_b = R_{a+b} \tag{1}

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 Δt=m^t−p(Nt)\Delta_t = \hat{m}_t - p(N_t) (distance in physical rotary positions from query to needed span) grows with session length.

Problem Statement

Let step tt issue its query at logical position mtm_t (seen at physical rotary position m^t\hat{m}_t), with NtN_t the set of earlier tokens it needs. Define:

  • Residency: ρt=∣Nt∩Ct∣/∣Nt∣\rho_t = |N_t \cap C_t| / |N_t| (fraction of needed tokens resident in cache CtC_t)
  • Rotary gap: Δt=m^t−p(Nt)\Delta_t = \hat{m}_t - p(N_t) (distance in physical rotary positions)

Eviction policies maximize ρt\rho_t. This paper shows ρt≈1\rho_t \approx 1 is insufficient: accuracy still falls with Δt\Delta_t.


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 (a,b,F)(a, b, F) where FF is the set of future steps reading tokens [a,b)[a, b).

Foresight bonus: At step tt, a token in span (a,b,F)(a, b, F) with next reference s⋆=min⁡{s∈F:s≥t}s^\star = \min\{s \in F : s \geq t\} receives:

βt=σ(12+12e−λ(s⋆−t))(2)\beta_t = \sigma \left( \frac{1}{2} + \frac{1}{2} e^{-\lambda(s^\star - t)} \right) \tag{2}

Tokens with no future reference receive 0. The scale σ=105\sigma = 10^5 (floor of σ/2=50K\sigma/2 = 50K) 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). λ=0.35\lambda = 0.35.

2. Bounded Positions

Renumbering: PACE keeps a physical position pp for every resident slot and a shift cc per sequence with m^=m−c\hat{m} = m - c for newly written tokens. When the next physical position approaches a limit LL (checked after each eviction), resident rows are renumbered to p′∈{0,…,R−1}p' \in \{0, \ldots, R-1\} in logical order, and every kept key is re-rotated from pp to p′p'.

Proposition 1 (Exact re-rotation): Let a cached key be k~=Rpk\tilde{k} = R_p k for pre-rotation key kk at physical position pp. Then Rp′−pk~=Rp′kR_{p'-p}\tilde{k} = R_{p'}k: re-rotating by the position difference yields exactly the key the engine would have cached at p′p'.

Re-rotation uses cosine/sine tables:

cos⁡((p′−p)θi)=cos⁡(p′θi)cos⁡(pθi)+sin⁡(p′θi)sin⁡(pθi)\cos((p'-p)\theta_i) = \cos(p'\theta_i)\cos(p\theta_i) + \sin(p'\theta_i)\sin(p\theta_i) sin⁡((p′−p)θi)=sin⁡(p′θi)cos⁡(pθi)−cos⁡(p′θi)sin⁡(pθi)\sin((p'-p)\theta_i) = \sin(p'\theta_i)\cos(p\theta_i) - \cos(p'\theta_i)\sin(p\theta_i)

Choosing LL: Must satisfy L>Bmax+HL > B_{max} + H where BmaxB_{max} is the largest resident row and HH the prefill chunk. With B=2,048B = 2{,}048 and 4,096-token chunks, L=8,192L = 8{,}192 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

ArmRetentionPositionsRole
H₂Ocumulative attentionas written (YaRN)standard eviction
SnapKVcurrent-query attentionas written (YaRN)standard eviction
retention onlyH₂O + foresight bonusas written (YaRN)ablation
positions onlyH₂Obounded, L=8KL=8Kablation
PACEH₂O + foresight bonusbounded, L=8KL=8Kfull system
PACE (L=40KL=40K)H₂O + foresight bonusbounded at model limitablation

Empirical Validation / Results

Headline: Pre-registered Comparison (Qwen3-8B, 100K tokens, B=2,048B=2{,}048)

ArmChecksFullRecallPaired vs. H₂O
H₂O63/150 (42%)7/3083%–
positions only94/150 (63%)10/3086%14/2/14, p=0.004p=0.004
retention only94/150 (63%)12/3096%15/2/13, p=0.002p=0.002
PACE145/150 (97%)28/30100%22/0/8, p<10−6p<10^{-6}

PACE beats retention only: 17/0/13, p=1.5×10−5p = 1.5 \times 10^{-5}; beats positions only: 19/1/10, p=4.0×10−5p = 4.0 \times 10^{-5}.

Mechanism: Residency ≠ Retrievability

Distance sweep (definitions placed DD tokens before probes, 100K-token sessions):

DDH₂Oretention onlyPACE L=40KL=40KPACE L=8KL=8KGap L=40KL=40KGap L=8KL=8K
2K44%93%100%97%2.9K2.7K
10K55%76%97%97%10.7K5.5K
25K45%76%56%100%24.4K5.5K
50K53%77%64%100%24.7K5.8K
90K52%64%93%100%23.9K6.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 D=2KD=2K, 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

SessionSessionsArmChecksFullRecall
100K5H₂O44%2/1581%
PACE100%15/15100%
300K3StreamingLLM-style0%0/911%
positions only58%2/979%
PACE100%9/9100%
1M5StreamingLLM-style0%0/158%
positions only33%1/1574%
PACE99%14/15100%
2M5positions only13%0/1550%
PACE100%15/15100%

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

  1. 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.
  2. Position renumbering is a general technique applicable to any score-based eviction policy, not just H₂O.
  3. The benchmark protocol (execution-scored, pre-registered, with hidden tests and separate recall metrics) provides a template for evaluating long-horizon agent memory systems.
  4. 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:

  1. Plan-aware retention keeps the context the plan says will be needed
  2. 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