What Makes Large Language Models Reason in (Multi-Turn) Code Generation?

Kunhao Zheng, Juliette Decugis, Jonas Gehring, Taco Cohen, benjamin negrevergne, Gabriel Synnaeve

Introduction

While Large Language Models (LLMs) have become proficient at writing simple functions, they still struggle with the complex algorithmic reasoning required for competitive programming. Techniques like Chain-of-Thought (CoT) are widely used to improve general reasoning, but their specific mechanics in the context of code generation—especially in multi-turn "dialogue" settings where a model receives feedback from a compiler—remain poorly understood.

Current state-of-the-art approaches often rely on massive sampling (generating thousands of solutions and picking the best) or extremely complex, hand-engineered prompt chains. This paper seeks to establish a unified framework to understand which prompts actually work, how they interact with execution feedback (compiler errors), and whether models can be trained to reason "naturally" without being told to do so every time.

Meta researchers systematically deconstructed how prompting affects LLM performance in competitive programming. They found that while CoT and instruction prompts significantly boost performance—especially for larger models—stacking too many reasoning steps actually hurts. Most importantly, they demonstrated that models can "internalize" these reasoning processes through fine-tuning, allowing them to solve complex problems in fewer turns without needing explicit reasoning prompts at inference time.

Methodology

The authors propose a framework that decomposes prompting into three distinct categories:

  1. Reasoning Prompts (NL → NL): These ask the model to think before coding (e.g., "explain these input/output pairs" or "reflect on the problem in your own words").
  2. Instruction Prompts (NL → Code): These guide the actual code generation style (e.g., "write helper functions first" or "propose a clever algorithm").
  3. Execution Feedback Prompts: These provide information from the runtime environment when code fails (ranging from a simple "Your code is wrong" to detailed debugger traces).
Taxonomy of prompting strategies

Using this taxonomy, the researchers conducted a massive grid search of 63 prompt combinations across the Llama 3.0/3.1 family (8B to 405B parameters) and GPT-4o. They also introduced Rejection Sampling Fine-tuning (RFT), where they took successful multi-turn reasoning trajectories and used them to fine-tune the model, essentially teaching it to "internalize" the successful reasoning patterns.

The study utilized rigorous competitive programming benchmarks, primarily CodeContests (featuring public and private test cases to prevent memorization) and TACO (categorized into five difficulty levels). LiveCodeBench was also used to test the generalization of their fine-tuned models on fresh, unseen problems.

Key Insights

  • Reasoning is not additive: One might assume that asking a model to reflect, then plan, then explain would lead to better code. In reality, the authors found that one step of reasoning provides the biggest boost; adding more steps often leads to a performance plateau or even a decline.
  • The "Exploration vs. Exploitation" Trade-off: Detailed feedback (like variable traces) actually makes models worse at hard problems. It encourages "exploitation"—making tiny, surgical fixes to broken logic. Simple feedback (just saying the test failed) encourages "exploration"—scrapping the bad idea and trying a fresh algorithmic approach, which is often necessary for difficult tasks.
  • Internalization via RFT: By fine-tuning on successful reasoning traces and then removing the prompts, the model learns to produce high-quality code directly. The fine-tuned Llama 3.1 70B outperformed the best-prompted version of the base model, even when the fine-tuned model wasn't explicitly asked to "think."
  • CoT is for the Big Players: Reasoning prompts provide much larger gains for 70B+ parameter models than for 8B models. Smaller models often fail to follow complex instructions or produce sub-optimal "brute force" plans that fail time limits.

Key Results

Combining reasoning and instruction prompts yielded up to a +10% absolute improvement in pass n@k scores on CodeContests for Llama 3.1 70B.

When evaluating multi-turn generation, the authors found that for a fixed computational budget, the "sweet spot" is usually around 3 turns. Beyond that, the gains from self-repair diminish compared to simply starting over with a new sample.

Effect of maximum turns on pass rates

Furthermore, the Llama 3.1 70B RFT model achieved a 57.2% pass rate (33@100) on CodeContests, significantly lifting the performance curve across all sampling budgets and generalizing well to the TACO "Very Hard" split.

Limitations and Future Work

The authors acknowledge several limitations:

  • Linear Trajectories: The study focused on linear multi-turn dialogues. They did not explore more complex "branching" or "tree-search" approaches (like Tree-of-Thought).
  • Reasoning Quality: The RFT process assumes that if the final code is correct, the intermediate reasoning was also correct. This isn't always true, and future work could involve using "critic" models to filter out bad reasoning traces.
  • Scope: The study is limited to competitive programming. Applying these insights to repository-level coding—where models must interact with thousands of files and complex environments—is a promising next step.

Summary updated

BibTeX
@inproceedings{zheng2025what,
  author = {Kunhao Zheng and Juliette Decugis and Jonas Gehring and Taco Cohen and benjamin negrevergne and Gabriel Synnaeve},
  title = {{What Makes Large Language Models Reason in (Multi-Turn) Code Generation?}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=Zk9guOl9NS},
}
Figure