Installation
First, install the AI package:Creating an Agent
Use theai.createAgent() factory function to create an agent:
Provider Configuration
Anthropic (Claude)
Anthropic’s Claude models are fully supported and recommended for browser automation.claude-sonnet-4-5-20250929(default) - Best balance of speed and intelligenceclaude-opus-4-5-20251101- Most capable, slower and more expensiveclaude-haiku-4-20241024- Fastest and cheapest, less capable
OpenAI (GPT)
Using the Agent
You can set the agent at two levels:Client-Level Agent (Recommended)
Set the agent when connecting to apply it to all browsers and pages:Page-Level Agent
Override the agent for a specific page:Environment Variables
Store API keys in environment variables for security:Complete Setup Example
Here’s a complete example with environment variables and error handling:Cost Considerations
AI automation makes API calls to your chosen provider, which incurs costs:- Anthropic Claude: ~$3-15 per 1M input tokens depending on model
- OpenAI GPT: ~$2.50-30 per 1M input tokens depending on model
act() or extract() call sends page context (usually 1-5K tokens) plus your instruction.
Tips to reduce costs:
- Use Haiku model for simple tasks
- Use
act()sparingly for complex interactions only - Combine multiple actions into one instruction when possible
- Cache results when extracting the same data repeatedly
Next Steps
Natural Language Actions
Use page.act() to perform actions with AI
Data Extraction
Extract structured data with page.extract()