TL;DR
State-of-the-art reasoning LLMs typically do all their "thinking" upfront before generating an answer, which falls short in code generation where bugs and complexities often emerge mid-implementation. THINK-ANYWHERE introduces a novel mechanism that allows LLMs to dynamically pause and invoke thinking blocks at any token position on-demand. This approach not only achieves state-of-the-art coding performance but surprisingly reduces overall token costs by replacing bloated upfront planning with targeted, inline deliberation.
What Problem Does This Paper Address?
Recent breakthroughs in reasoning models (like OpenAI’s o1 or DeepSeek-R1) rely heavily on "upfront thinking"—generating a long Chain-of-Thought (CoT) inside a <think> block before outputting the final solution.
While powerful, the authors argue this paradigm has two major flaws when applied to software engineering:
- Insufficient Foresight: The full complexity of a programming problem often only reveals itself during implementation. A model might create a perfect high-level plan upfront, but fail to realize a subtle off-by-one or index-out-of-bounds error until it is actively writing that specific line of code.
- Inflexible Compute Allocation: Upfront thinking treats all code equally. In reality, writing boilerplate code requires minimal cognitive effort, whereas deciding on a critical algorithmic pivot or handling an edge case demands deep reasoning.
Humans don't write an entire program flawlessly in one go after planning; we pause, think, and course-correct mid-sentence. This paper aims to give LLMs that exact capability.
Methods
THINK-ANYWHERE enables models to generate a mixed sequence of code segments and inline thinking blocks (wrapped in <thinkanywhere> tags) via a two-stage training pipeline:
- Cold-Start Training (Imitation): Because LLMs do not spontaneously pause to think mid-code, the authors used Gemini 2.5 Flash to automatically construct a dataset of ~5,000 high-quality examples where a model explicitly invokes inline reasoning at complex bottlenecks. The target model is fine-tuned on this data using LoRA to learn the structural pattern of pausing.
- Reinforcement Learning with Verifiable Rewards (RLVR): To teach the model where and when it actually needs to think, they apply Group Relative Policy Optimization (GRPO). The model is rewarded based on a hierarchical function: a small reward for maintaining the correct tag structure (
R_struct), and a primary reward based on whether the generated code passes compiler unit tests (R_correct).
The authors also experimented with a Dedicated Trigger Token variant (THINK-ANYWHERE*), initializing a single special token embedding by combining the semantic meanings of "think anywhere" with the structural properties of standard control delimiters (like <im_start>).
Datasets
- Training: 14K programming problems from the Skywork dataset.
- Code Generation Evaluation: Evaluated across four mainstream benchmarks: HumanEval, MBPP, LeetCode, and LiveCodeBench.
- Cross-Domain Evaluation: Tested on mathematical reasoning benchmarks (AIME 2024, AIME 2025, HMMT 2025) to evaluate out-of-domain generalization.
Key Insights and Ideas
- Thinking at High-Entropy Positions: Token entropy analysis revealed that when inline thinking was disabled, the token entropy spiked at the exact positions where the model originally wanted to think. This proves the model naturally learns to trigger
<thinkanywhere>blocks at moments of high uncertainty and local complexity. - Where Do Models Think? Syntactic analysis showed that models most frequently pause before assignment statements (dealing with complex variable updates) and return statements (double-checking correctness before finalizing a function).
- Compute Efficiency Win: Counter-intuitively, allowing a model to think anywhere decreases the total token cost. Instead of generating massive, exhaustive upfront planning traces to anticipate every single edge case, the model shifts to a short, high-level upfront plan and allocates concise, targeted reasoning blocks only when needed.
Key Results
THINK-ANYWHERE achieved significant absolute performance gains over standard upfront RL models (GRPO) and other post-training methods:
- Coding Benchmarks: Reached an average score of 70.3% across the four benchmarks, outperforming standard GRPO (68.4%) and the previous best post-training baseline, CodeRL+ (66.8%).
- Cross-Domain Math Generalization: Despite being trained only on code, the think-on-demand capability transferred powerfully to math. On AIME 2024, pass@1 accuracy skyrocketed from 5.3% (Base) and 6.0% (GRPO) to 17.3% (THINK-ANYWHERE).
- Model Agnostic: The approach yielded consistent improvements across multiple model families (Qwen, LLaMA) and scales, showing particularly strong benefits for smaller models (e.g., a +13.9% boost for Qwen2.5-Coder-1.5B).
Limitations and Future Work
- Special Token Constraints: While the text-based tag version performed best, the dedicated special token variant (
THINK-ANYWHERE*) was slightly bottlenecked by the limited post-training data. The authors suggest that natively integrating these trigger tokens during large-scale pre-training would unlock their full potential. - Learning "What Not to Think": Future work includes optimizing the compute trade-off even further by explicitly training models to recognize when reasoning is unnecessary, preventing redundant token generation.
Summary updated