From Token to Line: Enhancing Code Generation with a Long-Term Perspective

Tingwei Lu, Yangning Li, Liyuan Wang, Binghuai Lin, Jiwei Tang, Wanshi Xu, Hai-Tao Zheng, Yinghui Li, Bingxu An, Zhao Wei, Yong XuarXiv 2025 · DOI

Abstract

Traditional code generation in Large Language Models (LLMs) relies on token-by-token decoding, a method that often lacks the long-term structural perspective required for complex programming tasks. This paper presents LSR-MCTS (Line-level Self-Refine Monte Carlo Tree Search), a novel decoding strategy that shifts the unit of generation from individual tokens to complete lines of code. By identifying "summary tokens" at line breaks and integrating a self-refine mechanism within a Monte Carlo Tree Search framework, LSR-MCTS significantly improves logical coherence and performance across major Python benchmarks.

Introduction

Most current LLMs generate code sequentially, predicting the next token based on preceding context. While effective for short snippets, this "short-sighted" approach often leads to logical inconsistencies, redundant code, or structural hallucinations in longer programs. Programming languages possess a strict logical structure where a single line typically represents a discrete functional step. Existing decoding strategies, such as greedy search or token-level MCTS, struggle to maintain global coherence as program complexity increases.

The "Summary Token" and Line-Level Decoding

A key insight driving this research is the discovery of "summary tokens." By analyzing attention maps during code generation, researchers observed that attention scores spike significantly at the end of lines, specifically at the \n character.

Attention heatmap showing high attention scores at the end-of-line '\n' token, acting as a summary point.

This phenomenon suggests that the model naturally uses line breaks to consolidate and summarize information from the preceding line. LSR-MCTS leverages this biological-like justification to make the "line" the basic unit of its search tree, allowing for a shallower search space that can explore further into the program's potential future without the exponential complexity of token-level search.

The LSR-MCTS Framework

The proposed Line-level Self-Refine Monte Carlo Tree Search algorithm follows a four-phase process adapted specifically for structured code generation:

  1. Selection: The algorithm uses the Upper Confidence Bound (UCT) to navigate the tree and select the most promising partial code path.
  2. Expansion: The LLM generates multiple potential "next lines." To facilitate testing, a "supplement" is generated to complete the code block.
  3. Evaluation: The completed code is executed against public test cases. The resulting pass/fail status provides a reward score.
  4. Backpropagation: This reward is propagated back up the tree to update the value of ancestor nodes, informing future selections.
The LSR-MCTS loop illustrating the Selection, Expansion, Evaluation, and Backpropagation phases, including the Self-Refine mechanism.

Crucially, the framework includes a Self-Refine mechanism. If a generated line is found to be suboptimal during the expansion or evaluation phase, the LLM is prompted to "think again" and correct the line. This synergy between search and refinement is more effective at the line level than the token level, as the line provides sufficient context for the model to recognize and rectify its own errors.

Experimental Results

The researchers evaluated LSR-MCTS on three Python benchmarks: HumanEval, MBPP, and Code Contests. The method consistently outperformed traditional decoding methods (Beam-search, Top-p) and existing token-level MCTS approaches.

  • Complex Algorithmic Reasoning: The most significant gains were observed in the Code Contests benchmark, which features difficult competitive programming problems. Using the aiXcoder-7B model, LSR-MCTS achieved a 12.4% improvement in the pass@5 metric over the baseline.
  • Model Generalizability: The performance boost was consistent across various architectures, including CodeLlama, Llama3-8B, and GPT-4.
  • Ablation Studies: Removing either the line-level search or the self-refine module resulted in a performance drop, confirming that the integration of both components is vital for the system's success.

Conclusion and Future Work

LSR-MCTS demonstrates that shifting to a line-level perspective allows LLMs to generate more coherent and logically sound code. While the method is computationally more expensive than greedy decoding due to multiple rollouts and the requirement for test cases, it sets a new state-of-the-art for complex code generation. Future research will focus on developing neural discriminators to replace the dependency on pre-written test cases and expanding the framework to other programming languages like C++.

Summary updated

BibTeX
@misc{lu2025from,
  author = {Tingwei Lu and Yangning Li and Liyuan Wang and Binghuai Lin and Jiwei Tang and Wanshi Xu and Hai-Tao Zheng and Yinghui Li and Bingxu An and Zhao Wei and Yong Xu},
  title = {{From Token to Line: Enhancing Code Generation with a Long-Term Perspective}},
  year = {2025},
  eprint = {2504.07433},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.07433},
}
Figure