Tool use and function calling
The model does not run code. It asks your program to run a function and reads the result.
Words on this page (12)
- tool
- : A function the model can ask your program to run, described by a name, description and argument schema.
- JSON
- : A text format for structured data: keys and values in braces, like {"name": "Ada"}.
- schema
- : A description of the shape of data: which fields exist and what types they have.
- tool use
- : The model asks your program to run a function and reads the result.
- agent
- : A model run in a loop that chooses its own tools and steps until the task is done.
- parameter
- : One learned number inside a model. Modern models have billions of them. Training adjusts them.
- trace
- : The full record of one run: inputs, model calls, tool calls, outputs, timing and cost.
- policy
- : A rule for choosing actions. In a language model, the model itself is the policy.
- token
- : A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
- logarithm
- : The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.
- prompt
- : The input text you give a model, including instructions, examples and data.
- prompt injection
- : Untrusted text that contains instructions the model follows, such as a web page telling it to leak data.
What it is
You describe with a name, a description and a for arguments. When the model decides a tool is needed, it returns a instead of text. Your program executes it and sends the result back as a new message. The model then continues, possibly calling more tools. This round trip is the core of every .
What makes it work
The description is the interface. Say what the tool does, when to use it and what the arguments mean. Return results the model can read: concise, with clear errors that suggest what to try next. Validate arguments before running anything. Treat tool results as untrusted input, since they can contain text that tries to steer the model.
A worked example: one round trip
- You send the user question plus a tool description: name track_order, description "Look up the status of an order by id", order_id (string).
- The model replies with a tool call: track_order(order_id="8123").
- Your code runs it: status "shipped, arrives Friday".
- You send that result back.
- The model answers: "Your order shipped and should arrive Friday." The model decided when to call and with what. Your code controlled what actually ran.
Writing a good description
Write for a new colleague: what it does, when to use it, what each argument means, and an example. If two tools sound alike, say how they differ.
How the loop works
Think of a short loop. Your code sends the messages and tool list. If the reply contains a tool call, your code runs it, adds the result and sends everything again. If the reply is plain text, the loop ends.
for the question "Is order 8123 late, and what is the refund ?" The model calls track_order. Your code returns "shipped, arrives Friday". The model calls get_policy. Your code returns the policy text. The model then writes the final answer. That is three model calls, and each one resends the earlier messages and results. More tools and longer results mean more . Always set a maximum number of loop steps.
A second example: when a tool is not the answer
A tool for adding two numbers is fine, since code is exact where the model can slip. A tool that returns the capital of France wastes a round trip, because the model already knows. Add a tool when the answer needs live data, private data or an action. Skip it when the model can answer alone.
Narrow versus broad tools
A broad tool such as run_sql(query) can answer many questions, but it can also delete a table. A narrow tool such as get_order(id) is limited by design. Prefer narrow tools for anything with side effects.
What goes wrong
The model picks the wrong tool because two descriptions sound alike. every call during tests and count the wrong picks.
The arguments are slightly off, such as "#8123" instead of "8123". Validate and return an error that shows the expected format, so the model can correct itself.
The loop never ends because the model keeps calling tools. The step cap stops it.
A huge result floods the context. Return only what the model needs.
Try it yourself
Define two tools with nearly identical descriptions and ask five questions that need one or the other. Count wrong choices. Rewrite the descriptions to say when each applies, and rerun. Expect fewer wrong choices.
Common mistake
Letting the model's arguments flow straight into a database query or shell command. Always validate first.
Where this connects
Tool arguments come from a model that can be tricked by the text it reads. Read Prompt injection and the lethal trifecta before you connect a tool that can send, delete or spend, and see Tool errors and validation for how to check arguments.
The code
A tool is a name, a description and a JSON schema for its arguments. The model returns a tool request. Your code runs it and sends the result back.
tools = [{
"name": "track_order",
"description": "Look up the status of an order by its id. Use when the customer asks where an order is.",
"input_schema": {
"type": "object",
"properties": {"order_id": {"type": "string", "description": "The order number, such as 8123"}},
"required": ["order_id"],
},
}]
messages = [{"role": "user", "content": "Where is order 8123?"}]
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=300, tools=tools, messages=messages)
if reply.stop_reason == "tool_use":
call = next(b for b in reply.content if b.type == "tool_use")
result = "shipped, arrives Friday" # your real lookup goes here, using call.input["order_id"]
messages += [
{"role": "assistant", "content": reply.content},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": call.id, "content": result}]},
]
final = client.messages.create(model="claude-sonnet-5-5", max_tokens=300, tools=tools, messages=messages)
print(final.content[0].text)A model on its own cannot tell you if your package shipped. Give it a function, track_order(order_id), and it can. The model never runs the function. It writes "please call track_order with 8123", your program runs it, and the answer goes back so the model can reply in plain language. That small handshake is how AI starts doing things.
Check yourself
Who executes a tool call?
Not quite. The model only emits the request. Your code runs it and returns the result.
What most affects whether the model uses a tool well?
Not quite. The model only sees the description and schema, so they are the real interface.
The model asks to run delete_file with path "../../secrets". What should your code do?
Not quite. Tool arguments come from a model that can be tricked, so validate before running.
You got 0 of 3 right.
Go deeper
- Docs
Anthropic · Defining tools, handling calls and parallel tool use.
- Article
Anthropic · How to design, test and evaluate tools with the model.
- ArticleHow to build an agentArticle
Thorsten Ball · A working tool-using agent in under 400 lines.
Spotted a mistake or something unclear? Report it on GitHub.