Towards Effective Code-Integrated Reasoning

Fei Bai, Yingqian Min, Beichen Zhang, Zhipeng Chen, Wayne Xin Zhao, Lei Fang, Zheng Liu, Zhongyuan Wang, Ji-Rong WenAAAI 2025

Abstract

This paper introduces a stabilized tool-augmented Reinforcement Learning (RL) framework designed to train Large Language Models (LLMs) for code-integrated reasoning—the ability to write, execute, and learn from code during inference. By balancing exploration and stability during RL training, the proposed approach achieves state-of-the-art performance on difficult mathematics benchmarks using a 7B parameter model. Notably, this method matches the accuracy of long-chain-of-thought (Long-CoT) models while utilizing less than 20% of the token length, demonstrating significant gains in computational efficiency.

Introduction

Standard Large Language Models often struggle with tasks requiring precise algorithmic logic or heavy numerical computation. While recent reasoning models improve performance through extensive test-time computation (e.g., DeepSeek-R1), they remain limited by internal arithmetic flaws and knowledge gaps.

Allowing models to use external tools, specifically Python code interpreters, addresses these limitations by offloading computation to a deterministic environment. However, training models to use tools via Reinforcement Learning (tool-augmented RL) is notoriously unstable. Training often fails due to boundary disruptions (confusion over when to stop text and call a tool), distributional shifts (interpreter output differing from model text), and mode collapse (converging on narrow tool-use paths).

Methodology

The authors build their Code-Integrated Reasoning (CIR) model on top of Qwen2.5-Math-7B using the REINFORCE++ algorithm. To address the instability of tool-augmented RL, they implement two categories of interventions:

Stability Maintenance

To prevent training derailment, the system employs Precise Boundary Matching. Instead of relying on unreliable "stop tokens" to trigger the interpreter, the system uses exact regex-style matching for code blocks. This ensures the interaction between the model and the interpreter is predictable and robust.

Comparison of standard stop-token detection versus the proposed integrated code generation and detection framework.

Additionally, the framework utilizes Tool Feedback Masking. During RL loss calculation, the text output of the code interpreter is masked, preventing the model from updating its weights based on the unpredictable distribution of Python error logs. Finally, the standard RL entropy bonus is disabled to prevent chaotic behavior during tool interaction.

Exploration Enhancement

To prevent the model from converging on "lazy" shortcuts, the authors introduce Progressive Interaction Budgets. The model starts with a limit of two tool calls, which is gradually increased to four. This forces the model to explore diverse reasoning paths early in training. Furthermore, the KL divergence penalty is removed, and PPO clipping bounds are adjusted to encourage aggressive exploration of new tool-use strategies.

Experimental Setup

The model was trained on a balanced subset of 38,000 math problems from the STILL-3 dataset. Evaluation was conducted across five challenging benchmarks: MATH500, AMC23, AIME2024, AIME2025, and OlymMATH (an Olympiad-level benchmark).

Results and Discussion

The CIR (Qwen2.5-M7) model achieved an average accuracy of 52.4% across all benchmarks, significantly outperforming text-only RL baselines like EURUS-2-7B-PRIME (37.6%) and OAT-ZERO-7B (39.3%). It also surpassed existing tool-augmented models such as TORL-7B (50.5%).

Training dynamics showing the increase in training reward and the adjustment of response length as interaction rounds are progressively increased.

Efficiency and Capability

One of the most striking findings is the model's token efficiency. On AIME 2024, CIR achieved 42.3% accuracy using an average of only 1,603 tokens. In contrast, a Long-CoT model (Qwen3-8B Thinking) required 9,296 tokens to achieve a similar 44.6% accuracy. This suggests that code integration allows for more concise and effective reasoning than human-like "rambling."

Insights into Tool-Use

  • Capability Ceiling: Code-integrated RL pushes the model's actual capability ceiling (Pass@k) higher by bridging natural language with formal programming.
  • Value of Errors: Broken code is not a failure; Python error traces act as feedback signals that force the model to revise its logic.
  • Domain Specificity: While code execution boosts performance in Algebra and Combinatorics, it provides minimal benefit in Geometry, which relies more on spatial reasoning than computation.

Conclusion

This work demonstrates that stabilized RL can effectively integrate code execution into LLM reasoning paths. The resulting models are not only more accurate but significantly more efficient than their Long-CoT counterparts. Future research will focus on generalizing this framework to other external tools, such as search engines, and exploring the intersection of Long-CoT and tool-augmented RL.

Summary updated

BibTeX
@misc{bai2025towards,
  author = {Fei Bai and Yingqian Min and Beichen Zhang and Zhipeng Chen and Wayne Xin Zhao and Lei Fang and Zheng Liu and Zhongyuan Wang and Ji-Rong Wen},
  title = {{Towards Effective Code-Integrated Reasoning}},
  year = {2025},
  eprint = {2505.24480},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.24480},
}
Figure