BrowserEnv
Unified browser automation environment supporting both DOM-based (natural language) and CUA-based (vision + coordinates) control.Overview
BrowserEnv provides two distinct browser automation modes:
- DOM mode: Natural language operations via Stagehand SDK (
act,observe,extract,navigate) - CUA mode: Vision-based primitives (
click,scroll,type_text,screenshot)
Installation
Install with browser support:Inheritance
Constructor
Parameters
Mode Selection
Literal['dom', 'cua']
default:"dom"
Operating mode:
"dom": Natural language browser control via Stagehand SDK"cua": Vision-based control using coordinate primitives
Shared Configuration
str | None
default:"None"
Browserbase project ID. Required when using Browserbase.
str
default:"BROWSERBASE_API_KEY"
Environment variable name for Browserbase API key.
DOM Mode Parameters
str
default:"MODEL_API_KEY"
Environment variable name for model API key (OpenAI, Anthropic, etc.).
str
default:"openai/gpt-4o-mini"
Model used by Stagehand for DOM understanding and action planning.
bool
default:"False"
Whether to proxy model API calls through Stagehand.
CUA Mode Parameters
bool
default:"True"
Auto-deploy CUA server to sandbox. If False, connects to
server_url.str
default:"http://localhost:3000"
CUA server URL when
use_sandbox=False.Literal['LOCAL', 'BROWSERBASE']
default:"BROWSERBASE"
Browser execution environment:
"BROWSERBASE": Cloud browsers via Browserbase"LOCAL": Local browser execution
int
default:"1024"
Browser viewport width in pixels.
int
default:"768"
Browser viewport height in pixels.
bool
default:"True"
Save screenshots to disk during execution.
int | None
default:"2"
Number of recent screenshots to keep in message context. Set to None to keep all.
bool
default:"False"
Enable Browserbase proxies for IP rotation.
bool
default:"False"
Enable Browserbase Advanced Stealth mode for anti-bot detection.
CUA Sandbox Configuration
int
default:"3000"
Port for CUA server in sandbox.
int
default:"120"
Timeout in seconds waiting for sandbox server to be ready.
float
default:"2.0"
Poll interval in seconds for sandbox server health checks.
str
default:"node:18-slim"
Docker image for sandbox (only used when
use_prebuilt_image=False).int
default:"2"
CPU cores allocated to sandbox.
int
default:"4"
Memory in GB allocated to sandbox.
int
default:"10"
Disk size in GB for sandbox.
int
default:"60"
Sandbox timeout in minutes.
int
default:"60"
Per-command timeout in sandbox.
bool
default:"True"
Use pre-built SEA binary when
use_prebuilt_image=False. If False, installs from npm.Pre-built Image Configuration
bool
default:"True"
Use pre-built Docker image for fastest startup. Recommended for production.
str
default:"deepdream19/cua-server:latest"
Docker image to use when
use_prebuilt_image=True.Error Handling
list[type[Exception]] | None
default:"None"
Exception types that should trigger cleanup. Defaults to
[vf.SandboxError].Any
Additional arguments passed to StatefulToolEnv.
DOM Mode Tools
navigate
act
observe
extract
CUA Mode Tools
click
type_text
scroll
screenshot
Key Methods
setup_state
update_tool_args
get_prompt_messages
keep_recent_screenshots.
cleanup_session
teardown
Example Usage
DOM Mode
CUA Mode with Sandbox (Default)
CUA Mode with Local Server
CUA Mode Execution Options
CUA mode supports three execution strategies (from fastest to most flexible):1. Pre-built Docker Image (Default, Recommended)
- Fastest startup (no binary upload or npm install)
- Uses pre-built
deepdream19/cua-server:latestimage - Best for production and rapid iteration
2. Binary Upload
- Builds/uploads SEA binary to sandbox
- Useful for custom server versions
- Slower startup than pre-built image
3. Local Server
- Connect to manually started CUA server
- Useful for local development and debugging
- Requires running
npm startinassets/templates/browserbase/cua/
Screenshot Management (CUA Mode)
CUA mode automatically manages screenshots in the message history:save_screenshots=True: Screenshots saved to diskkeep_recent_screenshots=2: Only 2 most recent screenshots kept in context- Older screenshots filtered out via
get_prompt_messages()to reduce token usage
See Also
- Browser Examples - Complete examples for both modes
- StatefulToolEnv - Base class documentation
- ToolEnv - Tool environment basics
- MultiTurnEnv - Multi-turn interaction patterns