AI Agents & Tool Use · Lesson 2
Function calling in Claude
The full tool_use loop in Claude: tool_use and tool_result blocks, parallel calls, and forced tool selection via tool_choice.
The tool_use loop in Claude
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,
}],
})
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.
