invoke() calls share the same sessionId, so the second agent run can see files created by the first. The Docker container persists between runs and is cleaned up automatically when idle.
Create session-scoped sandbox tools
The key difference from Sandbox Tools isscope: 'session'. This tells the SandboxManager to reuse the same Docker container across multiple invocations that share the same sessionId.
Define the agent
Run multiple invocations with a shared session
Pass the samesessionId to multiple invoke() calls. Each run streams events and waits for the agent to complete before starting the next. The second run sees files created by the first.
How it works
sandboxTools({ scope: 'session' })creates tools that look up or create a container keyed bysessionId- The first
invoke()creates a new Docker container and runs the agent inside it - The second
invoke()with the samesessionIdreuses the existing container — files from Run 1 are still on disk - The container is cleaned up automatically after an idle timeout
Comparison with default sandbox
| Feature | Default (scope: 'invocation') | Session (scope: 'session') |
|---|---|---|
| Container lifetime | Single invocation | Shared across invocations |
| File persistence | Destroyed after run | Survives between runs |
| Use case | Isolated tasks | Multi-turn workflows |
| Session ID | Not required | Required (shared key) |
Run it
Docker must be installed and running. The container image (
node:20-slim) will be pulled automatically on first run.