Overview
Agent Browser can run in serverless environments by using lightweight Chromium builds and the programmatic API. The bundled Chromium (~684MB) is too large for most serverless platforms, so you’ll need to use optimized alternatives like@sparticuz/chromium (~50MB).
Quick Start
For serverless deployment, use theBrowserManager class directly and provide a custom executablePath:
Platform-Specific Guides
Vercel
Deploy as a Vercel Function with@sparticuz/chromium.
Installation:
api/screenshot.ts):
- Increase memory to at least 3008 MB for Chromium
- Increase
maxDurationif operations take longer than 10s - Cold starts can take 5-10 seconds
AWS Lambda
Deploy using Lambda Functions with@sparticuz/chromium.
Installation:
index.ts):
- Set memory to at least 3008 MB
- Set timeout to at least 30 seconds
- Lambda’s
/tmpdirectory has 512 MB limit; Chromium uses it for downloads and caching @sparticuz/chromiumis optimized for Lambda and includes necessary shared libraries
AWS Lambda with Docker
For more control, use a Lambda container image: Dockerfile:Google Cloud Functions
Installation:index.ts):
Testing Serverless Setup Locally
Before deploying, test your serverless setup locally to verify@sparticuz/chromium integration:
test/serverless.test.ts in the source code for a complete example.
Performance Optimization
Reduce Cold Start Time
- Use deployment packages: Pre-bundle dependencies to reduce cold start
- Minimize dependencies: Only include what you need
- Use provisioned concurrency (AWS Lambda) for zero cold starts
- Keep functions warm with periodic invocations
Memory and CPU Allocation
Chromium requires significant resources:- Minimum: 1024 MB memory (will be slow)
- Recommended: 2048-3008 MB memory for better performance
- CPU: Scales with memory on most platforms
Reuse Browser Instances
Warning: Reusing browser instances across invocations can cause memory leaks and stale state. Safe approach:Alternatives to @sparticuz/chromium
chrome-aws-lambda
Older alternative, less maintained:System Chrome/Chromium
If your serverless environment has Chrome/Chromium pre-installed:Cloud Browser Services
For production deployments, consider cloud browser services instead of running Chromium in serverless functions:Browserbase
Browser Use
Kernel
Environment Variables
Key environment variables for serverless deployment:| Variable | Description |
|---|---|
AGENT_BROWSER_EXECUTABLE_PATH | Path to Chromium binary |
AGENT_BROWSER_DEFAULT_TIMEOUT | Playwright timeout in ms (default: 25000) |
AGENT_BROWSER_PROVIDER | Cloud browser provider (browserbase, browseruse, kernel) |
NODE_ENV | Set to production for optimizations |
Troubleshooting
”Failed to launch browser” errors
Ensure:- Memory is sufficient (at least 1024 MB, recommended 2048+ MB)
- Timeout is adequate (at least 30 seconds)
- executablePath is correct:
Missing shared libraries
@sparticuz/chromium includes necessary libraries for Lambda. For other platforms, install system dependencies: