Abstract
Writing code requires significant time and effort in software development. To automate this process, researchers have made substantial progress for code generation. Recently, large language models (LLMs) have demonstrated remarkable proficiency in function-level code generation, yet their performance significantly degrades in the real-world software development process, where coding tasks are deeply embedded within specific repository contexts. Existing studies attempt to use retrieval-augmented code generation (RACG) approaches to mitigate this demand. However, there is a gap between natural language (NL) requirements and programming implementations. This results in the failure to retrieve the relevant code of these fine-grained subtasks. To address this challenge, we propose GraphCodeAgent, a dual graph-guided LLM agent for retrieval-augmented repo-level code generation, bridging the gap between NL requirements and programming implementations. Our approach constructs two interconnected graphs: a Requirement Graph (RG) to model requirement relations of code snippets within the repository, as well as the relations between the target requirement and the requirements of these code snippets, and a Structural-Semantic Code Graph (SSCG) to capture the repository's intricate code dependencies. Guided by this, an LLM-powered agent performs multi-hop reasoning to systematically retrieve all context code snippets, including implicit and explicit code snippets, even if they are not explicitly expressed in requirements. We evaluated GraphCodeAgent on three advanced LLMs with the two widely-used repo-level code generation benchmarks DevEval and CoderEval. Extensive experiment results show that GraphCodeAgent significantly outperforms state-of-the-art baselines.
Introduction
In real-world software development, code is rarely written in isolation. New functions must interact with existing APIs, classes, and project-specific conventions distributed across a repository. While Large Language Models (LLMs) excel at standalone function generation, their performance drops significantly in repository-level tasks. Standard Retrieval-Augmented Code Generation (RACG) attempts to solve this by fetching relevant snippets, but often fails because high-level natural language (NL) prompts (e.g., "implement user registration") do not explicitly mention fine-grained, implicit dependencies like database connections or utility functions. This semantic gap leads to hallucinations or redundant code.
Methodology: The GraphCodeAgent Framework
To bridge the gap between high-level intent and complex implementation, GraphCodeAgent utilizes two interconnected graphs to guide an LLM-powered agent:
- Requirement Graph (RG): This graph maps relationships between the requirements (descriptions or docstrings) of code snippets. It captures parent-child subtask hierarchies and semantic similarities, allowing the system to translate high-level goals into sub-goals before searching for syntax.
- Structural-Semantic Code Graph (SSCG): This graph captures the repository's actual code structure, including syntactic relationships (imports, inheritance, invocations) and semantic similarities between code elements.
Agentic Workflow
The system employs a ReAct (Reasoning and Acting) agent equipped with specialized tools. The agent uses RGRetrieval to find related sub-requirements, DualGraphMapping to link those requirements to code nodes in the SSCG, and SSCGTraverse to perform multi-hop reasoning. This allows the agent to actively hunt down implicit dependencies that are structurally vital but not textually similar to the initial prompt.
Experimental Evaluation
GraphCodeAgent was evaluated using GPT-4o, Gemini-1.5-Pro, and QwQ-32B on two execution-based benchmarks: DevEval (1,825 examples from 117 repositories) and CoderEval (Python tasks from open-source projects).
Key Results
- State-of-the-Art Performance: On DevEval, GraphCodeAgent achieved 58.14% Pass@1 with GPT-4o, representing a 43.81% relative improvement over standard Dense RACG baselines.
- Complex Scenario Dominance: In tasks requiring complex cross-file dependencies, the system nearly doubled baseline performance, achieving a 94.30% relative improvement.
- Retrieval Efficiency: As shown in the comparison below, GraphCodeAgent's multi-hop traversal successfully retrieves code snippets that existing RACG methods miss.
Discussion and Insights
An ablation study revealed that removing the SSCGTraverse tool caused a 12% drop in performance, confirming that active, multi-hop navigation is critical for capturing implicit knowledge. Furthermore, the system's success highlights that NL-to-NL mapping (via the Requirement Graph) is more effective than direct NL-to-Code mapping for bridging the semantic gap. As reasoning-heavy models like QwQ-32B evolve, providing them with structured, graph-based context becomes increasingly powerful.
Conclusion
GraphCodeAgent demonstrates that an agentic approach guided by dual requirement and code graphs can significantly improve repository-level code generation. By explicitly modeling both the intent (requirements) and the structure (code dependencies), the framework enables LLMs to navigate complex software architectures and produce functionally correct code that integrates seamlessly into existing projects.
Summary updated