The KV-Surgeon: Compensation-Aware Offline KV-Cache Compaction
Organizations: Department for AI in Society, Science, and Technology, Zuse Institute Berlin, Germany · Institute of Mathematics, Technische Universität Berlin, Germany
Abstract
Offline compaction of the key--value cache (KV-cache) reduces the cost of repeatedly querying a fixed context by constructing a smaller cache once and reusing it subsequently. The central challenge of compaction is deciding which cache entries to delete and which to retain. Existing methods score each candidate for deletion on its own, with the rest of the cache being held fixed. However, softmax renormalization of the attention operation couples these decisions in a nonlinear manner: removing one entry changes the attention weights of every remaining one. In this work, we introduce KV-Surgeon, which uses calibration data to take interactions into account in a tractable way. We derive a quadratic surrogate that admits closed-form expressions for jointly making the optimal greedy removal decision as well as compensation within the remaining entries. On QuALITY, LongHealth, and Long Code Arena, and at every evaluated compaction ratio, KV-Surgeon substantially advances the cost--accuracy Pareto frontier of dense and mixture-of-experts models from 4B to 30B parameters. At compaction, KV-Surgeon improves question-answering accuracy by up to four points over attention matching (Zweiger et al., 2026), the state of the art, at comparable cost. Lastly, we introduce an even faster variant, which also exceeds the state of the art while requiring only two fifths of its cost.
Figures & tables
| Method | Time (s) | Qwen3-4B | Llama-3.1-8B | Gemma-3-12B | Qwen3-30B | Mean | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| QuAL. | LongH. | QuAL. | LongH. | QuAL. | LongH. | QuAL. | LongH. | ||||
| Repeat-pass | |||||||||||
| KV-Surgeon -fast (ours) | 54 | 65.9 0.4 | 66.9 0.5 | 61.0 0.4 | 61.5 0.6 | 68.1 0.5 | 52.1 0.4 | 71.4 0.3 | 64.1 0.2 | 63.9 0.15 | |
| OBCache (adapted) | 58 | 62.4 0.7 | 62.8 0.9 | 59.8 0.3 | 52.4 0.2 | 66.6 0.4 | 50.2 0.8 | 67.3 0.4 | 62.8 0.4 | 60.5 0.20 | |
| AM-HAK-fast | 54 | 58.1 0.5 | 53.5 1.3 | 58.7 0.6 | 53.5 1.1 | 62.2 0.4 | 45.6 1.2 | 65.8 0.5 | 59.8 0.6 | 57.1 0.30 | |
| Self-study | |||||||||||
| Method | Selection rule | Compensation | Accuracy (%) |
|---|---|---|---|
| KV-Surgeon | Equation 11 | ✓ | 66.9 |
| KV-Surgeon w/o compensation | Equation 11 | ✗ | 63.9 |
| LOO | Equation 13 | ✗ | 64.7 |
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
| Query gen. | LOO | KV-Surgeon | AM-HAK | ||||
|---|---|---|---|---|---|---|---|
| Model, benchmark | (s) | ||||||
| Qwen3-4B, QuALITY | 33 | 7.7 | 29 | 10.5 | 53 | 7.0 | 28 |
| Llama-3.1-8B, LongHealth | 274 | 66 | 284 | 93 | 578 | 57 | 277 |
| Gemma-3-12B, QuALITY | 29 | 1.2 | 4.8 | 1.7 | 11 | 1.1 | 4.8 |
| Qwen3-30B-A3B, QuALITY | 162 | 5.5 | 20 | – | – | 4.6 | 20 |
| Qwen3-30B-A3B, LongHealth | 1062 | 47 | – | 65 | – | – | – |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 37.0 0.3 | 37.9 0.4 | 41.2 0.6 | 51.1 0.3 | 64.2 0.3 | 0.7 | 0.6 | 0.5 | 0.6 | 0.6 |
| Q-Filters | 47.1 0.7 | 52.4 0.5 | 62.0 0.2 | 66.5 0.3 | 69.9 0.2 | 0.5 | 0.6 | 0.5 | 0.5 | 0.6 |
| StreamingLLM | 40.4 0.6 | 44.7 0.4 | 52.0 0.4 | 58.4 0.4 | 64.2 0.2 | 0.7 | 0.5 | 0.5 | 0.8 | 0.7 |
| KVzip | 48.0 0.4 | 49.8 0.4 | 61.6 0.4 | 69.1 0.5 | 70.5 0.2 | 16 | 17 | 21 | 26 | 40 |
| H2O | 60.0 0.5 | 62.3 0.4 | 68.2 0.4 | 70.3 0.4 | 71.3 0.2 | 16 | 17 | 20 | 27 | 40 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 38.2 0.8 | 37.5 0.4 | 36.0 0.6 | 51.8 0.5 | 64.8 0.8 | 7.0 | 4.9 | 4.9 | 6.0 | 5.3 |
| Q-Filters | 37.6 0.4 | 43.6 0.4 | 56.2 0.6 | 69.1 0.5 | 76.5 0.4 | 4.8 | 4.8 | 5.1 | 5.4 | 5.2 |
| StreamingLLM | 34.6 0.5 | 39.4 0.7 | 42.9 0.8 | 55.4 0.3 | 64.2 0.4 | 5.3 | 5.6 | 5.2 | 4.6 | 5.6 |
| KVzip | 31.8 1.0 | 39.9 0.8 | 64.6 0.6 | 75.9 0.3 | 77.7 0.4 | 206 | 222 | 278 | 363 | 530 |
| H2O | 49.6 0.6 | 58.4 0.4 | 71.2 0.4 | 76.2 0.9 | 78.5 0.2 | 203 | 222 | 277 | 370 | 529 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 38.0 0.4 | 40.2 0.5 | 48.1 0.5 | 56.4 0.5 | 60.6 0.1 | 0.5 | 0.5 | 0.5 | 0.5 | 0.6 |
| Q-Filters | 40.8 0.3 | 47.7 0.8 | 56.7 1.0 | 60.9 0.4 | 62.5 0.3 | 0.6 | 0.6 | 0.6 | 0.6 | 0.6 |
| StreamingLLM | 38.7 0.5 | 43.2 0.4 | 49.0 0.7 | 54.1 0.4 | 59.2 0.2 | 0.5 | 0.5 | 0.6 | 0.6 | 0.6 |
| KVzip | 46.0 0.6 | 50.5 0.6 | 58.9 0.1 | 64.1 0.6 | 63.4 0.5 | 14 | 15 | 18 | 24 | 34 |
| H2O | 55.7 0.5 | 59.1 0.4 | 63.1 0.3 | 65.5 0.3 | 64.2 0.4 | 14 | 15 | 18 | 25 | 34 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 33.0 1.2 | 34.1 1.6 | 37.6 0.7 | 50.5 0.3 | 62.5 0.6 | 5.6 | 5.9 | 5.6 | 5.6 | 6.2 |
| Q-Filters | 36.0 0.8 | 36.1 1.1 | 39.9 0.6 | 49.5 0.8 | 61.4 0.2 | 7.0 | 5.7 | 6.0 | 6.6 | 5.7 |
| StreamingLLM | 37.1 1.1 | 37.2 0.8 | 42.2 1.1 | 45.4 0.5 | 56.3 0.8 | 5.9 | 5.2 | 5.7 | 5.6 | 5.7 |
| KVzip | 23.3 1.1 | 41.0 1.4 | 63.5 0.7 | 71.3 0.4 | 72.7 0.5 | 173 | 179 | 229 | 302 | 437 |
| H2O | 43.5 0.7 | 50.6 0.9 | 66.2 0.6 | 70.2 0.5 | 73.2 0.6 | 173 | 189 | 220 | 307 | 437 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 41.3 0.4 | 42.5 0.6 | 45.9 0.4 | 52.4 0.2 | 68.3 0.3 | 0.3 | 0.3 | 0.3 | 0.3 | 0.4 |
| Q-Filters | 46.3 0.6 | 50.9 0.3 | 59.9 0.7 | 70.6 0.4 | 77.3 0.1 | 0.3 | 0.4 | 0.4 | 0.4 | 0.3 |
| StreamingLLM | 44.7 0.5 | 47.5 0.3 | 53.8 0.1 | 60.9 0.4 | 70.2 0.3 | 0.3 | 0.3 | 0.3 | 0.3 | 0.3 |
| KVzip | 48.3 0.2 | 53.0 0.4 | 60.4 0.3 | 70.3 0.7 | 78.7 0.2 | 56 | 63 | 66 | 96 | 78 |
| H2O | 60.6 0.4 | 66.5 0.3 | 73.7 0.7 | 78.3 0.2 | 80.5 0.4 | 53 | 53 | 59 | 64 | 76 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| Q-Filters | – | 43.2 0.5 | – | – | – | – | – | – | – | – |
| OBCache | 52.7 0.4 | 62.8 0.4 | 68.6 0.5 | 74.4 0.1 | 76.7 0.3 | 391 | 395 | 402 | 409 | 424 |
| AM-HAK-fast | 50.4 1.1 | 59.8 0.6 | 66.9 0.4 | 74.3 0.5 | 77.2 0.2 | 358 | 361 | 362 | 372 | 389 |
| KV-Surgeon -fast | 54.7 0.3 | 64.1 0.2 | 70.2 0.5 | 75.0 0.3 | 77.1 0.2 | 363 | 366 | 373 | 386 | 413 |
| AM-HAK | 51.4 0.4 | 61.4 0.4 | 69.8 0.5 | 73.7 0.1 | 76.8 0.3 | 1121 | 1129 | 1130 | 1159 | 1162 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 47.4 0.5 | 48.5 0.3 | 49.3 0.2 | 50.8 0.1 | 55.3 0.5 | 0.3 | 0.3 | 0.3 | 0.3 | 0.3 |
| Q-Filters | 53.2 0.5 | 55.9 0.3 | 61.7 0.4 | 65.4 0.4 | 68.2 0.4 | 0.3 | 0.3 | 0.3 | 0.3 | 0.3 |
| StreamingLLM | 49.3 0.6 | 50.2 0.3 | 52.4 0.5 | 55.7 0.3 | 61.4 0.4 | 0.3 | 0.3 | 0.3 | 0.3 | 0.3 |
| KVzip | 56.0 0.2 | 57.6 0.8 | 63.1 0.4 | 67.2 0.5 | 69.1 0.4 | 6.0 | 6.2 | 6.7 | 7.4 | 9.1 |
| H2O | 61.5 0.3 | 66.2 0.2 | 68.1 0.2 | 71.1 0.4 | 71.3 0.4 | 6.0 | 6.1 | 6.6 | 7.4 | 9.0 |
| Accuracy (%) | Pipeline time (s) | |||||||||
| Method | ||||||||||
| key-norm | 37.6 0.5 | 38.9 1.2 | 37.8 0.6 | 40.4 0.4 | 41.0 0.6 | 6.2 | 6.2 | 6.3 | 6.1 | 6.0 |
| Q-Filters | 38.4 0.2 | 39.0 0.7 | 39.4 0.9 | 44.5 0.8 | 53.9 0.7 | 6.0 | 6.1 | 6.3 | 6.1 | 6.1 |
| StreamingLLM | 40.9 0.3 | 39.5 0.9 | 39.3 0.6 | 42.0 0.3 | 44.4 0.6 | 6.1 | 5.9 | 6.0 | 6.1 | 6.3 |
| KVzip | 38.1 0.5 | 39.8 0.7 | 44.5 0.6 | 52.9 0.8 | 60.7 0.6 | 55 | 59 | 64 | 74 | 95 |
| H2O | 38.5 0.6 | 47.3 0.5 | 55.1 0.2 | 63.5 0.6 | 68.8 0.7 | 54 | 60 | 64 | 74 | 91 |
| Accuracy (%) | Time (s) | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Method | ||||||||||
| StreamingLLM | 22.6 0.2 | 23.3 0.2 | 22.0 0.1 | 24.5 0.1 | 32.5 0.1 | 7.62 | 8.07 | 7.78 | 8.18 | 8.39 |
| key-norm | 23.2 0.1 | 23.4 0.1 | 23.4 0.2 | 23.4 0.1 | 27.6 0.2 | 7.45 | 8.55 | 8.77 | 7.70 | 8.12 |
| Q-Filters | 22.8 0.1 | 22.4 0.2 | 23.8 0.1 | 27.4 0.1 | 34.7 0.2 | 7.11 | 7.41 | 7.50 | 8.02 | 7.82 |
| KVzip | 20.5 0.2 | 22.4 0.2 | 28.6 0.4 | 36.2 0.5 | 38.3 0.2 | 217 | 232 | 292 | 379 | 596 |
| H2O | 22.5 0.1 | 24.1 0.3 | 30.9 0.2 | 36.4 0.2 | 39.4 0.2 | 221 | 230 | 307 | 385 | 572 |
| Default | Ablated | ||||
| Ablation | Method | Acc. (%) | Time (s) | Acc. (%) | Time (s) |
| Final refit off (plain eviction on the selected support) | |||||
| LOO | 65.6 0.2 | 38 | 60.2 0.4 | 35 | |
| Mass term off ( ) | |||||
| KV-Surgeon | 66.8 0.3 | 41 | 67.0 0.4 | 43 | |
| Mild-compaction fallback off (threshold ) | |||||
| uniform budgets | optimized budgets | |||
| Method | Queries | no refit | refit | + refit |
| AM-HAK | self-study | |||
| LOO | self-study | |||
| KV-Surgeon | self-study | |||
| AM-OMP | self-study | – | ||
| AM-HAK-fast | repeat | |||
| Method | as published | calib. queries | + refit | + budgets |
|---|---|---|---|---|
| H2O | ||||
| OBCache | ||||
| KVMerger | ||||
| KVzip |
| All | Mixed | Mixed | All | Mixed | Mixed | |
| Multipliers | compacted | compacted | uncompacted | compacted | compacted | uncompacted |
| Refit (default) | 57.5 0.8 | 58.0 0.7 | 78.2 0.6 | 66.9 0.5 | 67.9 0.6 | 79.4 0.5 |
| From the elimination | 56.7 0.4 | 56.5 0.7 | 79.2 0.5 | 68.5 0.7 | 67.8 0.3 | 78.7 0.5 |
| Unit | 55.9 0.6 | 45.8 0.8 | 81.3 0.2 | 65.2 0.6 | 59.0 0.9 | 80.6 0.5 |
| Ratio | median head | heads w/ any | rel. move | corr. | at bounds | |
|---|---|---|---|---|---|---|
| / | ||||||
| / | ||||||
| / | ||||||
| / | ||||||
| / |
KV-Cache Management
Key-value (KV) cache management reduces and coordinates Transformer attention state to make long-context inference faster and more memory-efficient.
See all 201 papers