NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Planning with Large Language Models for Code Generation

Shun Zhang, Zhenfang Chen, Yikang Shen, Mingyu Ding, Joshua B. Tenenbaum, Chuang Gan

Introduction

Generating code is fundamentally different from generating natural language. In a standard paragraph, a single typo or a slightly awkward word choice rarely destroys the overall meaning. In a computer program, however, a single incorrect token—a misplaced bracket or an incorrect variable name—can lead to a compilation error or a total logic failure.

Current Large Language Model (LLM) pipelines typically rely on beam search (keeping the most likely sequences) or sampling and filtering (generating many programs and picking the one that passes tests). The primary flaw in these approaches is that models generate code "blindly." They do not consider the execution results until the entire program is finished. This is incredibly sample-inefficient; the model might spend massive computational resources completing a program that was doomed to fail after the third line.

To address this, researchers have developed Planning-Guided Transformer Decoding (PG-TD), a novel method that allows LLMs to generate code by "thinking ahead." By integrating a planning algorithm that tests partial code against unit tests during the generation process, PG-TD significantly outperforms standard decoding methods, producing code that actually works rather than just looking correct.

The PG-TD Methodology

Instead of merely predicting the next most likely token, PG-TD employs a planning algorithm—inspired by Monte Carlo Tree Search (MCTS)—to look ahead.

The system operates in a continuous loop consisting of four key phases:

  1. Selection: The planner uses a heuristic called P-UCB (Predictor Upper Confidence Bound) to decide which partial code path to explore. This balances "likely" tokens suggested by the LLM with "unexplored" paths in the search tree.
  2. Expansion: The algorithm identifies the top-$k$ most likely next tokens to branch the search tree further.
  3. Evaluation: For a given partial program, the model uses the LLM to quickly complete the code (using a fast beam search) and runs it against public test cases. The pass rate of these completions serves as a critical reward signal.
  4. Backpropagation: The reward is sent back up the tree, informing the model which early tokens are actually leading to successful, compiling programs.
The Planning-Guided Transformer Decoding (PG-TD) loop, illustrating the Selection, Expansion, Evaluation, and Backpropagation steps.

To make this computationally feasible, the authors introduced two critical caching mechanisms. Tree Structure Caching reuses information about the search tree across different iterations, while Sequence Caching stores completed sequences so the model doesn't re-run the same beam search multiple times for an identical code prefix.

Experimental Setup

The researchers evaluated PG-TD on two major competitive programming benchmarks:

  • APPS: A dataset containing 10,000 problems ranging from "Introductory" to "Competition" difficulty levels.
  • CodeContests: A benchmark from DeepMind’s AlphaCode project featuring complex problems from platforms like Codeforces.

Crucially, the available test cases were split into public (used by the planner to guide generation during the Evaluation phase) and private (used exclusively for the final evaluation to ensure the model didn't simply overfit to the visible tests).

Key Insights and Results

PG-TD acts as a model-agnostic "plug-and-play" upgrade. It works with models ranging from GPT-2 to high-end models like Codex without requiring any fine-tuning of the underlying weights. Furthermore, because the planner is guided by a reward function, it enables controllable generation. By adding penalties for code length or rewards for comments into the reward function, the LLM can be forced to generate more concise or better-documented code.

Most importantly, PG-TD consistently outperformed baseline methods across the board. It allows the model to "stop and pivot" early; if a certain line of code leads to completions that fail all tests, the planner de-prioritizes that path immediately.

Pass rate comparison showing PG-TD outperforming baseline sampling and filtering methods across varying numbers of Transformer generations.

Key performance highlights include:

  • Pass Rate Boost: On the APPS "Introductory" set using GPT-2, standard beam search achieved an 11.95% pass rate. PG-TD more than doubled this to 26.70%.
  • Superior Efficiency: Even when compared to "Sampling + Filtering" (the method used by AlphaCode), PG-TD achieved higher pass rates with the same computational budget because it was far more intentional about its token choices.
  • Scaling to Codex: When applied to the powerful code-davinci-002 (Codex) model on the HumanEval benchmark, PG-TD improved strict accuracy from 26.82% to a staggering 74.53% when using auto-generated test cases.

Limitations and Future Work

The primary limitation of PG-TD is its reliance on test cases. If a problem lacks unit tests, or if the provided tests are poor, the planner loses its primary compass. While the authors demonstrated that the LLM can be used to generate its own test cases to mitigate this, those generated tests are not always perfectly accurate.

Additionally, while caching helps significantly, PG-TD remains more computationally expensive than a single pass of standard beam search. Future work aims to replace the expensive "evaluation" step (actually running the code) with a learned "value function"—a neural network trained to predict whether a piece of code will work just by analyzing its structure, akin to how AlphaGo evaluates board positions without playing out the entire game.

Summary updated

BibTeX
@inproceedings{zhang2023planning,
  author = {Shun Zhang and Zhenfang Chen and Yikang Shen and Mingyu Ding and Joshua B. Tenenbaum and Chuang Gan},
  title = {{Planning with Large Language Models for Code Generation}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=Lr8cOOtYbfL},
}
Figure