LongCodeZip: Compress Long Context for Code Language Models

Yuling Shi, Yichun Qian, Hongyu Zhang, Beijun Shen, Xiaodong GuarXiv 2025 · DOI

Abstract

Large Language Models (LLMs) are increasingly applied to repository-level tasks, requiring them to process extensive code contexts. However, long contexts introduce significant computational overhead and often lead to performance degradation due to the "lost in the middle" phenomenon. We present LongCodeZip, a training-free, hierarchical framework that compresses code contexts by up to 5.6x. By leveraging Approximated Mutual Information (AMI) and structural code analysis, LongCodeZip preserves critical functional dependencies while discarding irrelevant noise. Our evaluation across multiple benchmarks demonstrates that LongCodeZip not only reduces latency and API costs by over 70% but also maintains or improves model performance compared to full-context baselines.

Introduction

The integration of LLMs into software engineering workflows has shifted from simple snippet generation to complex repository-level reasoning. Processing these large codebases presents three primary challenges: efficiency, where quadratic attention complexity leads to high latency; information retrieval, where models struggle to identify relevant "needles" in vast "haystacks"; and structural integrity, as traditional text compression often breaks code syntax. Existing solutions like Retrieval-Augmented Generation (RAG) often fail to capture implicit functional dependencies that lack surface-level text similarity.

Methodology

LongCodeZip addresses these issues through a "coarse-to-fine" hierarchical compression pipeline that is model-agnostic and requires no additional training.

Hierarchical Compression Pipeline

  1. Coarse-Grained Compression (Function Level): The codebase is partitioned into function-level chunks. These are ranked using Approximated Mutual Information (AMI), which quantifies how much a specific code block reduces the model's perplexity regarding the target task.
  2. Fine-Grained Compression (Block Level): Within selected functions, semantic boundaries are identified by monitoring perplexity spikes, allowing for the removal of non-essential internal blocks.
  3. Adaptive Budget Allocation: The framework dynamically allocates token budgets, allowing highly relevant functions to retain more detail while aggressively pruning less critical ones.
  4. Dynamic Block Selection: Final selection is optimized as a 0/1 Knapsack Problem to maximize total relevance within a fixed token budget.

Perplexity as a Relevance Signal

A core innovation of LongCodeZip is the use of perplexity as a relevance signal. Unlike RAG, which relies on embedding similarity, AMI identifies code that is functionally informative. As shown in Figure 1, a configuration class might have low text similarity to a training function but high mutual information because it defines critical hyperparameters. This approach captures deep functional dependencies that lexical similarity misses.

Evaluation and Results

LongCodeZip was evaluated on three benchmarks: Long Code Completion, Long Module Summarization, and RepoQA.

  • Efficiency: The framework achieved up to 5.6x compression. On the Long Code Completion task, it reduced generation time from 15.7s to 6.6s and slashed input token costs by approximately 77%.
  • Performance: It consistently outperformed baselines like LLMLingua, RAG, and specialized code compressors (DietCode, SlimCode). Notably, in RepoQA, compressed contexts sometimes yielded better results than full contexts, suggesting that LongCodeZip effectively filters out distracting noise.
  • Generalizability: The method proved effective across various models, including DeepSeek-Coder, Qwen2.5-Coder, GPT-4o, and Claude-3.7-Sonnet, even when using a small 0.5B parameter model as the compressor.

Discussion and Conclusion

LongCodeZip demonstrates that intelligent, structure-aware compression is a viable path toward efficient repository-level LLM applications. By respecting function and class boundaries, the compressor avoids the "syntax gore" created by token-level pruners. While it currently depends on the clarity of task instructions for ranking, its ability to preserve functional logic while slashing costs makes it a powerful tool for developers. Future work will explore hybrid approaches combining RAG's retrieval breadth with LongCodeZip's compression density.

Summary updated

BibTeX
@misc{shi2025longcodezip,
  author = {Yuling Shi and Yichun Qian and Hongyu Zhang and Beijun Shen and Xiaodong Gu},
  title = {{LongCodeZip: Compress Long Context for Code Language Models}},
  year = {2025},
  eprint = {2510.00446},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.00446},
}
Figure