AsyncSandbox is the async counterpart of Sandbox. Every method is a coroutine (async def) and must be awaited. It is the preferred choice when:
- You need to create or manage multiple sandboxes concurrently with
asyncio.gather. - Your application is built on an async framework (FastAPI, LangGraph, LiteLLM, etc.).
- You want to stream command output using
async fororasyncio.Queue.
When to use async
Class methods
AsyncSandbox.create()
Create a new sandbox and return a connected AsyncSandbox instance.
Sandbox.create().
Returns AsyncSandbox
AsyncSandbox.connect()
Connect to an existing sandbox by ID.
Sandbox.connect().
Returns AsyncSandbox
Instance methods
await sbx.kill()
Kill and destroy the sandbox.
bool
await sbx.is_running()
Check whether the sandbox is in the running state.
bool
await sbx.set_timeout()
Update the sandbox timeout.
New timeout in seconds.
None
await sbx.get_info()
Fetch the current metadata and state.
SandboxInfo
await sbx.get_metrics()
Retrieve resource usage metrics for a time range.
list[SandboxMetrics]
await sbx.pause()
Pause the sandbox.
None
await sbx.create_snapshot()
Create a sandbox snapshot.
SnapshotInfo
await sbx.snapshot()
Create a manual snapshot of this sandbox. Manual snapshots accumulate — every
call creates a new persistent checkpoint that survives sbx.kill(). Use
AsyncSandbox.restore() or
sbx.list_snapshots() to retrieve and fork from
them.
Per-request HTTP timeout in seconds.
Snapshot
await sbx.list_snapshots()
List all snapshots (periodic, pause, and manual) for this sandbox, newest first.
Per-request HTTP timeout in seconds.
list[Snapshot]
await AsyncSandbox.restore()
Restore a sandbox from a snapshot. The restored sandbox may run on a different
worker than the original. Returns a usable AsyncSandbox instance already
connected to the restored sandbox.
The sandbox to restore.
Specific snapshot to restore from. If omitted, the most recent snapshot is
used (preference order: pause > periodic > manual).
API key override.
Domain override.
Per-request HTTP timeout in seconds.
AsyncSandbox
Command methods (inline on AsyncSandbox)
Unlike the synchronousSandbox, AsyncSandbox exposes command operations directly as methods rather than through a sub-module:
await sbx.run_command()
Run a command and return its result (or a handle if background=True).
Shell command to execute.
When
True, returns an AsyncCommandHandle immediately without waiting for
the command to finish.Environment variables for the command.
Unix user to run the command as.
Working directory for the command.
Callback invoked for each stdout line after completion (foreground only).
Callback invoked for each stderr line after completion (foreground only).
Command execution timeout in seconds.
CommandResult | AsyncCommandHandle
await sbx.list_commands()
List all running processes in the sandbox.
list[ProcessInfo]
await sbx.kill_command()
Kill a running command by PID.
Process ID to kill.
bool
Filesystem methods (inline on AsyncSandbox)
await sbx.read_file()
Read a file’s content.
Absolute path inside the sandbox.
"text" returns a str; "bytes" returns a bytearray.Unix user context.
str | bytearray
await sbx.write_file()
Write content to a file.
Absolute path inside the sandbox.
Content to write.
WriteInfo
await sbx.list_files()
List directory entries.
Directory path.
Recursion depth.
1 lists only the immediate directory.list[EntryInfo]
Async context manager
__aexit__ calls await sbx.close() which releases the HTTP client. It does not kill the sandbox — call sbx.kill() explicitly.
AsyncCommandHandle
Returned bysbx.run_command(background=True). Allows you to wait for or kill the background process.
| Method | Returns | Description |
|---|---|---|
await handle.wait(on_stdout, on_stderr) | CommandResult | Wait for completion. Raises CommandExitException on non-zero exit. |
await handle.kill() | bool | Send SIGKILL to the process. |
handle.pid | int | Process ID. |