arXiv(opens in a new tab)cs.LGOct 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2610.25933

The KV-Surgeon: Compensation-Aware Offline KV-Cache Compaction

Moritz Wagner, Christophe Roux, Max Zimmer, Sebastian Pokutta

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

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 50×50\times 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.

1 INTRODUCTION

Many large language model (LLM) applications repeatedly query a fixed context such as a document, patient record, or codebase. The key-value cache (KV-cache) for such a context grows linearly with its length and occupies a large fraction of the memory used during long-context inference. Every attention layer reads this cache for every generated token, so every request pays for its size in both memory and latency. Offline compaction reduces this cost by processing the context once and compressing the resulting cache, which is then used to serve all later requests (Eyuboglu et al., 2026; Kim et al., 2026; Zweiger et al., 2026).

Unlike serving-time eviction, which discards cache entries within the forward pass of a request (Zhang et al., 2023; Li et al., 2024), offline compaction can spend a larger one-time budget that is amortized over future requests. In particular, it can use calibration queries, i.e., query vectors the model produces while re-reading or discussing the context, to decide which entries to retain. Eyuboglu et al. (2026) introduce this regime with Cartridges, which trains a new latent cache per context. More recent work makes offline compaction much cheaper by replacing end-to-end training with eviction-inspired selection and efficient optimization of the remaining cache (Zweiger et al., 2026).

The central problem in cache compaction is deciding which cache entries can be removed while preserving the resulting attention outputs. For one attention head, a cache of TT entries contains a key kj∈Rdkk_j \in \mathbb{R}^{d_k} and a value vj∈Rdvv_j \in \mathbb{R}^{d_v} at each position j=1,…,Tj = 1, \ldots, T. A query q∈Rdkq \in \mathbb{R}^{d_k} produces

P≜softmax((q⊤kℓdk)ℓ=1T)∈RT,o≜∑j=1TPjvj∈Rdv,P\triangleq softmax\left(\left(\frac{q^{\top}k_{\ell}}{\sqrt{d_{k}}}\right)_{\ell=1}^{T}\right)\in\mathbb{R}^{T},\qquad o\triangleq\sum_{j=1}^{T}P_{j}v_{j}\in\mathbb{R}^{d_{v}},

*Correspondence to: Moritz Wagner wagner@zib.de

Figure

Figure 1: Cost–accuracy frontier at 50×50\times KV-cache compaction on QuALITY with Qwen3-30B-A3B. KV-SURGEON reaches 72.5% accuracy in 159 seconds and KV-SURGEON-fast reaches 71.4% in 54 seconds. Pipeline time per context includes calibration-query generation and compaction. OBCache, KVMerger, and KVzip are adapted to our pipeline (Appendix G). Points are five-seed means. The solid and dash-dotted curves are the Pareto frontiers with and without our methods.

where PjP_j is the attention weight of entry jj and oo is the resulting output. Removing cache entry jj does not simply subtract PjvjP_j v_j, since softmax renormalization changes the attention weights of all remaining entries. The effect of one deletion therefore depends on which other entries remain, making selection a combinatorial problem rather than a ranking problem. Scores based only on received attention, as in attention matching (Zweiger et al., 2026), additionally overlook value redundancy: if an entry's value is close to the output the head already produces, i.e., ∥vj−o∥22\|v_j - o\|_2^2 is small, removing it might change the output little, even when its attention weight is large. Good selection must therefore account not only for the contribution of each entry, but also for how deletions interact.

Recent output-aware methods instead estimate the output error caused by deleting a single entry (Goel et al., 2026; An et al., 2026; Gu et al., 2026; Feng et al., 2026). CAOTE (Goel et al., 2026) derives the exact change caused by deleting one entry for a single calibration query, and OBCache (Gu et al., 2026) uses a Taylor approximation of the attention output error aggregated across queries. These methods nevertheless evaluate the deletion of one entry while the rest of the cache is held fixed. Attention matching (Zweiger et al., 2026) refits the retained cache to minimize the error of the compacted output, but only after the retained set has been chosen. What is missing is compensation-aware selection: each candidate deletion should be scored by the error remaining after the surviving entries have been adjusted to compensate for it, so that selection and refitting are optimized jointly rather than in sequence. This is difficult under softmax attention because deletion changes the normalization itself, so the error after both deletion and compensation has no obvious closed form.

In this work, we substantially advance the cost–accuracy Pareto frontier for offline KV-cache compaction. We derive a quadratic surrogate that makes compensation-aware selection tractable. We introduce KV-SURGEON, which uses this surrogate for greedy, compensation-aware elimination. For each candidate deletion, the surrogate admits closed-form expressions for both the optimal compensating update and the resulting increase in surrogate loss. Each candidate entry is assigned a score after the remaining entries have been adjusted to compensate for its removal, and the update is then applied to the cache, so the next deletion is chosen against the compensated entries, as Optimal Brain Surgeon (OBS) (Hassibi & Stork, 1993) does for network weights.

We evaluate KV-SURGEON on QuALITY, LongHealth, and Long Code Arena, using dense and mixture-of-experts models from 4B to 30B parameters and compaction ratios from 5×5\times to 100×100\times. Across these settings, KV-SURGEON improves the cost–accuracy frontier at every ratio, achieving higher mean accuracy at comparable cost. Mean question-answering accuracy across our experiments exceeds the state of the art by 4.3 percentage points at 100×100\times compaction and 3.0 percentage points at 50×50\times. The gains are less pronounced at 5×5\times and 10×10\times, where the strongest methods al-ready suffer almost no deterioration relative to the uncompacted cache. At 50×50\times compaction the state of the art, attention matching with its AM-HAK selection rule (Zweiger et al., 2026), reaches 62.0% accuracy in 138 seconds 1^{1} per context. KV-SURGEON reaches 65.1% in 145 seconds, and KV-SURGEON-fast reaches 63.9% in 54 seconds. Figure 1 shows the resulting frontier at 50×50\times compaction on the largest model we evaluate.

Our contributions are the following.

  1. Quadratic surrogate. We derive a quadratic surrogate to the exact attention-output error, which allows us to design efficient methods for approximately solving the compaction problem.

  2. Compensation-aware selection. Based on this surrogate, we introduce KV-SURGEON, which iteratively removes entries while adjusting the remaining entries in closed form after each deletion, then jointly refits the retained cache. Unlike prior output-aware scores or post-selection refitting, compensation directly influences which entries are retained.

  3. A new cost–accuracy Pareto frontier. Across QuALITY, LongHealth, and Long Code Arena, dense and mixture-of-experts models from 4B to 30B parameters, and 5×5\times to 100×100\times compaction, KV-SURGEON substantially advances the frontier, with gains at every evaluated ratio and the largest improvements at 50×50\times and 100×100\times. We show empirically that accounting for compensation during selection substantially improves accuracy over exact single-deletion scoring.

References (65)

  • Longhealth: A question answering benchmark with long clinical documents, 2024 (opens arXiv in a new tab)
  • Rest-kv: Robust kv cache eviction with layer-wise output reconstruction and spatial-temporal smoothing
  • a set of benchmarks for long-context code models (opens arXiv in a new tab)
  • Palu: Kv-cache compression with low-rank projection
  • A simple and effective 1.2 norm-based strategy for kv cache compression
  • Cartridges:
  • Criticalkv: Optimizing kv cache eviction from an output perturbation perspective, 2026
  • Sparsegpt: Massive language models can be accurately pruned in one-shot
  • Q-filters: Leveraging qk geometry for efficient kv cache compression (opens arXiv in a new tab)
  • Caote: Optimizing kv cache memory through attention output error-based token eviction
  • Obcache: Optimal brain kv cache pruning for efficient long-context llm inference
  • Attention score is not all you need for token importance indicator in kv cache reduction: Value also matters
  • Second order derivatives for network pruning: Optimal brain surgeon
  • Kvquant: Towards 10 million context length llm inference with kv cache quantization
  • Kvzip: Query-agnostic kv cache compression with context reconstruction
  • Optimal brain damage
  • Snapkv: Llm knows what you are looking for before generation
  • Lagkv: Lag-relative information of the kv cache tells which tokens are important (opens arXiv in a new tab)
  • Scissorhands: Exploiting the persistence of importance hypothesis for llm kv cache compression at test time
  • KIVI: A tuning-free asymmetric 2bit quantization for KV cache
  • Dynamic memory compression: Retrofitting LLMs for accelerated inference
  • Transformers are multi-state rnns
  • Quality: Question answering with long input texts, yes!, 2022 (opens arXiv in a new tab)
  • Eigen attention: Attention in low-rank space for kv cache compression
  • Gemma 3 Technical Report
  • Dynamic discriminative operations for efficient long-context inference of large language models (opens arXiv in a new tab)
  • Model tells you where to merge: Adaptive kv cache merging for llms on long-context tasks (opens arXiv in a new tab)
  • Efficient streaming language models with attention sinks
  • Qwen3 Technical Report
  • CaM: Cache merging for memory-efficient LLMs inference
  • Heavy-hitter oracle for efficient generative inference of large language models
  • Fast kv compaction via attention matching
  • Throughout, $S$ is the current support, $A \triangleq P_S^\top P_S + \lambda I$, $G = A^{-1}$, and $B = GP_S^\top Y$ as in (10)
  • Removing column $j$ from $P_S$ removes row and column $j$ from $P_S^\top P_S$ and changes no other entry, and $\lambda I$ loses the same row and column
  • The second equation gives $A_{-j,j} = -A_{-j,-j} G_{-j,j} / G_{jj}$, and substituting this into the third gives $A_{-j,-j} \left( G_{-j,-j} - G_{-j,j} G_{j,-j} / G_{jj} \right) = I$
  • Equivalently, the matrix $M_j \triangleq G - G_{:,j}G_{j,:}/G_{jj}$ has zero row $j$ and zero column $j$, and its remaining block is $(A_{-j,-j})^{-1}$
  • Then $U = V_S$, $\mu_i = 1 - P_{ij}$, and $\sum_{k \in S} P_{ik} v_k - \mu_i o_i = (o_i - P_{ij} v_j) - (1 - P_{ij}) o_i = -P_{ij}(v_j - o_i)$, so (2) and (4) give
  • The error is the product of two factors
  • The pool consists of the $m = \min\{T, \lceil ct \rceil\}$ entries with the largest scores (13), with $c = 4$
  • All $d_v$ columns of $\widehat{V}$ share the Gram matrix $\widehat{P}^\top \widehat{P}$ and are solved at once
  • Appendix C.1 states the protocol and conventions and Appendix C.2 breaks down where the pipeline time goes
  • The model reads a context once, the resulting cache is compacted once by a target factor between $5 \times$ and $100 \times$, and all benchmark questions about that context are answered from the compacted cache, so calibration and compaction are one-time costs per context
  • >Model, benchmark</td><td rowspan=
  • The benchmark is the large_context configuration of its project-level code completion task: each datapoint is a repository snapshot at a fixed commit, one held-out completion file, and a list of target lines in that file
  • We walk the test split in order and keep the first snapshot of each not-yet-seen repository whose Python files plus completion file fit 120,000 Gemma-3 tokens, until 20 repositories are collected; the dataset order and commit hashes are recorded, so the selection is deterministic
  • As for LongHealth (Appendix H), each repository is compacted in chunks: consecutive files are packed into chunks of at most 10,000 tokens without crossing file boundaries, about nine per repository, each chunk is compacted at the target ratio, and the compacted chunks are concatenated
  • Each entry is the mean over five seeds; the grey number is the standard error over seeds
  • Every ablation runs on Qwen3-4B with QuALITY at 50×, changes one component of the pipeline of Section 3 at a time, and reports the mean over five seeds with one standard error over seeds
  • Table 15 switches them independently for our methods and the attention-matching baselines; the ported eviction methods are walked through the same switches in Table 16
  • >Ablation</th><th rowspan=
  • Time is pipeline time per context (calibration-query generation plus compaction)
  • >Pool factor</td><td colspan=
  • >Final refit re-solves</td><td colspan=
  • The three panels are separate runs, so the same configuration can appear in more than one panel with slightly different numbers
  • the support-fixed refit (values and multipliers) and the optimized per-head budgets
  • >Method</th><th rowspan=
  • All measurements use Qwen3-4B and Llama-3.1-8B, five QuALITY articles, and every head, that is, 1440 head–article pairs on Qwen3-4B and 1280 on Llama-3.1-8B
  • Each port replaces the method's own queries by the shared repeat-pass calibration queries, keeps its selection score, and serves the selected support with the shared per-head budgets and final refit of Section 3.4
  • As published: own queries (H2O and KVMerger use context prefill, OBCache its last-64 observation window, KVzip its repeat prompt with a global cross-head threshold), uniform or own head allocation, no refit
  • Even with every component, the best ported rule, OBCache at 62.4, stays 3.5 points below KV-SURGEON-fast (65.9) at the same cost, and H2O at 62.3 is within noise of OBCache
  • Then the served output is
  • Two consequences of Section 3.2 matter here: for how the refit must be run, and for what mass matching can tell selection
  • All compacted: all five records compacted
  • >Multipliers</td><td colspan=
  • Fewer than half a percent of kept coefficients end negative at any ratio, the median head has no negative coefficients, and the numbers are unchanged between the repeat and self-study query tiers