Claude and the Anthropic SDK · Chapter 6 of 21
Tool use
By the end of this chapter you can give Claude a tool (a name, a description and an input schema), recognise when it asks for one (stop_reason "tool_use" and a tool_use block), run the tool yourself, send the result back in a tool_result block the way the API requires, and write the loop that keeps going until Claude has answered, with a limit on the steps. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Tools and structure
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.
Why you need this
Until now Kamla has pasted every bill into the message. Families do not send bills, though: they send a claim id. "How much will CLM-2411 pay?" needs Gopal's bill from Parchi's claims database and his date of birth from his family's policy, and Claude has neither. Asked anyway, with the policy as the system prompt, it does what chapter 1 warned about:
The policy should pay the full bill of ₹31,200 for CLM-2411, since viral fever with dehydration is a covered hospitalisation.
Pasting every record into every prompt does not scale either: Claude does not know in advance which claim or which policy a question needs. A tool lets Claude ask your code for exactly what it needs, while it works.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
define_tool.py: your first tool
Define GET_CLAIM, a tool dictionary (anthropic.types.ToolParam) named get_claim with a one-sentence description and an input_schema of type object with one required string property, claim_id. Send How much will CLM-2404 pay? to "claude-opus-5-5" (max_tokens 4096, system=SYSTEM) with tools=[GET_CLAIM].
Print stop_reason: STOP, then tool_use: NAME INPUT (json.dumps of the input), then id: ID, from the reply's tool_use block.
- Task 2
one_round.py: one round trip, by hand
Send QUESTION (system=SYSTEM, tools=[GET_CLAIM], max_tokens 4096) to "claude-opus-5-5", run the tool_use block with run_tool(block.name, block.input), and send a second request with the question, {"role": "assistant", "content": reply.content}, and a user message holding one tool_result (tool_use_id: the block's id, content: the tool's text).
Print asked for: NAME INPUT, answer: TEXT and stop_reason: STOP (the second reply's).
- Task 3
sahay_loop.py: Sahay's tool loop
Define sahay(question, max_steps=6) -> str: keep one messages list; send it ("claude-opus-5-5", max_tokens 4096, system=SYSTEM, tools=TOOLS); when stop_reason is not "tool_use", return the text blocks joined; otherwise append Claude's turn and ONE user message with a tool_result for every tool_use (is_error from run_tool's is_error), and go round again.
At most max_steps requests; when they run out, return Stopped after N steps.
- Task 4 · interview question
Interview: pairing.py
"Our loop gets a 400 after the first tool call. What are the rules?" Send FIRST, run its tool, then send four follow-ups after FIRST's user message: made-up id (a tool_result for "toolu_01MadeUpId"), text only (only Claude's text blocks as its turn), text first (a text block before the tool_result), and right.
Catch each anthropic.BadRequestError and print NAME: 400, or NAME: STOP for the right one.
What you will learn
- A tool definition: name, description, input_schema, and what each is for.
- stop_reason "tool_use" and the tool_use block: what to call, with what input, and its id.
- tool_result: sending the result back, and the pairing rules the API enforces.
- The tool loop: run tools until stop_reason is something else, with a step limit.
- What tools cost: their definitions and a hidden tool prompt are input on every request.
More in Tools and structure
Build "Tool use" yourself
Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.