Skip to main content
A sandbox fits running generated code, testing a patch, or giving an agent a development environment. Each one is a container in the cloud that your code starts, drives, and terminates.

Create a sandbox

Save this as sandbox_demo.py and run it with uv run python sandbox_demo.py:
It prints exit code 0 and the sandbox’s Python version. A sandbox bills until it stops, so the finally block terminates it even when a command fails. A plain number for memory means MiB.

Reconnect to a running sandbox

Another process can reconnect by ID before the sandbox expires:
sandbox.list() returns the workspace’s sandboxes, and sandbox.stats() counts them.

Use a running sandbox

Commands and processes

run waits for the command to exit, and process controls long-running ones:
A process has pid, status(), kill(), wait(), and stdout, stderr, and logs streams with read() and lines().

Files

instance.fs reads and writes the sandbox filesystem, rooted at /workspace:

Ports and network

An exposed URL lives as long as the container. ports=[8000] on the sandbox exposes ports at start. Outbound access is either blocked or limited to CIDR ranges.

Snapshots

A memory snapshot restores a running sandbox. A filesystem image captures the disk for reuse with Image.from_id(...).

Async

Each API has an async form under .aio, such as await instance.aio.run(...).

Sandbox options

Sandboxes accept env, secrets, volumes, gpu and gpu_count, sync_local_dir=True to copy your working directory in at creation, command for the initial process, ports, docker_enabled, and machine. keep_warm_seconds sets how long an idle sandbox stays up, 600 by default.

Docker inside a sandbox

image=Image().with_docker() with docker_enabled=True runs a Docker daemon in the sandbox, and instance.docker wraps run, build, ps, logs, compose_up, and the rest. Nested containers share the sandbox’s network, so each service binds its own port and instance.expose_port(port) gives its URL.

Stop and clean up

instance.terminate() stops a sandbox at once. Otherwise it stops after keep_warm_seconds idle. The dashboard shows running sandboxes with a terminal, file browser, and process list.

Shared settings

Images and resources.