Lifecycle states
A sandbox progresses through the following states:Pending
Initial state when a sandbox is created. The runtime is pulling the image, injecting execd, and starting the container.
Running
Sandbox is fully operational and ready to accept commands, execute code, and perform file operations.
State transitions
Creating sandboxes
Sandboxes are created with theSandbox.create() method:
- Python
- JavaScript
- Kotlin
Required parameters
Docker image to use for the sandbox. Can be from Docker Hub or a private registry.
Sandbox lifetime (60 seconds to 24 hours). After this time, the sandbox is automatically terminated.
Command to run as the main process. This keeps the sandbox running.
Optional parameters
Environment variables to set in the sandbox.
CPU, memory, and GPU limits (e.g.,
{"cpu": "500m", "memory": "512Mi"}).Custom metadata for tracking and filtering sandboxes.
Egress network policy rules (allow/deny domains).
Managing sandbox lifetime
Automatic expiration
All sandboxes have a TTL (time-to-live) specified at creation. When the timeout expires, the sandbox is automatically terminated.Renewing expiration
You can extend the sandbox lifetime before it expires:Manual termination
To immediately terminate a sandbox:Pausing and resuming
You can temporarily pause a sandbox to save resources:Note: Pause/resume is currently supported in Docker runtime with host networking mode. Kubernetes runtime support is planned.
Monitoring sandbox state
Check the current state of a sandbox:Polling for readiness
When you create a sandbox, it starts in the Pending state. The SDK automatically polls until it reaches Running:Custom health checks
For sandboxes running services, you can implement custom health checks:- Python
- JavaScript
Error handling
Sandboxes can enter the Failed state due to:- Image pull failures (invalid image, authentication issues)
- Resource limit exceeded during startup
- Entrypoint command errors
- Network policy violations
Best practices
Choose appropriate timeouts
Choose appropriate timeouts
Set timeouts based on expected workload duration:
- Short tasks (< 5 min): 5-10 minutes
- Interactive sessions: 30-60 minutes
- Long-running jobs: 2-24 hours
renew_expiration() for unpredictable durations.Always clean up resources
Always clean up resources
Use context managers (Python) or try-finally blocks to ensure sandboxes are terminated:
Monitor sandbox state
Monitor sandbox state
For long-running workloads, periodically check sandbox state:
Handle transient errors
Handle transient errors
Image pulls can fail due to network issues. Implement retry logic:
Execution API
Learn how to execute commands and code in running sandboxes
API Reference
View the complete Lifecycle API reference
Resource management
Optimize resource usage and limits
Troubleshooting
Diagnose and fix common sandbox issues