Overview
The@sandbox decorator lets you run browser automation code in a production-ready cloud environment with zero infrastructure setup. Your local code is serialized, sent to Browser Use Cloud, and executed in a managed environment with automatic browser provisioning, authentication handling, and result streaming.
Sandboxes are the easiest way to run Browser-Use in production. We handle agents, browsers, persistence, auth, cookies, and LLMs. The agent runs right next to the browser, so latency is minimal.
Quick Start
Wrap your existing local code with@sandbox() - that’s it:
The
browser parameter is automatically injected - don’t pass it when calling your function.How It Works
Code Serialization
The sandbox decorator:- Extracts your function code - removes the decorator and gets clean source
- Captures all dependencies - explicit parameters, closure variables, and globals
- Serializes with cloudpickle - robust serialization for complex Python objects
- Injects dependencies - recreates your execution environment remotely
- Streams results back - real-time logs and final return value
browser_use/sandbox/sandbox.py:215-350:
Function Requirements
Configuration Parameters
API Authentication
Cloud Browser Settings
Cloud proxies provide residential IPs that bypass captchas, Cloudflare, and geo-restrictions. See Cloud Browser for details.
Logging & Output
Custom Environment Variables
Event Callbacks
Monitor execution with real-time callbacks:Available Events
Frombrowser_use/sandbox/views.py:14-24:
BROWSER_CREATED- Browser session provisioned with live URLINSTANCE_READY- Container ready, execution startingLOG- Runtime logs (stdout, stderr, info, warning, error)RESULT- Final execution resultERROR- Execution errors
Return Values
The sandbox preserves your function’s return type:Live Browser Viewing
Every sandbox execution gets a live browser URL where you can watch the automation in real-time:- Real-time browser screen
- Element highlights as agent interacts
- Network activity
- Console logs
Advanced Examples
Passing Complex Data
The sandbox usescloudpickle for robust serialization:
Closure Variables
Closure variables are automatically captured:Error Handling
Production Best Practices
1. Use Profiles for Authentication
2. Set Appropriate Timeouts
3. Use Proxies for Production
4. Monitor with Callbacks
Limitations
- Function signature: Must include
browser: Browserparameter - Return types: Must be JSON-serializable or cloudpickle-compatible
- Imports: Only used imports are automatically extracted
- Global state: Module-level variables are serialized via cloudpickle
- File system: No access to local files (use
env_varsfor config)
Debugging
Enable Debug Logging
Check Serialization
The decorator shows what code is sent:Local Testing First
Test locally before deploying:Cost Optimization
- Free tier: 15 minute sessions, standard browsers
- Paid tier: 240 minute sessions, faster provisioning, priority support
- Billing: Per-minute browser execution + LLM costs
See Also
- Cloud Browser - Browser provisioning and proxies
- Structured Output - Type-safe return values
- Production Guide - Full production deployment guide