Prerequisites
- The
boxlitePython package and a machine with hardware virtualization — see Installation.
pip show boxlite):
Quick Example (shortest path)
Save the following ashello.py and run it with python hello.py. It is a complete, standalone script: it pulls an image, starts a Box, runs a command, prints the result, and cleans up automatically.
The first run downloads the python:alpine image and may take tens of seconds; later runs hit the local cache and start quickly.
What happens:
- BoxLite pulls the
python:alpineOCI image (first run only). - It starts a lightweight microVM from that image.
- It runs
python -c "..."inside the VM. - It streams stdout / stderr / exit code back to the host process.
- On exit from
async with, it stops and removes the Box automatically.
Next Step: Executing Untrusted Code (CodeBox)
CodeBox is a subclass of SimpleBox with a built-in Python image. It provides run() to run code directly and install_package() to install dependencies dynamically — the typical pattern for an AI agent executing LLM-generated code. Save this as codebox.py:
CodeBox.run(code)returns the stdout string, whereasSimpleBox.exec(...)returns anExecResultobject (withexit_code/stdout/stderr). Do not confuse the two.
Parameters and Returns
Common SimpleBox(...) constructor parameters
SimpleBox is lazily created: the constructor only validates arguments; the actual create/start happens when you enter async with.
Note:cpusandmemory_mibdefault toNone, which means BoxLite applies the runtime’s default VM size rather than a value you set. To see the actual resources inside a Box, runnprocand read/proc/meminfoinside it. In production, set the resources explicitly. See Compute resources.
box.exec(cmd, *args, ...) parameters
ExecResult (the return value of box.exec)
Listing and cleanup (runtime methods live on the runtime)
Listing and removing Boxes are methods on the runtime (Boxlite), not on a Box:
Boxliteis entered with a synchronouswith, and theBoxlite.default()factory is synchronous too. Its instance methods (list_info()/create()/get()/remove()/metrics()/shutdown()) are async — alwaysawaitthem, or you getRuntimeError: no running event loop. The one exception isbox.info(), which is synchronous (do notawaitit).
Troubleshooting
ValueError: Either 'image' or 'rootfs_path' must be provided
The SimpleBox constructor requires either image or rootfs_path. CodeBox has a built-in default image (python:slim), so you do not pass image.
A command exited non-zero but no exception was raised
exec does not raise on a non-zero exit code; it returns ExecResult(exit_code != 0). You must check it yourself:
RuntimeError: image pull failure / startup failure without virtualization
On a network failure during the image pull or when the environment has no hardware virtualization, a standard builtins.RuntimeError is raised (not BoxliteError). Catch it with try/except RuntimeError and retry:
Environment constraint: hardware virtualization required
- macOS (Apple Silicon): uses the built-in Hypervisor.framework, needs no
/dev/kvm, and works out of the box. - Linux: needs KVM (
/dev/kvmpresent and accessible). - WSL2: needs KVM enabled and the user in the
kvmgroup. - No virtualization: the Box fails to start (the exception is catchable; the process does not crash).
- macOS Intel: not supported.
Confusing dict and list for environment variables
box.exec(env=...) takes a dict: env={"FOO": "bar"}. The SimpleBox constructor parameter env= (passed through to BoxOptions) takes a list of tuples: env=[("FOO", "bar")]. The two layers use different formats; do not confuse them.
Next Steps
Now that you have your first sandbox, you can continue with:- Reuse and lifecycle: use
name+reuse_existing=Trueto reuse the same Box across sessions; manage Boxes withruntime.list_info()/runtime.remove(id, force=False). - Specialized boxes:
BrowserBox(Playwright browser automation),ComputerBox(desktop automation),InteractiveBox(interactive PTY shell), andSkillBox(running Claude Code). - Resources and isolation: use
BoxOptionsto configure cpus/memory, volumes, ports, an egress allowlist, and secrets, plus security presets viaadvanced=AdvancedBoxOptions(security=...). - Other languages: see the Node.js quickstart, the Rust quickstart, and the Go quickstart.

