Skip to main content

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:
  1. Extracts your function code - removes the decorator and gets clean source
  2. Captures all dependencies - explicit parameters, closure variables, and globals
  3. Serializes with cloudpickle - robust serialization for complex Python objects
  4. Injects dependencies - recreates your execution environment remotely
  5. Streams results back - real-time logs and final return value
From browser_use/sandbox/sandbox.py:215-350:

Function Requirements

Your function MUST have browser: Browser as a parameter. The browser is automatically injected - do NOT pass it when calling.

Configuration Parameters

API Authentication

Get your API key at cloud.browser-use.com/new-api-key

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

From browser_use/sandbox/views.py:14-24:
  • BROWSER_CREATED - Browser session provisioned with live URL
  • INSTANCE_READY - Container ready, execution starting
  • LOG - Runtime logs (stdout, stderr, info, warning, error)
  • RESULT - Final execution result
  • ERROR - 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:
Click the URL to see:
  • Real-time browser screen
  • Element highlights as agent interacts
  • Network activity
  • Console logs

Advanced Examples

Passing Complex Data

The sandbox uses cloudpickle for robust serialization:

Closure Variables

Closure variables are automatically captured:

Error Handling

Production Best Practices

1. Use Profiles for Authentication

Then use the profile ID:

2. Set Appropriate Timeouts

3. Use Proxies for Production

4. Monitor with Callbacks

Limitations

  • Function signature: Must include browser: Browser parameter
  • 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_vars for 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