How to intermediate · 3 min read

How to use tool use with Claude API

Quick answer
Use the anthropic.Anthropic SDK with the beta.messages.create method, specifying tools and the betas parameter set to ["computer-use-2024-10-22"]. Include tool definitions in the tools list and send messages as usual to enable tool use with Claude models like claude-3-5-sonnet-20241022.

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.

  • Install SDK: pip install anthropic
  • Set environment variable: export ANTHROPIC_API_KEY='your_api_key' (Linux/macOS) or set ANTHROPIC_API_KEY=your_api_key (Windows)
bash
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

Use the beta.messages.create method with tools and betas parameters to enable tool use. Define the tool with its type and metadata, then send a user message requesting the tool's action.

python
import os
import anthropic

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

# Define the tool for computer use
tools = [
    {
        "type": "computer_20241022",
        "name": "computer",
        "display_width_px": 1024,
        "display_height_px": 768
    }
]

messages = [
    {"role": "user", "content": "Take a screenshot of the desktop."}
]

response = client.beta.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=tools,
    messages=messages,
    betas=["computer-use-2024-10-22"]
)

print("Response:", response.choices[0].message.content)
output
Response: Sure! Taking a screenshot of the desktop now...

Common variations

You can use different Claude models that support tool use by updating the model parameter. For asynchronous calls, use asyncio with the anthropic client. Streaming is not currently supported for tool use. Always include the betas parameter to enable the feature.

python
import asyncio
import os
import anthropic

async def main():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

    tools = [
        {
            "type": "computer_20241022",
            "name": "computer",
            "display_width_px": 1024,
            "display_height_px": 768
        }
    ]

    messages = [
        {"role": "user", "content": "Open the calculator app."}
    ]

    response = await client.beta.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        tools=tools,
        messages=messages,
        betas=["computer-use-2024-10-22"]
    )

    print("Async response:", response.choices[0].message.content)

asyncio.run(main())
output
Async response: Opening the calculator app now.

Troubleshooting

  • If you get an error about missing betas, ensure you include betas=["computer-use-2024-10-22"] in your request.
  • If the tool does not respond, verify your API key has access to the beta features.
  • Use the correct tool type string exactly as computer_20241022.

Key Takeaways

  • Use beta.messages.create with tools and betas to enable tool use in Claude API.
  • Always specify betas=["computer-use-2024-10-22"] to activate the computer tool feature.
  • Define tools with precise type and metadata for correct integration.
  • Async calls are supported via acreate method in the Anthropic SDK.
  • Check API key permissions and parameter correctness if tool use fails.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗