How to use multiple tools with Claude API
Quick answer
Use the
anthropic Python SDK to call client.messages.create with system instructions that orchestrate multiple tools, or chain calls programmatically by invoking different tools sequentially. Manage tool integration logic in your code while leveraging claude-3-5-sonnet-20241022 for natural language understanding and control.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the Anthropic Python SDK and set your API key as an environment variable.
pip install anthropic>=0.20 Step by step
This example demonstrates how to orchestrate multiple tools by calling the Claude API with system instructions and chaining calls in Python.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Define system prompt to instruct Claude to use multiple tools
system_prompt = (
"You are an assistant that uses two tools: a calculator and a dictionary. "
"When given a math expression, use the calculator. When given a word, use the dictionary. "
"Respond with the tool name and the result."
)
# User input that requires multiple tools
user_messages = [
{"role": "user", "content": "Calculate 12 * 8 and define 'serendipity'."}
]
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=300,
system=system_prompt,
messages=user_messages
)
print("Response from Claude:")
print(response.content[0].text)
# Alternatively, chain calls programmatically:
# 1. Calculator tool call
calc_prompt = "Calculate 12 * 8."
calc_response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=50,
system="You are a calculator.",
messages=[{"role": "user", "content": calc_prompt}]
)
calc_result = calc_response.content[0].text
# 2. Dictionary tool call
dict_prompt = "Define the word 'serendipity'."
dict_response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=100,
system="You are a dictionary.",
messages=[{"role": "user", "content": dict_prompt}]
)
dict_result = dict_response.content[0].text
print("\nChained tool results:")
print(f"Calculator result: {calc_result}")
print(f"Dictionary result: {dict_result}") output
Response from Claude: Calculator: 96 Dictionary: Serendipity means the occurrence of events by chance in a happy or beneficial way. Chained tool results: Calculator result: 96 Dictionary result: Serendipity means the occurrence of events by chance in a happy or beneficial way.
Common variations
You can use async calls with the Anthropic SDK or switch models like claude-3-opus-20240229 for different performance or cost profiles. Streaming responses are not currently supported in the Anthropic Python SDK.
import asyncio
import anthropic
async def async_example():
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.messages.acreate(
model="claude-3-5-sonnet-20241022",
max_tokens=100,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Hello from async!"}]
)
print(response.content[0].text)
asyncio.run(async_example()) output
Hello from async!
Troubleshooting
- If you get authentication errors, verify your
ANTHROPIC_API_KEYenvironment variable is set correctly. - If responses are incomplete, increase
max_tokens. - For unexpected behavior, ensure your
systemprompt clearly defines tool usage instructions.
Key Takeaways
- Use the
systemparameter to instruct Claude on how to use multiple tools in one call. - Chain multiple
client.messages.createcalls programmatically to integrate distinct tools. - Use the latest Claude model
claude-3-5-sonnet-20241022for best coding and reasoning performance. - Always set your API key via environment variables to avoid security risks.
- Async calls are supported in the Anthropic SDK for scalable tool orchestration.