Skip to content

One-program sandboxes

Pass command or entrypoint to run one program in a sandbox, like docker run. Unlike a session, it accepts no commands from your code and stdin is closed. Wait for exit, then read output.

python/examples/programs.py
with Sandbox.create(
"alpine:3.20",
command=["sh", "-c", "echo out; echo err >&2; exit 3"],
name="exits-three",
cpus=2,
memory_mib=2048,
seed=7,
) as sandbox:
print(sandbox.wait())
output = sandbox.output()
print(output.stdout)
print(output.stderr)
print(sandbox.state().kind)
  • command is argv, replacing the image’s CMD.
  • entrypoint replaces the image’s ENTRYPOINT, with command as its arguments.
python/examples/programs.py
with Sandbox.create("alpine:3.20", entrypoint="sh", command=["-c", "uname -a"]) as sandbox:
sandbox.wait()
print(sandbox.output().stdout.decode(), end="")

The program inherits the image’s environment, user, and working directory, plus any env you pass (Environment and secrets). Images must be public; pulls are anonymous.

Default Range
cpus 1 1 to 8 vCPUs
memory (memory_mib, memoryMib) 1024 MiB 128 to 16384 MiB
seed 0 0 to 253 − 1
name from the image up to 63 lowercase letters, digits and -, not starting or ending with -

Your plan may cap resources below these ranges; larger requests fail with your plan’s limit (Plans, limits and credit). The same image, command, and seed run the same way, except where the program reads something from outside that differs, such as the network (What a recording holds). Name is a label for you.

These options also apply to a session (a sandbox accepting commands).

  • wait returns the exit code once the program exits, or raises the matching error if stopped or failed (Errors).
  • output returns stdout and stderr written so far, as bytes.

For a session, output returns the session server’s response frames rather than command output (The sandbox protocol). Use run there.

python/examples/programs.py
with Sandbox.create("alpine:3.20", command=["sh", "-c", "while :; do :; done"]) as sandbox:
sandbox.pause()
print(sandbox.state().kind)
sandbox.resume()
elsewhere = Sandbox.connect(sandbox.id)
elsewhere.stop()
elsewhere.close()
print(sandbox.state().kind)
  • pause freezes the sandbox; resume continues it.
  • stop terminates it (no-op if already ended). Stopped sandboxes cannot be inspected; exited ones can.
  • state returns queued, running, or paused, then exited (with exit code), stopped, or failed (with reason).
  • connect attaches another handle by sandbox ID from any process using an API key on the same account.

A sandbox that never exits runs until its time limit, then fails with that reason (Plans, limits and credit). Exiting a with block, await using, or scope stops a running one-program sandbox.