GenAIHub
← Back to Technical Section

Claude Tools & Computer Use

Function Calling, Tool Use & Computer Control with Anthropic's API

What are Claude Tools?

Claude's Tool Use (also called Function Calling) allows Claude to interact with external systems, APIs, and databases. You define tools with their names, descriptions, and input schemas—Claude decides when to use them and generates the required parameters. Plus, the new Computer Use feature lets Claude control a computer like a human!

"Claude 3.5 Sonnet is the first frontier AI model to offer computer use in public beta, and we're beginning to explore how it may be applied."

Tool Use

Function calling

Computer Use

Control screens

JSON Schema

Structured inputs

Parallel Calls

Multiple tools

How Tool Use Works

1

Define Tools

Name, description, schema

2

Send Message

User query + tools

3

Claude Decides

Returns tool_use block

4

Execute & Return

Run tool, send result

Quick Start: Tool Use

Define a weather tool and let Claude use it:

# pip install anthropic
import anthropic

client = anthropic.Anthropic()

# Define your tools
tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City name, e.g., San Francisco"
                }
            },
            "required": ["location"]
        }
    }
]

# Send message with tools
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=tools,
    messages=[{
        "role": "user",
        "content": "What's the weather in Tokyo?"
    }]
)

# Check if Claude wants to use a tool
for block in response.content:
    if block.type == "tool_use":
        print(f"Tool: {block.name}")
        print(f"Input: {block.input}")

Expected Output

Tool: get_weather
Input: {"location": "Tokyo"}

Complete Tool Loop

Execute the tool and return the result to Claude:

import anthropic

client = anthropic.Anthropic()

# Your actual tool implementation
def get_weather(location: str) -> dict:
    # Call your weather API here
    return {
        "location": location,
        "temperature": "22°C",
        "condition": "Partly cloudy"
    }

tools = [{
    "name": "get_weather",
    "description": "Get current weather for a location",
    "input_schema": {
        "type": "object",
        "properties": {
            "location": {"type": "string"}
        },
        "required": ["location"]
    }
}]

messages = [{"role": "user", "content": "What's the weather in Paris?"}]

# Agentic loop
while True:
    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        tools=tools,
        messages=messages
    )
    
    # Check stop reason
    if response.stop_reason == "end_turn":
        # Claude is done, print final response
        for block in response.content:
            if hasattr(block, "text"):
                print(block.text)
        break
    
    # Process tool calls
    if response.stop_reason == "tool_use":
        messages.append({"role": "assistant", "content": response.content})
        
        tool_results = []
        for block in response.content:
            if block.type == "tool_use":
                # Execute the tool
                result = get_weather(**block.input)
                tool_results.append({
                    "type": "tool_result",
                    "tool_use_id": block.id,
                    "content": str(result)
                })
        
        messages.append({"role": "user", "content": tool_results})

Expected Output

"The weather in Paris is currently 22°C and partly cloudy. 
It's a pleasant day—perfect for a stroll along the Seine!"

BETA Computer Use

Claude 3.5 Sonnet can control a computer like a human—viewing the screen, moving the cursor, clicking, and typing. This is a breakthrough capability released in October 2024.

What Claude Can Do

  • View screenshots of the desktop
  • Move cursor to coordinates
  • Click buttons and links
  • Type text and keyboard shortcuts
  • Fill forms, navigate websites

Important Notes

  • Still experimental—can make mistakes
  • Run in isolated containers (Docker)
  • Requires special API header
  • 14.9% success on OSWorld benchmark
import anthropic

client = anthropic.Anthropic()

# Computer use requires these special tools
tools = [
    {
        "type": "computer_20241022",
        "name": "computer",
        "display_width_px": 1024,
        "display_height_px": 768,
        "display_number": 1
    },
    {
        "type": "text_editor_20241022",
        "name": "str_replace_editor"
    },
    {
        "type": "bash_20241022",
        "name": "bash"
    }
]

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=4096,
    tools=tools,
    betas=["computer-use-2024-10-22"],  # Required!
    messages=[{
        "role": "user",
        "content": "Open Firefox and search for 'Anthropic Claude'"
    }]
)

# Claude will return computer actions like:
# {"action": "mouse_move", "coordinate": [500, 400]}
# {"action": "click", "button": "left"}
# {"action": "type", "text": "Anthropic Claude"}

Controlling Tool Use

Use tool_choice to control when and how Claude uses tools:

Option Behavior
{"type": "auto"} Claude decides whether to use tools (default)
{"type": "any"} Force Claude to use at least one tool
{"type": "tool", "name": "..."} Force a specific tool
# Force Claude to use the calculator tool
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=tools,
    tool_choice={"type": "tool", "name": "calculator"},
    messages=[{"role": "user", "content": "What is 25 * 17?"}]
)

Claude Tools vs OpenAI Function Calling

Aspect Claude (Anthropic) OpenAI
SDK Name Tool Use Function Calling
Schema Key input_schema parameters
Response Block tool_use function_call / tool_calls
Parallel Calls ✓ Yes ✓ Yes
Computer Use ✓ Yes (Beta) ✗ No
Built-in RAG ✗ No ✓ File Search

Best Practices

Do

  • • Write clear, detailed descriptions
  • • Define precise JSON schemas
  • • Handle all stop_reason values
  • • Validate tool inputs before executing
  • • Include examples in descriptions

Don't

  • • Run computer use on production systems
  • • Trust tool inputs without validation
  • • Ignore error responses from tools
  • • Use vague tool descriptions
  • • Forget to handle parallel tool calls

Supported Models

Claude 3.5 Sonnet

claude-3-5-sonnet-20241022

Best for tools Computer Use

Claude 3.5 Haiku

claude-3-5-haiku-20241022

Fast & cheap

Claude 3 Opus

claude-3-opus-20240229

Most capable

Resources & References

Related Topics