with SmolVM() as vm) handles all of this for you. This guide covers the full lifecycle for cases where you need more control.
Lifecycle states
A sandbox progresses through these states:CREATED- VM configured but not startedRUNNING- VM is booted and operationalPAUSED- VM execution is suspended (Firecracker only)STOPPED- VM has been gracefully shut downERROR- VM encountered a fatal error
Creating a sandbox
1
Import SmolVM
2
Create VM instance
3
Verify VM is created
The VM is now registered in SmolVM’s state database but not yet running.
Starting a sandbox
Start the sandbox to boot the guest operating system:Boot process
When you callstart(), SmolVM:
- Launches the Firecracker/QEMU process
- Boots the kernel with configured boot arguments
- Waits for the VM to become responsive
- Injects environment variables if configured (see
env_varsin VMConfig)
Method signature
If a sandbox is already running, calling
start() is a no-op and returns immediately.Stopping a sandbox
Gracefully shut down a running sandbox:stop() method:
- Sends a shutdown signal to the guest
- Waits up to
timeoutseconds for graceful shutdown - Cleans up port forwarding rules
- Closes SSH connections
Method signature
Pausing and resuming a VM
On the Firecracker backend, you can pause a running VM and resume it later. This suspends execution without stopping the VM process, so the guest resumes exactly where it left off.Pausing cleans up port forwarding rules and closes SSH connections. After resuming, the next
run() call re-establishes SSH automatically.If you call
start() on a paused VM, it automatically calls resume() instead.Deleting a sandbox
Permanently delete a sandbox and release all resources:Context manager pattern
The recommended way to manage the lifecycle is with Python’swith statement:
Context manager behavior
- New VMs (owns_vm=True)
- Reconnected VMs (owns_vm=False)
When you create a VM with
SmolVM(config) or SmolVM():__enter__: Automatically starts the VM__exit__: Stops AND deletes the VM
Reconnecting to existing sandboxes
Reconnect to a sandbox that was created earlier:Class method signature
Manual lifecycle management
For advanced use cases where you need explicit control:Long-running sandboxes
For persistent sandboxes that survive across Python sessions:1
Create a VM with a stable ID
2
Reconnect in another session
3
Clean up when done
Waiting for SSH
You can explicitly wait for SSH to become available:You typically don’t need to call
wait_for_ssh() explicitly. The run() method automatically waits for SSH on first use.Refreshing state
Properties likestatus and info are cached. Refresh them from the state store:
Snapshots
You can save the full state of a running VM and restore it later using snapshots. This is useful for checkpointing before risky operations or reusing a configured environment.Next steps
Snapshots
Save and restore sandbox state
Port forwarding
Expose services running inside sandboxes to your host
Environment variables
Configure sandbox environment dynamically
AI agent integration
Build secure AI agent sandboxes
