Language-model agents revise their working context as information becomes obsolete, but deleting text does not erase its influence on cached computation. Later key-value (KV) states remain conditioned on the deleted content, while recomputing the entire retained suffix can be costly. We introduce budgeted KV-cache repair: recovering task performance by recomputing a user-chosen fraction of the retained suffix. Comparing repair allocations under identical relevance scores and token budgets, we find that refreshing coherent regions can substantially outperform refreshing isolated tokens. This finding motivates Semantic-Unit Refresh (SUR), which allocates the budget to query-relevant text regions using inexpensive embedding scores, without training or an auxiliary transformer forward. To execute these repairs efficiently, a layer-packed executor processes selected states together at each layer while preserving full causal cache access. Across five models, SUR achieves strong recovery in controlled deletion tasks while recomputing only 2-5% of the suffix. On four natural multi-hop QA datasets, it achieves near-full-recompute exact match at a 5% budget with lower post-edit latency. The executor accelerates fragmented refresh by up to 16.1x over interval-wise execution.
Figures & tables
Figure 1: Budgeted repair at an agent’s context-edit boundary. After deletion and position correction, scores retained tokens against the next query using frozen embeddings, ranks text-boundary units by aggregated relevance, and selects regions under a user budget. Lower-scoring neighbors can be refreshed with their selected unit. Packed refresh updates selected states while retaining full causal cache access. Relevance bars and the four-row allowance are schematic.
Figure 2: Same selected tokens, a different execution order. (a) Interval-wise refresh completes every layer for positions {3,4} before traversing the model again for {8,9} . (b) Packed refresh processes all four positions together at each layer, sharing one model traversal. (c) After selected K/V are updated, attention still reads all causally available entries, including reused gaps. Filled cells indicate visible K/V, not attention weights; crosses mark future positions.
Figure 3: Budgeted repair across five model systems. Every panel uses the same 96 cases and baseline set, including already-correct cases. Lines connect measured suffix budgets; Shift and Replay are reference operators. Prior-derived selectors share the same repair operator (full results in Appendix 8 ). The bottom row uses a magnified vertical range; Qwen3-30B’s Shift score is annotated off-scale.
Figure 4: Answer quality versus repair-and-generation latency. Exact match across four datasets (1,200 cases from 400 questions) versus mean post-edit latency, including selection and decoding. Small and large markers denote 2% and 5% recomputation. All methods use the same sequential executor. Paired quality comparisons appear in Table 9 .
1
B←min{∣s∣,⌈∣s∣b/100⌉} .
2
If B=∣s∣ : return full suffix Replay from the valid prefix.
3
C←Shift(C,e) ; if B=0 : return C .
4
Compute token scores ri using Equation 1 .
5
Partition suffix into units; rank by their top-four-mean scores.
6
A←∅ ; visit units u in ranked order until ∣A∣=B :
7
w←B−∣A∣ ; if ∣u∣≤w , add all positions of u to A .
Algorithm 1 Semantic-Unit Refresh under a suffix budget
Allocator
Implementation in this comparison
ProphetKV-Adapted ( Wang et al., 2026 )
Query-only forward against the Shift cache; suffix score is attention averaged uniformly over query tokens, heads, and layers.
InfoFlow-Motivated ( Teng et al., 2026 )
L2 norm of the same query-to-suffix attention tensor over query tokens, heads, and all layers. This differs from the original attention-mass formula, selected-layer scope, and chunk-reordering procedure.
Cache-Craft-Adapted ( Agarwal et al., 2025 )
Replay the dirty suffix in 64-token chunks with deleted keys still visible. Score each retained token by its attention mass on deleted keys, averaged over heads and layers.
CacheBlend-Adapted ( Yao et al., 2025 )
Fresh two-layer edited prefill; score layer-1 KV deviation from Shift by ∥ΔK1,i∥22+∥ΔV1,i∥22 . One selected support is reused across layers.
A 3 -Adapted ( Zhou et al., 2025 )
Fresh two-layer edited prefill followed by a shallow query forward; sum layer-1 query-to-context attention over heads and query positions.
LegoLink-Adapted ( Hu et al., 2025 )
Treat the valid prefix as the first chunk and split the suffix into 512-token chunks. Rank positions by depth from their chunk boundary, then chunk index. Depths beyond the native 32-token limit are extensions required by the common budget grid.
Table 1: Prior-work-derived allocation signals. Layer indices start at zero; layer 1 is the first contextual KV layer. All rows use the common suffix budget and all-layer refresh operator.
Method
Reference
2%
5%
10%
20%
40%
80%
Dirty
0.00
–
–
–
–
–
–
Shift
0.00
–
–
–
–
–
–
Replay
98.96
–
–
–
–
–
–
Re-prefill
98.96
–
–
–
–
–
–
SUR (ours)
–
89.58
98.96
98.96
98.96
98.96
98.96
Dense tokens (same score)
–
7.29
14.58
28.13
58.33
91.67
97.92
Table 2: Qwen3-8B: complete NIAH allocation sweep. Success rate (%, ↑ ) on the same N=96 cases. Column headings give the recomputed suffix budget; references occupy their own column. The clean-state oracle uses privileged information.
Method
Reference
2%
5%
10%
20%
40%
80%
Dirty
1.04
–
–
–
–
–
–
Shift
13.54
–
–
–
–
–
–
Replay
95.83
–
–
–
–
–
–
Re-prefill
98.96
–
–
–
–
–
–
SUR (ours)
–
95.83
96.88
95.83
96.88
95.83
96.88
Dense tokens (same score)
–
89.58
91.67
92.71
94.79
92.71
97.92
Table 3: Qwen3-30B-A3B: complete NIAH allocation sweep. Success rate (%, ↑ ) on the same N=96 cases. Column headings give the recomputed suffix budget; references occupy their own column. The clean-state oracle uses privileged information.
Method
Reference
2%
5%
10%
20%
40%
80%
Dirty
0.00
–
–
–
–
–
–
Shift
1.04
–
–
–
–
–
–
Replay
100.00
–
–
–
–
–
–
Re-prefill
100.00
–
–
–
–
–
–
SUR (ours)
–
90.63
98.96
100.00
100.00
100.00
100.00
Dense tokens (same score)
–
7.29
8.33
7.29
16.67
29.17
89.58
Table 4: Llama-3.3-70B: complete NIAH allocation sweep. Success rate (%, ↑ ) on the same N=96 cases. Column headings give the recomputed suffix budget; references occupy their own column. The clean-state oracle uses privileged information.
Method
Reference
2%
5%
10%
20%
40%
80%
Dirty
0.00
–
–
–
–
–
–
Shift
11.46
–
–
–
–
–
–
Replay
100.00
–
–
–
–
–
–
Re-prefill
100.00
–
–
–
–
–
–
SUR (ours)
–
100.00
100.00
100.00
100.00
100.00
100.00
Dense tokens (same score)
–
43.75
57.29
67.71
85.42
91.67
100.00
Table 5: GLM-4.5-Air: complete NIAH allocation sweep. Success rate (%, ↑ ) on the same N=96 cases. Column headings give the recomputed suffix budget; references occupy their own column. The clean-state oracle uses privileged information.
Method
Reference
2%
5%
10%
20%
40%
80%
Dirty
0.00
–
–
–
–
–
–
Shift
68.75
–
–
–
–
–
–
Replay
97.92
–
–
–
–
–
–
Re-prefill
100.00
–
–
–
–
–
–
SUR (ours)
–
97.92
100.00
100.00
100.00
100.00
100.00
Dense tokens (same score)
–
83.33
91.67
96.88
95.83
97.92
97.92
Table 6: Qwen3-235B-A22B: complete NIAH allocation sweep. Success rate (%, ↑ ) on the same N=96 cases. Column headings give the recomputed suffix budget; references occupy their own column. The clean-state oracle uses privileged information.
Method
Budget
EM (%) ↑
Mean latency (s) ↓
SUR (ours)
2%
42.58
1.663
SUR (ours)
5%
43.42
2.039
BM25 (same units)
2%
41.08
1.688
BM25 (same units)
5%
41.50
2.130
CacheBlend-Adapted
2%
41.42
3.486
CacheBlend-Adapted
5%
41.75
6.306
Table 7: Qwen3-30B-A3B: Natural QA at low budgets. Exact match (%) on 1,200 cases (300 per dataset) and mean post-edit latency, including selection and decoding.
Method
Budget
EM (%) ↑
Mean latency (s) ↓
SUR (ours)
2%
56.67
0.500
SUR (ours)
5%
56.92
0.535
BM25 (same units)
2%
54.75
0.490
BM25 (same units)
5%
55.08
0.552
CacheBlend-Adapted
2%
54.67
1.572
CacheBlend-Adapted
5%
55.17
2.759
Table 8: Llama-3.3-70B: Natural QA at low budgets. Exact match (%) on 1,200 cases (300 per dataset) and mean post-edit latency, including selection and decoding.
Budget
Reference
Δ EM
95% CI
Qwen3-30B-A3B
2%
Shift
+0.92
[+0.28,+1.56]
2%
Replay
−0.42
[−0.95,+0.13]
2%
Re-prefill
−0.75
[−1.14,−0.02]
5%
Shift
+1.75
[+1.12,+2.24]
5%
Replay
+0.42
[−0.15,+0.88]
Table 9: Paired exact-match differences on Natural QA. SUR minus each reference in percentage points, with 95% paired bootstrap confidence intervals. Each model is evaluated on 400 source questions and 1,200 placement cases.
Method
Budget
2Wiki
HotpotQA
IIRC
MuSiQue
Pooled
SUR (ours)
2%
52.33
47.33
36.67
34.00
42.58
SUR (ours)
5%
53.00
48.33
37.67
34.67
43.42
BM25 (same units)
2%
52.00
46.33
35.00
31.00
41.08
BM25 (same units)
5%
52.33
46.33
35.67
31.67
41.50
CacheBlend-Adapted
2%
52.67
46.33
35.33
31.33
41.42
CacheBlend-Adapted
5%
52.67
47.00
35.67
31.67
41.75
Table 10: Qwen3-30B-A3B: performance by dataset. Exact match (%, ↑ ) across four datasets, with 300 cases per dataset and 1,200 pooled cases.
Method
Budget
2Wiki
HotpotQA
IIRC
MuSiQue
Pooled
SUR (ours)
2%
65.00
59.33
53.33
49.00
56.67
SUR (ours)
5%
65.00
59.67
53.67
49.33
56.92
BM25 (same units)
2%
61.00
58.00
51.33
48.67
54.75
BM25 (same units)
5%
61.67
58.67
51.33
48.67
55.08
CacheBlend-Adapted
2%
61.33
58.33
51.00
48.00
54.67
CacheBlend-Adapted
5%
62.00
58.67
51.33
48.67
55.17
Table 11: Llama-3.3-70B: performance by dataset. Exact match (%, ↑ ) across four datasets, with 300 cases per dataset and 1,200 pooled cases.
Qwen3-8B
Qwen3-30B
Llama-70B
GLM-Air
Qwen3-235B
Allocation rule
2%
5%
2%
5%
2%
5%
2%
5%
2%
5%
SUR
89.6
99.0
95.8
96.9
90.6
99.0
100.0
100.0
97.9
100.0
Same-score tokens
7.3
14.6
89.6
91.7
7.3
8.3
43.8
57.3
83.3
91.7
Single relevance window
75.0
86.5
84.4
79.2
65.6
72.9
89.6
88.5
72.9
77.1
Same-unit BM25
82.3
94.8
90.6
94.8
75.0
94.8
94.8
100.0
99.0
100.0
Random units
2.1
5.2
16.7
17.7
2.1
5.2
18.8
19.8
78.1
83.3
Table 12: Allocation geometry on Erasing-NIAH. Exact match (%, ↑ ) over the same 96 cases per model, at 2% and 5% retained-suffix budgets. Token selection and the single window use the same token scores as SUR; the window maximizes their sum over all width- B suffix windows. BM25 and random units retain SUR’s partition. Bold denotes the best value within each column.
Qwen3-8B
Qwen3-30B
Llama-70B
GLM-Air
Qwen3-235B
Allocation rule
2%
5%
2%
5%
2%
5%
2%
5%
2%
5%
SUR
89.6
99.0
95.8
96.9
90.6
99.0
100.0
100.0
97.9
100.0
ProphetKV-Adapted
77.1
97.9
91.7
94.8
85.4
99.0
93.8
100.0
92.7
95.8
InfoFlow-Motivated
82.3
97.9
91.7
96.9
92.7
100.0
95.8
99.0
95.8
96.9
Cache-Craft-Adapted
77.1
93.8
92.7
95.8
52.1
82.3
91.7
97.9
85.4
96.9
CacheBlend-Adapted
0.0
4.2
71.9
79.2
1.0
1.0
21.9
28.1
78.1
83.3
Table 13: Alternative allocation policies. Exact match (%, ↑ ) on the same 96 cases and budgets. Adapted/motivated methods use the common repair operator, not their native systems. Their score-driven token allocation differs from SUR’s unit allocation; this is a comparison of complete selection rules, not an isolated scoring ablation.
Mean selected tokens
SUR V1
Query
144.6
97.5
87.6
359.2
99.7
97.8
Table 14: Matched-token evaluation. Qwen3-8B exact match (%) over 599 cases. SUR V1 and token-query selection use matched mean selected-token counts, evaluated separately from the suffix-budget sweep.
Endpoint
Contrast
Estimate
95% CI
Holm p
NLL
Evidence side
−0.039
[ −0.099 , 0.021]
—
Neutral tail
−0.031
[ −0.066 , 0.004]
0.308
Interaction
−0.001
[ −0.075 , 0.068]
1.000
EM
Evidence side
5.99
[2.87, 9.26]
0.002
Neutral tail
0.49
[ −1.6 , 2.6]
1.000
Interaction
−1.2
[ −4.8 , 2.2]
1.000
Table 15: Matched factorial effects. EM effects are in percentage points and NLL effects in nats. The preregistered NLL evidence-side interval includes zero, whereas the EM secondary is positive after correction.
Endpoint
Complete replay
Fixed-slot refresh
First-token TV
.0696 [.0560, .0832]
.0709 [.0571, .0849]
First-token JSD
.0311 [.0238, .0385]
.0315 [.0240, .0391]
Argmax agreement (pp)
8.37 [6.27, 10.49]
8.17 [5.96, 10.42]
Sequence agreement (pp)
11.77 [9.27, 14.25]
11.45 [8.81, 14.16]
Table 16: Evidence-side effects on clean-reference fidelity. Positive effects mean a larger repair benefit over Shift with downstream evidence. Entries are estimates and source-bootstrap 95% intervals. TV and JSD use reductions in distance; agreement effects are in percentage points (pp).
Model
Output
Shared (%)
Re-prefill (%)
Cross-path (%)
Excess (pp)
Qwen3-8B
Sequence
0.42
0.83
2.29
1.46
Exact logits
0.00
0.00
100.00
100.00
Argmax
0.00
0.00
1.88
1.88
Llama-3.1-8B
Sequence
2.29
1.67
3.68
2.01
Exact logits
0.00
0.00
100.00
100.00
Argmax
0.00
0.00
0.62
0.62
Table 17: Numerical-repeat controls on a separate 160-cell cohort per model. Categorical entries are disagreement percentages; excess is in percentage points. NLL entries summarize absolute differences. The reference model Llama-3.1-8B differs from the main sweep’s Llama-3.3-70B.
N
R
Sequential
Direct seq.
One-shot
SDPA
Custom
+ fused
Flex
2048
1
33.051
31.465
34.593
32.485
32.389
34.169
53.008
2048
4
131.290
121.456
34.624
32.672
32.470
34.063
56.661
2048
16
530.068
485.378
34.942
32.952
32.775
34.588
56.858
8192
1
38.848
32.571
42.126
34.860
33.372
35.228
95.613
8192
4
142.898
120.679
41.198
34.733
32.235
34.051
144.759
8192
16
552.498
494.826
42.186
34.757
33.375
35.161
144.376
Table 18: Complete matched-work refresh timings (ms). Qwen3-8B, H100, B=64 ; medians of twenty measurements after ten warmups. Direct sequential updates the cache without repeated full assembly. Custom uses Torch writes unless marked fused and remains experimental (Appendix 13.5 ).
N
R
HF SDPA
Direct SDPA+GQA
Compiled Flex
Custom
2048
1
167.3
124.9
1019.2
135.1
2048
4
167.6
121.8
1106.8
148.3
2048
16
164.8
122.2
1108.3
147.6
8192
1
411.1
273.9
2145.7
232.5
8192
4
401.8
266.6
3477.7
293.7
8192
16
402.8
277.9
3468.8
295.7
Table 19: Attention-only median time ( μ s). Resident BF16 operands; thirty measurements after ten warmups. Direct SDPA enables GQA rather than explicitly repeating K/V heads.
Method
Refresh/operator
Recovery
2%, interval-wise
32.87
51.39
2%, packed
31.98
50.43
5%, interval-wise
48.99
67.25
5%, packed
31.98
50.71
Full suffix Replay
48.57
48.57
Clean Re-prefill
60.25
60.25
Table 20: Real-input recovery timing examples (ms). Four fixed QA inputs on Qwen3-8B/H100. Refresh includes the executor’s planning and output ownership; recovery additionally includes relevance, unit construction, allocation, and Shift. Reference operators have no SUR selection stage.
Long-running LLM agent workflows often revise interior context spans while retaining long suffixes. Although suffix tokens remain unchanged, altered causal histories and rotary positions prevent exact reuse of their offloaded key-value (KV) states. Full suffix recomputation wastes prefill work, while indiscriminate reuse propagates stale states and full-precision restoration adds data movement. We present PatchKV, a profile-guided recovery system for suffix-preserving revisions. PatchKV decomposes adjacent context versions into an exact prefix, an updated span, and an aligned suffix. It predicts an edit-local dirty region using an offline length-conditioned drift model, augments this region with sparse nonlocal blocks selected from stored attention, and block-rounds their union into a fixed repair set. The remaining suffix blocks are restored from CPU memory using frozen per-block precision tags and a fused path for dequantization, RoPE correction, and KV-page placement. Across three models and three long-context question-answering workloads, PatchKV achieves a 2.51-3.85× speedup in mean resume time-to-first-token over full suffix recomputation and a 1.26-2.06× speedup over CacheBlend, while matching or exceeding CacheBlend's F1 score in six of nine settings and remaining within 1.36 points in the others.
KV-cache reuse can reduce inference cost in retrieval-augmented generation and agentic systems, but cached contexts may become stale when retrieved knowledge, working memory, or user state is edited. Under causal self-attention, even a local edit can affect downstream KV states. A full re-prefill reliably restores consistency but is costly, whereas refreshing only the edited span can leave downstream dependencies stale. We formulate in-place repair as budgeted recomputation and compare training-free position-selection policies on a factual RAG benchmark with matched direct and derived edits. Across three model families, all policies repair direct cases, but derived cases clearly separate them. At the primary budget, a contiguous edit-local window recovers at least 0.94 of the post-edit answer margin and substantially outperforms attention-based, KV-deviation, and structural selectors. Mechanistic analysis shows that position sets effective under clean-state transplantation can fail under actual recomputation because scattered positions inherit surrounding staleness. The edit-local advantage also depends on adjacency and largely disappears when the answer-bearing text moves downstream. Because answer-relevant edits almost always corrupt model behavior, failure severity is difficult to predict, and repair is 13-21 times faster than full re-prefill, our results support unconditional edit-local repair when the dependent text remains adjacent to the edit.
Mingyang Mao, Wyatt Mackey, Xiaomin Lin
University of South Florida, Electrical and Computer Engineering · DEVCOM Army Research Laboratory
Cross-context KV-cache reuse predicts a shared segment's keys and values under a new prefix instead of recomputing them, and has been reported to do so without quality loss. We find otherwise, and identify two problems. (1) A hidden cost: on MMLU and GSM8K, reuse costs substantial accuracy. (2) A decision at the wrong unit: no rule for deciding whether to reuse a cache removes that cost. What does help is choosing which parts of the cache to recompute, and the value of choosing well falls as the unit of choice grows: informed selection removes 49.5% of the cache error beyond chance at single rows (one token's keys and values), 10.6% at 64-token chunks, and nothing at the level of whole calls. Budgeted Cache Repair (BCR) acts at the unit where selection still pays. It drafts two tokens from the assembled cache, ranks cache rows by the attention those tokens pay them, and recomputes a fixed budget of rows exactly, in one of three layouts. The cost is paid rather than predicted away, and the draft that fails as a gate succeeds as a selector. BCR restores GSM8K to dense-prefill accuracy while still serving most calls from cache, and its best layout outperforms every reuse baseline's mean in the reference grid. The draft also beats a coin-flip selector at the same budget - a control prior evaluations lack.