The examples on this page omit
auth. To authenticate with your W&B API key, install cwsandbox[wandb] and pass auth=AuthStrategy.WANDB to Sandbox.run(), Sandbox(), or Sandbox.session(). See Get started.Session to manage multiple sandboxes with shared configuration. It’s for client developers who need to coordinate groups of sandboxes, apply common defaults, and ensure consistent cleanup across a workload.
Session overview
ASession provides:
- Shared default configuration for all sandboxes.
- Automatic cleanup when the session closes.
- A scope for the
@session.function()decorator.
Basic usage
Create sandboxes
The following sections describe how to create sandboxes through a session and override session defaults.session.sandbox()
Creates a sandbox with session defaults. The sandbox doesn’t start until first use:Override defaults
Pass additional arguments to override session defaults:Multiple sandbox management
Sessions work well for managing sandbox pools:Session lifecycle
The following sections describe how to close a session, whatclose() does, and how to handle errors.
Manual close
When you don’t use a context manager, close a session explicitly:What close() does
Callingclose() performs the following actions:
- Stops all sandboxes created through the session.
- Waits for cleanup to complete.
- Returns
OperationRef[None].
Error handling
Sessions clean up even if exceptions occur:Adopt external sandboxes
Bring sandboxes created outside the session under session management:Session properties
Inspect session state with built-in properties such assandbox_count: