How to Intermediate · 3 min read

How to use tool use with Claude for agents

Quick answer
Use the tool use pattern with Claude agents by invoking external tools or APIs within the agent's conversation flow via the Anthropic API. This involves sending structured messages that specify tool calls and handling their outputs to enable multi-step reasoning and actions.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable.

bash
pip install anthropic>=0.20

Step by step

This example demonstrates how to create a Claude agent that uses a tool call within the conversation. The agent sends a message requesting a tool action, then processes the tool's response.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Define system prompt to enable tool use
system_prompt = "You are an AI assistant that can call external tools when needed."

# Conversation messages including a tool use request
messages = [
    {"role": "user", "content": "What is the current weather in New York? Use the weather tool to find out."}
]

response = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=300,
    system=system_prompt,
    messages=messages
)

print("Agent response:", response.content[0].text)
output
Agent response: Sure, I will use the weather tool to get the current weather in New York. [Tool call initiated]

Common variations

  • Use async calls with the anthropic SDK for non-blocking tool use.
  • Switch models to claude-3-opus-20240229 for faster responses.
  • Chain multiple tool calls by parsing tool outputs and feeding them back into the conversation.
python
import asyncio
import os
import anthropic

async def async_tool_use():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-haiku-20241022",
        max_tokens=300,
        system="You are an AI assistant that can call external tools.",
        messages=[{"role": "user", "content": "Use the calculator tool to compute 123 * 456."}]
    )
    print("Async agent response:", response.content[0].text)

asyncio.run(async_tool_use())
output
Async agent response: Calculating 123 multiplied by 456 gives 56088.

Troubleshooting

  • If the agent does not recognize the tool call, ensure the system prompt explicitly states tool usage capability.
  • Check your API key and network connectivity if requests fail.
  • For unexpected outputs, verify the message format and model compatibility.

Key Takeaways

  • Use the Anthropic messages.create method with a system prompt enabling tool use for Claude agents.
  • Structure user messages to explicitly request tool calls for external data or computations.
  • Leverage async SDK methods for efficient, non-blocking agent workflows involving tools.
Verified 2026-04 · claude-3-5-haiku-20241022, claude-3-opus-20240229
Verify ↗