Anthropic Core Workflow A — Tool Use (Function Calling)
Overview
Implement Claude's tool use capability where the model can call functions you define. Claude returns tool_use content blocks with structured JSON inputs; your code executes the function and returns tool_result blocks. This is the foundation for building AI agents.
Prerequisites
- Completed
anth-install-auth setup
- Understanding of the Messages API request/response cycle
- Functions or APIs you want Claude to call
Instructions
Step 1: Define Tools
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Get current weather for a city. Use when the user asks about weather conditions.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g. 'San Francisco, CA'"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature units"
}
},
"required": ["city"]
}
},
{
"name": "search_database",
"description": "Search product database by query string. Returns matching products.",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string"},
"max_results": {"type": "integer", "default": 10}
},
"required": ["query"]
}
}
]
Step 2: Send Request with Tools
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
Step 3: Execute Tool and Return Result
def execute_tool(name: str, input_data: dict) -> str:
"""Route tool calls to actual implementations."""
if name == "get_weather":
return '{"temp": 22, "condition": "partly cloudy", "humidity": 65}'
elif name == "search_database":
return '{"results": [{"name": "Widget A", "price": 29.99}]}'
raise ValueError(f"Unknown tool: {name}")
tool_results = []
for block in message.content:
if block.type == "tool_use":
result = execute_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
follow_up = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"},
{"role": "assistant", "content": message.content},
{"role": "user", : tool_results}
]
)
(follow_up.content[].text)
Step 4: Agentic Loop (Multiple Tool Calls)
def run_agent(user_message: str, tools: list, max_turns: int = 10) -> str:
"""Run an agentic loop that handles multiple sequential tool calls."""
messages = [{"role": "user", "content": user_message}]
for _ in range(max_turns):
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=4096,
tools=tools,
messages=messages
)
if response.stop_reason == "end_turn":
return next(
(b.text for b in response.content if b.type == "text"), ""
)
messages.append({"role": "assistant", "content": response.content})
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = execute_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
messages.append({: , : tool_results})
Output
- Tool definitions with JSON Schema input validation
- Agent loop handling sequential tool calls
- Proper
tool_use / tool_result message threading
Error Handling
| Error | Cause | Solution |
|---|
invalid_request_error: tool schema invalid | Malformed input_schema | Validate against JSON Schema spec |
tool_use_id mismatch | Result ID doesn't match tool_use ID | Copy block.id exactly |
| Claude ignores tools | Description too vague | Add clear "Use when..." descriptions |
| Infinite loop | Claude keeps calling tools | Add max_turns guard + tool_choice: {"type": "auto"} |
Tool Choice Options
tool_choice={"type": "auto"}
tool_choice={"type": "tool", "name": "get_weather"}
tool_choice={"type": "any"}
Resources
Next Steps
For streaming with tools, see anth-core-workflow-b.