deep · 6 min
Tool calling
The model asks your code to run a function, your code runs it and sends the result back, and the model carries on.
updated 29 Sept 2026
A model can only write text. So when someone asks a chatbot "What's the weather in San Francisco?" and it answers with today's temperature, what actually happened? It didn't open a weather site. It can't run anything.
What it did was ask. Tool calling (also called function calling) lets a model write a structured request to run one of our functions, saying which function and with what arguments. Our code runs it and sends the result back, and the model carries on with that in hand. Let's follow one question all the way through.
One question, two requests
We give the model one tool, get_weather. A tool definition has three
parts: a name, a plain description of what it does and when to use it,
and a JSON Schema for its inputs. Here the schema has one required field,
location.
The first request goes through the normal chat API with the question and the tool list. The model can't look up weather, so instead of an answer it sends back a tool call:
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "San Francisco, CA" }
}
The response also says why it stopped. On Claude that's
stop_reason: "tool_use", and that's our signal. Our code reads the name
and arguments, runs the real lookup, and gets "15 degrees Celsius, partly
cloudy".
Now a second request. It carries the whole conversation again (the
question, the model's tool call) plus a new user message with a
tool_result block, which points back to the call by its id. With the
result in front of it, the model writes the answer in plain words.
OpenAI's version is the same five steps with different field names.
And if the model wants another tool after that? It just asks again. So in general it's a loop: while the stop reason says "tool use", run the tools, send back the results, call again. Any other stop reason (finished, hit the token limit, refused) ends it. Every one of those rounds is a full extra request. When the model is choosing its own next step around that loop, we have the agent loop.
Behind the curtain
So the model never runs anything. It emits a request, our code (or the provider's servers, for built-in tools like web search) does the work, and the model only ever sees the schema we gave it and the result we sent. It never sees our function's code.
Open models make the trick easy to see. With Hugging Face Transformers, the chat template writes our tool definitions into the prompt as text. When the model decides to call one, it writes the call as ordinary text, in a format it was trained on:
<tool_call>
{"arguments": {"location": "Paris, France", "unit": "celsius"}, "name": "get_current_temperature"}
</tool_call>
Our code has to parse that string into a name and arguments. Hosted APIs
do the same thing out of sight: they put the tool definitions into the
system prompt in a syntax the model knows, and parse what it writes back
into a clean tool_use object. So tool calling is still
next-token prediction. The model has been
trained, during post-training, to write calls in a
format the API can read.
That shows up on the bill. Tool definitions are input tokens on every request, and so are the calls and results sitting in the history. Claude also adds a hidden tool-use system prompt whenever tools are present, roughly 300 to 800 tokens as of 2026-09 depending on the model and setting.
Should it call a tool at all?
By default the model decides. It calls a tool when the request matches
what a tool's description says it does and the answer isn't already in
front of it, and answers directly otherwise. A tool_choice setting
changes that: let the model decide (auto), make it call some tool, make
it call one particular tool, or turn tools off.
Forcing has side effects worth knowing. On Claude, forcing a tool means
the model writes no explanation before the call, even if we ask for one.
And as of 2026-09 the newest Claude models reject forced tool calls with a
400 error. On those, the advice is auto with strict mode, or
structured output when what we want is a fixed JSON
answer and not an action.
The model can also ask for several tools in one turn, say the weather and the local time. Both APIs have a switch to limit it to one call per turn.
Valid isn't the same as right
Without extra settings, the arguments are the model's best effort. They
usually match the schema, but nothing promises it. Set strict: true on a
tool and the provider uses the same grammar-constrained sampling as
structured output (constrained decoding), so the
arguments are guaranteed to fit. OpenAI recommends always turning it on.
It brings the same schema rules: on OpenAI every field must be listed as
required, with null allowed for the optional ones.
But strict mode fixes the shape, not the judgment. Ask "What's the weather?" with no city at all, and a Sonnet model might call the tool anyway with a guessed "New York, NY". Opus models are more likely to ask which city, though that isn't guaranteed either. The arguments are valid and the call is wrong. Anything with side effects deserves a check in our own code before it runs.
When is a tool the right move?
When the model needs something text alone can't give: an action with side effects (send the email, update the record), fresh or private data (today's prices, our database), or a call into a system we already have. Not when the model can answer from what it knows, like summarising or translating, or when the task is so small that an extra round trip costs more than the work.
There's one more case people miss. If we catch ourselves writing a regex
to pull a decision out of the model's prose ("find the word after
Intent:"), Anthropic's advice is that the decision should have been a
tool call. Put the choices in the schema as an enum and let the API hand
us a typed value. (A real example of this is in
classification.)
How good are models at this?
The usual yardstick is the Berkeley Function Calling Leaderboard. It checks each call by parsing it and comparing the function name and arguments with the expected call, which tracks closely with actually running it. Its 2025 paper mostly tested late-2024 models, so the exact scores are dated, but the pattern is the useful part.
Picking the right tool from a list, in one turn, is close to solved. Knowing what we can't do is not. That GPT-4o version scored 93.5% at choosing among several tools, but 6% at noticing, in a longer conversation, that no available tool could do what the user asked. The best model on the memory tasks reached 12%.
Also from that data: some late-2024 flagship models scored close to zero on calling tools in parallel, while doing well on single calls. Asking for 20 stock prices at once is much faster than 20 round trips, but when each call needs the last one's result, one at a time may be both faster and more accurate.
What comes back in
There's one more thing in our weather example we glossed over. The result went straight into the model's context. Today it was "15 degrees, partly cloudy". But if the tool reads a web page or an email, the result is whatever that page or email says, including any instructions someone planted in it (prompt injection). And a model holding tools that can send data out can be talked into leaking it (data exfiltration). Every tool result is untrusted input. That matters more the more tools we hand over, which is where tool design and shared tool servers like MCP come in.
Further reading
- How tool use works, Anthropic, undated. The clearest statement of the deal between our code and the model, and when tools are the wrong choice. Start here.
- Tool use with Claude,
Anthropic, undated. The full
get_weatherround trip in code, and the table of hidden tool-prompt tokens per model. - Define tools,
Anthropic, undated. Writing tool definitions,
tool_choice, and which models refuse forced calls. - Function calling, OpenAI, undated. The same loop from another vendor, with the strict mode rules and the "fewer than 20 tools" rule of thumb.
- Tool use (chat templates), Hugging Face, undated. What the APIs hide: an open model writing its tool calls as plain text.
- The Berkeley Function Calling Leaderboard (BFCL), Patil et al., 2025. How tool calling gets measured, and where models fail. Table 1 has every category.