25 min
Lesson 2 of 5
Setting Up Computer Use Environment
From the course Anthropic Computer Use Agent
Proper environment setup is crucial for reliable Computer Use functionality. This lesson covers installation, configuration, and best practices.
Installation Requirements
Python Environment
# Create virtual environment
python -m venv computer-use-env
source computer-use-env/bin/activate # macOS/Linux
# computer-use-env\Scripts\activate # Windows
# Install required packages
pip install anthropic pillow pyautogui opencv-python
API Configuration
import anthropic
import os
# Set up API key
client = anthropic.Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY")
)
Basic Computer Use Implementation
Screen Capture Setup
from PIL import ImageGrab
import base64
import io
def capture_screen():
screenshot = ImageGrab.grab()
# Convert to base64 for API
buffered = io.BytesIO()
screenshot.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return img_str, screenshot.size
def send_computer_use_request(task, screenshot_data):
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=[{
"type": "computer_20241022",
"name": "computer",
"display_width_px": screenshot_data[1][0],
"display_height_px": screenshot_data[1][1],
}],
messages=[{
"role": "user",
"content": [
{"type": "text", "text": task},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": screenshot_data[0]
}
}
]
}]
)
return message
Security and Permissions
macOS Setup
- Enable Screen Recording permission in System Preferences
- Grant Accessibility permissions for input control
- Configure Gatekeeper settings if needed
Windows Setup
- Run applications with appropriate privileges
- Configure Windows Security settings
- Handle UAC prompts appropriately
Linux Setup
- Install X11 development libraries
- Configure display server access
- Set proper user permissions for input devices
Environment Variables
# .env file
ANTHROPIC_API_KEY=your_api_key_here
DISPLAY_WIDTH=1920
DISPLAY_HEIGHT=1080
SCREEN_SCALE=1.0
Testing Your Setup
def test_computer_use():
try:
# Capture screen
screenshot, size = capture_screen()
# Simple test task
response = send_computer_use_request(
"Take a screenshot and describe what you see",
(screenshot, size)
)
print("Setup successful!")
print(response.content[0].text)
except Exception as e:
print(f"Setup error: {e}")
test_computer_use()
Key Takeaways
- •Requires Python environment with specific packages and API authentication
- •Screen capture and input permissions must be configured at OS level
- •Proper error handling and testing ensures reliable functionality
- •Environment variables help manage configuration and credentials securely
0 of 5 lessons marked complete
Progress is kept in this browser only.