Static methods
Sandbox.create()
str) or a full config (dict). Keyword arguments provide individual config fields. Pulls the image if needed, boots the VM, starts the guest agent, and waits until it is ready to accept commands.
Parameters
| Name | Type | Description |
|---|---|---|
| name_or_config | str | dict | Sandbox name or full configuration dict |
| image | str | ImageSource | OCI image, local path, or disk image (required) |
| cpus | int | Virtual CPUs (default 1) |
| memory | int | Guest memory in MiB (default 512) |
| workdir | str | Default working directory for commands |
| shell | str | Shell for shell() calls (default "/bin/sh") |
| hostname | str | Guest hostname |
| user | str | Default guest user |
| entrypoint | list[str] | Override image entrypoint |
| replace | bool | Replace existing sandbox with same name |
| max_duration | float | Maximum sandbox lifetime in seconds |
| idle_timeout | float | Idle timeout in seconds |
| env | dict[str, str] | Environment variables visible to all commands |
| scripts | dict[str, str] | Named scripts mounted at /.msb/scripts/ |
| pull_policy | str | PullPolicy | Image pull behavior |
| log_level | str | LogLevel | Override log verbosity |
| registry_auth | RegistryAuth | Private registry credentials |
| volumes | dict[str, dict] | Volume mounts. See Volumes. |
| patches | list[PatchConfig] | Rootfs modifications applied before boot |
| ports | dict[int, int] | Port mappings (host_port: guest_port) |
| network | Network | Network policy and configuration |
| secrets | list[SecretEntry] | Secret injection |
| detached | bool | If True, sandbox survives after your process exits |
| Type | Description |
|---|---|
Sandbox | Running sandbox |
Sandbox.create_with_progress()
Sandbox.create() but returns a PullSession that lets you track image pull progress before the sandbox is ready. This method is synchronous (not awaitable) — the async work happens through the PullSession.
Parameters
Same as Sandbox.create().
Returns
| Type | Description |
|---|---|
PullSession | Session for tracking pull progress and obtaining the final sandbox |
Sandbox.start()
| Name | Type | Description |
|---|---|---|
| name | str | Name of a stopped sandbox |
| detached | bool | If True, sandbox survives after your process exits (default False) |
| Type | Description |
|---|---|
Sandbox | Running sandbox |
Sandbox.get()
| Name | Type | Description |
|---|---|---|
| name | str | Sandbox name |
| Type | Description |
|---|---|
SandboxHandle | Handle with status and lifecycle control |
Sandbox.list()
| Type | Description |
|---|---|
list[SandboxHandle] | All sandboxes |
Sandbox.remove()
| Name | Type | Description |
|---|---|---|
| name | str | Sandbox name |
Instance properties
name
await sb.name.
owns_lifecycle
True in attached mode, False in detached mode. This is an async property — use await sb.owns_lifecycle.
fs
sb.fs (no await). See Filesystem for the full API.
Returns
| Type | Description |
|---|---|
SandboxFs | Filesystem handle |
Instance methods
attach()
| Name | Type | Description |
|---|---|---|
| cmd | str | Command to run |
| args_or_options | list[str] | ExecOptions | None | Command arguments or execution options |
| Type | Description |
|---|---|
int | Exit code of the process |
attach_shell()
| Type | Description |
|---|---|
int | Exit code |
detach()
Sandbox.get().
drain()
exec calls are rejected.
exec()
exec_stream() instead.
Parameters
| Name | Type | Description |
|---|---|---|
| cmd | str | Command to execute (e.g. "python", "/usr/bin/node") |
| args_or_options | list[str] | ExecOptions | None | Command arguments (e.g. ["-c", "print('hello')"]) or ExecOptions |
| Type | Description |
|---|---|
ExecOutput | Collected stdout, stderr, and exit status |
exec_stream()
| Name | Type | Description |
|---|---|---|
| cmd | str | Command to execute |
| args_or_options | list[str] | ExecOptions | None | Command arguments or ExecOptions |
| Type | Description |
|---|---|
ExecHandle | Streaming handle for receiving events and controlling the process |
kill()
metrics()
| Type | Description |
|---|---|
SandboxMetrics | CPU, memory, disk, network metrics |
metrics_stream()
recv() and async for.
Parameters
| Name | Type | Description |
|---|---|---|
| interval | float | Seconds between metric snapshots (default 1.0) |
| Type | Description |
|---|---|
MetricsStream | Async stream of metrics |
remove_persisted()
shell()
/bin/sh). Shell syntax like pipes, redirects, and && chains works.
Parameters
| Name | Type | Description |
|---|---|---|
| script | str | Shell command string (e.g. "ls -la /app && echo done") |
| Type | Description |
|---|---|
ExecOutput | Collected stdout, stderr, and exit status |
shell_stream()
| Name | Type | Description |
|---|---|---|
| script | str | Shell command string |
| Type | Description |
|---|---|
ExecHandle | Streaming handle |
stop()
stop_and_wait()
| Type | Description |
|---|---|
tuple[int, bool] | (exit_code, success) — success is True if exit code is 0 |
wait()
| Type | Description |
|---|---|
tuple[int, bool] | (exit_code, success) — success is True if exit code is 0 |
Context manager
Sandbox as an async context manager to ensure cleanup. On exit, the sandbox is killed and its persisted state is removed.
Types
LogLevel
Sandbox process log verbosity.| Value | Description |
|---|---|
"debug" | Debug and higher |
"error" | Errors only |
"info" | Info and higher |
"trace" | Most verbose — all diagnostic output |
"warn" | Warnings and errors only |
MetricsStream
Async stream for receiving periodic metrics snapshots.| Method | Returns | Description |
|---|---|---|
__aiter__ | AsyncIterator[SandboxMetrics] | Use with async for |
recv() | SandboxMetrics | None | Receive next snapshot. Returns None when the stream ends. |
PullPolicy
Controls when the SDK fetches an OCI image from the registry.| Value | Description |
|---|---|
"always" | Pull the image every time, even if cached locally |
"if-missing" | Pull only if the image is not already cached. This is the default. |
"never" | Never pull; fail if the image is not cached locally |
PullSession
Returned bySandbox.create_with_progress(). Use as an async context manager to track image pull progress.
| Property / Method | Type | Description |
|---|---|---|
| progress | AsyncIterator[PullProgress] | Async iterator of pull progress events |
| result() | Awaitable[Sandbox] | Await to get the final running sandbox |
RegistryAuth
Private registry credentials.| Field | Type | Description |
|---|---|---|
| username | str | Registry username |
| password | str | Registry password |
SandboxConfig
The keyword arguments accepted bySandbox.create() and Sandbox.create_with_progress().
| Field | Type | Default | Description |
|---|---|---|---|
| image | str | ImageSource | - | OCI image, local path, or disk image (required) |
| cpus | int | 1 | Virtual CPUs |
| memory | int | 512 | Guest memory in MiB. This is a limit, not a reservation. |
| workdir | str | - | Default working directory for commands |
| shell | str | "/bin/sh" | Shell for shell() calls |
| hostname | str | - | Guest hostname |
| user | str | - | Default guest user |
| entrypoint | list[str] | - | Override image entrypoint |
| replace | bool | False | Replace existing sandbox with same name |
| max_duration | float | - | Maximum sandbox lifetime in seconds |
| idle_timeout | float | - | Idle timeout in seconds |
| env | dict[str, str] | {} | Environment variables visible to all commands |
| scripts | dict[str, str] | {} | Named scripts mounted at /.msb/scripts/ |
| pull_policy | str | PullPolicy | "if-missing" | Image pull behavior |
| log_level | str | LogLevel | - | Override log verbosity |
| registry_auth | RegistryAuth | - | Private registry credentials |
| volumes | dict[str, dict] | {} | Volume mounts. See Volumes. |
| patches | list[PatchConfig] | [] | Rootfs modifications applied before boot |
| ports | dict[int, int] | {} | Port mappings (host_port: guest_port) |
| network | Network | public_only | Network policy and configuration |
| secrets | list[SecretEntry] | [] | Secret injection |
| detached | bool | False | If True, sandbox survives after your process exits |
SandboxHandle
A lightweight handle to an existing sandbox (running or stopped). Obtained viaSandbox.get() or Sandbox.list(). Provides status, configuration, and lifecycle control without an active connection to the guest agent. Call .start() or .connect() to upgrade to a full Sandbox.
| Property / Method | Type | Description |
|---|---|---|
| name | str | Sandbox name |
| status | str | Current status ("running", "stopped", "crashed", "draining", "paused") |
| config_json | str | Raw JSON configuration |
| created_at | float | None | Creation timestamp (ms since epoch) |
| updated_at | float | None | Last update timestamp (ms since epoch) |
| connect() | Awaitable[Sandbox] | Connect to a running sandbox |
| start(*, detached=False) | Awaitable[Sandbox] | Start in attached or detached mode |
| stop() | Awaitable[None] | Graceful shutdown |
| kill() | Awaitable[None] | Force terminate |
| remove() | Awaitable[None] | Delete sandbox and state |
| metrics() | Awaitable[SandboxMetrics] | Point-in-time resource metrics |
SandboxMetrics
Point-in-time resource usage snapshot.| Field | Type | Description |
|---|---|---|
| cpu_percent | float | CPU usage as a percentage |
| disk_read_bytes | int | Total bytes read from disk since boot |
| disk_write_bytes | int | Total bytes written to disk since boot |
| memory_bytes | int | Current memory usage in bytes |
| memory_limit_bytes | int | Memory limit in bytes |
| net_rx_bytes | int | Total bytes received over the network since boot |
| net_tx_bytes | int | Total bytes sent over the network since boot |
| timestamp_ms | float | When this measurement was taken (ms since epoch) |
| uptime_ms | int | Time since the sandbox was created (ms) |