Claude returns stop_reason: "tool_use", and a tool_use block appears in content with id, name, input. You run the function and continue the same conversation, appending a role: "user" message with a tool_result block.
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Get current weather for a city. Use when the user asks about weather.",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
]
messages = [{"role": "user", "content": "What's the weather in Paris?"}]
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=messages,
)
if response.stop_reason == "tool_use":
tool_block = next(b for b in response.content if b.type == "tool_use")
result = run_weather(tool_block.input["city"]) # your code
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": tool_block.id,
"content": result,
}],
})
final = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=messages,
)
print(final.content[0].text)
Key point: you return the entire assistant response.content (it may contain text + several tool_use blocks), and each tool_result references a tool_use_id.
Parallel calls
In a single reply Claude can request several tools at once. Then content holds multiple tool_use blocks. You must return a tool_result for every id in one user message, otherwise the API rejects the request.
tool_choice={"type": "tool", "name": "get_weather"} # must use this tool
tool_choice={"type": "any"} # any, but definitely a tool
tool_choice={"type": "auto"} # default: the model decides
tool_choice: "any"/"tool" disables the text answer — useful for extraction tasks where you want strictly structured JSON. Return a function error as a tool_result with "is_error": true — Claude will try to recover.