Generalizable End-to-End Tool-Use RL with Synthetic CodeGym

Weihua Du, Hailei Gong, Zhan Ling, Kang Liu, Lingfeng Shen, Xuesong Yao, Yufei Xu, Dingyuan Shi, Yiming Yang, Jiecao Chen

Large Language Models (LLMs) are increasingly deployed as "agents" capable of calling external tools like databases, search engines, or APIs. However, despite their impressive capabilities, these agents remain remarkably brittle. Most are trained using Supervised Fine-Tuning (SFT) on static trajectories, which essentially teaches them to follow a rigid script. When confronted with a new tool, an unexpected error, or a slightly altered workflow, they often fail because they haven't learned how to actively explore or recover from mistakes.

While Reinforcement Learning (RL) offers a path to more robust agents, existing RL environments are often too narrow—focusing on single domains like web shopping—or lack the scale needed to teach broad generalization. To address this, researchers have introduced CodeGym, a scalable framework that transforms static coding problems into interactive, multi-turn RL environments.

Overview of the CodeGym framework

Treating Code as a Workflow Proxy

The core insight behind CodeGym is that computer code serves as an excellent "structured substrate" for agent behavior. The execution patterns found in code—such as loops, conditionals, and state changes—are structurally identical to the multi-step workflows agents face in the real world. By mastering these code-based environments, agents learn transferable interaction strategies rather than just memorizing domain-specific API calls.

The CodeGym pipeline operates in three main stages:

The Synthesis and Verification pipeline
  1. Gym Synthesis: The framework takes a standard coding problem and its solution (sourced from datasets like KodCode). Using an LLM, it extracts "atomic functions" from the solution and converts them into callable tools. For instance, a binary search algorithm might be broken down into tools like observe() to get the array length and check(i) to inspect a specific index.
  2. Gym Verification: To ensure the synthesized environment is functional and solvable, CodeGym generates unit tests. It uses an "oracle" solution function to verify that the task can actually be completed using only the provided tools, filtering out broken or impossible environments.
  3. RL Training: Agents are placed in these verified environments and trained using Group Relative Policy Optimization (GRPO), an RL variant efficient for LLMs. The agents must interactively call tools to reach the correct answer, receiving a binary reward (1 for success, 0 for failure). To prevent models from "cheating" by solving the problem entirely in their internal context, the authors employ Difficulty Augmentation, scaling up problem parameters so that tool use becomes strictly necessary.

Key Results and Emergent Behaviors

The resulting CodeGym dataset boasts over 80,000 task configurations across 13,000 unique environments, averaging 6.5 tools per task. When evaluated on out-of-distribution (OOD) benchmarks, the results are striking:

  • Significant OOD Gains: After training on CodeGym, the Qwen2.5-32B-Instruct model achieved an average improvement of +7.3 points across all OOD tasks. On the complex $\tau$-Bench tool-use evaluation, it saw an absolute accuracy gain of 8.7 points, with a massive 13.0-point jump in the Retail domain specifically.
  • Emergent Planning: Qualitative analysis revealed that CodeGym-trained models began exhibiting "explicit multi-step planning" in their chain-of-thought reasoning before taking their first action. In contrast, base models tended to act impulsively and get stuck.
  • Scaling Benefits: The capacity to learn complex workflows scales with model size. Larger models (32B, 72B parameters) benefited significantly more from CodeGym training than smaller 7B models.

Limitations and Future Directions

While CodeGym represents a major step forward, it is not without limitations. For "Long-CoT" models (like QwQ), researchers noted a slight decrease in pure reasoning performance on benchmarks like ZebraLogic after RL training. This suggests a potential "alignment tax," where the model prioritizes tool-use patterns over raw logical deduction.

Additionally, during RL, trajectory lengths can grow massive, sometimes hitting the limits of the model's context window. This can lead to overly conservative or ineffective behavior in later training stages. Finally, because LLMs are notorious "reward hackers," they may still find unintended shortcuts to solve tasks. Future work will focus on creating environments with even stronger theoretical guarantees against such exploits, further solidifying CodeGym as a premier training ground for general-purpose AI agents.

Summary updated

BibTeX
@inproceedings{du2026generalizable,
  author = {Weihua Du and Hailei Gong and Zhan Ling and Kang Liu and Lingfeng Shen and Xuesong Yao and Yufei Xu and Dingyuan Shi and Yiming Yang and Jiecao Chen},
  title = {{Generalizable End-to-End Tool-Use RL with Synthetic CodeGym}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=QRSeFZfu8E},
}
Figure