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

Effective Large Language Model Debugging with Best-first Tree Search

Jialin Song, Jonathan Raiman, Bryan Catanzaro

TL;DR

Researchers at NVIDIA have introduced BESTER (BEst Self-reflection Tree sEaRch), a novel algorithm that allows Large Language Models (LLMs) to iteratively debug their own code. By combining unit test feedback with "self-reflection" and a best-first search strategy, BESTER achieves state-of-the-art performance on major coding benchmarks, even when controlling for the extra computational cost of searching.

What Problem Does This Paper Address?

While LLMs like GPT-4 and Llama-3 are impressive at writing simple functions, they often stumble on complex programming tasks. A single "wrong" token generated due to the randomness of sampling can break an entire program.

The core issue is that standard evaluations usually ask a model to generate code in one shot (zero-shot). In contrast, human programmers rarely get it right the first time; we write, test, see what broke, and iterate. Current LLMs lack a systematic way to "look in the mirror," identify their own logical flaws, and explore different paths to a fix. This paper aims to bridge that gap by giving LLMs a structured debugging loop.

Methods

The authors propose BESTER, which stands for BEst Self-reflection Tree sEaRch. The process works in a loop:

  1. Program Generation: The model generates an initial solution.
  2. Execution Evaluation: The code is run against a suite of unit tests. If it passes all tests, the process stops.
  3. Self-reflection: If the code fails, the model is prompted to "reflect" on the error. It receives the problem description, its buggy code, and the specific test failures. It then generates a natural language description of the bug and a plan to fix it.
  4. Program Repair: The model uses its own reflection to write a new, "repaired" version of the code.
  5. Best-first Search: Instead of just trying one fix, BESTER samples multiple reflections ($k$) and generates multiple repairs. It then selects the "best" repair (the one passing the most tests) to serve as the starting point for the next round of debugging.

This "best-first" approach prevents the model from getting stuck in a "rabbit hole" of bad fixes, a common problem in simpler iterative methods like Reflexion.

Datasets

The researchers evaluated BESTER using three popular code generation benchmarks:

  • HumanEval: 164 hand-written Python problems.
  • MBPP (Mostly Basic Python Problems): Around 400 entry-level programming tasks.
  • APPS: A more challenging dataset containing 300 problems with at least 10 test cases each.

For HumanEval and MBPP, they used the EvalPlus versions, which include significantly more test cases to ensure the code isn't just "getting lucky" on a few simple inputs.

Key Insights and Ideas

  • The Power of "Best-First": Simple iterative refinement often fails because if the first "fix" is bad, the model just keeps building on a broken foundation. By branching out and picking the most successful repair at each step, BESTER is much more robust.
  • Reflections are Targeted: Using an interpretability tool called Captum, the authors found that the model's self-reflections actually "attend" to the specific lines of code that contain bugs. This proves that the model isn't just generating generic advice; it is pinpointing the exact location of the error.
  • Compute Efficiency: A common criticism of search-based methods is that they use more "compute" (more tokens) than a single-shot attempt. The authors introduced a metric called Pass@Infer to show that BESTER still outperforms other methods even when you give the other methods the same total number of inference calls.

Key Results

  • State-of-the-Art Performance: BESTER achieved the highest Pass@1 scores across all three benchmarks for GPT-4, Deepseek-coder, and Llama-3.
  • Solving the "Hard" Problems: In cases where a standard greedy generation failed, BESTER was able to solve up to 2x more problems than baseline feedback methods.
  • Model Agnostic: The method works across different model families and sizes. For example, running BESTER with a "weaker" model like Deepseek-coder-33b can actually outperform a "stronger" model like GPT-4 using standard sampling.
  • Reflection Quality Matters: The study showed that "correct" self-reflections (as judged by GPT-4) are significantly more likely to lead to a successful repair than incorrect ones, highlighting that the reasoning step is a bottleneck for further improvement.

Limitations and Future Work

  • Code Scale: The study focused on small programs (under 100 lines). Real-world software engineering involves tens of thousands of lines and complex dependencies, which poses a massive scalability challenge for current LLM context windows.
  • Inference-Time Only: BESTER is a search technique used during inference. The authors suggest that fine-tuning models specifically on the "reflection-repair" trajectories generated by BESTER could yield even better results.
  • Agent Integration: The authors see a future where BESTER is integrated into autonomous "coding agents" that can navigate entire repositories, not just single functions.

Summary updated

BibTeX
@misc{song2024effective,
  author = {Jialin Song and Jonathan Raiman and Bryan Catanzaro},
  title = {{Effective Large Language Model Debugging with Best-first Tree Search}},
  year = {2024},
  eprint = {2407.19055},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.19055},
}
Figure