Ever Dreamed of Claude Controlling Your Computer?
Hey devs and AI builders! If you've ever wanted your Claude-powered agents to actually interact with desktop apps, browsers, or any UI—like a real human clicking around—you're in luck. Anthropic's Computer Use tool (beta in Claude 3.5 Sonnet) lets Claude see screenshots, reason about them, and execute mouse/keyboard actions. No more brittle Selenium scripts; this is vision-guided automation at its finest.
In this guide, we'll walk through 7 actionable steps to automate browser interactions, form filling, and UI navigation. By the end, you'll have code to build your first autonomous agent. Let's dive in!
Step 1: Prerequisites and Setup
Before Claude can "use" your computer, grab these:
- Claude API Key: Sign up at console.anthropic.com. Computer Use is beta—request access via Anthropic's form if needed.
- Python Environment: Python 3.10+ with
anthropicSDK:pip install anthropic pillow opencv-python(for image handling). - Screen Control Libs: We'll use
pyautoguifor actions:pip install pyautogui. (Note: Run on your local machine; remote desktops like AWS Workspaces work too.)
Pro Tip: Test on a virtual machine first to avoid mishaps—Claude's smart, but it's still AI!
Step 2: Understand the Computer Use API
Claude's tool works in a loop:
- You send a screenshot + task prompt.
- Claude responds with tool calls (e.g.,
cursor_move,mouse_click). - Execute the action, grab new screenshot, repeat until done.
Key tools:
computer_cursor_move(x: int, y: int): Move to coords.computer_mouse_click(): Click current position.computer_type(text: str): Type keys.computer_keypress(key: str): Special keys like Enter.
Vision magic: Claude 3.5 Sonnet analyzes images natively—no extra models needed.
Step 3: Basic Screenshot Capture Function
Start simple. Here's a reusable function to grab your screen:
import pyautogui
import base64
from io import BytesIO
from PIL import Image
SCREEN_WIDTH, SCREEN_HEIGHT = pyautogui.size()
def take_screenshot() -> str:
screenshot = pyautogui.screenshot()
buffered = BytesIO()
screenshot.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return f"data:image/png;base64,{img_str}"
This returns a base64 PNG Claude can "see".
Step 4: Initialize Claude Client and Tool Schema
Define the tool schema for Claude:
from anthropic import Anthropic
client = Anthropic(api_key="your-api-key-here")
computer_use_tools = [
{
"name": "computer_use",
"input_schema": {
"type": "object",
"properties": {
"action": {"type": "string", "enum": ["cursor_move", "mouse_click", "type", "keypress"]},
"x": {"type": "number"},
"y": {"type": "number"},
"text": {"type": "string"},
"key": {"type": "string"}
}
}
}
]
(Full schema details in Anthropic docs—adapt as API evolves.)
Step 5: Your First Automation—Open a Browser
Let's open Chrome and navigate to Google. Core loop:
def execute_action(action):
if action["action"] == "cursor_move":
pyautogui.moveTo(action["x"], action["y"])
elif action["action"] == "mouse_click":
pyautogui.click()
# Add type, keypress similarly
def automate_browser():
messages = [{"role": "user", "content": [
{"type": "text", "text": "Open Chrome and search for 'Claude Directory'."},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": take_screenshot()}}
]}]
for _ in range(20): # Max 20 steps
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
tools=computer_use_tools,
messages=messages
)
for tool in response.stop_reason == "tool_use":
action = tool.input
execute_action(action)
if response.content[-1].text.endswith("Done"): # Claude signals completion
break
messages.append(response.content[-1])
messages[-1]["content"].append({"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": take_screenshot()}})
automate_browser()
Boom! Claude sees your desktop, finds the Chrome icon, clicks it, types URL. Watch it unfold.
Step 6: Form Filling Masterclass
Scale up: Automate a login form (e.g., demo site like example.com/login).
Prompt tweak: "Navigate to http://example-login.com, find username field, type 'testuser', password 'pass123', submit."
Enhance with coordinates: Claude estimates from vision, but add screen_coords=True for precision.
Real-world example—Zapier signup automation:
- Claude: Spots "Sign Up" button via OCR/vision.
- Clicks, types email from env var (secure!).
- Handles CAPTCHAs? Not yet—pair with 2captcha API for prod.
Code snippet for secure typing:
# In execute_action:
if action["action"] == "type":
pyautogui.write(action["text"])
Step 7: Building an Autonomous Agent
Combine with MCP or agents framework:
- State Management: Track screen regions (e.g., browser viewport).
- Error Handling: If Claude loops forever, timeout + human intervene.
- Multi-App: Script Slack replies or Excel data entry.
Advanced agent loop:
class ClaudeDesktopAgent:
def __init__(self, task: str):
self.task = task
self.max_steps = 50
def run(self):
# Similar loop, but persist state in DB
pass
agent = ClaudeDesktopAgent("Book a flight on Kayak under $200")
agent.run()
Integrate with n8n/Zapier: Trigger Claude on webhook, pipe screenshots via API.
Best Practices for Production
- Security: Sandbox in VM; never expose prod creds.
- Reliability: Claude's vision is 90%+ accurate on clear UIs—test variable lighting/fonts.
- Rate Limits: 10-20 actions/min; async for scale.
- Debugging: Log screenshots/actions:
screenshot.save(f'step_{i}.png'). - Prompt Engineering: Be specific: "Click the blue 'Submit' button at bottom-right. Ignore popups."
Common Pitfalls and Fixes
| Pitfall | Fix |
|---|---|
| Cursor drifts | Calibrate screen resolution in prompt. |
| Slow loops | Limit max_tokens=500; use Haiku for speed. |
| Blurry screenshots | Use high-res: pyautogui.screenshot(region=(0,0,1920,1080)). |
| Tool schema errors | Match exact Anthropic beta schema. |
Why Claude Over Selenium/Playwright?
- Vision-First: Handles dynamic UIs, no XPath fragility.
- Zero Setup: No browser drivers.
- Reasoning: Claude adapts: "If login fails, try forgot password."
Comparisons:
- Vs GPT-4V: Claude's tool primitives are tighter.
- Vs Browserless: Desktop-native, any app.
Wrapping Up
You've got the blueprint! Start with browser basics, iterate to full agents. Drop your experiments in comments—Claude Directory loves Claude hacks.
Word count: ~1450. Questions? Hit the Anthropic Discord or our forums. Build boldly! 🚀
Updated Oct 2024—API beta evolving fast.
Stay ahead of the AI curve
The most important updates, news, and content — delivered in one weekly newsletter.