Originally published at claudeguide.io/claude-tool-use-function-calling
Claude Tool Use: Complete Guide to Function Calling
Tool use (also called function calling) lets Claude request execution of external functions during a conversation — search engines, calculators, databases, APIs — then incorporate the results into its response. The pattern: define tools with JSON schema, send them to the API, handle tool_use blocks in the response, execute the function, return the result, and continue the conversation in 2026. This guide covers the complete implementation in Python and TypeScript, with patterns for reliable production use.
How tool use works (the request-execute-return loop)
Every tool use interaction follows the same cycle:
- You define tools — name, description, input schema
-
Claude decides to use a tool — returns a
tool_useblock with the tool name and inputs - You execute the tool — call your actual function with Claude's inputs
-
You return the result — add a
tool_resultto the next message - Claude continues — incorporates the result and either uses more tools or gives a final answer
This cycle repeats until Claude returns stop_reason: "end_turn" with no tool use.
Defining tools
Tools are defined as JSON schema objects. The schema must be precise — Claude uses it to generate valid inputs:
import anthropic
TOOLS = [
{
"name": "get_weather",
"description": "Get the current weather for a location. Returns temperature, conditions, and humidity.",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name or 'City, Country' format (e.g., 'Seoul, Korea')"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature units. Default: celsius"
}
},
"required": ["location"]
}
},
{
"name": "search_web",
"description": "Search the web for current information. Use when you need real-time data not in your training.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
},
"num_results": {
"type": "integer",
"description": "Number of results to return (1-10). Default: 5",
"minimum": 1,
"maximum": 10
}
},
"required": ["query"]
}
}
]
Tool description guidelines:
- Be specific about what the tool returns (not "gets weather", but "returns temperature, conditions, and humidity")
- Tell Claude when to use the tool ("use when you need real-time data")
- Describe the input format precisely ("'City, Country' format")
Complete Python implementation
python
import anthropic
import json
from typing import Any
client = anthropic.Anthropic()
def get_weather(location: str, units: str = "celsius") -
[→ Get the Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-tool-use-function-calling)
*30-day money-back guarantee. Instant download.*
Top comments (0)