Abstract
While Large Language Models (LLMs) have demonstrated significant proficiency in code generation, they often struggle with complex logic and strict constraints, frequently falling into the trap of "hallucinated" algorithms. We present CODESIM, a multi-agent framework designed to mimic human-like mental simulation during the coding process. By integrating step-by-step simulation into both the planning and debugging phases, CODESIM ensures that logic is verified before code is ever written. Our approach achieves new state-of-the-art (SoTA) results on major benchmarks, including 95.1% on HumanEval and 90.7% on MBPP, while demonstrating superior token efficiency compared to existing multi-agent methods.
Introduction
The current paradigm for LLM-based code generation typically follows a "dual-pass" approach: an initial draft is generated and then iteratively refined using an external compiler or debugger. However, this method suffers from a "garbage in, garbage out" bottleneck. If the initial plan is fundamentally flawed, iterative debugging against a compiler often fails to steer the model toward a correct solution. Furthermore, these methods often require a high volume of expensive API calls.
CODESIM addresses these challenges by introducing a robust internal mechanism for logic verification. Rather than relying solely on external execution, CODESIM uses simulation to ground the model's reasoning in concrete, step-by-step state transitions, preventing the model from "coding into a corner" with flawed logic.
Methodology
The Multi-Agent Framework
CODESIM utilizes three specialized agents that operate in a collaborative loop:
- The Planning Agent: This agent begins by recalling a similar "exemplar" problem to inform its strategy. It devises a high-level logic plan and—critically—simulates that plan step-by-step using sample inputs. If the simulation results do not match the expected output, the agent revises the plan internally before any code is generated.
- The Coding Agent: Once a simulation-proven plan is established, the Coding Agent translates the verified logic into executable code.
- The Debugging Agent: In the event of a test case failure, this agent performs an internal "trace." By simulating the code's execution on the failed input, it identifies the specific line or logic gate responsible for the discrepancy, rather than guessing a fix based on generic error messages.
Adaptive Iteration
The framework employs an Adaptive Iteration strategy, looping through the planning phase ($p$ times) and the debugging phase ($d$ times). This allows the system to catch logical errors internally, though it remains compatible with external tools for further refinement.
Experimental Evaluation
Datasets
CODESIM was evaluated across seven diverse benchmarks to test various aspects of programming proficiency:
- Basic Programming: HumanEval, HumanEval-ET, EvalPlus, MBPP, and MBPP-ET.
- Contest-Level Complexity: APPS and CodeContests (featuring hidden test cases and complex algorithmic requirements).
- Multi-Language Support: xCodeEval (Python, C, and Rust).
Key Results
CODESIM established new SoTA benchmarks for pass@1 accuracy:
- HumanEval: 95.1% (rising to 97.6% when cascaded with an external debugger).
- MBPP: 90.7%.
- CodeContests: 29.1% (a significant improvement on this difficult dataset).
- APPS: 22.0%.
Compared to its predecessor, MapCoder, CODESIM achieved a 7.1% average accuracy gain across all datasets while reducing API calls by nearly 40%. On average, CODESIM used 4,130 fewer tokens per problem, proving that simulation-driven precision is more efficient than generating multiple ungrounded candidates.
Discussion and Insights
Simulation as a Mental Model
The core innovation of CODESIM is the realization that LLMs are more effective at following a trace than predicting the final outcome of a complex code block. By forcing the model to explicitly state variable values at each step (e.g., "Iteration 1: x=2, list=[2]"), the framework grounds reasoning in concrete steps, effectively providing the model with a "mental model" of the problem.
Robustness and Efficiency
While GPT-4o provides the highest performance, the simulation-driven approach significantly enhances smaller, open-source models like LLaMa-3.1-70B and Gemma2-9B. This suggests that the framework's structure helps compensate for the inherent reasoning limitations of smaller models.
Limitations
Despite its strengths, CODESIM faces challenges with Dynamic Programming (DP). The state space required to track complex DP tables can become too large for the model to manage effectively within a text prompt, leading to simulation errors in high-complexity algorithmic tasks.
Conclusion
CODESIM demonstrates that simulation-driven planning and debugging can significantly bridge the gap between LLM code generation and human-like problem-solving. By prioritizing logical verification at the source, CODESIM achieves superior accuracy and efficiency, setting a new standard for multi-agent coding frameworks.
Summary updated