CodeCoR: An LLM-Based Self-Reflective Multi-Agent Framework for Code Generation

Ruwei Pan, Hongyu Zhang, Chao LiuarXiv 2025 · DOI

Introduction

Large Language Models (LLMs) have demonstrated remarkable capabilities in automated code generation. However, complex logic and strict syntactical requirements often lead to failures in standard sequential workflows. CodeCoR (Code Collaboration and Repair) addresses these limitations by introducing a self-reflective multi-agent framework that incorporates systematic "pruning" at every stage of the generation process. By filtering out low-quality outputs before they propagate through the system, CodeCoR achieves a state-of-the-art average Pass@1 score of 77.8% across major benchmarks.

The Problem: Misunderstanding Propagation

In traditional multi-agent systems, tasks are handled sequentially: a task is analyzed, code is written, and tests are generated. While intuitive, this structure is inherently brittle. A minor error or "misunderstanding" introduced by the initial analysis agent can derail the entire workflow.

Figure 1: An illustration of how a misunderstanding in the initial prompt agent (confusing 'sum' with 'subtraction') propagates through the coding and testing phases, leading to a 'passed' status for incorrect code.

As shown in Figure 1, if the foundation of the task analysis is flawed, subsequent agents—despite performing their specific roles correctly relative to the flawed input—will produce an incorrect final result. This "misunderstanding propagation" is a primary bottleneck in scaling autonomous coding agents.

The CodeCoR Framework

CodeCoR mitigates error propagation by utilizing four specialized agents in a non-linear, iterative architecture:

  1. Prompt Agent: Deconstructs natural language requirements into detailed Chain-of-Thought (CoT) plans.
  2. Coding Agent: Translates these plans into executable Python code.
  3. Test Agent: Independently generates unit tests based on the original task description.
  4. Repair Agent: Diagnoses failures and provides corrective advice when code fails to pass tests.

Self-Reflective Pruning: The "Secret Sauce"

The core innovation of CodeCoR is its Pruning Method. Rather than relying on a single output, each agent generates multiple candidates. These candidates are then evaluated against specific quality criteria and "pruned" if they fail to meet the required standard.

Figure 5: The CodeCoR workflow featuring branching generation and systematic pruning at the prompt, code, test, and repair stages.

As illustrated in Figure 5, the system maintains a pool of candidates at each phase. Prompts are evaluated for clarity and relevance; tests are filtered for executability; and code is pruned if it contains syntax errors. This ensures that only the highest-quality "branches" of reasoning and implementation move forward to the local testing and repair phases. If a code snippet fails, it enters an iterative repair loop (typically optimized at 3 rounds) where the Repair Agent provides feedback to the Coding Agent.

Experimental Evaluation

The framework was evaluated on four Python benchmarks: HumanEval, MBPP, and their "Extended Test" (-ET) counterparts. The -ET datasets are particularly rigorous, featuring a larger volume of test cases designed to expose edge-case failures that standard benchmarks might miss.

Key Results

  • State-of-the-Art Performance: Using GPT-3.5-turbo, CodeCoR achieved a 77.8% Pass@1 score, significantly outperforming previous systems like MapCoder (72.8%). With GPT-4, the performance on HumanEval reached a staggering 94.5%.
  • Robustness: CodeCoR demonstrated superior stability on the harder -ET datasets, maintaining an 80.5% score on HumanEval-ET, whereas simpler models saw much steeper declines.
  • Efficiency: Despite the multi-candidate approach, CodeCoR is more resource-efficient than many competitors. Its pruning logic prevents agents from pursuing "dead-end" reasoning paths, resulting in a runtime nearly twice as fast as standard "Self-Planning" methods.

Conclusion

CodeCoR demonstrates that for LLM-based agents, the ability to "filter" is as critical as the ability to "generate." By implementing self-reflection and pruning, the framework prevents the propagation of errors and ensures high-fidelity code generation. Future work will focus on extending this multi-agent collaborative approach to multi-file software projects and additional programming languages beyond Python.

Summary updated

BibTeX
@misc{pan2025codecor,
  author = {Ruwei Pan and Hongyu Zhang and Chao Liu},
  title = {{CodeCoR: An LLM-Based Self-Reflective Multi-Agent Framework for Code Generation}},
  year = {2025},
  eprint = {2501.07811},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.07811},
}
Figure