BoxLite Sandbox
Run your agent’s commands and file edits in a BoxLite micro-VM on your own machine, isolated from your files and processes, without a cloud account.
While Pydantic AI Harness is on 0.x releases, the API may change between minor releases; when it does, deprecation warnings and release-note migration guidance tell you (or your agent) exactly how to upgrade. See the version policy.
flowchart TB
accTitle: BoxLite Sandbox: your agent's commands run in a local micro-VM
accDescr: On your machine, the model calls Coder, Shell or FileSystem tools. As ctx.workspace, BoxLiteSandbox's runtime in your process runs commands and file operations in a BoxLite micro-VM (own kernel, filesystem, kept until destroy(ref)), and a later run reattaches by the WorkspaceRef. By default the VM cannot connect to any host. With allow_internet_access=True and allowed_hosts it reaches only the listed hosts.
subgraph machine["Your machine"]
model(["model"])
tools["Coder, Shell or<br/>FileSystem tools,<br/>such as run_command"]
ref["WorkspaceRef<br/>in the message history"]
sandbox["BoxLiteSandbox()<br/>runtime in your process"]:::capability
vm["BoxLite micro-VM<br/>own kernel, filesystem<br/>kept until destroy(ref)"]:::external
end
allowed["hosts in allowed_hosts"]:::external
other["any other host,<br/>every host by default"]:::blocked
model <-->|"tool call, result"| tools
tools -->|"ctx.workspace"| sandbox
ref -.->|"a later run reattaches"| sandbox
sandbox <-->|"commands and<br/>file operations"| vm
vm -->|"allow_internet_access=True"| allowed
vm -.-x|"cannot connect"| other
classDef capability stroke:#E520E9,stroke-width:2px
classDef external stroke:#0E9C86,stroke-width:2px
classDef blocked stroke:#E5533D,stroke-width:2px,stroke-dasharray:4 3
pip install "pydantic-ai-harness[coder,boxlite,anthropic]"
uv add "pydantic-ai-harness[coder,boxlite,anthropic]"
The boxlite extra installs BoxLite’s Python SDK, which runs VMs in your process, with no daemon. It needs Python 3.14 or older on one of:
- macOS 14 or newer on Apple silicon.
- Linux on x86_64 or aarch64 with glibc 2.28 or newer. The user running the agent needs read and write access to
/dev/kvm.
The examples also use Coder and an Anthropic model, hence the coder and anthropic extras and ANTHROPIC_API_KEY; swap them for your own tools and model provider.
from pydantic_ai import Agent
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
from pydantic_ai_harness.coder import Coder
agent = Agent('anthropic:claude-opus-5-5', capabilities=[BoxLiteSandbox(image='python:3.12-slim'), Coder()])
result = agent.run_sync('Write a script that prints the first 10 primes, and run it.')
Coder’s shell and file tools now run in a VM with its own kernel and filesystem. For a narrower agent, use Shell, FileSystem or your own tools on ctx.workspace.
Codercreates it when the run starts, to load the repository’s instructions, even if no tool is called. PassCoder(repo_context=False)to wait for the first tool call.- Its disk stays in BoxLite’s home directory after the run. The box stops when your process exits, but nothing deletes it. See Clean up.
- It has no network access. Pass
allow_internet_access=True, andallowed_hoststo reach only what it needs. See Network access. - Commands run as the image’s user, usually
root, in/workspace, which is created if missing. - Without an
image, the box runs Alpine Linux, which has BusyBox but no Python, git or ripgrep. Animageis any OCI image with a POSIX/bin/sh; BoxLite pulls it the first time. - One process at a time can use BoxLite’s home directory (
~/.boxlite, orBOXLITE_HOME). Another process getsWorkspaceUnavailableErroruntil the first one exits.
Pass the message history, and the next run works in the same box, booting it again if stopped:
from pydantic_ai import Agent
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
from pydantic_ai_harness.coder import Coder
agent = Agent('anthropic:claude-opus-5-5', capabilities=[BoxLiteSandbox(image='python:3.12-slim'), Coder()])
result = agent.run_sync('Write a script that prints the first 10 primes, and run it.')
followup = agent.run_sync('Now make it print the first 100.', message_history=result.all_messages())
ref = result.workspace.ref # or store this, and pass `workspace=ref` to a later run
result.workspace.ref is a WorkspaceRef holding the box’s name. Store it to continue later or to delete the box.
If the box has been deleted, the run raises WorkspaceUnavailableError. Pass workspace='new' to start over. Workspaces covers how runs find their workspace.
To seed files or install packages before the agent starts, create the backend yourself, work in it through Workspace, and pass it to the run:
import asyncio
from pydantic_ai import Agent
from pydantic_ai.workspaces import Workspace
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox, BoxLiteSandboxBackend
from pydantic_ai_harness.coder import Coder
agent = Agent('anthropic:claude-opus-5-5', capabilities=[BoxLiteSandbox(), Coder()])
async def main() -> None:
backend = BoxLiteSandboxBackend(
image='python:3.12-slim',
allow_internet_access=True,
allowed_hosts=['pypi.org', 'files.pythonhosted.org'],
)
try:
workspace = Workspace(backend)
await workspace.write_text('calc.py', 'def add(a, b):\n return a - b\n')
install = await workspace.run(['pip', 'install', 'pytest'], timeout=300)
if install.exit_code != 0:
raise RuntimeError(f'pip install failed: {install.stderr}')
result = await agent.run('Fix the bug in calc.py.', workspace=backend)
print(result.output)
finally:
ref = backend.ref # set once the box exists
if ref is not None:
await BoxLiteSandbox().destroy(ref)
if __name__ == '__main__':
asyncio.run(main())
The finally deletes the box even when setup or the run fails. To keep it, store backend.ref instead.
BoxLiteSandbox’s settings don’t apply to a backend you pass as workspace=. Give the backend its own.
For features the workspace API doesn’t cover, such as copy_in, snapshot or stop, backend.get_sandbox() returns the SDK’s boxlite.Box.
Pass desktop=True and add ComputerUse:
capabilities=[BoxLiteSandbox(desktop=True, allow_internet_access=True), ComputerUse()]. The screen is 1280x800;
pass a size such as desktop=(1440, 900) for another.
Without an image, a new box runs debian:bookworm-slim, and its first start installs the display’s programs, with no web browser. That needs allow_internet_access=True (what that lets the box reach), with deb.debian.org in any allowed_hosts. Creating a new box without it raises UserError; for a box without network access, pass an image that has the programs. To install a browser, pass box_options={'disk_size_gb': 4}: the default disk is too small.
The agent can type only ASCII: an action that types or presses a character such as é fails, and the model is told why.
Delete a box and its disk with the ref you stored. A box that is already gone returns quietly:
from pydantic_ai.workspaces import WorkspaceRef
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
async def delete_box(ref: WorkspaceRef) -> None:
await BoxLiteSandbox().destroy(ref)
A failed run returns no result, so delete its box in an on_run_error hook:
from typing import Any
import anyio
from pydantic_ai import Agent, RunContext
from pydantic_ai.capabilities import Hooks
from pydantic_ai.run import AgentRunResult
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
from pydantic_ai_harness.coder import Coder
hooks = Hooks()
@hooks.on.run_error
async def delete_failed_run_box(ctx: RunContext[None], *, error: BaseException) -> AgentRunResult[Any]:
if ctx.workspace.ref is not None:
# Shielded so a deadline around the run can't cut the delete short, bounded so a hung call can't hang it.
with anyio.move_on_after(30, shield=True):
await BoxLiteSandbox().destroy(ctx.workspace.ref)
raise error
agent = Agent('anthropic:claude-opus-5-5', capabilities=[BoxLiteSandbox(image='python:3.12-slim'), Coder(), hooks])
If your program stops before it stores the ref, for example after a crash or Ctrl-C, the box is left behind. To find it:
- List boxes with
await boxlite.Boxlite.default().list_info(). - Or search your logs for
Creating BoxLite box <id>, logged at INFO by thepydantic_ai_harnesslogger.
Then delete it: await BoxLiteSandbox().destroy(WorkspaceRef(provider='boxlite', id='<id>')).
from pydantic_ai import Agent
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
from pydantic_ai_harness.coder import Coder
sandbox = BoxLiteSandbox(
image='python:3.12-slim',
allow_internet_access=True,
allowed_hosts=['pypi.org', 'files.pythonhosted.org'],
)
agent = Agent('anthropic:claude-opus-5-5', capabilities=[sandbox, Coder()])
result = agent.run_sync('Install requests and check that it imports.')
With allow_internet_access=True alone, the box can connect anywhere this machine can, including your local network and services and a cloud metadata endpoint.
allowed_hosts limits it to the hostnames, IP addresses and CIDR ranges you list. BoxLite’s network options describe how it matches them.
box_options passes any other boxlite.BoxOptions argument to a new box. This one mounts the current directory into the box read-only:
from pathlib import Path
from pydantic_ai import Agent
from pydantic_ai_harness.boxlite_sandbox import BoxLiteSandbox
from pydantic_ai_harness.coder import Coder
sandbox = BoxLiteSandbox(
image='python:3.12-slim',
box_options={'volumes': [(str(Path.cwd()), '/project', True)]},
)
agent = Agent('anthropic:claude-opus-5-5', capabilities=[sandbox, Coder()])
result = agent.run_sync('Summarize what the code in /project does.')
Other common ones:
ports, to reach a server the agent starts.secrets, for BoxLite to add credentials to the box’s HTTPS requests to the hosts you name.disk_size_gbanduser.
BoxLiteSandbox sets image, cpus, memory_mib, network, working_dir, env, auto_delete and the deprecated auto_remove itself. Passing one of those in box_options raises UserError.
Runtime-wide settings, such as private registry logins or BoxLite’s home directory, go to boxlite.Boxlite.init_default(boxlite.Options(...)), called before the first box is used.
Pass one of these as workspace=:
BoxLiteSandboxBackend(sandbox=box), with aboxlite.Boxfromboxlite.Boxlite.default().BoxLiteSandboxBackend(ref=WorkspaceRef(provider='boxlite', id=...)), with the box’s name, or its id if it has none.
Give the backend its own working_dir= and env=. The backend never deletes the box.
The VM is the boundary. It has its own kernel and filesystem. Unless box_options adds volumes or ports, nothing from this machine is mounted into it and no ports are published. None of your environment variables reach it. Commands run as the image’s user, root on most images, so anything the agent runs can change anything in the box.
- The BoxLite runtime runs in your process with your OS user’s permissions, and keeps its state in its home directory.
imageis pulled by tag; pin a digest, such aspython:3.12-slim@sha256:..., to get the same image every time.cpusandmemory_mbcap one box. Nothing limits how many boxes a process creates.
Whoever passes a ref runs commands in the box it names. BoxLite can open any box on this machine by name, including ones other tools made with your folders mounted. So:
- Refs reaching
BoxLiteSandbox(message history,workspace=ref,backend(ref),destroy(ref)) only open boxesBoxLiteSandboxcreated, namedpydantic-ai-and 32 hex digits. Any other name raisesValueError. Durable execution rebuilds the workspace from its ref, so it only works with boxesBoxLiteSandboxcreated. BoxLiteSandboxBackend(...)built by your code opens any box of this process’s default BoxLite runtime, because your code chose it. A box from another runtime raisesUserError. A later run given only its message history raisesValueError; pass the backend again, orworkspace='new'.- The name doesn’t prove origin: any program running as your OS user can create a box with such a name. Only pass refs your application stored itself, and don’t build
BoxLiteSandboxBackend(ref=...)from user input.
| Option | What it does |
|---|---|
image | OCI image for a new box. Default: None, BoxLite’s default, Alpine Linux, or debian:bookworm-slim with desktop. |
cpus | vCPUs for a new box. Default: None, BoxLite’s default. |
memory_mb | Memory for a new box in MiB. Default: None, BoxLite’s default. |
allow_internet_access | Whether a new box has outbound network access, from this machine’s network. Default: False. |
allowed_hosts | With allow_internet_access, the only hostnames, IP addresses and CIDR ranges a new box can connect to. Default: None, any. |
working_dir | Absolute directory commands start in and relative paths resolve against, created if missing. Default: /workspace. None uses the box’s own, / on Alpine. |
env | Environment variables every command gets, on top of the image’s. Default: None. Nothing from your machine’s environment reaches the box. The agent’s commands can read these, so leave out secrets it shouldn’t see. |
authority | Who wrote the box’s files: 'none' for files nobody on your side wrote, such as a cloned third-party repository, or 'user' for the user’s own. Capabilities that put a file into the prompt give it no more authority than this. Default: 'operator', files you wrote. |
path_authority | An authority level for specific directories and files, overriding authority under them. The longest matching path wins. Paths are box paths, relative to the working directory or absolute, and ~ isn’t expanded. Default: None. |
box_options | Other boxlite.BoxOptions arguments for a new box, such as volumes, ports, secrets or disk_size_gb. Default: None. |
desktop | Give the box a graphical display for ComputerUse: True for 1280x800, or its (width, height). The size applies when the display first starts. Default: False. |
image, cpus, memory_mb, the network settings and box_options only shape a new box. working_dir and env apply to every command, including after you reattach.
- On an unsupported platform, or Python 3.15, importing
pydantic_ai_harness.boxlite_sandboxraisesImportError. - A host that can’t run VMs raises
WorkspaceUnavailableErroron first use. On Linux, checkls -l /dev/kvmfirst. - On Ubuntu 24.04, AppArmor stops BoxLite’s VM engine from starting until you install
bubblewrapand the AppArmor profile from BoxLite’s troubleshooting guide. - Files move 64 KiB per command, so large files are slow.
- Images need the usual file utilities, which BusyBox has.
- A command whose output passes 10 MiB is stopped with
WorkspaceOutputLimitError. Redirect large output to a file. - On a timeout, the command and the processes it started are killed. Jobs that outlive their command, like a Shell background job, keep running. In an image without
setsid, only the command itself is killed. - The BoxLite SDK needs asyncio. Under Trio, operations raise
UserError.
BoxLiteSandbox emits no spans of its own. Core’s instrumentation records pydantic_ai.workspace.provider (boxlite) and pydantic_ai.workspace.id on the agent run span, and each command or file operation runs inside its tool call’s span.
Bases: AbstractCapability[AgentDepsT]
Supply a BoxLite micro-VM on this machine as the run’s workspace.
A run with no reference creates a fresh box on first use. Pass a WorkspaceRef supplied by the
application to attach to a box from an earlier run; only ids in the form BoxLiteSandbox names its boxes are accepted.
This capability supplies execution only. Compose it with tools or capabilities that use the
workspace, such as Coder, Shell, or FileSystem.
OCI image a new box runs, such as 'python:3.12-slim'; BoxLite’s default, Alpine Linux, when None, unless
desktop is set.
Type: str | None Default: None
vCPUs for a new box; BoxLite’s default when None.
Type: int | None Default: None
Memory for a new box in MiB; BoxLite’s default when None.
Type: int | None Default: None
Whether a new box gets outbound network access, made from this machine’s network: the internet, but also its local network and services.
Type: bool Default: False
With allow_internet_access, the only hosts, IPs or CIDRs a new box can connect to, such as ['pypi.org', 'files.pythonhosted.org']; None allows any.
Type: Sequence[str] | None Default: None
Absolute directory commands start in and relative paths resolve against, created if missing; None uses the box’s own.
Type: str | None Default: '/workspace'
Environment variables every command gets, also on an attached box; nothing is read from the host.
Type: Mapping[str, str] | None Default: field(default=None, repr=False)
Other keyword arguments for a new box’s boxlite.BoxOptions, such as volumes, ports, secrets or disk_size_gb.
The settings above own image, cpus, memory_mib, network, working_dir, env and auto_delete (and
its deprecated auto_remove), so passing one of those here raises UserError.
Type: Mapping[str, Any] | None Default: field(default=None, repr=False)
Give the box a graphical display, for ComputerUse: True for
1280x800, or its (width, height). Without an image, a new box runs debian:bookworm-slim, and the first start
installs the display programs, which needs allow_internet_access.
Type: bool | tuple[int, int] Default: False
The authority of the sandbox’s files: 'none' for files nobody on the
application’s side wrote, such as a cloned third-party repository, and 'user' for the user’s own files.
Anything below 'operator' wraps the workspace in an AuthorityWorkspace.
Type: Authority Default: 'operator'
Levels for some directories and files, overriding authority for the paths under them; the longest matching
path wins. Paths are sandbox paths: relative ones resolve against the working directory, and ~ isn’t expanded.
See Workspace.authority_of.
Type: Mapping[str, Authority] | None Default: None
def backend(ref: WorkspaceRef) -> BoxLiteSandboxBackend
Attach lazily to an existing box, one BoxLiteSandbox created.
BoxLiteSandboxBackend
@async
def destroy(ref: WorkspaceRef) -> None
Delete a box by ref; one that is already gone returns quietly.
It waits for a creation of the box still in progress in this process. Once the deletion has started, a cancelled caller waits for it to end.
def get_workspace(
ctx: RunContext[AgentDepsT],
*,
ref: WorkspaceRef | None,
) -> WorkspaceBackend | None
The backend for this run, in an AuthorityWorkspace when authority or path_authority asks for one.
WorkspaceBackend | None
Bases: WorkspaceBackend, SupportsCommands, SupportsDisplay
A local BoxLite micro-VM as a Pydantic AI WorkspaceBackend.
Commands run inside a VM on this machine, with its own kernel and filesystem, so they can’t
touch the host’s files or processes. File operations run as shell commands in the box (see
Writing a backend),
so a custom image needs /bin/sh and the usual file utilities.
Building one does no I/O. The first operation creates and boots a new box, or boots the one sandbox
is or ref names, and get_sandbox() returns the SDK’s boxlite.Box. ref is set as soon as a
creation starts; a box the backend creates is named pydantic-ai- and 32 hex digits. The backend
never deletes the box; whoever holds the ref does. The box stops when this process lets go of it or
exits, keeping its disk, and the next use of the ref boots it again.
sandbox : Any Default: None
A boxlite.Box you already have. Whoever created it owns deleting it.
ref : WorkspaceRef | None Default: None
Identity of an existing box to attach to on first use: its name, or its id if it has none.
OCI image a new box runs, such as 'python:3.12-slim'; BoxLite’s default, Alpine Linux, when
None, unless desktop is set.
vCPUs for a new box; BoxLite’s default when None.
Memory for a new box in MiB; BoxLite’s default when None.
allow_internet_access : bool Default: False
Whether a new box gets outbound network access, made from this machine’s network: the internet, but also its local network and services.
With allow_internet_access, the only hosts, IPs or CIDRs a new box can connect to;
None allows any.
Absolute directory commands start in and relative paths resolve against, created
if missing when the backend first reaches the box; None uses the box’s own.
Environment variables every command gets; per-command env is layered on top. Nothing is
read from the host environment.
Other keyword arguments for a new box’s boxlite.BoxOptions, such as volumes,
ports, secrets or disk_size_gb. The ones the arguments above set are refused.
Whether the box has a display for ComputerUse:
True for 1280x800, or its (width, height). The first start installs the programs it needs, which
needs allow_internet_access, with allowed_hosts reaching the Debian mirrors. Without an image, a new
box runs debian:bookworm-slim.
Identity of the box, or None before one starts being created.
Set before the creation finishes, so cleanup that runs while it is still in progress can pass it to
BoxLiteSandbox.destroy, which waits for it.
Type: WorkspaceRef | None
The box’s desktop, when created with desktop=True.
Type: Display | None
@async
def get_sandbox() -> Any
Return the SDK’s boxlite.Box, for BoxLite features the workspace API does not cover.
On a backend with no box yet, this creates one or attaches to the one ref names, just like the
first operation. Attaching to a deleted box raises WorkspaceUnavailableError; it does not
create a replacement.
@async
def working_dir() -> str
The canonical absolute directory commands start in.
@async
def run(
command: WorkspaceCommand,
*,
shell: bool = False,
env: Mapping[str, str] | None = None,
timeout: float | None = None,
) -> CommandResult
Run a command in the box, stopping it and the processes it started on timeout or cancellation.
CommandResult