Research map / Large language models / Language, safety and interpretability
Hierarchical Context Pruning: Optimizing Real-World Code Completion with Repository-Level Pretrained Code LLMs
Read the paperCheck your idea against 108,967 papers
AI-extracted summary. Written by AI from the paper's full text. Numbers and claims can be wrong or missing context, so check the paper before you cite or build on it.
Proposes Hierarchical Context Pruning (HCP), which models a repository at function level, keeps direct-dependency files with global context removed, and samples/prunes other functions by embedding relevance to build shorter, more informative prompts for repo-level code completion with Repo-Code LLMs.
Problem
Concatenating whole repositories into prompts for repository-level pretrained code LLMs exceeds context windows (over 50,000 tokens on average at full dependency level) and degrades completion accuracy, so the prompt must be constructed to fit the window while keeping useful cross-file information.
Approach
Use file-dependency levels and function-level pruning: keep current file in full, strip global context from direct dependency files, and use embedding-based top-k/top-p function sampling (functions kept fully or as headers only) to rank and assemble repository files into a compact prompt.
Key results
- HCP (top-k=5, top-p=0.3) improves EM over Random-All for all six Repo-Code LLMs, e.g., DeepSeek-Coder-6.7B from 33.94 to 39.74 at top-k 5 and CodeGemma-7B from 36.69 to 42.44 at top-k 5 (top-p 1.0).
- HCP reduces input from over 50,000 tokens to about 8,000 tokens according to the paper's introduction.
- Keeping only function headers of dependency-related functions (P-Level 2) yields results only slightly worse than full dependency content, and increasing top-k beyond 5 gives little gain.
Measured effects reported in the paper
| What was changed | Where | Measured | Effect |
|---|---|---|---|
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | DeepseekCoder-1.3B · CrossCodeEval: Python | EM: 6.18 -> 12.88 | Helped |
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | DeepseekCoder-6.7B · CrossCodeEval: Python | EM: 33.94 -> 38.49 | Helped |
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | StarCoder2-3B · CrossCodeEval: Python | EM: 28.32 -> 32.84 | Helped |
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | StarCoder2-7B · CrossCodeEval: Python | EM: 31.45 -> 35.22 | Helped |
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | CodeGemma-2B · CrossCodeEval: Python | EM: 26.93 -> 30.13 | Helped |
| Apply HCP (top-k 5, top-p 0.3) instead of random repository concatenation | CodeGemma-7B · CrossCodeEval: Python | EM: 36.69 -> 41.21 | Helped |
| Retain left truncation when using contexts that exceed the model window (ablation) | Repo-Code LLMs · CrossCodeEval: Python | EM | Mixed |
| Increase dependency context from level 1 to level 2 (ablation) | Repo-Code LLMs · CrossCodeEval: Python | EM: 14.99 -> 12.73; 32.20 -> 30.21; 26.33 -> 26.63; 28.66 -> 29.83; 23.16 -> 21.24; 32.17 -> 28.36 | Mixed |
| Remove global context from cross-file content (P-Level 1 vs P-Level 0) (ablation) | Repo-Code LLMs · CrossCodeEval: Python | EM: 6.18 -> 6.55; 33.94 -> 36.20; 28.32 -> 30.73; 31.45 -> 34.43; 26.93 -> 29.30; 36.69 -> 39.55 | Helped |
| Remove global context, function bodies, and class method bodies from cross-file files (P-Level 2) (ablation) | Repo-Code LLMs · CrossCodeEval: Python | EM: 9.83; 34.73; 30.02; 31.26; 27.34; 38.31 | Mixed |
| Increase top-k from 0 to 5 (ablation) | HCP on DeepseekCoder-6.7B · CrossCodeEval: Python | EM: 36.87 -> 39.74 | Helped |
| Increase top-k from 5 to 10 (ablation) | HCP on DeepseekCoder-6.7B · CrossCodeEval: Python | EM: 39.74 -> 40.30 | Helped |
Surprising findings
- Pruning all function bodies in cross-file dependencies barely reduces accuracy, suggesting function headers suffice for most cross-file context.
- DeepSeek-Coder-1.3B degrades severely with Random-All (EM 6.18) while DeepSeek-Coder models lacking file-separator tokens show no extra fluctuation.
Limitations
- Evaluation relies on exact match and edit similarity, which can misjudge semantically correct completions.
- Function-level sampling requires an embedding model, which may increase latency for repositories with many files.
- Evaluated only on Python in CrossCodeEval.
Experimental setup
Datasets: CrossCodeEval
Benchmarks: CrossCodeEval
Metrics: Exact Match Edit Similarity throughput prompt length
Baselines: Infile-Only RAG-BM25 Random-All
Builds on: CrossCodeEval Tree-Sitter BM25
Closest papers by meaning
- Repoformer: Selective Retrieval for Repository-Level Code Completion (ICML 2024 · similarity 0.89)
- RepoBench: Benchmarking Repository-Level Code Auto-Completion Systems (ICLR 2024 · similarity 0.88)
- LiveCodeBench: Holistic and Contamination Free Evaluation of Large Language Models for Code (ICLR 2025 · similarity 0.85)
- Security Attacks on LLM-based Code Completion Tools (AAAI 2025 · similarity 0.85)
- ChatQA 2: Bridging the Gap to Proprietary LLMs in Long Context and RAG Capabilities (ICLR 2025 · similarity 0.85)
- Structured Packing in LLM Training Improves Long Context Utilization (AAAI 2025 · similarity 0.85)
Writing a paper in this area? scime draft is a free LaTeX editor with AI included, real-time co-authors and a check of your references and venue rules before you submit. It also imports OpenAI Prism projects.
