Skip to main content
Every SmolVM sandbox follows a simple lifecycle: you create it, start it, run your work, and tear it down. Most of the time the context manager (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 started
  • RUNNING - VM is booted and operational
  • PAUSED - VM execution is suspended (Firecracker only)
  • STOPPED - VM has been gracefully shut down
  • ERROR - 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 call start(), SmolVM:
  1. Launches the Firecracker/QEMU process
  2. Boots the kernel with configured boot arguments
  3. Waits for the VM to become responsive
  4. Injects environment variables if configured (see env_vars in 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:
The stop() method:
  • Sends a shutdown signal to the guest
  • Waits up to timeout seconds 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:
Deletion is permanent. The VM cannot be recovered after deletion.

Context manager pattern

The recommended way to manage the lifecycle is with Python’s with statement:

Context manager behavior

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 like status 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.
See the Snapshots guide for full details.

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
Last modified on May 5, 2026