You can configure an idle timeout for your sandboxes to automatically stop or pause them when no active connections are detected. This helps reduce costs by ensuring unused sandboxes are not running indefinitely.
The idle timeout is configured via the metadata field when creating a sandbox. The key is idle_timeout and the value is the number of seconds (as a string).
Pass the idle_timeout key in the metadata object when creating a sandbox. The value is the idle timeout duration in seconds (as a string). When no client is connected to the sandbox for the specified duration, the sandbox will be automatically killed or paused.
import { Sandbox } from 'novita-sandbox/code-interpreter'// Create a sandbox that will be automatically killed after 60 seconds of inactivity.const sandbox = await Sandbox.create({ metadata: { idle_timeout: '60', },})// The sandbox is running...// After 60 seconds with no active connections, it will be killed automatically.
The idle timeout feature monitors active connections to your sandbox:
When no connections are active — the sandbox’s end time is set to current_time + idle_timeout_seconds.
When a connection becomes active again — the sandbox’s end time is restored to the original maximum sandbox lifetime.
When the idle timeout elapses without any reconnection — the sandbox is killed (or paused if autoPause is enabled).
This means a sandbox won’t be stopped as long as there is at least one active client connected to it (e.g., via Sandbox.connect() or open WebSocket/HTTP connections).
By default, an idle sandbox is killed when the idle timeout expires. If you want the sandbox to be paused instead (so you can resume it later), enable the autoPause option:
import { Sandbox } from 'novita-sandbox/code-interpreter'// Create a sandbox that will be paused (instead of killed) after 60 seconds of inactivity.const sandbox = await Sandbox.create({ metadata: { idle_timeout: '60', }, autoPause: true,})// After 60 seconds of inactivity, the sandbox will be paused.// You can resume it later with Sandbox.connect().
When autoPause is enabled, the sandbox state transitions to paused on idle timeout. You can connect to it later to resume execution.
Values below 30 seconds are treated as “idle disabled” to prevent rapid start/stop cycles.
Default
Disabled (0)
If idle_timeout is not specified, the feature is disabled and the sandbox runs until its maximum lifetime.
Maximum
Sandbox lifetime
The idle timeout cannot exceed the sandbox’s configured timeout (maximum lifetime).
If you set idle_timeout to a value below the minimum threshold (30 seconds), the idle timeout feature will be silently disabled for that sandbox. The sandbox will run until its maximum lifetime expires.
Use a short idle timeout for sandboxes that run one-off tasks and don’t need to persist after the client disconnects:
import { Sandbox } from 'novita-sandbox/code-interpreter'const sandbox = await Sandbox.create({ metadata: { idle_timeout: '60' },})// Execute code and get results...const result = await sandbox.runCode('print("Hello!")')// Disconnect — sandbox will be killed after 60 seconds of inactivity.await sandbox.kill()
Use a longer idle timeout for sandboxes used in interactive sessions where users may step away temporarily:
import { Sandbox } from 'novita-sandbox/code-interpreter'const sandbox = await Sandbox.create({ metadata: { idle_timeout: '600' }, // 10 minutes autoPause: true,})// The sandbox will pause after 10 minutes of inactivity,// and can be resumed when the user returns.