Tool-Based Agents
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.
The tools you give an agent decide what it can do, how well it does it and how badly it can go wrong. Anthropic's guidance says as much: invest as much effort in the interface between the model and its tools as you would in a user interface for people. This lesson turns that into concrete choices: which tools to give, how to describe them, where risky ones wait for a person, and how to keep an agent's mistakes recoverable.
What you'll learn
- How to design a small, clear set of tools for an agent.
- The read/write split and why only one side needs approval.
- How to make tool errors teach the model what to do next.
- Giving different agents different tools.
Before you start
Fewer, clearer tools
Every tool you add is something the model has to choose between. Ten overlapping tools produce worse choices than four distinct ones. A few habits that hold up:
- One purpose per tool.
lookup_order and propose_refund, not a single manage_order with a mode flag.
- Write the description like a briefing. Say what it returns, when to use it and what it doesn't do. In the capstone's lookup tool the description ends with "Does not return payment details or the address", which tells the model not to bother asking.
- Return what the next decision needs, no more. A tool that returns an entire database row wastes tokens and may leak fields the model doesn't need.
- Make mistakes hard. Use required fields, enums and
additionalProperties: false, so a malformed call is refused before it runs.
The read/write split
Reading is mostly safe to repeat. Writing isn't. So our Tool carries a risk: read runs immediately, write waits for a person. The agent doesn't have to know the difference. It calls propose_refund like any other tool, the loop pauses, and a person decides from a queue outside the conversation:
tools = {
"lookup_order": Tool(LOOKUP, lookup_fn), # runs at once
"propose_refund": Tool(REFUND, record_refund_fn, risk="write"), # pauses for approval
}
Name risky tools honestly. propose_refund tells the model, and every reader of the logs, that calling it asks a question rather than moving money. The tool's own description says the same thing.
Errors that help
When a tool fails, the loop sends back an error result instead of raising. What's in that message matters. "Error" teaches nothing. "No order A1000. Ask the customer to check the number" lets the model recover on its own. In our capstone, an unknown order raises LookupError with exactly that sentence, and the agent's next reply asks the customer to double-check. Keep stack traces and internal names out of these messages, since the model may repeat them to a customer.
Different agents, different tools
Tools are chosen per agent, not per application. A research worker gets read-only search. A drafting agent gets a lookup and nothing else. Only the agent that really needs a risky action gets it, and even then it waits for approval. If an agent never receives a tool, no prompt injection can make it call it. Our Agent takes its tools as a plain dictionary, so building a narrow one is one line.
Trust what the code knows, not what the model says
If a tool needs the customer's identity, take it from your authenticated session, not from a parameter the model fills in. A model can be talked into passing a different customer id. The tool should look up who is signed in on the server side. Lesson 4.8 covered this in detail; it matters more once an agent is calling tools unattended.
Testing tool-based agents
We tested the agent with scripted model replies, so tool behaviour is checked without a live model. A turn with a read tool and a write tool in the same message runs the read at once, pauses for the write, and after approval sends both results back in the model's original order. We also tested a rejection: the model receives an error saying a person declined, so it can tell the customer instead of silently retrying.
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 internal finance helper starts with one tool that runs any SQL query, because that was quick to build. It works until someone asks it to "clean up old invoices". The team replaces it with three tools: find_invoices (read, with filters), get_invoice (read) and propose_void (write, needs approval). The agent can still answer most questions, and the one dangerous action now goes through an approval queue with a clear record.
Mistakes to avoid
- One do-everything tool with a "mode" parameter.
- Descriptions that say what a tool is called rather than when to use it.
- Returning raw database rows or stack traces.
- Letting the model supply identity or permission fields.
- Giving every agent every tool.
Security notes
Treat each tool as a permission you grant. Read-only first, narrow filters, size limits on results and approval for anything that changes state or sends something out. Remember that a tool result can carry hostile text, such as the body of an email. Keep it clearly separated from instructions.
Cost and speed
Tool definitions are sent with every request, so a long list costs tokens on every iteration. Large results do too, since they stay in the conversation. Trim results and give each agent only the tools it needs.
Key takeaways
- Give an agent a few distinct tools with careful descriptions.
- Split read from write; writes wait for a person.
- Write error messages that tell the model what to do next.
- Choose tools per agent, and take identity from your own code.
Lesson recap: few clear tools, writes wait for a person, and errors tell the model what to do next.
Quick quiz
- Why prefer several focused tools over one flexible one?
- What should a good tool description include?
- How does the example agent treat a write tool differently?
- Why return a helpful error message instead of "Error"?
- Why shouldn't the model supply the customer id?
Answers and explanations
- The model chooses better between a few clear tools, and each can be validated and permissioned separately.
- What it returns, when to use it and what it doesn't do.
- It pauses the run and waits for a person to approve or reject before the tool actually runs.
- A specific message lets the model recover, for example by asking the customer to check an order number.
- The model can be manipulated into passing someone else's id. Identity should come from your authenticated session on the server.
Official references and further reading
Related tutorials