Skip to main content

Prime Sandboxes SDK

Lightweight Python SDK for managing Prime Intellect sandboxes - secure remote code execution environments.

Features

  • Synchronous and async clients - Use with sync or async/await code
  • Full sandbox lifecycle - Create, list, execute commands, upload/download files, delete
  • Type-safe - Full type hints and Pydantic models
  • Authentication caching - Automatic token management
  • Bulk operations - Create and manage multiple sandboxes efficiently
  • No CLI dependencies - Pure SDK, ~50KB installed

Installation

uv pip install prime-sandboxes

Or with pip:

pip install prime-sandboxes

Quick Start

from prime_sandboxes import APIClient, SandboxClient, CreateSandboxRequest, StartCommand

# Initialize
client = APIClient(api_key="your-api-key")
sandbox_client = SandboxClient(client)

# Create a sandbox. Leaving `vm` unset uses the platform default runtime:
# VM-backed sandboxes (public beta).
request = CreateSandboxRequest(
    name="my-sandbox",
    docker_image="python:3.11-slim",
    cpu_cores=2,
    memory_gb=4,
)

sandbox = sandbox_client.create(request)
print(f"Created: {sandbox.id}")

# VM workloads use a structured argv contract; no shell is implied.
vm = sandbox_client.create(CreateSandboxRequest(
    name="vm-workload",
    docker_image="user-1/vm-image:latest",
    vm=True,
    start_command=StartCommand(
        executable="/worker",
        args=["--platform", "linux/amd64"],
    ),
))

# Opt out to a container sandbox explicitly with `vm=False` (containers
# support string start commands, SSH, and port exposure).
container = sandbox_client.create(CreateSandboxRequest(
    name="container-workload",
    docker_image="python:3.11-slim",
    vm=False,
    start_command="python -m http.server 8080",
))

# Wait for it to be ready
sandbox_client.wait_for_creation(sandbox.id)

# Execute commands
result = sandbox_client.execute_command(sandbox.id, "python --version")
print(result.stdout)

# Clean up
sandbox_client.delete(sandbox.id)

Async Usage

import asyncio
from prime_sandboxes import AsyncSandboxClient, CreateSandboxRequest

async def main():
    async with AsyncSandboxClient(api_key="your-api-key") as client:
        # Create sandbox
        sandbox = await client.create(CreateSandboxRequest(
            name="async-sandbox",
            docker_image="python:3.11-slim",
        ))

        # Wait and execute
        await client.wait_for_creation(sandbox.id)
        result = await client.execute_command(sandbox.id, "echo 'Hello from async!'")
        print(result.stdout)

        # Clean up
        await client.delete(sandbox.id)

asyncio.run(main())

List Platform Images

Use a platform admin or manager key with sandbox-read access to list platform images:

from prime_sandboxes import ImageArtifactType, ImageBuildStatus, ImageClient

page = ImageClient().list(platform=True)
vm_images = [
    image.display_ref
    for image in page.data
    if image.artifact_type == ImageArtifactType.VM_SANDBOX
    and image.status == ImageBuildStatus.COMPLETED
]

Authentication

The SDK looks for credentials in this order:

  1. Direct parameter: APIClient(api_key="sk-...")
  2. Environment variable: export PRIME_API_KEY="sk-..."
  3. Config file: ~/.prime/config.json (created by prime login CLI command)

Advanced Features

Environment Variables and Secrets

# Create sandbox with environment variables and secrets
request = CreateSandboxRequest(
    name="my-sandbox",
    docker_image="python:3.11-slim",
    environment_vars={
        "DEBUG": "true",
        "LOG_LEVEL": "info"
    },
    secrets={
        "API_KEY": "sk-secret-key-here",
        "DATABASE_PASSWORD": "super-secret-password"
    }
)

sandbox = sandbox_client.create(request)

Note: Secrets are never displayed in logs or outputs. When retrieving sandbox details, only the secret keys are shown with values masked as ***.

File Operations

# Upload a file
sandbox_client.upload_file(
    sandbox_id=sandbox.id,
    file_path="/app/script.py",
    local_file_path="./local_script.py"
)

# Download a file
sandbox_client.download_file(
    sandbox_id=sandbox.id,
    file_path="/app/output.txt",
    local_file_path="./output.txt"
)

Bulk Operations

# Create multiple sandboxes
sandbox_ids = []
for i in range(5):
    sandbox = sandbox_client.create(CreateSandboxRequest(
        name=f"sandbox-{i}",
        docker_image="python:3.11-slim",
    ))
    sandbox_ids.append(sandbox.id)

# Wait for up to 100 sandboxes with one batched lifecycle-status request per poll
statuses = sandbox_client.bulk_wait_for_creation(sandbox_ids)

# Delete by IDs or labels
sandbox_client.bulk_delete(sandbox_ids=sandbox_ids)
# OR by labels
sandbox_client.bulk_delete(labels=["experiment-1"])

Labels & Filtering

# Create with labels
sandbox = sandbox_client.create(CreateSandboxRequest(
    name="labeled-sandbox",
    docker_image="python:3.11-slim",
    labels=["experiment", "ml-training"],
))

# List with filters
sandboxes = sandbox_client.list(
    status="RUNNING",
    labels=["experiment"],
    page=1,
    per_page=50,
)

for s in sandboxes.sandboxes:
    print(f"{s.name}: {s.status}")

Long-Running Tasks

Use start_background_job to run long-running tasks that continue after the API call returns. Poll for completion with get_background_job.

from prime_sandboxes import APIClient, SandboxClient, CreateSandboxRequest

sandbox_client = SandboxClient(APIClient())

# Create sandbox with extended timeout
sandbox = sandbox_client.create(CreateSandboxRequest(
    name="training-job",
    docker_image="python:3.11-slim",
    timeout_minutes=1440,  # 24 hours
    cpu_cores=4,
    memory_gb=16,
))
sandbox_client.wait_for_creation(sandbox.id)

# Start a long-running job in the background
job = sandbox_client.start_background_job(
    sandbox.id,
    "python train.py --epochs 100"
)
print(f"Job started: {job.job_id}")

# VM sandboxes can check up to 100 SDK-started jobs across sandboxes with one
# platform request. Results preserve input order; completed jobs include the
# same bounded stdout/stderr tails as get_background_job().
statuses = sandbox_client.get_background_jobs([job])

# Poll for completion
import time
while True:
    status = sandbox_client.get_background_job(sandbox.id, job)
    if status.completed:
        print(f"Job finished with exit code: {status.exit_code}")
        print(status.stdout)
        break
    print("Still running...")
    time.sleep(30)

# Download results
sandbox_client.download_file(sandbox.id, "/app/model.pt", "./model.pt")

get_background_jobs is VM-only. Container sandboxes retain the existing get_background_job polling behavior.

Async version

import asyncio
from prime_sandboxes import AsyncSandboxClient, CreateSandboxRequest

async def run_training():
    async with AsyncSandboxClient() as client:
        sandbox = await client.create(CreateSandboxRequest(
            name="async-training",
            docker_image="python:3.11-slim",
            timeout_minutes=720,
        ))
        await client.wait_for_creation(sandbox.id)

        # Start background job
        job = await client.start_background_job(
            sandbox.id,
            "python train.py"
        )

        # Poll until done
        while True:
            status = await client.get_background_job(sandbox.id, job)
            if status.completed:
                print(status.stdout)
                break
            await asyncio.sleep(30)

        await client.delete(sandbox.id)

asyncio.run(run_training())

Documentation

Full API reference: https://github.com/PrimeIntellect-ai/prime/tree/main/packages/prime-sandboxes

Related Packages

  • prime - Full CLI + SDK with pods, inference, and more (includes this package)

License

MIT License - see LICENSE file for details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prime_sandboxes-0.2.39.tar.gz (103.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prime_sandboxes-0.2.39-py3-none-any.whl (58.2 kB view details)

Uploaded Python 3

File details

Details for the file prime_sandboxes-0.2.39.tar.gz.

File metadata

  • Download URL: prime_sandboxes-0.2.39.tar.gz
  • Upload date:
  • Size: 103.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for prime_sandboxes-0.2.39.tar.gz
Algorithm Hash digest
SHA256 7ff6c23aa2c96985a9129d977d532c0ae2fb6afb6d394dac746393ce0c53b597
MD5 4085764a480d1266347691968dbe0ce1
BLAKE2b-256 598c0bf89180addb74f75e41fbf6fa06c5401ea802b69ec0b565330562270be2

See more details on using hashes here.

Provenance

The following attestation bundles were made for prime_sandboxes-0.2.39.tar.gz:

Publisher: release-sandboxes.yml on PrimeIntellect-ai/prime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file prime_sandboxes-0.2.39-py3-none-any.whl.

File metadata

File hashes

Hashes for prime_sandboxes-0.2.39-py3-none-any.whl
Algorithm Hash digest
SHA256 3ea355158473c1697f9ef6ec2a07f3189e9a2555c80d1c88e1f7e22979772e0a
MD5 8b86f2907d8cc3c127c0436395780915
BLAKE2b-256 dba58a2a1aefecfc58ca926d6b21e8302a127884bf2a45952ed390b5f702eeda

See more details on using hashes here.

Provenance

The following attestation bundles were made for prime_sandboxes-0.2.39-py3-none-any.whl:

Publisher: release-sandboxes.yml on PrimeIntellect-ai/prime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page