CodeComp: Structural KV Cache Compression for Agentic Coding

Qiujiang Chen, Jing Xiong, Chenyang Zhao, Sidi Yang, Ngai WongarXiv 2026

TL;DR

CodeComp is a training-free Key-Value (KV) cache compression framework designed specifically for repository-scale agentic coding tasks. By incorporating static program analysis via Code Property Graphs (CPGs), it explicitly protects structurally critical tokens—like function calls, branch conditions, and assignments—from being incorrectly pruned during long-context LLM inference. This approach allows CodeComp to drastically reduce memory usage while outperforming attention-only compression baselines and matching full-context accuracy.

What Problem Does This Paper Address?

When Large Language Models (LLMs) act as software engineering agents to localize bugs or generate patches across entire codebases, they must process tens of thousands of tokens. This creates a massive bottleneck in the Key-Value (KV) cache, which stores intermediate attention states to prevent redundant computations. The KV cache grows linearly with context length and can consume up to 70% of total GPU memory, often exceeding hardware limits.

Existing KV cache compression methods (like SnapKV or H2O) rely purely on attention scores to decide which tokens to keep and which to evict. While this works well for natural language, it fails catastrophically for source code. Code semantics are governed by formal structures (control flows, data dependencies) rather than surface-level token co-occurrences. As a result, attention-only methods systematically discard low-attention but structurally critical tokens—such as variable definitions or error-checking branch conditions—rendering the model blind to vital program logic.

Figure 1: Attention-only compression discards structurally critical tokens such as function calls, branch predicates, and return statements. CodeComp uses CPG priors to explicitly preserve these tokens.

Methods

CodeComp introduces a training-free, structure-aware compression pipeline that integrates static program analysis directly into the LLM inference process via SGLang:

  1. Query-Conditioned Chunk Selection: Codebases are partitioned into function- or method-level chunks. CodeComp uses query-conditioned perplexity (PPL) to filter and select the top-$k$ most relevant chunks, narrowing down the search space.
  2. Structural Feature Extraction: For the selected chunks, CodeComp uses the open-source static analysis tool Joern to extract a Code Property Graph (CPG). It counts critical syntax elements (function calls, control flows, returns, assignments) to compute a normalized structural importance score for each chunk.
  3. Structure-Aware Budget Allocation: Instead of distributing the KV cache budget uniformly, CodeComp dynamically allocates more cache capacity to chunks with higher structural importance.
  4. Span-Level Structural Protection: Within each chunk, contiguous sequences of tokens (spans) representing critical code elements are explicitly locked in the KV cache, ensuring they survive eviction regardless of their attention scores. It also applies a hard "query-aware" rule to protect spans containing symbols present in the user prompt.
  5. Residual Attention Selection: Standard attention-based eviction is finally applied to fill whatever remaining cache capacity is left over after structural protection.

Datasets

The authors evaluated CodeComp across five long-context benchmarks spanning two core task types:

  • Code Generation / Patching: SWE-bench Lite and the Library-based Code Generation (LCA) benchmark.
  • Bug Localization: InfiniteBench-CodeDebug, DebugBench, and LongCodeQA.

Key Insights and Ideas

  • The Attention-Structure Mismatch: The authors discovered that attention scores are a poor proxy for code structure. The Jaccard overlap between the top 20% of code chunks ranked by attention vs. those ranked by structural importance is a meager 0.0944—meaning they select almost entirely disjoint parts of the codebase.
  • Systematic Mis-pruning: Under aggressive attention-only compression, models discard roughly 52% of callsites and 42% of assignments, which explains the steep drop-off in coding capabilities for compressed LLMs.
  • Local Evidence Matters Most: Ablation studies revealed that span-level protection (keeping local syntax intact) is the dominant driver of performance gains, far outweighing the benefits of simply reallocating cache budgets between different files.
Figure 2: Motivation analysis for KV cache compression in code. (a) Attention–structure mismatch: attention-based importance is weakly aligned with CPG-derived structural importance, causing structurally critical chunks to receive low attention scores. (b) Structural mis-pruning: under attention-only compression, a large fraction of critical structural tokens (e.g., callsite, assignment, and return) are incorrectly discarded. (c) Structure-aware retention: incorporating structural priors significantly improves the preservation of critical structural tokens across categories compared to attention-based methods.

Key Results

  • SWE-bench Lite: At a strict KV cache capacity ratio of 0.4, the attention-only baseline ParallelComp collapsed to a ground-truth file localization F1 score of 0.021 on DeepSeek-Coder. CodeComp recovered this to 0.250 while achieving a perfect 1.000 patch validity rate, matching the uncompressed full-context baseline.
  • Bug Localization: On DebugBench using Llama3-8B (at cap=0.4), ParallelComp scored a near-zero 0.03 accuracy. CodeComp achieved 0.43 (a 14× improvement). On Qwen3-8B at cap=0.6, CodeComp achieved an average accuracy of 0.53 across benchmarks, recovering 91% of the uncompressed model's performance.
  • Negligible Overhead: Despite invoking an external static analysis tool (Joern), CodeComp maintains stable end-to-end latency (112–118 seconds on SWE-bench Lite), proving that the structural analysis overhead does not hinder high-throughput agentic workflows.

Limitations and Future Work

While CodeComp significantly advances the Pareto frontier of accuracy vs. memory efficiency, the absolute resolve rates on challenging benchmarks like SWE-bench Lite remain modest for all tested models (compressed or otherwise), indicating that KV cache size is not the sole bottleneck in agentic coding. Additionally, CodeComp currently relies on Joern for parsing, introducing an external dependency. The authors suggest that future work should explore deeper, native structure-aware inference optimizations tailored specifically for agentic software engineering pipelines.

Summary updated

BibTeX
@misc{chen2026codecomp,
  author = {Qiujiang Chen and Jing Xiong and Chenyang Zhao and Sidi Yang and Ngai Wong},
  title = {{CodeComp: Structural KV Cache Compression for Agentic Coding}},
  year = {2026},
  eprint = {2604.10235},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2604.10235},
}
Figure