Tool use (function calling) is the capability that transforms Claude from a text generator into an agent that can interact with the real world. With tools, Claude can query APIs, read databases, run calculations, or call any function you define.
How the tool use cycle works
The flow is a 3-step cycle:
- You send Claude a list of available tools with their descriptions and schemas
- Claude decides whether it needs a tool and returns a
tool_useblock with the parameters - You execute the tool, return the result, and Claude generates the final response
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Gets the current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
}
]
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather like in London?"}]
)Processing the tool use response
Claude can return tool_use in the stop_reason. You need to handle this case:
if response.stop_reason == "tool_use":
tool_block = next(b for b in response.content if b.type == "tool_use")
tool_name = tool_block.name
tool_input = tool_block.input
# Execute your real function here
result = call_your_function(tool_name, tool_input)
# Return the result to Claude
final_response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "What's the weather like in London?"},
{"role": "assistant", "content": response.content},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": tool_block.id, "content": str(result)}
]}
]
)Multiple tools and tool choice
You can define several tools and Claude will choose the appropriate one. With tool_choice you can force the use of a specific tool or disable automatic selection.
# Force a specific tool
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
tool_choice={"type": "tool", "name": "get_weather"},
messages=[...]
)When to use tool use vs plain prompt
Tool use adds latency and complexity. Use it when you need real-time external data, executing actions with side effects, or when the result depends on information not in context. For pure reasoning or text generation, a direct prompt is faster and cheaper.