Returning Tool Results
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 3 of 22.
After you run a tool, the model still has to see the result to answer. Add two turns to the conversation and call again: the assistant turn with the model's whole content (its tool call included), then a user turn holding a tool_result block. The block's tool_use_id is the call's id, and its content is what your function returned.
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": call.id, "content": "Lima: 14:05"}],
})
final = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=tools, messages=messages)Send the same tools again: the model may want another call. The API checks the pairing: a tool_use without a tool_result in the next message, or a result whose id matches no call, is refused with a BadRequestError. The stand-in then answers from the result with Here is what I found: ...; a real model writes its own sentence around it.
Challenge
MediumFinish the round trip: run the tool, send its result back and print the model's answer. The starter sends the question and keeps the tool call in call.
- Call
get_opening_hourswith thedayfrom the call's input and printResult:and what it returns. - Append the assistant turn holding
response.content, then a user turn with onetool_resultblock forcall.idwhose content is the result. - Call the model again with the same model,
max_tokens, tools and messages. - Print
Answer:and the text of the text block.
For example, for the input When are you open on Saturday? the program prints:
Result: Saturday: 10 am to 4 pm
Answer: Here is what I found: Saturday: 10 am to 4 pmTry it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
HOURS = {"monday": "9 am to 6 pm", "friday": "9 am to 8 pm", "saturday": "10 am to 4 pm", "sunday": "closed"}
def get_opening_hours(day):
return f"{day}: {HOURS.get(day.lower(), '9 am to 6 pm')}"
hours_tool = {
"name": "get_opening_hours",
"description": "Get the shop's opening hours for a day of the week",
"input_schema": {
"type": "object",
"properties": {"day": {"type": "string", "description": "A day of the week"}},
"required": ["day"],
},
}
question = input()
messages = [{"role": "user", "content": question}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=[hours_tool], messages=messages)
call = next(block for block in response.content if block.type == "tool_use")
# 1. Run the tool and print "Result: " and what it returns.
# 2. Append the assistant's turn, then a user turn with the tool_result.
# 3. Ask again with the same tools and print "Answer: " and the text of the reply.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Tools and Agents
1Tool Use Basics
Defining a ToolReading a Tool CallReturning Tool ResultsChoosing Between ToolsRecap: Weather DeskPractice on your own: Online Python compiler