The SDK ships two clients with identical methods: SandboxClient for synchronous scripts and AsyncSandboxClient for concurrent workloads. Both are importable from prime_sandboxes.
Sync Client
SandboxClient is the simplest way to get started — no async/await needed.
Pass idempotency_key to CreateSandboxRequest to make create retries safe: requests that share the same key are deduplicated, so a network failure mid-create can be re-run without producing a second sandbox.
Every method shown in the async sections below has an identical synchronous counterpart on SandboxClient.
Async Client
Most sandbox automations spin up more than one environment. The async client lets you fan out creates, waits, commands, and teardown without juggling threads.
Launch a Fleet
bulk_wait_for_creation polls via the list endpoint, backing off automatically if the API throttles you.
Run Commands
Command responses include stdout, stderr, and exit code so you can short-circuit pipelines when something breaks.
Move Data In and Out
Note: File uploads are limited to 16 MiB per file. Reads are also size-limited; use download_file for large files.
Uploads/downloads use short-lived gateway tokens stored in a local cache. Call sandboxes.clear_auth_cache() if you rotate credentials or hit 401s.
Start Command
Pass start_command to run your own process when the sandbox boots. There is no shell, so the executable and each argument are separate values:
To get shell features (pipes, variables, &&), invoke a shell explicitly:
The start command is one-shot: when the process exits, the sandbox stops. Omit it to keep the sandbox up and ready for execute_command calls.
Environment Variables & Secrets
Pass configuration and credentials when creating a sandbox:
Environment variables are stored in plain text. Secrets are encrypted at rest and never returned in API responses — use them for API keys, passwords, and other sensitive values. Both are injected into the sandbox as standard environment variables.
You can also pass per-command environment variables to execute_command:
Network Isolation
Sandboxes can restrict outbound traffic at creation time. Use an allowlist or a denylist — the two are mutually exclusive.
Allow only specific destinations with network_allowlist=["api.openai.com"], or block specific destinations and allow the rest with network_denylist=["evil.example.com"]. Entries are domains (example.com, *.example.com) or IPv4 addresses/CIDRs; IPv6 is not supported.
Long-Running Commands
execute_command accepts a timeout parameter in seconds (default 300):
For tasks that should survive the current connection, use background jobs
instead. They keep running in the sandbox while you poll for completion.
Background Jobs
Use start_background_job for tasks that run independently of the command connection. The job continues running in the sandbox while you poll for completion.
The timeout_minutes parameter controls how long the sandbox stays alive. For VM sandboxes, any negative value removes the lifetime deadline. Background jobs persist across API calls until completion or sandbox termination.
Idle Timeout
Set idle_timeout_minutes on CreateSandboxRequest to have the sandbox terminate itself when nothing has touched it for a while:
Any execute_command, upload_file, download_file, or file-read call resets the idle clock. Long-running execs stay pinned for their full duration.
- Disabled by default. Omit the field to keep the legacy lifetime-only behavior.
- Must be between 1 and 1,440 minutes. For a finite lifetime, it must not exceed
timeout_minutes.
- SSH sessions do not count as activity.
When a sandbox shuts down for this reason, the response object exposes termination_reason="idle_timeout".
Error Handling
The SDK raises typed exceptions so you can handle specific failure modes. All exceptions are importable directly from prime_sandboxes.
Sandbox Lifecycle Errors
These are raised when a sandbox is no longer in RUNNING state. They form a hierarchy — catch the base class for broad handling, or specific subclasses for targeted recovery.
SandboxOOMError, SandboxTimeoutError, and SandboxImagePullError are all subclasses of SandboxNotRunningError.
Operation Errors
Raised during specific operations when the sandbox is still running but the operation itself fails.
API Errors
Raised for HTTP-level failures when communicating with the platform API.
Clean Exit
Delete a single sandbox or use bulk_delete to tear down many at once by IDs or labels:
You must pass either sandbox_ids or labels, not both.
For a full script, see prime/examples/sandbox_async_demo.py, which covers create → wait → run → delete.