What to Retrieve for Effective Retrieval-Augmented Code Generation? An Empirical Study and Beyond

Wenchao Gu, Juntao Chen, Yanlin Wang, Tianyue Jiang, Xingzhe Li, Mingwei Liu, Xilin Liu, Yuchi Ma, Zibin ZhengarXiv 2025 · DOI

Introduction

Generating standalone functions from natural language is a task modern Large Language Models (LLMs) perform with high proficiency. However, repository-level code generation—writing code that integrates seamlessly with a massive, complex existing codebase—remains a significant challenge. The primary hurdles include intricate cross-file dependencies and the limited context windows of current LLMs. To address this, state-of-the-art approaches employ Retrieval-Augmented Generation (RAG) to pull relevant information from the repository into the LLM's prompt.

Despite the popularity of RAG, the field has lacked a rigorous analysis of what information actually assists the model. Do LLMs benefit most from similar code snippets, a list of available APIs, or simply the local file context? This paper presents an empirical evaluation of these information sources and introduces AllianceCoder, a framework designed to accurately retrieve the right APIs by bridging the semantic gap between natural language and source code.

Empirical Study: What Information Matters?

The authors conducted a preliminary study testing combinations of three information types: local context, similar code snippets, and invoked APIs. The findings challenged several common assumptions in the RAG community.

The Synergy of Context and APIs

The study found that providing the local file context (such as imports and class definitions) alongside specific APIs to be invoked yields the best results. This combination effectively reduces a complex repository-level task into a simpler function-level task, allowing the LLM to focus on logic rather than searching for definitions.

Venn diagram illustrating the intersection of correct code generations using Context, API, and combined ConAPI information.

The "Similar Code" Trap

Contrary to the design of many existing RAG frameworks, retrieving "similar" code snippets often degrades performance. Because it is rare to find a functionally identical snippet within the same repository, the retrieved code is usually just different enough to mislead the LLM, introducing noise rather than helpful patterns. In the preliminary study, adding similar code to the prompt degraded performance by up to 15% compared to using just context and APIs.

Prompt Length and Information Density

Naively increasing the amount of retrieved information can be counterproductive. The authors observed that stuffing the prompt with context, APIs, and similar code can cause the LLM to fail on test cases it would have passed with less, more targeted information. Analysis of prompt lengths suggests that more concise, relevant prompts are often associated with higher success rates.

Comparison of prompt lengths for cases where only API retrieval was correct versus when both were correct, indicating that longer prompts do not always equate to better performance.

AllianceCoder: Bridging the Semantic Gap

Based on these insights, the authors developed AllianceCoder, a pipeline that automates the retrieval of relevant APIs while avoiding the "semantic gap"—the difficulty of matching natural language queries directly to source code.

AllianceCoder operates in three phases:

  1. Repository API Processing: The system uses an LLM to generate natural language descriptions for every API in the repository. These descriptions are then encoded into vector embeddings, ensuring that the retrieval index is based on semantics rather than raw syntax.
  2. Query Processing: When a user submits a coding query, AllianceCoder uses Chain-of-Thought (CoT) prompting to decompose the request into a step-by-step implementation plan. It then generates hypothetical descriptions of the APIs needed for each step.
  3. Context-Integrated Generation: These hypothetical descriptions are matched against the repository's API vectors using cosine similarity. The top retrieved APIs are appended to the local file context and fed into the LLM for final generation.

Key Results

AllianceCoder was evaluated on two benchmarks: CoderEval (real-world open-source tasks) and RepoExec (complex cross-file dependencies). The framework achieved state-of-the-art results using GPT-4o Mini and Gemini 1.5 Flash, improving the Pass@1 metric by 10% to 20% over strong baselines like RepoCoder and RLCoder. On CoderEval, AllianceCoder's performance approached the theoretical upper bound, suggesting that its retrieval strategy is highly effective at identifying the necessary components for successful code generation.

Conclusion and Future Work

This research highlights that for repository-level code generation, the quality and type of retrieved information are more important than the quantity. While AllianceCoder sets a new standard for API-based RAG, future work remains. The current evaluation is primarily focused on Python, and the generalizability to other languages or poorly documented codebases (where API semantics are harder to extract) requires further exploration. Additionally, moving beyond functional correctness to assess code quality and maintainability will be a crucial next step for the field.

Summary updated

BibTeX
@misc{gu2025what,
  author = {Wenchao Gu and Juntao Chen and Yanlin Wang and Tianyue Jiang and Xingzhe Li and Mingwei Liu and Xilin Liu and Yuchi Ma and Zibin Zheng},
  title = {{What to Retrieve for Effective Retrieval-Augmented Code Generation? An Empirical Study and Beyond}},
  year = {2025},
  eprint = {2503.20589},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.20589},
}
Figure