Skip to main content

How to Send a Tool Result Back to Claude in Python

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

When Claude asks for a tool, it stops and waits: the answer only comes after your code runs the tool and sends the result back. This guide completes that round trip with a café's opening hours, and loops until Claude has everything it needs, with real output from anthropic 2.14.0.

Syntax​

Syntax
messages.append({"role": "assistant", "content": response.content})
messages.append(
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": <the tool_use block's id>, "content": <result>}
],
}
)
response = client.messages.create(model=<model ID>, max_tokens=<limit>, tools=<tools>, messages=messages)
PartWhat it means
{"role": "assistant", "content": response.content}Claude's reply, with its tool_use block, added to the conversation unchanged
"type": "tool_result"A block that carries a tool's result
tool_use_idThe id of the tool_use block this result answers
contentThe result, as text

Anthropic's documentation gives the steps: "add the response's content unchanged as an assistant message", then "Add a user message with one tool_result block for each tool_use block in that content, even if the tool failed." Send the request with the same tools, and "If the next response's stop_reason is also tool_use, repeat these steps."

What It Costs​

The Claude API is paid: Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens. A tool round trip takes two requests, and the second resends the whole conversation. The run below made two calls, 419 + 487 input and 54 + 27 output tokens, about $0.005.

Prerequisites​

  • A Claude API key, in a .env file as ANTHROPIC_API_KEY=....
  • The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with uv add anthropic, on Python 3.15.2.

Step 1: Write the Tool​

Create tool_result.py. The tool is an ordinary Python function: it looks a day up in the café's opening hours.

tool_result.py
import anthropic

client = anthropic.Anthropic()

OPENING_HOURS = {
"Monday": "closed",
"Tuesday": "7:30 to 18:00",
"Wednesday": "7:30 to 18:00",
"Thursday": "7:30 to 18:00",
"Friday": "7:30 to 18:00",
"Saturday": "8:00 to 16:00",
"Sunday": "9:00 to 14:00",
}


def get_opening_hours(day: str) -> str:
return OPENING_HOURS.get(day.capitalize(), "no opening hours for that day")

day.capitalize() turns sunday into Sunday, so the lookup works however Claude writes the day.

Step 2: Describe It to Claude​

Add the tool's definition and the question below the function:

tool_result.py
tools = [
{
"name": "get_opening_hours",
"description": (
"Get the café's opening hours for one day of the week. "
"Use it whenever someone asks when the café is open."
),
"input_schema": {
"type": "object",
"properties": {
"day": {"type": "string", "description": "A day of the week, such as Monday."},
},
"required": ["day"],
},
}
]
messages = [{"role": "user", "content": "Is the café open on Sunday?"}]

messages holds the whole conversation, because each request sends all of it. How to Give Claude a Tool with the Claude API in Python explains each field of the definition.

Step 3: Run the Tools Until Claude Answers​

Add the first request and the loop:

tool_result.py
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
hours = get_opening_hours(**block.input)
print(f"Tool: {block.name}({block.input}) returned {hours!r}")
results.append({"type": "tool_result", "tool_use_id": block.id, "content": hours})
messages.append({"role": "user", "content": results})
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)

print("Claude:", response.content[-1].text)
  • while response.stop_reason == "tool_use" keeps going as long as Claude asks for tools.
  • messages.append({"role": "assistant", ...}) adds Claude's reply, tool_use block included, unchanged.
  • get_opening_hours(**block.input) runs the tool with Claude's arguments: ** turns {'day': 'Sunday'} into day='Sunday'.
  • Each result is a tool_result block whose tool_use_id is the call's block.id, so Claude knows which call it answers.
  • All the results go back in one user message, and the next request sends the same tools.
  • When Claude stops asking, its last block is the answer's text.

The complete script:

tool_result.py
import anthropic

client = anthropic.Anthropic()

OPENING_HOURS = {
"Monday": "closed",
"Tuesday": "7:30 to 18:00",
"Wednesday": "7:30 to 18:00",
"Thursday": "7:30 to 18:00",
"Friday": "7:30 to 18:00",
"Saturday": "8:00 to 16:00",
"Sunday": "9:00 to 14:00",
}


def get_opening_hours(day: str) -> str:
return OPENING_HOURS.get(day.capitalize(), "no opening hours for that day")


tools = [
{
"name": "get_opening_hours",
"description": (
"Get the café's opening hours for one day of the week. "
"Use it whenever someone asks when the café is open."
),
"input_schema": {
"type": "object",
"properties": {
"day": {"type": "string", "description": "A day of the week, such as Monday."},
},
"required": ["day"],
},
}
]
messages = [{"role": "user", "content": "Is the café open on Sunday?"}]

response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
hours = get_opening_hours(**block.input)
print(f"Tool: {block.name}({block.input}) returned {hours!r}")
results.append({"type": "tool_result", "tool_use_id": block.id, "content": hours})
messages.append({"role": "user", "content": results})
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)

print("Claude:", response.content[-1].text)

The complete tool_result.py script in a macOS-style code window: the opening hours dictionary, the get_opening_hours function, the tool definition, and a while loop that appends Claude's reply, runs each tool call, sends the tool_result blocks back and asks again until Claude stops asking for tools.

Step 4: Run It​

Terminal
uv run --env-file .env tool_result.py
Output
Tool: get_opening_hours({'day': 'Sunday'}) returned '9:00 to 14:00'
Claude: Yes, the café is open on Sunday from 9:00 to 14:00.

The output in a terminal: the tool get_opening_hours with day Sunday returned 9:00 to 14:00, and Claude answers Yes, the café is open on Sunday from 9:00 to 14:00.

Claude asked for Sunday's hours once, got 9:00 to 14:00 back, and answered from it. The loop ran one round, then Claude's second reply was the answer.

Common Mistakes​

MistakeWhat happensDo this instead
Sending the tool_result without first appending Claude's reply400: unexpected `tool_use_id` found in `tool_result` blocks, then the ID and Each `tool_result` block must have a corresponding `tool_use` block in the previous message.Append response.content as the assistant turn first
A tool_use_id that isn't the call's idThe same 400Copy block.id from the tool_use block
Text before the results in the user message400: `tool_use` ids were found without `tool_result` blocks immediately after, then the IDsPut every tool_result block first; any text goes after them

Troubleshooting​

SymptomLikely causeFix
unexpected `tool_use_id` found in `tool_result` blocksThe result doesn't answer a call in the message just before itCheck that the assistant turn was appended and the IDs match
`tool_use` ids were found without `tool_result` blocks immediately afterA call has no result, or text came before the resultsSend one tool_result per tool_use block, results first

FAQ​

Why append response.content and not just the text?​

The tool_result must answer a tool_use block in the message before it, so Claude's reply has to go back with that block in it. Anthropic's steps say to add it "unchanged".

What can a tool result contain?​

Text, as here, or a list of content blocks. The documentation lists text, image, document and search_result blocks.

Do I send the tools again in the second request?​

Yes. Anthropic's steps say to send it "with the same tools as the first request."

Conclusion​

Running the tool and sending its result back as a tool_result block, with the call's tool_use_id, let Claude finish its answer: the café is open on Sunday from 9:00 to 14:00. The while loop keeps that going for as many rounds as Claude needs.

Additional Resources​