Skip to main content
Agents and tests often need a real browser: to read a page that needs JavaScript, fill in a form, take a screenshot, or test a web app. Run that browser in a sandbox instead of on your server. This guide builds a template with Playwright and Chromium, then shows two ways to use it: run a browser script in the sandbox, or connect to the browser from your own code. For a full desktop with a visible browser window that a model controls with the mouse and keyboard, see Computer use agents.

Why use a sandbox

  • Isolation: pages that the browser opens, and files that they download, stay in the sandbox’s microVM, away from your servers and their credentials.
  • No browser on your servers: Chromium and its system libraries live in the template, so your app needs only the SDK and, to connect, the Playwright client.
  • Parallel sessions: start one sandbox per browser session, each with its own cookies, storage, and IP traffic.
  • Network control: a network rule can limit which sites the browser can reach.

How it works

1

Build a browser template

Install Playwright and Chromium once, in a template.
2

Create a sandbox from it

Each sandbox starts with the browser installed.
3

Run the browser

Run a Playwright script in the sandbox, or start a Playwright server there and connect to it from your code through a preview URL.
4

Read the results and delete the sandbox

Read screenshots and other files with files.read, then delete the sandbox.

1. Build a browser template

playwright install --with-deps installs Chromium and the system libraries that it needs. --only-shell installs only the headless build, which is smaller. You build the template once and reuse it.
  • PLAYWRIGHT_BROWSERS_PATH puts Chromium in a fixed directory. Build steps run as root with a different home directory than sandbox commands, so the default cache path in the home directory doesn’t work.
  • The Node.js runtime inside the Playwright package needs libatomic1, which slim images don’t include.

2. Run a browser script in the sandbox

Send a Playwright script in with files.write and run it. The script prints the page title as JSON and saves a screenshot, which you read back with files.read.
Give a timeout to every commands.run that opens pages. Without one, commands.run stops a command after 30 seconds. See Timeouts.

3. Drive the browser from your code

To control the browser step by step from your app, for example from an agent’s tools, start a Playwright server in the sandbox and connect to it over a private preview URL. Your code then uses the normal Playwright API, and the browser runs in the sandbox. Install the Playwright client in your app with the same version as the template: npm install playwright@1.65.0 or pip install playwright==1.65.0. The client doesn’t need its own browsers.
  • Keep the port private. Anyone who can open a public Playwright server port can control the browser.
  • The client and the server must have the same Playwright version, or connect fails.
  • page.goto runs in the sandbox, so the page loads from the sandbox’s network, and network rules apply to it.

Clean up

Integrations

To let a model control a full desktop browser with the mouse and keyboard, see Claude computer use and OpenAI computer use.

Computer use agents

A full desktop with a browser that a model drives.

Preview URLs

Publish ports with public or private access.

Templates

Bake tools into a reusable environment.

Code interpreter

Run model-generated code and read the output back.