Day 3, 25 minutes · beginner
Why it matters
So far Claude has asked for a tool, and nothing happened. A request on its own is not an answer: your code has to run the function and send the result back, in exactly the shape the API expects. Get the shape wrong and the API refuses the request with an error.
Today you close the loop for the first time. A customer asks whether Copperleaf Plants has a monstera, Claude asks for check_stock, your code runs it, and Claude answers with the real number from Margriet's shop. At the end you have a script that turns one customer question into one answer based on real shop data, and you know the rules for sending a tool result back.
What Claude sends you
When Claude wants a tool, the response has stop_reason set to tool_use. In content you find a tool_use block, often after a short text block in which Claude says what it is about to do. Shortened:
{
"stop_reason": "tool_use",
"content": [
{ "type": "text", "text": "Let me check the stock." },
{
"type": "tool_use",
"id": "toolu_...",
"name": "check_stock",
"input": { "plant": "monstera" }
}
]
}
- id: a unique label for this one call. You need it to send the answer back.
- name: which tool to run.
- input: the inputs, shaped by your
input_schema.
What you send back
The answer goes in a tool_result block:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_...",
"content": "4 x monstera in stock today"
}
]
}
- tool_use_id: the
idof thetool_useblock you are answering. This is how Claude knows which result belongs to which call. - content: the result, as text (or as a list of blocks, for example with an image).
- is_error: optional. Set it to
truewhen the tool failed. That is lesson 5.
Note the role: tool results go in a user message. The Claude API has no separate role for tools.
The rules for the second request
The docs list them, and they are strict:
- Keep the earlier messages and add Claude's response unchanged as an
assistantmessage. Do not pick out parts or rewrite it: just send backresponse.content. - Then add one
usermessage with the tool results. It must come straight after the assistant message, with nothing in between. - Tool results come first in that user message. If you want to add text, it goes after the results, never before.
- One
tool_resultfor everytool_useblock, also when a tool failed. - Send the same
toolsas in the first request.
Break rule 2, 3 or 4 and the API answers with a 400 error that says tool_use ids were found without tool_result blocks immediately after. You will cause that error on purpose in lesson 5.
One tool per answer, for now
Claude can ask for several tools in one response. Today's script keeps it simple by asking for at most one tool call per response, with this setting:
"tool_choice": { "type": "auto", "disable_parallel_tool_use": true }
tool_choice controls how Claude uses tools. auto is the default: Claude decides whether to call a tool. disable_parallel_tool_use lives inside tool_choice and limits Claude to one tool per response. Lesson 4 turns it off again.
Where this breaks
The script does one round trip. Ask a question that needs two tools, such as stock and price, and Claude's second response may ask for another tool instead of answering. The script then prints stop_reason: tool_use and no answer. That is not a bug in your code: it is why lesson 7 turns this into a loop.
The steps
- Add
run_tooltoshop.py: it finds the function by the name Claude gave and runs it with Claude's input. - Create
ask_copperleaf.py: first request, run the tool, second request with the tool result, print the answer. - Run it with the default question and with a price question.
- Run it with a question that needs two tools and see where one round trip stops.
Follow the steps below.
Today's actions
Ticks are not saved without an accountDo it in Claude API
Claude API
- Open a terminal in your copperleaf-tools folder and turn on the virtual environment as in lesson 2.
- Add this to the end of
shop.py.run_toollooks up the function by the name Claude gave and calls it with Claude's input.FUNCTIONS = { "check_stock": check_stock, "get_price": get_price, "get_opening_hours": get_opening_hours, } def run_tool(name, tool_input): """Run the function Claude asked for, with the input Claude gave.""" if name not in FUNCTIONS: raise ValueError(f"Unknown tool '{name}'. Known tools: {', '.join(FUNCTIONS)}.") return FUNCTIONS[name](**tool_input) - Create
ask_copperleaf.pywith this code. It sends the question, runs the one tool Claude asks for, sends the result back in a tool_result block and prints the answer.import sys from dotenv import load_dotenv import anthropic from shop import run_tool from tools import TOOLS load_dotenv() client = anthropic.Anthropic() MODEL = "claude-opus-5-5" ONE_TOOL = {"type": "auto", "disable_parallel_tool_use": True} question = " ".join(sys.argv[1:]) or "Do you have a monstera in stock?" messages = [{"role": "user", "content": question}] response = client.messages.create( model=MODEL, max_tokens=1024, tools=TOOLS, tool_choice=ONE_TOOL, messages=messages ) if response.stop_reason == "tool_use": tool_use = next(b for b in response.content if b.type == "tool_use") result = run_tool(tool_use.name, tool_use.input) print(f"Ran {tool_use.name}({tool_use.input}) -> {result}") messages.append({"role": "assistant", "content": response.content}) messages.append( { "role": "user", "content": [ {"type": "tool_result", "tool_use_id": tool_use.id, "content": result} ], } ) response = client.messages.create( model=MODEL, max_tokens=1024, tools=TOOLS, tool_choice=ONE_TOOL, messages=messages ) print("stop_reason:", response.stop_reason) print("".join(b.text for b in response.content if b.type == "text")) - Run
python ask_copperleaf.py. You see the lineRan check_stock(...)with the result from shop.py, thenstop_reason: end_turnand Claude's answer with the number 4. - Run
python ask_copperleaf.py "What does a pothos cost?". Now Claude asks forget_price, and the answer uses the price from shop.py. - Run
python ask_copperleaf.py "Is the pothos in stock and what does it cost?". If the second stop_reason istool_useand there is no answer, Claude wanted a second tool. Write that down: lesson 7 fixes it with a loop.
Note: In this lesson an unknown plant still crashes the script with a ValueError. That is on purpose: lesson 5 sends such failures back to Claude instead.
Paid planPay as you go: buy prepaid credits in the Claude Console (minimum 5 US dollars). There is no free tier. Max, Ultra and Team plans include monthly API credits.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up