Abstract
Modern AI code assistants often suffer from "nearsightedness," primarily focusing on the immediate file under edit while ignoring broader repository context. This paper introduces ContextModule, a framework developed by ByteDance that enhances LLM-based code completion by integrating three critical contextual streams: recent user behavior, similar code patterns from the repository, and deep symbol definitions. By utilizing a custom Code Knowledge Graph (CKG) and an efficient caching mechanism, ContextModule improves completion accuracy and user acceptance rates while maintaining the sub-80ms latency necessary for real-time development.
1. Introduction
Current Large Language Model (LLM) based code completion tools often fail to capture the full scope of a developer's intent because they lack access to cross-file information. Software development is inherently interconnected; logic often depends on functions defined in disparate directories or follows patterns established elsewhere in a large codebase. The authors identify three primary challenges: limited context beyond the active file, neglect of developer intent (browsing history), and the "latency trap" where retrieving repository-wide information exceeds the real-time response threshold.
2. Methodology
ContextModule addresses these challenges by enriching the LLM's prompt with three distinct types of repository-level information.
2.1 User Behavior Code
The system tracks developer interactions, such as cursor movements and file browsing history. If a developer recently viewed a specific utility or configuration file, ContextModule extracts relevant snippets from those files. This approach assumes that recent browsing history is a strong signal of current intent, providing the model with the "big picture" of the task at hand.
2.2 Similar Code Retrieval
To find relevant patterns across the repository, ContextModule employs a token-based similarity search using Jaccard similarity. A key technical innovation is the "Cut Token" strategy, which decomposes camelCase and snake_case identifiers into constituent parts. This allows for more granular semantic matching compared to standard whole-token approaches.
2.3 CKG-based Symbol Definition
Relying on standard Language Server Protocol (LSP) can be slow for large-scale retrieval. ContextModule utilizes a proprietary Code Knowledge Graph (CKG) to perform high-speed lookups of precise function signatures and struct definitions. This ensures that the LLM has access to the exact interfaces it needs to reference, reducing hallucinations during struct initialization or function calls.
3. System Implementation and Caching
To meet the "golden rule" of sub-80ms latency for developer tools, ContextModule implements a sophisticated caching and retrieval workflow.
The system uses a Breadth-First Search (BFS) priority for file indexing, focusing on files within the same or adjacent directories. The index is capped at the 3,000 most relevant files to ensure that retrieval remains instantaneous even in massive repositories.
4. Evaluation and Results
The framework was evaluated across several coding scenarios—including function definitions, if-blocks, and return statements—using custom datasets for Go, Python, and TypeScript, each containing 1,500 samples. These datasets were constructed using a "digging holes" methodology and real-world anonymized editing data from ByteDance developers.
Key results using the DeepSeek-Coder-6.7B model include:
- Accuracy Gains: Incorporating user behavior improved the Soft Exact Match (SEM) metric by up to 2.55%.
- Pattern Recognition: For TypeScript, using "Similar Code" context within an 8k token window increased SEM from 27.26% to 45.3%.
- Reduced Hallucination: Symbol definitions via CKG improved struct initialization accuracy by over 10%.
- Real-world Impact: Internal deployment at ByteDance saw a 7.1% increase in the "Acceptance Rate" for suggestions influenced by user behavior and a 4.9% increase for those using symbol definitions.
5. Conclusion
ContextModule demonstrates that repository-level context is vital for high-quality code completion. By intelligently combining developer intent signals with structural code data and maintaining strict performance constraints, the framework significantly enhances the utility of AI-driven development tools. Future work will focus on expanding language support for the CKG and incorporating context from external open-source libraries.
Summary updated