TL;DR
A language model only produces text, so on its own it cannot check today's weather, read a database or run a calculation. Tool use gives it a way out: the model writes a call in a fixed format, the surrounding program runs the named function with the arguments the model wrote, and the result is pasted back into the text so the model can go on. The model decides what to ask; the program does the asking.
Builds on: Language Model, Structured Output · Leads to: LLM agents, Toolformer
Where it's used
Customer support. A shop's chat assistant needs the status of order 48213. The model cannot know it, so it writes a call
to get_order with that number. The shop's system looks up the order and returns "shipped on 14 November". The assistant then
tells the customer the parcel is on its way, with the real date rather than a guess.
Coding assistants. A coding assistant asked "why do the tests fail?" calls a tool that runs the test suite, then reads the failure messages that come back. Each run is a call, and the assistant's next edit depends on what the last call printed.
Arithmetic and search. A model asked to multiply 48213 by 17 is unreliable when it writes the digits itself. With a
calculator tool it writes calc("48213*17"), receives 819621, and copies that number into its answer. Search tools work the
same way for facts that change after the model's training data was collected.
How it works
Take one tiny instance. The program has one tool, get_weather, which takes a city name and returns a temperature in
degrees Celsius. The tool's description is part of the prompt, so the model knows the name and the argument it needs. The user
asks: "What is the weather in Oslo?" The model's training did not include today's weather, so it cannot answer from memory.
- Declare the tool. The prompt contains a line such as
get_weather(city: string) -> temperature in Celsius. Nothing has run yet; the model has only read text. Check: the prompt now names the tool and its argument. - The model chooses a call. Asked the question, the model scores its next choices. A call marker scores highest, so the
model writes
CALL get_weather {"city": "Oslo"}and stops. The braces are the argument object, written as text. Check: the call follows the declared name and argument, so it can be parsed. - The program parses and checks the call. The generation stops at a stop marker. The program reads the name
get_weatherand the object{"city": "Oslo"}, and checks thatcityis present and is a string. Check: the arguments match the declaration, so the call is allowed to run. - The program runs the real function. The program calls the weather service with "Oslo". The service returns 4. The model did not do this; it only wrote the request. Check: the value 4 came from outside the model.
- The result goes back into the text. The program appends
RESULT 4to the context, so the text now readsCALL get_weather {"city": "Oslo"} RESULT 4. The model reads this like any other words. Check: the result is now part of the context the model conditions on. - The model writes the answer. Given the result, the model writes "It is 4 degrees Celsius in Oslo right now." The
number came from step 4, not from the model's memory. Check: removing
RESULT 4would change the answer.
sequenceDiagram participant U as User participant M as Model participant P as Program participant T as Tool U->>M: "What is the weather in Oslo?" M->>P: CALL get_weather with city Oslo P->>P: Parse and check the arguments P->>T: get_weather(city = "Oslo") T-->>P: 4 P->>M: Append RESULT 4 to the context M->>U: "It is 4 degrees Celsius in Oslo."
Step 2 is the part that depends on training. In the simplest systems the model writes the call because its training examples showed calls in that format in the right places. Step 3 is the part that does not depend on the model at all: the program, not the model, decides whether the call is well formed. That is why the call format is usually enforced with structured output, so the arguments always parse.
The math
The model still defines a probability distribution over the next token given everything written so far. Write for the context (the prompt plus all text and results up to step ), and for the next token. The model gives a distribution , where are its weights.
A tool call is a span of tokens name, where the name is one of the declared tools and the argument object satisfies the declared schema (the list of argument names and the type each must have). The program computes an observation and the next context is , where means appending the text of .
Step 2 in symbols: the choice to call
At a decision point, the model has logits (raw, unnormalised scores for each option) for starting a call and for writing the answer directly. The probabilities come from the Softmax:
and . The model does not know about the weather service; it knows only that in its training text, a question about live data was usually followed by a call.
Step 3 in symbols: the check
The program accepts the call only if , that is, the arguments satisfy the schema. If , the program can refuse the call and return an error as the observation, , so the model can try again. Nothing runs unless the check passes.
Steps 4 to 6 in symbols: the loop
The whole process is a loop. The model samples tokens from ; when it finishes a call, the context becomes ; sampling continues from there, until the model writes an answer and stops. The model's weights never change during this loop. Only the context grows, which is why the same model can use a tool it has never seen before, as long as the tool's description is in the prompt (in-context learning at work).
Worked examples
At the start of an answer, the logits are and .
- Compute the exponentials: and .
- Add them: .
- Divide for the call: .
- Get the answer probability: .
- Check: .
The interpretation: the model calls the tool in about 88 of every 100 samples at this point. A larger gap between the two logits would make calls nearly certain, and a gap of zero would make them a coin flip.
At a decision point the model can call get_weather, call get_calendar, or answer directly. The logits are
get_weather = 2.0, get_calendar = 0.5 and answer = 1.0.
- Compute the exponentials: , and .
- Add them: .
- Divide each by the sum:
get_weather,get_calendar, answer . - Check: .
- The program samples
get_weatherin about 63 of 100 runs. In the rest, the model calls the calendar or answers without any tool, which is a mistake for a weather question. That is why the tool descriptions in the prompt matter: they move these logits.
The interpretation: the choice among tools is the same softmax as any other choice of next token. The model is not reasoning about the schema of each tool; it is picking the most likely continuation, which the prompt shaped.
A support assistant has the tool get_order(order_id). The user writes: "Where is my order 48213?" The tool returns:
{"status": "shipped", "date": "14 November", "items": 2}.
- The model writes
CALL get_order {"order_id": "48213"}. The program checks thatorder_idis a string. It passes. - The program looks up the order and gets the result above. It appends that text to the context.
- The model now has three facts to use: the status is shipped, the date is 14 November, and there are 2 items.
- The model writes "Your order 48213 shipped on 14 November. It has 2 items and is on its way." Each fact in the reply appears in the result. None is invented.
- Suppose instead the tool returned
{"error": "not found"}. A well-behaved assistant then writes "I could not find order 48213. Please check the number." It does not guess a status.
The interpretation: the tool supplies the facts, and the model's job is to report them correctly, including the case where the tool reports that nothing was found.
Try it: a tool get_price(item) returns 7.50 for pen. The user asks for the total for 4 pens. Using the tool result,
what total should the assistant give, and what must the assistant do first?
Answer: the assistant calls get_price("pen"), gets 7.50, and computes . The total is 30.00. The
assistant must make the call first, because the price is not in its own knowledge.
Common mistakes and tradeoffs
It is tempting to think the model runs the tool. In fact the model only writes the call as text. The program parses it, checks it, runs the function and writes the result back. This matters because the program is where security and error handling live.
It is tempting to think a tool result is always true. In fact a tool can return stale, wrong or empty data, and a model can misread a correct result. The program should check the result shape, and the model should state when a result is missing or doubtful, as in Example 3.
It is tempting to think the model should call a tool for every question. In fact each call adds latency and cost, and a call can fail. For "what is the capital of France?" the model should answer directly.
It is tempting to think tool output is safe to trust as instructions. In fact a web page or a document returned by a search tool can contain text such as "ignore your instructions". The model reads that text as context, so tool results must be treated as data, not commands. This is called prompt injection.
When to use tool use: the answer depends on live data, on an exact computation, or on an action outside the text (sending a message, changing a record). When not to use it: when the model's own knowledge is enough, or when the tool would be slower than the answer. Alternatives: retrieval-augmented generation fetches documents into the context without a call to a named function; Fine-Tuning changes the weights, which suits a fixed style or skill but cannot track live data; and ordinary code can handle a fixed workflow with no model choice at all.
History
By 2022 language models could reason step by step when asked to (chain-of-thought prompting), but they could not act on the world. In October 2022 Yao and colleagues introduced ReAct, which interleaves written thoughts with actions and their observations, using the ReAct format. Its results showed that letting the model look at real observations reduced invented facts on the tasks studied. In February 2023 Schick and colleagues published Toolformer, which trained a model on its own generated text, with API calls inserted where they helped predict the following words. In mid-2023 several model providers added function-calling interfaces to their APIs, so that developers could declare tools in a structured way and receive calls in a fixed format. That made tool use a standard part of building applications on language models.
Recap
- A language model writes text; it cannot act or look things up by itself.
- A tool is a named function with a declared schema. The model writes a call to it as text.
- The program parses and checks the call, runs the real function and appends the result to the context.
- The model's next tokens are sampled from the same softmax as any other choice, with the context now containing the result.
- The model's weights do not change; only the context grows.
- Results can be wrong or hostile, so the program checks them and the model reports what it actually found.
What to read next: LLM agents, which run this loop repeatedly to reach a goal, and Toolformer, which shows how the model can learn when to call a tool.
Check yourself
QuestionIn one sentence, which part of tool use does the model do, and which part does the program do?Show answer
The model decides what to ask and writes the call as text, choosing the name and arguments. The program parses and checks the call, runs the real function, and appends the result to the context. The model then continues writing from that result.
QuestionAt a decision point the logits are and . What is to three decimals?Show answer
and , so .
QuestionWhy does the program check the arguments before running a tool, when the model was trained to write them?Show answer
The model writes arguments from patterns in its text, so it can produce values that do not match the schema, such as a missing field, a number where a string is required, or a value that is harmful. The check is cheap and runs outside the model, so it still protects the system when the model makes a mistake.
QuestionA shop assistant calls get_stock(item) and gets the result 0, but the customer says the shop has the item. What should the assistant do?Show answer
The assistant should report that the stock system shows zero for the item, not assert that the shop has it. It can offer to check again or to pass the question to a person, because the tool result may be stale or the item code may be wrong.