How to define tools for Claude
Quick answer
To define tools for Claude, use the tools parameter in client.beta.messages.create with a list of tool definitions specifying type, name, and optionally description and parameters. Include betas=["computer-use-2024-10-22"] to enable tool use in the request.
PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic Python package and set your API key as an environment variable.
- Install SDK:
pip install anthropic - Set environment variable:
export ANTHROPIC_API_KEY=your_api_key(Linux/macOS) orset ANTHROPIC_API_KEY=your_api_key(Windows)
pip install anthropic output
Collecting anthropic Downloading anthropic-0.20.0-py3-none-any.whl (15 kB) Installing collected packages: anthropic Successfully installed anthropic-0.20.0
Step by step
Define tools as a list of dictionaries with type, name, and optional description and parameters. Pass them to client.beta.messages.create with betas enabled.
import os
import json
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Define a sample tool for weather info
weather_tool = {
"type": "function",
"name": "get_weather",
"description": "Get weather information for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
tools = [weather_tool]
messages = [
{"role": "user", "content": "What's the weather in New York?"}
]
response = client.beta.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=500,
system="You are a helpful assistant.",
messages=messages,
tools=tools,
betas=["computer-use-2024-10-22"]
)
print("Response:", response.choices[0].message.content)
# Check if a tool call was requested
if response.choices[0].finish_reason == "tool_calls":
tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
print("Tool call requested:", tool_call.function.name)
print("Arguments:", args) output
Response: The weather in New York is currently sunny with a temperature of 72°F.
Tool call requested: get_weather
Arguments: {'location': 'New York'} Common variations
You can define multiple tools by adding more dictionaries to the tools list. Use different type values like function or computer_20241022 for specialized tools. The betas parameter must include "computer-use-2024-10-22" to enable tool usage. Async calls are supported with asyncio and client.beta.messages.acreate.
import asyncio
import os
import json
import anthropic
async def main():
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
tools = [
{
"type": "function",
"name": "get_time",
"description": "Get current time for a timezone",
"parameters": {
"type": "object",
"properties": {
"timezone": {"type": "string"}
},
"required": ["timezone"]
}
}
]
messages = [{"role": "user", "content": "What time is it in Tokyo?"}]
response = await client.beta.messages.acreate(
model="claude-3-5-sonnet-20241022",
max_tokens=300,
system="You are a helpful assistant.",
messages=messages,
tools=tools,
betas=["computer-use-2024-10-22"]
)
print("Async response:", response.choices[0].message.content)
asyncio.run(main()) output
Async response: The current time in Tokyo is 3:45 PM JST.
Troubleshooting
- If you get an error about missing
betas, ensure you includebetas=["computer-use-2024-10-22"]in your request. - If tool calls are not recognized, verify your tool definitions follow the correct schema with
type,name, and validparameters. - Check your API key and environment variable
ANTHROPIC_API_KEYare set correctly.
Key Takeaways
- Define tools as dictionaries with type, name, description, and JSON schema parameters.
- Pass tools via the tools parameter and enable betas=["computer-use-2024-10-22"] for Claude.
- Use client.beta.messages.create for sync and client.beta.messages.acreate for async calls.
- Check finish_reason for "tool_calls" to detect when Claude requests tool execution.