Tool Use Basics
Give Claude superpowers by connecting it to external tools — with complete working code
What Is Tool Use?
Claude can process text and images, and can search the web. With tool use (also called function calling), Claude can do much more — query databases, call APIs, run code, send emails, and interact with any system you connect. You define the tools. Claude decides when and how to use them.
Think of tools as giving Claude hands to go with its brain. Without tools, Claude can only tell you what it would do. With tools, it can actually do it.
The Tool Use Flow
Every tool use interaction follows the same 5-step pattern:
Complete Working Example
Here is a fully functional tool use implementation. This code actually works — you can run it with your API key:
import anthropic, json
client = anthropic.Anthropic()
# Step 1: Define your tools
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location.",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. Tokyo"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature units"
}
},
"required": ["location"]
}
}
]
# Step 2: Your tool implementation (replace with real API)
def execute_tool(name: str, params: dict) -> str:
if name == "get_weather":
# In production: call a real weather API here
return json.dumps({
"temp": 22,
"condition": "Partly Cloudy",
"humidity": 65
})
raise ValueError(f"Unknown tool: {name}")
# Step 3: Send message with tools
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
# Step 4: Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
# Find the tool_use block
tool_block = next(
b for b in response.content
if b.type == "tool_use"
)
print(f"Claude wants to call: {tool_block.name}")
print(f"With params: {tool_block.input}")
# Execute the tool
result = execute_tool(tool_block.name, tool_block.input)
# Step 5: Send result back to Claude
final = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"},
{"role": "assistant", "content": response.content},
{"role": "user", "content": [{
"type": "tool_result",
"tool_use_id": tool_block.id,
"content": result
}]}
]
)
print(final.content[0].text)
# "It's currently 22C and partly cloudy in Tokyo
# with 65% humidity."
This lesson is for Pro members
Unlock all 520+ lessons across 52 courses with Academy Pro.
Already a member? Sign in to access your lessons.