cs.SEMar 31, 2026

Think Anywhere in Code Generation

Authors: Xue Jiang, Tianyu Zhang, Ge Li, Mengyang Liu, Taozhi Chen, Zhenhua Xu, Binhua Li, Wenpin Jiao, 

Organizations: School of Computer Science, Peking University · Tongyi Lab, Alibaba Group

Abstract

Recent advances in reasoning Large Language Models (LLMs) have primarily relied on upfront thinking, where reasoning occurs before final answer. However, this approach suffers from critical limitations in code generation, where upfront thinking is often insufficient as problems' full complexity only reveals itself during code implementation. Moreover, it cannot adaptively allocate reasoning effort throughout the code generation process where difficulty varies significantly. In this paper, we propose Think-Anywhere, a novel reasoning mechanism that enables LLMs to invoke thinking on-demand at any token position during code generation. We achieve Think-Anywhere by first teaching LLMs to imitate the reasoning patterns through cold-start training, then leveraging outcome-based RL rewards to drive the model's autonomous exploration of when and where to invoke reasoning. Extensive experiments on four mainstream code generation benchmarks (i.e., LeetCode, LiveCodeBench, HumanEval, and MBPP) show that Think-Anywhere achieves state-of-the-art performance over both existing reasoning methods and recent post-training approaches, while demonstrating consistent generalization across diverse LLMs. Our analysis further reveals that Think-Anywhere enables the model to adaptively invoke reasoning at high-entropy positions, providing enhanced interpretability.

Figures & tables

Appendix figures & tables2 assets

Supplementary material from the paper’s appendix.

Appendix

Explore similar work

CardsList
  1. Enhancing the Code Reasoning Capabilities of LLMs via Consistency-based Reinforcement Learning

    May 18, 2026Zhanyue Qin, Jia Feng, Yibo Lyu +4Reinforcement LearningRL for Language Model Reasoning

  2. Probabilistic Programs of Thought

    Apr 19, 2026Poorva Garg, Renato Lui Geh, Daniel Israel +3LLM-Based Program SynthesisEfficient Language Model Reasoning

  3. When Do Intrinsic Rewards Work for Code Reasoning? A Comprehensive Study

    Jun 18, 2026Xiaolong Jin, Xuandong Zhao, Wenbo Guo +2RL for Code GenerationIntrinsic Reward Methods