Abstract
Traditional Large Language Model (LLM) tool invocation relies on structured text formats like JSON, which often struggle with complex, multi-step tasks due to verbosity, lack of programmatic logic, and error propagation. We present CodeTool, a framework that shifts tool use to stepwise Python code generation. CodeTool employs a dual-reward process supervision mechanism—combining an On-the-spot Reward for executability and a Latent Reward for logical progress. Our evaluations on StableToolBench and RestBench-TMDB demonstrate that CodeTool significantly improves success rates and efficiency, with Qwen2.5-Coder-7B achieving a 69.75% Solvable Pass Rate, outperforming traditional JSON-based methods.
Introduction
As LLMs are increasingly integrated with external tools, the limitations of standard invocation methods have become apparent. When models use JSON-based structures, they often encounter context window limits due to verbose API responses. Furthermore, tasks requiring repetitive actions (e.g., querying multiple items) force the model into inefficient, linear reasoning paths. CodeTool addresses these challenges by enabling models to write executable Python snippets, leveraging loops and persistent memory to handle complex workflows more effectively.
Methodology: Programmatic Invocation and Process Supervision
CodeTool treats tool invocation as a sequence of Python code blocks. This approach allows the model to use standard programming constructs like for loops to batch requests, reducing the number of reasoning steps required.
To ensure the reliability of these code-based steps, CodeTool introduces a process supervision mechanism based on two rewards:
- On-the-spot Reward ($R_{spot}$): An immediate signal indicating whether the generated code executes successfully in a Python interpreter and returns a valid response.
- Latent Reward ($R_{latent}$): A signal from a trained Process Reward Model (PRM) that evaluates the strategic value of the step toward reaching the final answer.
By sampling multiple candidate steps and selecting the one with the highest combined reward, the model maintains a path that is both technically valid and logically sound.
Experimental Setup
We evaluated CodeTool across several benchmarks:
- StableToolBench: A reproducible version of ToolBench using cached API responses.
- RestBench-TMDB: A movie-domain dataset utilizing the TMDB API.
The PRM was trained on automatically generated process data from the ToolBench training set, eliminating the need for manual human labeling of intermediate steps.
Results and Discussion
CodeTool demonstrates superior performance across both open-source and closed-source models. As shown in the comparative results, CodeTool consistently improves the Solvable Pass Rate (SoPR) compared to standard Chain-of-Thought (CoT) and Depth-First Search Decision Tree (DFSDT) strategies.
Key findings include:
- Efficiency Gains: CodeTool reduces token consumption by using programmatic logic to handle data-intensive tasks.
- State-of-the-Art Performance: On RestBench-TMDB, CodeTool achieved a 92% success rate, surpassing previous benchmarks.
- Robustness: The dual-reward system effectively filters out non-executable code and unhelpful reasoning paths.
Conclusion
CodeTool represents a significant step forward in LLM tool use. By moving from static JSON to dynamic Python code and implementing rigorous process supervision, the framework enables models to solve complex, multi-step instructions with unprecedented precision and efficiency. Future work will explore extending this programmatic approach to more diverse languages and multi-agent environments.
Summary updated