Execution Guided Line-by-Line Code Generation

Boaz Lavon, Shahar Katz, Lior Wolf

Abstract

While Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation, they often struggle with logical consistency, producing code that is syntactically correct but fails during execution. This paper introduces Execution-Guided Classifier-Free Guidance (EG-CFG), a novel framework that integrates real-time execution feedback directly into the token-by-token generation process. By sampling candidate completions at line boundaries and using runtime traces to guide the model via Classifier-Free Guidance, EG-CFG achieves new state-of-the-art results across major benchmarks, including MBPP, HumanEval, and CodeContests, even outperforming leading proprietary models like GPT-4o.

Introduction

A fundamental limitation of current LLMs in programming tasks is their reliance on static pattern recognition. Unlike human programmers, who iteratively write, test, and debug code, standard LLMs typically generate entire blocks of code in a single pass. This often leads to "hallucinations" where the model loses track of variable states or logic flow, resulting in runtime errors that are only discovered after the generation is complete.

Existing "self-debugging" methods attempt to rectify this by generating a full solution and then fixing it in a separate pass. However, this approach can be inefficient and prone to compounding errors. EG-CFG proposes a "bottom-up" alternative: providing the model with a "test-drive" capability at every line of code, allowing it to adjust its trajectory based on actual execution data.

Methodology: The EG-CFG Framework

The EG-CFG process operates as a continuous loop during the generation phase, consisting of several key stages:

  1. Line-by-Line Generation: The model generates code until it reaches a logical line boundary.
  2. Candidate Sampling & Completion Horizon: At the end of a line, the system uses beam search to sample multiple potential completions for the remainder of the function. This provides a "lookahead" to see where the current path might lead.
  3. Executable Extraction: To ensure these completions can actually run, the system employs Abstract Syntax Tree (AST) parsing. This step automatically adds necessary syntax, such as pass statements, to close open loops or conditional blocks.
  4. Real-time Execution & Trace Capture: These candidates are executed against provided test cases. The system captures detailed "execution traces," including variable names, values, types, and specific exception details, rather than a simple binary pass/fail signal.
  5. Classifier-Free Guidance (CFG): This is the core mathematical innovation. The model calculates two sets of probabilities for the next token: one based on the original prompt and one conditioned on the execution traces. By interpolating between these two distributions, the model is nudged toward tokens that align with successful execution paths.
  6. Task-Level Parallelism: The system runs multiple agents with varying configurations (e.g., different temperatures or guidance strengths) in parallel. The first agent to produce a solution that passes all test cases is selected as the winner.

Experimental Evaluation

The researchers evaluated EG-CFG across six diverse benchmarks, with a particular focus on "Extended Test" (-ET) variants designed to prevent models from passing through "lucky" logic:

  • MBPP & MBPP-ET: Entry-level Python tasks.
  • HumanEval & HumanEval-ET: Standard hand-written Python challenges.
  • DS-1000: Complex data science problems involving libraries like Pandas and NumPy.
  • CodeContests: High-difficulty competitive programming problems.

Key Results

EG-CFG established new state-of-the-art (SOTA) benchmarks across the board. Notably, when applied to the open-source DeepSeek-V3 model, the method surpassed the performance of top-tier proprietary models:

Benchmark EG-CFG Accuracy Previous Best / Competitor
MBPP 96.6% 94.2% (Previous SOTA)
HumanEval-ET 89.02% 84.8% (GPT-4o)
DS-1000 69.9% New SOTA
CodeContests 60.6% Significant jump over standard LLMs

Ablation studies confirmed that both the lookahead (beam search) and the guidance mechanism (CFG) are critical; removing either component resulted in a significant drop in performance.

Discussion and Insights

Soft Guidance vs. Hard Feedback

A primary insight of the study is the efficacy of "soft" guidance. By providing raw execution traces (variable values and types) rather than just error messages, EG-CFG allows the model to autonomously interpret why a specific path is failing at the token level. This mirrors the utility of a debugger for a human programmer.

The Cost of Precision

The primary trade-off for this increased accuracy is computational cost. EG-CFG is significantly slower than standard inference because it requires multiple executions and beam searches for every line generated. However, for high-stakes or complex programming tasks, the jump in reliability may justify the additional compute.

Limitations and Future Directions

The system currently requires at least one test case to provide a guidance signal, limiting its utility in scenarios where tests are not yet written. Furthermore, the approach is strictly "bottom-up." The authors suggest that future work could combine EG-CFG with "top-down" task decomposition—breaking large problems into sub-functions before applying line-by-line guidance—to achieve even higher levels of reasoning.

Conclusion

EG-CFG represents a significant step toward more reliable AI coding assistants. By grounding the generation process in the reality of execution, it moves LLMs away from mere pattern matching and toward a more iterative, human-like approach to software development. The success of this method with open-source models suggests a promising path for narrowing the gap between open and proprietary AI capabilities.

Summary updated

BibTeX
@inproceedings{lavon2025execution,
  author = {Boaz Lavon and Shahar Katz and Lior Wolf},
  title = {{Execution Guided Line-by-Line Code Generation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=ySFDPoiANu},
}
Figure