Skip to content
AI360Xpert
Paper Breakdowns
Paper breakdown

Toolformer

A 2023 Meta paper that taught models to use external tools by automatically generating their own training data, fine-tuning the model to natively call APIs via special text tokens.

Paper: Toolformer: Language Models Can Teach Themselves to Use Tools

Authors: Timo Schick, Jane Dwivedi-Yu, Roberto Dessì, Roberta Raileanu, Maria Lomeli, Luke Zettlemoyer, Nicola Cancedda, Thomas Scialom · 2023

Read the paper
Toolformer bootstraps its own tool-use dataset. It injects potential API calls into normal text, executes them, and checks if the returned data reduces the perplexity of the subsequent text. If it helps, the API call is kept in the fine-tuning dataset.
Toolformer bootstraps its own tool-use dataset. It injects potential API calls into normal text, executes them, and checks if the returned data reduces the perplexity of the subsequent text. If it helps, the API call is kept in the fine-tuning dataset.

The Problem

While frameworks like ReAct showed that LLMs could use tools if carefully prompted, there were major drawbacks to relying purely on prompt engineering. First, few-shot prompts take up a huge amount of the context window. Second, models often struggled to know when exactly to use a tool versus when to rely on their own internal knowledge.

Ideally, a model should just inherently know how to use a calculator when it encounters math, or a calendar when it encounters dates, without needing a massive prompt explaining how to do it. However, creating a high-quality human-labeled dataset of "when and how to use APIs" for fine-tuning would be prohibitively expensive and time-consuming.

The Idea

The Meta AI team introduced Toolformer, a model that teaches itself how and when to use tools in a self-supervised way.

The core idea is to take a massive dataset of regular text and automatically annotate it with API calls. The model is allowed to propose API calls anywhere in the text. It then actually executes the API call, gets the result, and tests a simple hypothesis: Did the result from the API make it easier to predict the rest of the sentence? If yes, the API call is kept as good training data. If no, it is discarded. The model is then fine-tuned on the surviving dataset.

How It Works

The Toolformer training process is entirely automated:

  1. Sampling API Calls: Take a piece of plain text from a pre-training corpus (e.g., "The Pittsburgh Steelers were founded in 1933."). Use an LLM with a few-shot prompt to generate potential places where an API call might be useful. It might generate: "The Pittsburgh Steelers were founded in [QA("When were the Pittsburgh Steelers founded?")] 1933."
  2. Executing APIs: The system executes the proposed API calls. For the QA tool, it might return 1933. The text becomes: "The Pittsburgh Steelers were founded in [QA("When were the Pittsburgh Steelers founded?") -> 1933] 1933."
  3. Filtering via Perplexity: The model calculates the probability (perplexity) of generating the next word ("1933") under two conditions:
    • Condition A: Without the API result.
    • Condition B: With the API result. If Condition B is significantly higher (meaning the API result made the prediction easier/more accurate), the API call is considered "useful" and kept. If the API returns garbage, Condition B will be worse, and the call is discarded.
  4. Fine-Tuning: The original model is fine-tuned on the filtered dataset containing only the genuinely useful API calls.

At inference time, the model simply generates text as usual. When it naturally generates the start of an API token ([), generation is paused, the API is called, the result is appended, and the model continues generating the rest of the sentence.

Why It Mattered

Toolformer proved that you don't need humans to teach models how to use tools. By relying on next-token prediction loss as the ultimate judge of whether a tool is useful, the model discovers its own optimal strategy for interleaving API calls with natural language.

The resulting 6.7B parameter Toolformer model vastly outperformed much larger models (like GPT-3) on tasks requiring factual lookup or calculation, despite being a fraction of the size. It demonstrated that tool-use could be deeply ingrained into the model weights, freeing up the context window and making tool use a native capability rather than a prompted hack.

What Came After

Toolformer's self-supervised data generation approach became a standard recipe for training "agentic" models. While modern models (like GPT-4) use more complex forms of RLHF and instruction tuning to learn tool use, the core insight—that models can generate their own synthetic training data for tool use by executing code and observing the results—remains a pillar of modern post-training pipelines. It also directly inspired the widespread adoption of specific "function calling" APIs by major AI providers.