Skip to main content
GW
HomeWorkAI appsPapersAboutNotesStackLabPilotPlan↗ (opens in a new tab)INVST↗ (opens in a new tab)
Govind Waghmare

Finance systems and AI. Building in public from NYC.

Resume (PDF)Book 20 minEmail
HomeNotesWorkAboutStackPapersLabFor founders
XLinkedInGitHubllms.txt
© 2026 · New York City

Keyboard shortcuts

?
Open this list
g then h
Go to Home
g then w
Go to Work
g then n
Go to Notes
g then a
Go to About
c
Ask the AI guide
Esc
Close this list
Original paperSection A, The abstract
Open in new tab (opens in a new tab)
All papers
FoundationsClassic

A model that teaches itself when to use tools

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

Timo Schick, Jane Dwivedi-Yu, Roberto Dessì, Roberta Raileanu, Maria Lomeli, Luke Zettlemoyer, Nicola Cancedda, Thomas Scialom · arXiv · Posted 9 Feb 2023 · 16 min read · arXiv 2302.04761

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 599 KB

  • Language models fail at arithmetic and fact lookup.
  • They do not know today's date.
  • Earlier tool methods need many human labels or work for one task.
  • Models should learn when to use a tool.
44.0

MAWPS score for Toolformer, versus 19.8 for GPT-3 (175B).

  • They gave GPT-J (6.7B) five tools: calculator, Q&A, search, translator and calendar.
  • The model wrote its own tool calls in text.
  • Only calls that helped predict the next words were kept.
  • They tested math, facts, languages and dates.

  • Toolformer beat models up to 25 times its size on many tasks.
  • It kept its language skill: perplexity did not rise.
  • Tool use only appears at about 775M parameters.
  • It cannot chain tools or browse results.

With the calculator, Toolformer beats GPT-3 on math (ASDiv and SVAMP)From Table 4
GPT-JToolformer (disabled)ToolformerGPT-3 (175B)
01020304050Score (%)ASDiv7.5%14.8%40.4%14.0%SVAMP5.2%6.3%29.4%10.0%

Hover, tap or focus a bar to read its exact value.

DatasetGPT-J (%)Toolformer (disabled) (%)Toolformer (%)GPT-3 (175B) (%)
ASDiv7.514.840.414.0
SVAMP5.26.329.410.0

  • It cannot chain tools or use results from one tool in another.
  • It is sensitive to wording when deciding to call.
  • It ignores the cost of a call.
  • Small models gain nothing: tools help at about 775M parameters and up.

Jump to

  1. A The abstract
  2. 1 Introduction
  3. 2 Approach
  4. 3 Tools
  5. 4.1 Experimental setup
  6. 4.2 Downstream tasks and decoding
  7. 4.2.1 LAMA
  8. 4.2.2 Math datasets
  9. 4.2.3 Question answering
  10. 4.2.4 Multilingual question answering
  11. 4.2.5 Temporal datasets
  12. 4.3 Language modeling
  13. 4.4 Scaling laws
  14. 5 Analysis
  15. 6 Related work
  16. 7 Limitations
  17. 8 Conclusion
  18. App A Appendix A: API details
  19. App B Appendix B: training details
  20. App C Appendix C: zero-shot prompts
  21. App D Appendix D: DATESET
  22. W Words to know

Every detail from the paper

Cards follow the paper's own order. Dotted words link to Words to know. Numbers match the paper.

The problem

  • Language models solve new tasks from a few examples or instructions.
  • Yet they fail at basic things, such as arithmetic and fact lookup.
  • Much simpler and smaller models do these well.

The idea

  • Toolformer teaches itself to call tools through simple APIs.
  • It decides which API to call, when, and with what arguments.
  • It also decides how to use the result.
  • This is self-supervised. It needs only a handful of demonstrations per API.

The result

  • Tools: a calculator, a Q&A system, a search engine, a translator and a calendar.
  • Zero-shot results improve a lot, often matching much larger models.
  • Core language modeling does not suffer.

Limits of language models

  • Large models are strong at zero-shot and few-shot tasks.
  • But scaling alone only partly fixes some limits.
  • They lack up-to-date information on recent events.
  • They tend to hallucinate facts.
  • They struggle with low-resource languages.
  • They lack skill at exact math.
  • They do not sense the passing of time.

The obvious fix

  • Give models tools: search engines, calculators and calendars.
  • Existing ways need large amounts of human annotation.
  • Or they work only for one task, which limits wide use.

Two requirements

  • Tool use must be learned in a self-supervised way, with few human labels.
  • Humans may find different things useful than the model does.
  • The model must keep its generality.
  • It must decide for itself when and how to use each tool.

The approach

  • Give a model a handful of examples of how an API is used.
  • It annotates a huge text set with possible API calls.
  • A self-supervised loss keeps only the calls that truly help predict later tokens.
  • Then the model is finetuned on the kept calls.

Same data, same skills

  • The same text set can be the original pretraining data.
  • So the model keeps its generality and language skill.
  • The base is GPT-J, with 6.7B parameters.
  • It clearly beats much larger GPT-3 on many zero-shot tasks.

The goal

  • Goal: let a model M use tools through API calls.
  • Inputs and outputs of each API must be text.
  • Special tokens mark the start and end of each call.

How a call looks

  • A call is a pair: the API name and its input.
  • Format without result: start token, name, input in brackets, end token.
  • Format with result: the same, plus an arrow and the result.
  • In practice the tokens are " [", "]" and "->".
  • That avoids changing the model's word list.
How Toolformer builds its training dataFrom Section 2 and Figure 2
  1. Step 1Sample API calls
  2. Step 2Execute the calls
  3. Step 3Keep useful calls
  4. Finetune on the augmented text

A call is kept only if the answer helps predict the next words.

Step 1: sampling API calls
  • For each API, write a prompt that shows how to add calls to text.
  • Example: add Question Answering calls to "Joe Biden was born in Scranton, Pennsylvania".
  • The model gives each position a probability of starting a call.
  • Keep positions above a sampling threshold.
  • If there are more than k, keep the top k.
  • At each position, sample up to m calls.
  • Drop examples where the model never writes the end token.
Step 2: executing API calls
  • How a call runs depends on the API.
  • It may call another neural network, run a Python script or search a corpus.
  • Each response must be one text sequence.
Step 3: filtering API calls

The rule

  • Use a weighted cross entropy loss over the next tokens.
  • Weights are larger for tokens close to the call.
  • Loss A: the loss with the call and its result as a prefix.
  • Loss B: the lower of two losses: no call, or a call with no result.
  • Keep a call if loss B minus loss A is at least a threshold.
  • So the result must lower the loss by that much.

Why prefix

  • The result goes in as a prefix, not in the middle.
  • The model is not yet finetuned on calls.
  • Inserting mid-text would break the flow and raise perplexity.
Finetuning and inference

Finetuning

  • Merge the kept calls for all tools into the text.
  • The augmented text holds the exact same text as the original, plus calls.
  • Finetune with the normal language modeling goal.
  • Calls sit exactly where they help.
  • So the model learns when and how to use tools.
Using Toolformer at inferenceFrom Section 2
  1. Decode normally
  2. Model writes the arrow
  3. Pause and call the API
  4. Insert result and end token
  5. Continue decoding

Decoding stops when the model writes the arrow token.

The rules

  • Two rules: inputs and outputs are text, and a few demonstrations exist.
  • They test five tools.
APIExample inputExample output
Question AnsweringWhere was the Knights of Columbus founded?New Haven, Connecticut
Wikipedia SearchFishing Reel TypesA short snippet about spin fishing reels
Calculator27 + 4 * 235
Calendar(none)Today is Monday, January 30, 2023.
Machine Translationsûreté nucléairenuclear safety

Table 1: example input and output for each API

Tools 1 and 2

  • Question Answering: Atlas, a retrieval-augmented model finetuned on Natural Questions.
  • It answers simple factoid questions.
  • Calculator: the four basic operations. Results round to two decimals.

Tools 3 and 4

  • Wikipedia Search: returns short snippets for a search term.
  • The model must pick out the useful part.
  • It uses a BM25 retriever over the Wikipedia dump from KILT.
  • Machine Translation: the 600M NLLB model, for 200 languages.
  • A fastText classifier finds the source language. The target is English.

Tool 5

  • Calendar: returns today's date and takes no input.
  • It gives time context.

Three questions

  • Zero-shot tests where at least one tool should help.
  • Check that language modeling skill does not drop (Section 4.3).
  • Test the effect of model size (Section 4.4).

Data generation

  • Text set: a subset of CCNet. Language model: GPT-J.
  • Heuristics select texts where calls are likelier to help.
  • Example: calculator texts must have at least three numbers.
  • They also drop examples where all calls were filtered out.
APIThreshold 0.5Threshold 1.0Threshold 2.0
Question Answering51,98718,5265,135
Wikipedia Search207,24160,97413,944
Calculator3,680994138
Calendar61,81120,5873,007
Machine Translation3,1561,034229

Table 2: examples with API calls for different filter thresholds

Weights and training

  • Weights fall as tokens get farther from the call, so calls sit near their use.
  • Thresholds are set per tool to give enough examples.
  • Finetune with batch size 128 and learning rate 1e-5.
  • Use linear warmup for the first 10% of training.

Baselines

  • GPT-J: plain, no finetuning.
  • GPT-J + CC: finetuned on the text set with no calls.
  • Toolformer: finetuned on the text set with calls.
  • Toolformer (disabled): the same, with calls turned off in decoding.
  • Also OPT (66B) and GPT-3 (175B), about 10 and 25 times bigger.

Zero-shot

  • All tests are prompted zero-shot: no in-context examples.
  • Earlier tool work gave dataset-specific examples.
  • Zero-shot is harder, but it matches users who do not say which tool to use.

Decoding change

  • Greedy decoding, with one change.
  • The model starts a call when the call token is among the k most likely.
  • Plain greedy is k = 1.
  • They use k = 10 to make tool use likelier.
  • At most one call per input, so the model cannot loop.

The task

  • Complete a short statement with a missing fact, such as a date or place.
  • They use the SQuAD, Google-RE and T-REx subsets.
  • They keep examples where the blank is last.
  • A correct word in the first five words counts.
  • The Wikipedia Search tool is off, to be fair.
ModelSQuADGoogle-RET-REx
GPT-J17.84.931.9
GPT-J + CC19.25.633.2
Toolformer (disabled)22.16.334.9
Toolformer33.811.553.5
OPT (66B)21.62.930.1
GPT-3 (175B)26.87.039.8

Table 3: LAMA subsets

Toolformer beats models up to 25 times its size on LAMAFrom Table 3
GPT-JToolformerOPT (66B)GPT-3 (175B)
0204060Score on LAMA subset (%)SQuAD17.8%33.8%21.6%26.8%Google-RE4.9%11.5%2.9%7.0%T-REx31.9%53.5%30.1%39.8%

Hover, tap or focus a bar to read its exact value.

SubsetGPT-J (%)Toolformer (%)OPT (66B) (%)GPT-3 (175B) (%)
SQuAD17.833.821.626.8
Google-RE4.911.52.97.0
T-REx31.953.530.139.8

Result

  • Toolformer beats the best baseline by 11.7, 5.2 and 18.6 points.
  • It beats OPT and GPT-3 despite being much smaller.
  • It asks the Q&A tool in 98.1% of cases.
  • It uses another tool in 0.7% and none in 1.2%.

The task

  • Tests: ASDiv, SVAMP and MAWPS.
  • The score takes the first number in the output.
  • If there is an equation, it takes the number after the equals sign.
ModelASDivSVAMPMAWPS
GPT-J7.55.29.9
GPT-J + CC9.65.09.3
Toolformer (disabled)14.86.315.0
Toolformer40.429.444.0
OPT (66B)6.04.97.9
GPT-3 (175B)14.010.019.8

Table 4: math benchmarks

Result

  • Toolformer does better than GPT-J even with calls off.
  • They guess it learned from many calls and their results.
  • Turning calls on more than doubles the score on all tasks.
  • It beats OPT and GPT-3. It asks the calculator in 97.9% of examples.

The task

  • Tests: Web Questions, Natural Questions and TriviaQA.
  • A correct answer in the first 20 words counts.
  • The Q&A tool is off, because it was trained on Natural Questions.
ModelWebQSNQTriviaQA
GPT-J18.512.843.9
GPT-J + CC18.412.245.6
Toolformer (disabled)18.912.646.7
Toolformer26.317.748.8
OPT (66B)18.611.445.7
GPT-3 (175B)29.022.665.9

Table 5: question answering

Result

  • Toolformer beats all GPT-J models. It uses Wikipedia search in 99.3% of cases.
  • It still trails GPT-3 (175B).
  • The search tool is simple and often returns poor matches.
  • The model cannot rewrite a query or browse results.

The task

  • MLQA has an English context and a question in another language.
  • Languages: Arabic, German, Spanish, Hindi, Vietnamese and Chinese.
  • Translating the question into English may help.
  • A correct answer in the first 10 words counts.
ModelSpanishGermanHindi
GPT-J15.216.51.3
GPT-J + CC15.714.90.5
Toolformer (disabled)19.811.91.2
Toolformer20.613.51.4
OPT (66B)0.30.11.1
GPT-3 (175B)3.41.10.1
GPT-J (All En)24.327.023.9
GPT-3 (All En)24.727.226.1

Table 6 (part 1): MLQA

ModelVietnameseChineseArabic
GPT-J8.218.28.2
GPT-J + CC8.313.74.6
Toolformer (disabled)10.115.03.1
Toolformer10.616.83.7
OPT (66B)0.20.70.1
GPT-3 (175B)1.717.70.1
GPT-J (All En)23.323.123.6
GPT-3 (All En)24.923.624.0

Table 6 (part 2): MLQA

The All En rows give English contexts and English questions. They are an upper bound.

Result

  • Calls help Toolformer in every language.
  • The translation tool is used in 63.8% to 94.9% of examples.
  • The exception is Hindi: only 7.3%.
  • Toolformer does not always beat plain GPT-J.
  • Finetuning on CCNet hurts some languages, likely from data shift.

Why OPT and GPT-3 look weak

  • OPT and GPT-3 score very low.
  • They often fail to answer in English.
  • GPT-J saw more multilingual data, including EuroParl.
  • With English only, GPT-3 does best, so its weakness is the multilingual setup.

The two datasets

  • TEMPLAMA: cloze questions on facts that change over time, 2010 to 2020.
  • DATESET: a new set of date questions.
  • Example: "What day of the week was it 30 days ago?".
  • The current date is needed to answer.
ModelTEMPLAMADATESET
GPT-J13.73.9
GPT-J + CC12.92.9
Toolformer (disabled)12.75.9
Toolformer16.327.3
OPT (66B)14.51.3
GPT-3 (175B)15.50.8

Table 7: temporal datasets

Only the calendar tool lifts DATESET scoresFrom Table 7
0102030DATESET score (%)GPT-J3.9%Toolformer (disabled)5.9%Toolformer27.3%OPT (66B)1.3%GPT-3 (175B)0.8%

Hover, tap or focus a bar to read its exact value.

ModelDATESET (%)
GPT-J3.9
Toolformer (disabled)5.9
Toolformer27.3
OPT (66B)1.3
GPT-3 (175B)0.8

Result

  • Toolformer wins on both.
  • On TEMPLAMA, gains come from Wikipedia search and Q&A, not the calendar.
  • The calendar is used in only 0.2% of TEMPLAMA examples.
  • The best route is calendar first, then the Q&A tool.
  • Chains are not supported.
  • On DATESET the gain is fully from the calendar, used in 54.8% of examples.

The check

  • They check that finetuning on calls does not hurt perplexity.
  • Perplexity is how surprised a model is by text. Lower is better.
  • Datasets: WikiText and 10,000 random CCNet documents not used in training.
ModelWikiTextCCNet
GPT-J9.910.6
GPT-J + CC10.310.5
Toolformer (disabled)10.310.5

Table 8: perplexity

Result

  • Finetuning on CCNet helps a CCNet subset a little.
  • It hurts WikiText a little, since GPT-J's pretraining data is closer to WikiText.
  • Training on text with calls adds no perplexity cost with calls off.
  • Perplexity with calls on cannot be computed.
  • It needs a sum over all possible calls.

Setup

  • They apply the method to four GPT-2 models: 124M, 355M, 775M and 1.6B parameters.
  • They use three tools: Q&A, calculator and Wikipedia search.

Result

  • Tool use appears only at around 775M parameters.
  • Smaller models do the same with or without tools.
  • The exception is Wikipedia search on Q&A tests, which is easy to use.
  • Models improve without calls as they grow, and use calls better too.
  • A large gap with and without calls remains even for the biggest model.

Decoding strategy

  • Table 9 varies k, the number of top tokens that may start a call.
  • Higher k means more calls.
  • At k = 1: 40.3% and 8.5%.
  • At k = 10: 98.1% and 100%.
  • On T-REx, k = 1 already improves results.
  • On WebQS, calls start only when k grows.
kT-REx: AllT-REx: AC / NCWebQS: All
034.9none / 34.918.9
147.853.0 / 44.319.3
352.958.0 / 29.026.3
1053.554.0 / 22.526.3

Table 9: results by k, as All, then API-call subset / no-call subset

AC is where the model calls an API. NC is where it does not. WebQS AC / NC at k = 1 is 17.1 / 19.9.

Calibration

  • At k = 1 the model seems calibrated.
  • It calls APIs on examples it would do badly on.
  • Its no-call scores (44.3 and 19.9) beat the no-call average (34.9 and 18.9).
  • This calibration is lost at higher k.

Data quality

  • Table 10 shows sample calls sorted by the loss gain.
  • High scores are usually useful calls. Low scores add nothing.
  • Top example: Wikipedia search about the Flodden Window, score 5.49.
  • A calendar call scored 2.11. A Q&A call on the Nile scored 2.08.
  • A calculator call on Venus scored 1.59.

Noise

  • There are exceptions: a "Fast train success" call gave nothing useful but cut perplexity.
  • Some noise helps.
  • It stops the model from blindly trusting every result.

Language model pretraining

  • Some work adds extra text during pretraining: metadata, HTML tags, Wikipedia markup or retrieved texts.
  • That text is always given, useful or not.
  • Toolformer learns to ask for the right information.

Tool use

  • Tool use includes search engines, browsers, calculators, translators and Python interpreters.
  • Learning is either from large human supervision, or from few-shot prompts for one task.
  • Toolformer needs no task-specific prompt.
  • Closest is TALM, which uses a similar self-supervised goal.
  • TALM only works when finetuning for downstream tasks.

Bootstrapping

  • Self-training and bootstrapping has a long history, from word senses to reasoning.
  • Toolformer trains on its own predictions after a perplexity-based filter.

Chains and interaction

  • It cannot chain tools, because calls are made independently.
  • There are no chained examples in the finetuning data.
  • It cannot use a tool interactively, such as browsing search results.

Wording and efficiency

  • It is sensitive to the exact wording of the input.
  • It is sample-inefficient for some tools.
  • More than a million documents give only a few thousand useful calculator calls.
  • Repeating the method may help.

Cost

  • It ignores the computational cost of a call when deciding to call.

Summary

  • Toolformer learns, self-supervised, to use tools through simple API calls.
  • It finetunes on sampled calls filtered by whether they cut perplexity.
  • A 6.7B GPT-J gets much better zero-shot results.
  • It beats a much larger GPT-3 on several tasks.

Defaults

  • Defaults: sampling threshold 0.05 and filter threshold 1.0.
  • Keep the top k = 5 positions.
  • Sample up to m = 5 calls each.

Rare tools

  • For calculator and translation, few texts qualify.
  • So sampling threshold 0.0, k = 20, m = 10.
  • Filter threshold 0.5.

A.1 Implementation 1

  • Question answering: Atlas-large to build the data, the larger Atlas-xxl at inference.
  • Calculator: a Python script with +, -, * and /. Invalid equations return nothing.
  • Heuristics: three numbers within 100 tokens, or words like "equals" before a number.
  • Texts matching only the three-number rule keep a random 1%.

A.1 Implementation 2

  • Calendar: the date is taken from the document URL. About 18% of documents have one.
  • Translation: keep paragraphs with a non-English chunk between English text.
  • Chunks are 10 tokens. fastText confidence must exceed 0.8.
  • Drop calls whose input comes only after the call.

A.2 Prompts

  • A.2 lists the prompt for each tool.
  • Each asks the model to add API calls to text, with a few examples.

Settings

  • Up to 25k examples per API. Max sequence length 1,024.
  • Effective batch size 128.
  • Training uses DeepSpeed ZeRO-3 on 8 NVIDIA A100 40GB GPUs with BF16.
  • Up to 2k steps. Perplexity is checked every 500 steps on 1,000 CCNet examples.
  • Pick the best checkpoint.

Prompts

  • LAMA and TEMPLAMA: "Please complete the following text so that it is factually correct".
  • Math: the context and question followed by "The answer is".
  • Question answering: prefix "Answer the following question:".
  • MLQA: a prompt that answers in English from the paragraph.

Construction

  • They pick 500 random current dates.
  • For each, a past or future date within a four-year range fills the templates.
  • Example: "How many days ago was August 14, 2020?".
  • If called, the calendar returns the presumed date.
  • Example: "Today is Sunday, November 20, 2020".
  • Table 11 lists the templates, including US federal holidays.

Words to know

  • API: A way for a program to call a service.
  • Calibrated: Stated confidence matches how often the answer is right.
  • Filtering threshold: The minimum loss gain a call needs to be kept.
  • Hallucination: When a model states made-up facts as if true.
  • Perplexity: A score of how surprised a model is by text. Lower is better.
  • Self-supervised: Learning from signals the data already has, with no human labels.
  • Zero-shot: Doing a task with instructions only, with no examples.

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 599 KB

Ask AI about this paper

Stuck on a word or an idea? Ask in plain words. The answer comes from the write-up on this page.

Read the original paper on arXiv: Toolformer: Language Models Can Teach Themselves to Use Tools (opens in a new tab)

Jump to

One paper a week, in plain English

Every Monday: the new AI papers worth your time. Short lines, the key number, and what to do with it.

One email a week. Unsubscribe with one click. No spam.