Understanding Tool Use
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
A language model on its own can only produce text. It can't look up your customer's order, check a delivery, or write to your database. Tool use is the bridge. You tell Claude what functions exist, Claude decides when one would help, and your code does the actual work. Everything in this module builds on that one idea, so it's worth getting it clear before writing any real code.
What you'll learn
- What tool use (also called function calling) is, in plain terms.
- Who runs what: the difference between client tools and server tools.
- The full round trip of one tool call, with working code.
- When tools are the right answer, and when they just add delay.
Before you start
The idea: Claude asks, your code answers
Anthropic describes tool use as a contract between your application and the model. You say which operations are available and what shape their inputs have. Claude decides when to use one. And the key sentence from their docs is easy to skim past: the model never runs anything on its own. It emits a structured request, something like "call lookup_order with order_id = A100", and then it stops and waits. Your code runs the function, and sends the result back into the conversation.
If you've built a typed API before, this will feel familiar. You define the interface, handle the callback, return a result. The only unusual part is who's on the other end: a model choosing which function to call based on what the user said.
Three kinds of tools
The thing that matters most about any tool is where its code runs, because that decides what you're responsible for.
- Your own tools. You write the schema and the code. The model returns a call, your app runs it. This is where most real-world tool use happens: your database, your internal APIs, your business rules. This module is mostly about these.
- Anthropic-schema tools. For a few common jobs (
bash, text_editor, memory, computer use and browser use), Anthropic publishes the schema and your app still runs the call. Claude has been trained on those exact schemas, so it uses them more reliably than a lookalike you'd write yourself.
- Server tools.
web_search, web_fetch, code_execution and tool_search run on Anthropic's side. You switch them on and read the answer. You never build a tool_result for them.
For a Laravel or Python developer wiring Claude to a shop's data, the first category is the one that matters, and the rest of this lesson uses it.
One round trip, start to finish
Your app Claude API
| messages + tools -----> |
| | decides it needs lookup_order
| <---- stop_reason: tool_use
| tool_use { id, name, input }
|
| (your code runs lookup_order)
|
| messages + assistant turn + tool_result { tool_use_id, content } ----->
| |
| <---- stop_reason: end_turn, a normal text answer
Here's that as a small program. It has one tool, lookup_order, backed by a dictionary standing in for your database. Read the comments, because each step has a rule attached to it.
import json
import anthropic
client = anthropic.Anthropic()
MODEL = "claude-sonnet-5-5"
# Pretend this is your shop's database.
ORDERS = {
"A100": {"status": "shipped", "carrier": "DHL", "tracking": "JD0146", "total": "54.00 USD"},
"A101": {"status": "processing", "carrier": None, "tracking": None, "total": "19.50 USD"},
}
def lookup_order(order_id: str) -> dict:
"""Your code. Claude never sees this function, only the description below."""
return ORDERS.get(order_id, {"error": f"No order with id {order_id}"})
TOOLS = [
{
"name": "lookup_order",
"description": (
"Look up one customer order by its order id (for example A100). Returns the order status, "
"carrier, tracking number and total. Use it whenever the customer asks about the state of a "
"specific order. It does not return payment details or the customer's address."
),
"input_schema": {
"type": "object",
"properties": {"order_id": {"type": "string", "description": "The order id, such as A100."}},
"required": ["order_id"],
},
}
]
def ask(question: str) -> str:
messages = [{"role": "user", "content": question}]
# Round trip, step 1: Claude decides whether it needs the tool.
first = client.messages.create(model=MODEL, max_tokens=1024, tools=TOOLS, messages=messages)
print("stop_reason:", first.stop_reason)
if first.stop_reason != "tool_use":
return "".join(b.text for b in first.content if b.type == "text")
# Step 2: Claude only *asked*. Your code runs the function.
call = next(b for b in first.content if b.type == "tool_use")
print("Claude wants:", call.name, json.dumps(call.input))
result = lookup_order(**call.input)
# Step 3: send back the whole assistant turn, then the result tied to the call's id.
messages.append({"role": "assistant", "content": first.content})
messages.append({
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": call.id, "content": json.dumps(result)}],
})
second = client.messages.create(model=MODEL, max_tokens=1024, tools=TOOLS, messages=messages)
return "".join(b.text for b in second.content if b.type == "text")
if __name__ == "__main__":
print(ask("Where is my order A100?"))
Three details are worth pausing on.
The description is the whole interface. Claude never sees your Python function. It sees the name, the description and the schema, and that's what it uses to decide whether and how to call it. Anthropic says descriptions are "by far the most important factor in tool performance", and lesson 4.3 is about writing them well.
You send back the entire assistant turn. Notice {"role": "assistant", "content": first.content}. Claude's reply may contain an explanatory sentence as well as the tool_use block, and the follow-up request has to include all of it, unchanged.
The result is tied to the call by an id. Each tool_use block has an id, and your tool_result carries it as tool_use_id. That pairing is how the API knows which answer belongs to which question, which matters a lot once Claude makes several calls at once (lesson 4.7).
We ran this script against a mock API that plays Claude's part. The first reply asked for lookup_order, the script printed stop_reason: tool_use, ran the function, and the request it sent back had the assistant turn followed by a tool_result with the same id. A question that needs no tool ("say hello") skipped the whole dance and came back as plain text, which is exactly what tool_choice: auto is for.
Claude decides, and you can nudge it
By default (tool_choice of auto) Claude chooses on every turn whether to call a tool or answer directly. It calls one when the request matches what a tool says it can do and the answer isn't already in the conversation; it answers directly for general knowledge, creative work and small talk. You can steer this through the system prompt. Anthropic's examples run from "Use the tools to investigate before responding" (gently more tool use) to "Use your judgment about whether to call a tool or respond directly" (more conservative).
One behaviour to design around: if the user leaves out a required parameter, a model may ask for it, or it may guess. Anthropic notes that Opus models are more likely to notice and ask, while Sonnet models might infer a plausible value, and that this isn't guaranteed either way. If a wrong guess would matter, validate on your side (lesson 4.4), and consider telling Claude in the tool description when not to guess.
When a tool is the right answer
The docs give a useful split. Reach for tools when the task needs something text alone can't provide:
- Side effects: sending an email, updating a record. The model can describe it, only a tool can do it.
- Fresh or private data: today's prices, the contents of your database.
- A guaranteed shape: a JSON object with specific fields rather than prose that contains them.
- Calling existing systems: the bridge from "where's my parcel?" to your carrier's API.
And skip them when the model can answer from training alone (summaries, translation, general questions), when there's nothing to run, or when the extra round trip would cost more than the answer is worth. There's also a good smell test from Anthropic: if you're writing a regular expression to pull a decision out of the model's prose, that decision should have been a tool call.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
An online kitchen shop adds a chat box. Without tools, the assistant can chat about cast iron but has to say "I can't see your order." With one tool, lookup_order, the same assistant answers "your order shipped on Friday with tracking JD0146". Nothing about the model changed. The shop added one function and one schema. Everything that matters about safety, such as which orders a customer may see, lives in that function, not in the model. We'll build up to that across the rest of the module.
Mistakes to avoid
- Thinking Claude runs your code. It doesn't. If nothing sends a
tool_result, nothing happens.
- Returning only the
tool_use block instead of the whole assistant turn.
- Using a tool for everything. Each call is an extra round trip.
- A vague description and then blaming the model for choosing badly.
- Trusting the arguments. They came from a model that read user text. Validate them.
Security notes
A tool call is a request that originated from text a user typed. Treat it like any other untrusted input to your system: validate the arguments, check permissions in your own code, and never let the model decide who the user is. Lesson 4.8 covers this properly. For now, remember that adding a tool also adds a way for the model's mistakes, or an attacker's cleverness, to reach your systems.
Cost and speed
Tools cost tokens even when they aren't called. Anthropic's pricing notes say the tools parameter, tool_use blocks and tool_result blocks all count as input, and enabling tools adds a small built-in system prompt (286 tokens for the current Opus 5.5, Sonnet 5.5 and Haiku 5.5 with tool_choice set to auto). Every call is also at least one extra round trip, so keep tool lists short and descriptions tight, and only attach the tools a given request could need.
Key takeaways
- Claude asks for a function call; your code runs it and sends back the result. The model never executes anything itself.
- Tools differ by where they run: your tools, Anthropic-schema tools you run, and server tools Anthropic runs.
- A round trip is: request with tools,
tool_use, you run it, tool_result tied by id, final answer.
- Use tools for side effects, fresh or private data and existing systems, not for things the model already knows.
Lesson recap: Claude asks, your code answers, and where a tool runs decides what you own.
Quick quiz
- Who actually runs a user-defined tool, Claude or your application?
- Which three kinds of tool does Anthropic's overview describe?
- What links a
tool_result to the tool_use it answers?
- Why must you send back the whole assistant turn, not just the
tool_use block?
- Give two situations where a tool is a poor choice.
Answers and explanations
- Your application. The model only emits a structured request.
- Your own client tools, Anthropic-schema client tools (such as bash and text editor) and server tools (such as web search).
- The
tool_use_id, which must equal the id of the tool_use block.
- The follow-up request needs Claude's turn exactly as it was returned; the text and the tool call are part of one assistant message.
- When the model can answer from its training (summaries, translation), when there's nothing to run, or when the round trip would cost more than the benefit.
Official references and further reading
Related tutorials