Computer use human oversight patterns
Quick answer
Use the
anthropic.Anthropic client with the computer_20241022 tool type and enable the computer-use-2024-10-22 beta flag to invoke computer use with human oversight. Monitor tool_calls in responses to handle and review AI actions before execution.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic Python SDK and set your Anthropic API key as an environment variable.
- Install SDK:
pip install anthropic - Set environment variable:
export ANTHROPIC_API_KEY='your_api_key'(Linux/macOS) orset ANTHROPIC_API_KEY=your_api_key(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 demonstrates invoking the computer_20241022 tool with human oversight enabled via the computer-use-2024-10-22 beta flag. The AI's tool calls can be inspected and approved before execution.
import os
import json
import anthropic
client = anthropic.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": "Open a text editor and write a short note."}],
betas=["computer-use-2024-10-22"]
)
print("AI response:", response.content[0].text)
# Check if AI wants to call a tool
if response.choices[0].finish_reason == "tool_calls":
tool_call = response.choices[0].message.tool_calls[0]
print("Tool call detected:", json.dumps(tool_call, indent=2))
# Human oversight: review tool_call before executing
else:
print("No tool call detected.") output
AI response: Sure, I will open a text editor and write a short note.
Tool call detected: {
"name": "computer",
"arguments": "{\"action\": \"open_text_editor\", \"content\": \"This is a short note written by AI.\"}"
} Common variations
You can customize the tools parameter to include different computer capabilities or adjust display sizes. Use async calls with asyncio for concurrency. Different Claude models supporting computer use may be available; always check the latest model names and beta flags.
import asyncio
import os
import anthropic
async def main():
client = anthropic.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 of the desktop."}],
betas=["computer-use-2024-10-22"]
)
print("Async AI response:", response.content[0].text)
asyncio.run(main()) output
Async AI response: Taking a screenshot of the desktop now.
Troubleshooting
- If you receive an error about missing
betasortools, ensure you are using thebeta.messages.createmethod and have the correct beta flagcomputer-use-2024-10-22. - If no tool calls appear, verify the model supports computer use and that your prompt triggers tool invocation.
- Check your API key and environment variable setup if authentication errors occur.
Key Takeaways
- Use the Anthropic SDK's beta endpoint with the computer-use beta flag for human oversight.
- Inspect
tool_callsin responses to review AI's intended computer actions before execution. - Customize tool parameters and use async calls for flexible, concurrent workflows.