Skip to main content
sandbox_demo.py
uv run python sandbox_demo.py prints exit code 0 and the sandbox’s Python version. A sandbox is a container in the cloud that your code starts, drives, and terminates, for running generated code, testing a patch, or giving an agent a development environment. It 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.

Run 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().

Read and write files

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

Give a sandbox the 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.

Snapshot and restore

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

Call it from async code

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

Configure a sandbox

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.

Run 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 a sandbox

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.

Set what every workload shares

Images and resources.