Claude computer use screenshot tool
Quick answer
Use the
anthropic Python SDK with client.beta.messages.create specifying betas=["computer-use-2024-10-22"] and the tools parameter including a computer_20241022 type tool to enable screenshot functionality. Send a user message requesting a screenshot, and the model will execute it via the computer use tool.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic SDK and set your Anthropic API key as an environment variable.
- Run
pip install anthropic - Set
export ANTHROPIC_API_KEY=your_api_keyon Linux/macOS or set environment variable on Windows.
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
This example shows how to request a screenshot using Claude's computer use tool. The tools parameter must include a computer_20241022 tool, and betas must enable the computer use feature.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.beta.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=[{
"type": "computer_20241022",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768
}],
messages=[{"role": "user", "content": "Take a screenshot of the current screen."}],
betas=["computer-use-2024-10-22"]
)
print("Response:", response.content[0].text) output
Response: Screenshot taken and saved as 'screenshot.png'. Here is the image data encoded in base64...
Common variations
You can adjust the display_width_px and display_height_px to control the screenshot resolution. Use different model versions if available. The API supports asynchronous calls via asyncio with the same parameters.
import asyncio
import os
from anthropic import Anthropic
async def main():
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.beta.messages.acreate(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=[{
"type": "computer_20241022",
"name": "computer",
"display_width_px": 1280,
"display_height_px": 720
}],
messages=[{"role": "user", "content": "Take a screenshot."}],
betas=["computer-use-2024-10-22"]
)
print("Async response:", response.content[0].text)
asyncio.run(main()) output
Async response: Screenshot captured and ready for use.
Troubleshooting
- If you get an error about missing
betas, ensure you includebetas=["computer-use-2024-10-22"]in your request. - If the screenshot is not captured, verify your API key has access to the computer use beta features.
- Check network permissions if running in restricted environments.
Key Takeaways
- Use the
toolsparameter withtype: computer_20241022to enable screenshot functionality. - Always include
betas=["computer-use-2024-10-22"]to activate the computer use beta feature. - Set display resolution via
display_width_pxanddisplay_height_pxin the tool definition. - Use the
anthropicSDK'sbeta.messages.createmethod for synchronous calls andacreatefor async. - Ensure your API key has access to the computer use beta to avoid authorization errors.