[Paper Review] Teaching Code LLMs to Use Autocompletion Tools in Repository-Level Code Generation
This paper proposes ToolGen, a framework that integrates program-analysis-based autocompletion tools into code LLMs to reduce repository-level dependency errors during code generation. By fine-tuning LLMs on function corpora augmented with mark tokens that trigger autocompletion tools at critical points, and then invoking these tools during inference via constrained greedy search, ToolGen improves dependency coverage by 15.2%–45.8% and static validity rate by 10.9%–42.2% across three code LLMs, while maintaining strong performance on standard similarity metrics.
Code large language models (LLMs) face limitations in repository-level code generation due to their lack of awareness of repository-level dependencies (e.g., user-defined attributes), resulting in dependency errors such as undefined-variable and no-member errors. In this work, we introduce ToolGen, an approach that integrates autocompletion tools into the code LLM generation process to address these dependencies. ToolGen comprises two main phases: Trigger Insertion and Model Fine-tuning (Offline), and Tool-integrated Code Generation (Online). During the offline phase, ToolGen augments functions within a given code corpus with a special mark token, indicating positions to trigger autocompletion tools. These augmented functions, along with their corresponding docstrings, are then used to fine-tune a selected code LLM. In the online phase, ToolGen iteratively generates functions by predicting tokens step-by-step using the fine-tuned LLM. Whenever a mark token is encountered, ToolGen invokes the autocompletion tool to suggest code completions and selects the most appropriate one. We conduct comprehensive experiments to evaluate ToolGen's effectiveness in repository-level code generation. To facilitate this evaluation, we create a benchmark comprising 671 real-world code repositories and introduce two new dependency-based metrics: Dependency Coverage and Static Validity Rate. The results demonstrate that ToolGen significantly improves Dependency Coverage by 31.4% to 39.1% and Static Validity Rate by 44.9% to 57.7% across the three LLMs, while maintaining competitive or improved performance in widely recognized similarity metrics such as BLEU-4, CodeBLEU, Edit Similarity, and Exact Match. On the CoderEval dataset, ToolGen achieves improvements of 40.0% and 25.0% in Pass@1 for CodeT5 and CodeLlama, respectively.
Motivation & Objective
- To address the critical limitation of code LLMs in generating repository-level code due to unawareness of user-defined dependencies such as functions and attributes.
- To reduce dependency errors—like undefined-variable and no-member errors—common in real-world code generation.
- To integrate program-analysis-based autocompletion tools (e.g., Jedi) into the LLM generation pipeline to provide context-aware code suggestions.
- To develop a benchmark of 680 real-world repositories and introduce two new repository-level evaluation metrics: Dependency Coverage and Static Validity Rate.
- To validate the generalizability of the approach across diverse code LLM architectures and model scales.
Proposed method
- Augment a code corpus by inserting special mark tokens at positions where autocompletion should be triggered, based on syntactic and semantic context.
- Fine-tune a selected code LLM on the augmented dataset, where each function is paired with its docstring, to learn to generate the mark tokens at appropriate locations.
- During inference, the fine-tuned LLM generates tokens step-by-step; upon encountering a mark token, the system invokes an external autocompletion tool (e.g., Jedi) to retrieve valid code completions.
- Select the most appropriate completion from the tool’s suggestions using constrained greedy search to ensure syntactic and semantic correctness.
- Use a two-phase pipeline: offline data augmentation and fine-tuning, followed by online tool-integrated code generation.
- Leverage program analysis to enable autocompletion tools to infer accessible attributes and methods based on the current state of the incomplete function and project context.

Experimental results
Research questions
- RQ1RQ1: Can integrating autocompletion tools into code LLMs significantly reduce dependency errors in repository-level code generation?
- RQ2RQ2: How does ToolGen perform across different code LLMs in terms of dependency coverage and static validity rate?
- RQ3RQ3: Does ToolGen generalize across diverse code LLM architectures and model scales?
- RQ4RQ4: How does ToolGen compare to existing approaches that use IR-based tools or prompt engineering in handling repository-level dependencies?
Key findings
- ToolGen improves Dependency Coverage by 15.2% to 45.8% across three distinct code LLMs, indicating a substantial reduction in dependency-related errors.
- Static Validity Rate increases by 10.9% to 42.2%, demonstrating that generated code is more syntactically and semantically correct.
- The approach maintains competitive performance on standard similarity metrics, including BLEU-4, CodeBLEU, and Edit Similarity, indicating no degradation in generation quality.
- Generalizability evaluation confirms consistent performance gains across different code LLM architectures and model scales, validating the method’s robustness.
- The benchmark of 680 real-world repositories and the introduction of Dependency Coverage and Static Validity Rate provide new, relevant evaluation standards for repository-level code generation.
- The absence of data leakage effects in CodeLlama-based results is supported by generalizability results, indicating that performance gains are due to the method, not pretraining data overlap.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.