Source-linked AI summary

ToolCoder: A Systematic Code-Empowered Tool Learning Framework for Large Language Models

Hanxing Ding, Shuchang Tao, Liang Pang, Zihao Wei, Jinyang Gao, Bolin Ding, Huawei Shen, Xueqi Cheng

arXiv:2502.11404v2cs.CL

TL;DR

Existing tool-learning approaches rely on hand-crafted prompts, struggle with multi-step planning, and lack precise error diagnosis and reflection. ToolCoder reframes tool learning as code generation, and reports that it significantly outperforms state-of-the-art approaches across metrics on multiple benchmarks.

  • Problem

    Existing tool-learning approaches rely on hand-crafted prompts, struggle with complex multi-step planning, and lack precise error diagnosis and reflection.

    Source: §1 · p. 1
  • Method

    ToolCoder transforms natural-language tasks into structured Python function scaffolds, plans subtasks, executes generated code, reuses successful functions, and diagnoses failures with tracebacks.

    Source: §1 · p. 2, §3.2.4 · p. 5
  • Results

    ToolCoder significantly outperforms state-of-the-art approaches on all metrics across multiple benchmark datasets.

    Source: §1 · p. 2
  • Takeaways & Limitations

    The reported results validate the effectiveness of a systematic code-centric approach to tool learning.

    Source: §5 · p. 8

    ToolCoder depends on clear, comprehensive API documentation; incomplete, ambiguous, or inconsistent documentation hinders correct tool-behavior inference and task execution.

    Source: Limitations · p. 9

Abstract

from arXiv · show

Tool learning has emerged as a crucial capability for large language models (LLMs) to solve complex real-world tasks through interaction with external tools. Existing approaches face significant challenges, including reliance on hand-crafted prompts, difficulty in multi-step planning, and lack of precise error diagnosis and reflection mechanisms. We propose ToolCoder, a novel framework that reformulates tool learning as a code generation task. Inspired by software engineering principles, ToolCoder transforms natural language queries into structured Python function scaffold and systematically breaks down tasks with descriptive comments, enabling LLMs to leverage coding paradigms for complex reasoning and planning. It then generates and executes function implementations to obtain final responses. Additionally, ToolCoder stores successfully executed functions in a repository to promote code reuse, while leveraging error traceback mechanisms for systematic debugging, optimizing both execution efficiency and robustness. Experiments demonstrate that ToolCoder achieves superior performance in task completion accuracy and execution reliability compared to existing approaches, establishing the effectiveness of code-centric approaches in tool learning.

1 Introduction

Existing tool-learning approaches struggle with multi-step planning, error diagnosis, and reuse. ToolCoder reframes tool learning as code generation and reports stronger benchmark performance than state-of-the-art approaches.

1 Introduction

  • Existing approaches rely on hand-crafted prompts, struggle with complex multi-step planning, lack precise error reflection, and cannot reuse successful experiences.These limitations hinder tool-learning systems’ robustness, adaptability, and scalability.Source: §1 · pp. 1–2
  • ToolCoder reformulates tool learning as code generation, converting natural-language tasks into structured Python function scaffolds and modular subtasks.Descriptive comments support reasoning and planning; generated implementations are executed to produce responses.Source: §1 · p. 2
  • The framework stores successfully executed code for reuse and uses Python tracebacks to diagnose failed executions.The introduction presents these mechanisms as supporting efficiency and reliability.Source: §1 · p. 2
  • ToolCoder significantly outperforms state-of-the-art approaches on all metrics across multiple benchmark datasets.The introduction also reports significantly improved tool-learning success rate and reliability.Source: §1 · p. 2

2 Related works

Prior tool-learning work uses text prompts and code actions to handle tasks, but each approach has limits in complex planning or iterative refinement. ToolCoder builds on code generation with modular construction, verification, and experience reuse.

2.1 Text-based Tool Learning

  • Text-based methods follow plan-execute-observe workflows, but their reliance on carefully crafted prompts limits reliability on complex multi-step tasks.Examples include ReAct, Chameleon, and RestGPT, which use reasoning traces, compositional tool assembly, or online planning, respectively.Source: §2.1 · p. 2

2.2 Code-based Tool Learning

  • Code-based tool methods use generated snippets as actions, while prior one-pass approaches lack iterative refinement, intent alignment, and learning from past executions.ToolCoder addresses these limitations with modular code construction, stepwise verification, and experience-driven refinement.Source: §2.2 · pp. 2–3

3 Methodology

ToolCoder recasts tool learning as code generation, applying software-engineering principles to structure task requirements, planning, implementation, and error handling. It converts queries into Python scaffolds, plans subtasks and tool calls, executes implementations, and reflects on errors.

3.1 Task Formulation and Motivation

  • Tool learning maps a natural-language task to a response through tool selection and execution, while ToolCoder draws on software-engineering principles to address complex planning and execution.The motivation emphasizes modular design, error diagnosis, and code reusability.Source: §3.1 · p. 3

3.2 ToolCoder: Code-Empowered Tool Learning Framework

  • ToolCoder reformulates tool learning as code generation, applying modular construction, stepwise verification, and experience-driven refinement.The framework is presented as a response to challenges in complex task planning and robust execution.Source: §3.1 · p. 3, §3.2 · p. 3

3.2.1 Task-to-Code Transformation

  • ToolCoder transforms each natural-language task into a Python function scaffold with a descriptive name, specified inputs, and a docstring, leaving the body empty for later stages.The scaffold documents the function’s purpose and expected return value, clarifying requirements before decomposition.Source: §3.2.1 · pp. 3–4, Figure 3 · p. 4

3.2.2 Subtask Planning and Tool Selection

  • ToolCoder decomposes the scaffold into concrete subtasks, embeds them as code comments, and selects documented tools to form pseudocode with tool-call placeholders and data flows.The plan identifies parameter values passed between sequential tool calls.Source: §3.2.2 · p. 4

3.2.3 Implementation and Execution

  • ToolCoder implements subfunctions using selected-tool documentation and reusable functions, then executes the generated program to return a response or raise an exception.Successfully executed snippets are stored with execution contexts for reuse, which the paper says can prevent potential errors and reduce redundant development.Source: §3.2.3 · pp. 4–5

3.2.4 Error Reflection

  • ToolCoder checks plans for unavailable tools and revises them, while traceback-guided code review diagnoses failures and retries execution up to three times.Retries continue until execution succeeds or the attempt limit is reached.Source: §3.2.4 · p. 5

4 Experiments

ToolCoder achieves state-of-the-art performance across the reported benchmarks, and the experiments assess its components, generalization, and efficiency.

4.1 Experimental Settings

  • Experiments use RestBench and API-Bank, evaluating task success, path correctness, TMDB answer accuracy, and API-Bank output correctness.RestBench includes TMDB movie tasks and Spotify music tasks; API-Bank comprises 73 APIs and 314 tool-use dialogues.Source: §4.1 · p. 5

4.2 Baselines

  • ToolCoder is compared with seven baselines, including text-based agents such as ReAct and Chameleon and code-based approaches such as ATC and CodeAct.The main results table uses gpt-4o-mini for all methods.Source: §4.2 · pp. 5–6, Table 1 · p. 6

4.3 Main Results

  • ToolCoder achieves state-of-the-art results across benchmarks, including 10.79% higher success and 22.22% higher accuracy on RestBench-Spotify, and 8.34% higher correctness on API-Bank LV2 versus CodeAct.It consistently has the highest success, accuracy, and correct path rates.Source: Table 1 · p. 6, §4.3 · p. 6

4.4 Ablation Study

  • Removing the structured Python function scaffold significantly lowers correct path rates across benchmarks, while removing error reflection lowers correct path and success rates across multiple datasets.The authors attribute the scaffold result to weaker reasoning and planning, and the reflection result to losing diagnosis, iterative refinement, and self-correction.Source: §4.4 · pp. 6–7, Figure 4 · p. 6
  • Removing the reusable function repository causes a 2–5% performance drop across benchmarks; on TMDB, cumulative success approaches 90% as more samples are used, while cumulative accuracy improves.The authors report that accumulated experience from past successes aids generation of more robust code.Source: §4.4 · p. 7, Figure 5 · p. 7

4.5 Analysis of Generalization Capabilities

  • On ToolAlpaca real-world API tasks, ToolCoder outperforms ReAct, Chameleon, and CodeAct across Procedure, Response, and Overall metrics.The dataset evaluation omits the reusability mechanism because there are few questions per tool category.Source: Table 2 · p. 7, §4.5 · pp. 7–8
  • On RestBench-TMDB, ToolCoder outperforms baselines across open-source model configurations; scaling from 14B to 32B improves success, accuracy, and path correctness across methods.Coder-enhanced models show advantages over base models, especially with code-empowered methods.Source: §4.5 · p. 8, Table 3 · p. 8

4.6 Efficiency Analysis

  • ToolCoder uses roughly similar numbers of LLM calls per question as ReAct, Chameleon, and ConAgents while achieving higher correct path and success rates.The comparison covers RestBench and API-Bank.Source: Table 4 · p. 8, §4.6 · p. 8

5 Conclusion

ToolCoder reframes tool learning as code generation, applying software engineering principles to planning, execution, reuse, and error diagnosis. The authors report stronger task-completion accuracy and execution reliability than existing methods.

5 Conclusion

  • ToolCoder reformulates tool learning as code generation, using software engineering principles and Python function structures for complex reasoning and reliable execution.It transforms natural-language queries into function scaffolds with descriptive comments for multi-step planning.Source: §5 · p. 8
  • The framework maintains a repository of successfully executed functions and uses code-based feedback for error diagnosis and adaptation.These mechanisms are part of its systematic approach to tool learning.Source: §5 · p. 8
  • Experiments demonstrate superior task-completion accuracy and execution reliability compared with existing methods.The authors present this as evidence for the effectiveness of a code-centric approach to tool learning.Source: §5 · p. 8

Limitations

ToolCoder’s applicability is constrained by API-documentation quality, limited adaptation to changing conditions, and scalability challenges with many interdependent tools.

  • Incomplete, ambiguous, or inconsistent API documentation hinders the model’s ability to infer tool behavior and execute tasks correctly, limiting robustness where documentation quality is uncertain.Source: Limitations · p. 9
  • Global planning does not fully support adaptive or incremental responses to changing task requirements or partial observability in evolving environments.Source: Limitations · p. 9
  • Numerous interdependent tools can create combinatorial challenges and potentially yield suboptimal plans, although the approach is effective for moderately complex tool planning.Source: Limitations · p. 9

A Appendix

The appendix presents ToolCoder examples for a Sofia Coppola movie-count query and prompts used for its RestBench-TMDB modules.

  • The appendix’s examples answer a Sofia Coppola movie-count query with gpt-4o-mini and include prompts for each module on RestBench-TMDB.Source: A Appendix · p. 10
  • Figure 6 shows ToolCoder’s task decomposition for the query asking how many movies Sofia Coppola directed.Source: Figure 6 · p. 11
  • Figure 7 presents generated pseudocode, while Figure 8 shows the main function implementing its API-call placeholders according to API documentation.Source: Figure 7 · p. 11, Figure 8 · p. 12
  • Figures 9–12 show prompts for the task-to-code, subtask-planning, tool-selection, and code-generation modules, respectively.Source: Figure 9 · p. 13, Figure 10 · p. 14, Figure 11 · p. 15
Loading 2502.11404v2…