Skip to content

Toolformer: Language Models Can Teach Themselves to Use Tools

optionalcorefundamentalpaperrubric 10/122023 · ~40 min

Timo Schick, Jane Dwivedi-Yu, Roberto Dessì, …, Thomas Scialom

Status

TL;DR. Toolformer turns tool use into a data-labelling problem. GPT-J (6.7B) samples candidate API calls inside ordinary text and executes them. It keeps a call only if the result makes the next few tokens easier to predict, then fine-tunes on the text with the kept calls inlined. With five tools (QA, calculator, Wikipedia search, translation, calendar), the fine-tuned model beats much larger models on several zero-shot tasks without losing language-modelling ability. The paper says the model learns “in a self-supervised way”. That still requires a handful of demonstrations per tool to prompt the call sampler.

Why it matters now. Toolformer defines one of three answers to “how does a model learn when to search?”. Here the signal is next-token loss. In Self-RAG it is critic-labelled reflection tokens, and in RL-trained search agents it is answer reward. Its BM25 Wikipedia-search tool is the simplest possible retriever-as-tool. The filter criterion is a utility-based relevance signal: a passage is useful if it lowers the loss on the text that follows. That idea later reappears in work that trains retrievers from LM feedback.

An API call c=(ac,ic)c = (a_c, i_c) with tool name aca_c and input ici_c, plus its result rr, is written inline as text:

e(c,r)=⟨API⟩  ac(ic)→r  ⟨/API⟩e(c, r) = \langle\texttt{API}\rangle\; a_c(i_c) \rightarrow r \;\langle\texttt{/API}\rangle

using the plain tokens [, ] and -> so the vocabulary does not change. For a call inserted before position ii, Toolformer measures a weighted loss on the following tokens:

Li(z)=−∑j=inwj−i log⁡pM(xj∣z,x1:j−1),w~t=max⁡(0, 1−0.2 t),L_i(z) = -\sum_{j=i}^{n} w_{j-i}\, \log p_M\big(x_j \mid z, x_{1:j-1}\big), \qquad \tilde w_t = \max(0,\, 1 - 0.2\,t),

with the weights normalised. Let Li+=Li(e(c,r))L_i^+ = L_i(e(c, r)) and Li−=min⁡(Li(ε), Li(e(c,ε)))L_i^- = \min\big(L_i(\varepsilon),\, L_i(e(c, \varepsilon))\big). The first term is the loss with no call; the second is the loss with the call but an empty result. The call is kept if

Li−−Li+≥τf.L_i^- - L_i^+ \ge \tau_f .

The pipeline has four steps.

  1. Sample. Prompt the model with a few demonstrations of the tool. Find positions where p(⟨API⟩)>τsp(\langle\texttt{API}\rangle) > \tau_s and keep the top kk positions. Sample mm calls at each. The main settings are τs=0.05\tau_s = 0.05, k=5k = 5, m=5m = 5 and τf=1.0\tau_f = 1.0. Calculator and translation calls are rarer, so they use τs=0\tau_s = 0, k=20k = 20, m=10m = 10 and τf=0.5\tau_f = 0.5.
  2. Execute every call.
  3. Filter with the criterion above.
  4. Fine-tune GPT-J on the union of kept examples (a CCNet subset) with the ordinary LM loss.

At inference the model decodes normally. When <API> is among the top-kk tokens (k = 10 in the main experiments), it starts a call. Decoding pauses at →, the tool runs, and the result is inserted. At most one call is made per input.

Toolformer: sample, execute, filter, fine-tune Four boxes in a row. One: sample positions where the model's probability of starting an API call exceeds a threshold and sample several calls at each. Two: execute every call to get a result. Three: keep a call only if it lowers the weighted loss on the following tokens by at least a threshold. Four: fine-tune GPT-J on text with the kept calls inlined. Below, three bars compare the weighted loss with no call, with the call but no result, and with the call plus result; the call is kept if the smaller of the first two exceeds the third by at least tau f. Toolformer: the model labels its own corpus with API calls 1 · sample calls if p(<API>) > τs keep top k spots m calls per spot 2 · execute run each API call get result r QA · calc · search MT · calendar 3 · filter keep call only if L⁻ − L⁺ ≥ τf (bars below) 4 · fine-tune GPT-J on text with kept calls inlined plain LM loss Filter: weighted loss on the tokens after position i no call: L(ε) call, no result: L(e(c, ε)) call + result: L(e(c, r)) gain = L⁻ − L⁺ L⁻ = smaller of top two keep if gain ≥ τf τf = 1.0 (main runs)
Original sketch. A call survives only if its result lowers the weighted loss on the next tokens by at least τf, relative to both no call and a call with an empty result.

A worked example (illustrative). In the sentence “The library holds 12,000 books, of which 3,000 (25%) are fiction”, the sampler might propose [Calculator(3000 / 12000) → 0.25] before “25%”. With the result, “25” becomes easy to predict. Without the call, or with the call but no result, it is hard. So the gain is large and the call is kept. A call like [QA(What is a library?) → a collection of books] at the same spot would not lower the loss on “25%” and is filtered out.

What changed vs. prior tool use, and what didn’t.

Prior tool-augmented LMsReAct promptingToolformer
Where tool knowledge livestask-specific supervision or promptsfew-shot promptmodel weights, after fine-tuning
Supervisionhuman-labelled tool tracesa few trajectoriesa few demos per tool + LM-loss filter
Multi-step chainingvariesyesno: calls sampled independently
Interaction with resultsvariesyesone call, no follow-up

All results are zero-shot with GPT-J-based models. “Disabled” means that tool calls are blocked at inference.

Task (metric)GPT-JToolformer (disabled)ToolformerGPT-3 (175B)
LAMA SQuAD / Google-RE / T-REx (Table 3)17.8 / 4.9 / 31.9—33.8 / 11.5 / 53.526.8 / 7.0 / 39.8
Math: ASDiv / SVAMP / MAWPS (Table 4)7.5 / 5.2 / 9.914.8 / 6.3 / 15.040.4 / 29.4 / 44.014.0 / 10.0 / 19.8
QA: WebQS / NQ / TriviaQA (Table 5)18.5 / 12.8 / 43.9—26.3 / 17.7 / 48.829.0 / 22.6 / 65.9
  • Tool usage.
    • On LAMA the QA tool is called for 98.1% of examples; Wikipedia search is disabled there.
    • On math the calculator is used for 97.9% of examples.
    • On the QA benchmarks Wikipedia search is used 99.3% of the time; the QA tool is disabled there.
  • Toolformer still lags GPT-3 on open QA. The authors attribute this to the weak search tool and to the model’s inability to reformulate queries or browse.
  • Kept examples (Table 2). The filter is strict: at τf = 1.0, it keeps about 61k Wikipedia-search examples but only 994 calculator examples.
  • Dates. On DATESET the calendar tool lifts accuracy from 3.9 (GPT-J) to 27.3. On TempLAMA the calendar is used only 0.2% of the time.
  • Perplexity on WikiText / CCNet. Toolformer with tools disabled scores 10.3 / 10.5, the same as GPT-J fine-tuned on CCNet without tools.
  • No chaining or interaction. Each call is sampled independently, so the model cannot use one tool’s output as another’s input or refine a search. Later agents need exactly this.
  • Over-calling is controlled by decoding, not learning. Raising the top-kk threshold raises the call rate and accuracy but loses calibration (Table 9).
  • Sample inefficiency. Processing over a million documents yields only a few thousand useful calculator calls.
  • Prompt sensitivity. Whether the model makes a call depends heavily on wording, a weakness the paper reports.
  • Corpus-loss utility ≠ task utility. The filter rewards calls that help predict web text, which is a proxy for helping a user. How well the proxy transfers to agentic search tasks is open.
  • Before: RAG (retrieval wired into the architecture rather than called as a tool).
  • Contrast: ReAct (tool use from prompting, with multi-step interaction), Self-RAG (when-to-retrieve learned from critic labels; it compares against Toolformer as a baseline).
  • After: RL-trained search agents such as Search-R1 replace the LM-loss filter with an outcome reward and allow multi-turn calls; see outcome vs. process rewards for the trade-off in signal density.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    Use Toolformer's weights w̃_t = max(0, 1 − 0.2t), normalised over t = 0…4. For the five tokens after a candidate calculator call, the per-token NLLs are: no call [3.0, 2.0, 1.0, 1.0, 1.0]; call without result [2.8, 2.0, 1.0, 1.0, 1.0]; call with result [0.5, 0.6, 1.0, 1.0, 1.0]. Compute L⁺, L⁻ and the gain, and say whether the call is kept at τf = 0.5, 1.0 and 2.0.

    What a strong answer covers
    • Raw weights 1, 0.8, 0.6, 0.4, 0.2 sum to 3.0, so normalised weights are 1/3, 4/15, 1/5, 2/15, 1/15.
    • L(ε) = (3.0 + 1.6 + 0.6 + 0.4 + 0.2)/3 = 5.8/3 ≈ 1.933. L(e(c, ε)) = 5.6/3 ≈ 1.867. L⁺ = (0.5 + 0.48 + 0.6 + 0.4 + 0.2)/3 = 2.18/3 ≈ 0.727.
    • L⁻ = min(1.933, 1.867) = 1.867, so the gain is ≈ 1.14. Kept at τf = 0.5 and 1.0, dropped at τf = 2.0.
    • Only the first two tokens changed, and they carry the largest weights. The decaying weights encode the assumption that a tool result should help the text right after the call.
  2. Mechanism

    Why does Toolformer compare the call-with-result loss against the minimum of two baselines, no call at all and the call text with an empty result, rather than against no call alone? Describe a concrete call that would pass a no-call-only filter but fail Toolformer's filter.

    What a strong answer covers
    • The call text itself can lower loss: a QA or search query often contains the entity or phrasing that comes next, so writing the query helps prediction even with no result.
    • Example: the query "Where is the Eiffel Tower?" before the text "... in Paris" may lower loss slightly just by repeating the context, but the gain only counts if the returned result adds information.
    • Taking the min means the result must beat both the plain text and the query-only text. This isolates the information in the tool output.
    • During filtering the call and result are given as a prefix rather than inline, because the base model has never seen inline API syntax; this keeps the measurement in-distribution.
    • The filter is a utility signal defined by the LM itself, which is why the method needs no human labels beyond a few prompt demonstrations per tool.
  3. What if…

    Table 9 varies the decoding rule for starting an API call: the call token must be the most likely token (k = 1) or only in the top 10 (k = 10). On T-REx the call rate rises from 40.3% to 98.1% and accuracy from 47.8 to 53.5; on WebQS from 8.5% to 100% and 18.9 to 26.3. At k = 1, examples without a call score higher than the no-tool average. What does this say about whether Toolformer learned when to call a tool?

    What a strong answer covers
    • At k = 1 the model calls tools selectively, and the no-call examples are ones it can answer well alone (44.3 on T-REx vs a 34.9 no-tool average; 19.9 vs 18.9 on WebQS). That is some calibration.
    • At k = 10 nearly every example triggers a call, and accuracy is higher. Here calling is cheap and the tool is strong, so over-calling pays.
    • The calibration is lost at higher k: the model no longer distinguishes when it needs help. The paper evaluates its main results at k = 10.
    • So Toolformer learned which tool fits a context and how to form the call, but the decision of whether to call is mostly set by a decoding threshold, not by the model.
    • Tool cost is ignored in the objective, so there is no pressure to call less. A cost-aware variant would need a penalty per call.
  4. Critique

    On LAMA, Toolformer uses its QA tool on 98.1% of examples, and that tool is Atlas fine-tuned on Natural Questions. Argue whether the LAMA gains (for example T-REx 31.9 → 53.5 over GPT-J) measure learned tool use or the strength of the tool, and propose the experiment that separates them.

    What a strong answer covers
    • Atlas is itself a retrieval-augmented QA model. If nearly every example calls it, the score is close to "Atlas plus GPT-J formatting", not a measure of skill in deciding when and how to call.
    • Learned-skill evidence would be: picking the right tool (other tool use is only 0.7%), forming good questions, and integrating the answer into the text.
    • Experiment: a pipeline baseline that always calls Atlas with the masked sentence and fills in its answer. If it matches Toolformer, the learned part adds little on LAMA.
    • Second probe: swap in a weaker QA tool and see whether Toolformer's gain shrinks in proportion to the tool's standalone accuracy.
    • Note Wikipedia search is disabled for LAMA, and the QA tool is disabled for the QA benchmarks, which partly guards against the most obvious leakage.
  5. Design

    You want an 8B open model to learn when to call your dense retriever over a scholarly corpus, on an academic GPU budget. Lay out a Toolformer-style data pipeline (sampling, execution, filtering, fine-tuning), estimate where the cost goes, and say what you would measure to decide whether this beats RL with an answer-correctness reward.

    What a strong answer covers
    • Sampling: score p(<API>) at each position on a domain corpus; keep positions above τs, top-k per document; sample m candidate queries per position with a few-shot prompt.
    • Execution: run every candidate query through the retriever; this is cheap relative to LM scoring, but the three forward passes per candidate for L⁺, L(ε) and L(e(c, ε)) dominate.
    • Yield is low: the paper processes over a million documents to keep only a few thousand calculator calls, so budget for heavy sampling or seed positions with heuristics.
    • Measure: downstream QA or citation accuracy, call rate and its calibration (accuracy on no-call cases), perplexity on held-out text with tools disabled, and retrieval recall of the generated queries.
    • Compared with RL: Toolformer needs no reward or verifier and keeps LM ability, but cannot learn multi-step chains or query reformulation; RL with outcome reward can, at higher rollout cost.
  6. Connect

    Toolformer, Self-RAG and Search-R1 all teach a model when to call a retriever, but each uses a different training signal. Name the signal in each, what it rewards, and one failure mode each signal invites.

    What a strong answer covers
    • Toolformer: reduction in weighted next-token loss on the following text. It rewards calls that help predict the corpus, which may differ from calls that help a user's task.
    • Self-RAG: reflection-token labels from a critic distilled from GPT-4, inserted offline, trained with a plain LM loss. It inherits the judge's biases and cannot exceed what the labels say.
    • Search-R1: an outcome reward on the final answer, optimised with RL over multi-turn search. It can learn multi-hop search but invites reward hacking and long, costly trajectories.
    • Only the RL setup sees the result of a whole chain of calls; Toolformer samples each call independently, which is why it cannot chain tools.

Done with this page?

Status