A model that asks for a tool
A language model can only write text. Tool calling lets it ask for what it cannot do reliably on its own, such as exact arithmetic or a lookup in a table, by writing a structured request that the program around it, the caller, carries out. The caller describes each tool with a JSON schema: its name, what it does, and its arguments with their types. The model reads these descriptions in its prompt and, when it decides a tool would help, writes a call as JSON. The caller parses the call, runs the tool itself and hands the result back as a new message; the model then calls again or answers. The model never runs anything: every effect goes through code the caller controls.
On this page Qwen3-1.7B has two local, deterministic tools: a calculator that evaluates an arithmetic expression with exact decimal arithmetic (the expression is parsed, never run as Python) and a lookup in PubChem's periodic table of 118 elements. Every assistant turn is decoded greedily with thinking switched off.
The page records four questions. Qwen3-1.7B calls the calculator for 48,213 × 9,177, looks up iron and copper with two calls in one turn to compare their atomic numbers, and answers "What is the capital of France?" directly. It also answers "What is 23.5% of 4,812?" directly, working through the formula in text, and gets 1,129.5; the calculator gives 1,130.82. Having a tool does not mean the model uses it when it should.
The prompt the template builds
A chat template turns a list of messages into one sequence of tokens. Qwen3's wraps each message as <|im_start|>role … <|im_end|>. Given tools, it opens with a system message that lists their JSON schemas, one per line, between <tools> and </tools>, and tells the model to answer a call as {"name": …, "arguments": …} between <tool_call> and </tool_call>. The panel The prompt the template builds shows the result token by token, with the schemas tinted.
Some of the markers are single tokens and some are not. <|im_start|> and <|im_end|> are special tokens; <tool_call>, </tool_call>, <tool_response> and <think> are added tokens with one id each, which the model learned to read and write in post-training. <tools>, by contrast, is spelled out in ordinary sub-word tokens. With thinking off the template ends the prompt with <|im_start|>assistant and an empty <think></think> block, so the model starts its reply at once.
The decision, token by token
The assistant turn is written one token at a time. At each step the model gives a logit to every token of its 151,936-row output layer, and
greedy decoding takes the most likely token. The first token decides what kind of turn it is: <tool_call> starts a call, anything else starts a reply in text. The panel The model's turn shades each token by its surprise, , and lists the eight most likely next tokens at the selected step, so the probability of <tool_call> at the first token shows how sure the model was that it needed a tool.
In these runs the decision is never close. The first token is <tool_call> with probability 0.9993 for the product and 0.9997 for the two lookups. For the capital of France it is , and for the percentage : the model was even surer that it could do that sum itself than that the product needed the calculator.
A call is then ordinary text the model writes token by token: the JSON object with the function's name and its arguments, then </tool_call> and <|im_end|>. Nothing forces that text to be valid JSON or to match the schema; the Constrained tab shows how a decoder can guarantee it.
Parse, run, feed back
The caller reads the text between each <tool_call> and </tool_call> as JSON, the way Qwen's function-calling guide does, and treats a block that does not parse as no call. The Parse, run, feed back column validates the arguments against the tool's schema, runs the tool and shows its result. Several <tool_call> blocks in one turn are several calls, which the caller may run in any order or at the same time.
Each result goes back as a message with the role tool. Qwen3's template has no tool turn of its own: it writes the results into a user turn, each between <tool_response> and </tool_response>, and consecutive results share one turn. The panel Back into the next prompt shows exactly the tokens that the next prompt adds.
For iron and copper the model writes two <tool_call> blocks back to back in one 41-token turn. The caller runs both lookups, and the next prompt carries both results in one user turn; the model then answers that copper, 29, has the higher atomic number than iron, 26.
The loop
An agent framework runs this as a loop: render the conversation, decode one assistant turn, and if it holds calls, run them, append their results and render again; a turn without a call is the answer. The strip The conversation lays out every message of a run.
Rendering again is not the same as appending. The template writes an assistant turn's empty think block only for the latest turn, so an earlier call comes back without it, and the new prompt agrees with the old prompt and output only up to that point (grey in the prompt panel). A server that caches the keys and values of the last turn can reuse them only for that shared prefix.
In the product conversation the second prompt has 355 tokens. Its first 300 are the first prompt's, and the 4 tokens the template leaves out are exactly <think>, \n\n, </think>, \n\n. Everything after them is new: the call as the template re-serialises it, the result in a <tool_response> block, and a fresh generation prompt.
Where it came from
TALM and Toolformer taught models to insert calls to tools into their text, Toolformer by keeping the self-generated calls that made the following tokens more likely. Gorilla and ToolLLM fine-tuned models on the documentation and calls of thousands of real APIs. Today's chat models learn a template-specific call format during post-training, as Qwen3 did, and frameworks only render the schemas and parse the calls.
The ReAct and CodeAct tabs run longer loops in which the model reasons between actions or writes its actions as code, and the MCP tab serves the tools from a separate process through the Model Context Protocol.
Papers
- Toolformer: Language Models Can Teach Themselves to Use Tools A model learns to call a calculator, a search engine and other tools by keeping the calls it generated that helped it predict the following text.
- TALM: Tool Augmented Language Models An early text-to-text interface between a model and tools, with tool results fed back into its input.
- Gorilla: Large Language Model Connected with Massive APIs Fine-tunes a model to write correct API calls from their documentation and measures hallucinated calls.
- ToolLLM: Facilitating Large Language Models to Master 16000+ Real-world APIs Instruction data for thousands of real APIs and multi-step calling, with an evaluation of tool-use paths.
- Qwen3 Technical Report The model recorded here, whose chat template writes the tool schemas and reads its <tool_call> blocks.
- Tool Learning with Large Language Models: A Survey A survey of why and how models use tools: task planning, tool selection, calling and response generation.