Skip to main content
The Python SDK ships an async-native variant of the sandbox API on top of asyncio. Every method on the sync Sandbox handle has a one-to-one async counterpart on AsyncSandbox: same names, same parameters, just async def and awaited.

When to use it

Reach for the async API when:
  • You’re driving multiple sandboxes concurrently (e.g. fanning out work with asyncio.gather).
  • Your application is already asyncio-based (FastAPI, aiohttp, an LLM agent loop, etc.) and you don’t want to mix in blocking calls.
  • You’re streaming output from many processes at once.
If you only ever use one sandbox at a time and your code is otherwise synchronous, the sync Sandbox API is simpler and equivalent.

The shape of the API

AsyncSandbox is the runtime handle for a single sandbox. Use await AsyncSandbox.create(...) to provision and connect, await AsyncSandbox.connect(sandbox_id) to attach to an existing one, or await AsyncSandbox.get_or_create(name, ...) to reuse a named sandbox and create it on first use. Every instance method is awaited:
Refer to the SDK Reference for the full method list; the names, parameters, and return types are identical to the sync API. The pages below walk through the same workflow with async syntax.

Create and run

AsyncSandbox is also an async context manager. Use async with to terminate the sandbox automatically when the block exits:

Run many sandboxes in parallel

The async API is designed for fan-out. Use asyncio.gather to start and run sandboxes concurrently:
Each evaluate call creates, executes against, and terminates its own sandbox in parallel with the others.

Get or create a named sandbox

Use AsyncSandbox.get_or_create() when concurrent tasks may request the same named sandbox. The method creates the sandbox on first use, connects to it on later calls, resolves concurrent create conflicts, and resumes it when suspended.
Creation options apply only when the name is free. Pass resume=False to return an existing suspended sandbox without resuming it; call await sandbox.resume() before running commands against that handle. See Get or create in the SDK Reference for the complete behavior and limitations.

Connect to an existing sandbox

Reattach to a named sandbox after resume, or operate on a sandbox another process created:
Unlike the sync Sandbox.sandbox_id property, which transparently fetches sandbox info on first access, the async AsyncSandbox.sandbox_id cannot block on a network call. Call await sandbox.info() (or any other awaited method that resolves the sandbox, like status()) once before reading sandbox.sandbox_id on a freshly connected handle.

Background processes and streaming output

Start a process, keep the handle, and collect its output once it finishes:
For long-running processes you want to stop yourself, send a signal directly. Don’t follow_output first, since it would block waiting for the process to exit:

File operations

Suspend, resume, and snapshot

Suspend and resume require a named sandbox, so pass name= at creation time. checkpoint works on any sandbox, including ephemeral ones.

Learn more

SDK Reference

Full method list that applies to both sync and async APIs.

Lifecycle

State machine, suspend/resume, timeouts.

Commands & Processes

Run commands, capture output, and manage background processes.

Snapshots

Capture and restore full VM state.