Skip to main content
We can set a timeout for each sandbox (refer to Lifecycle Management). When the sandbox running time reaches the timeout, the sandbox will be automatically killed. However, in some scenarios, we cannot determine the expected running time of the sandbox, but we want the sandbox to be automatically killed when not in use to save costs. In this case, you can use the “Idle Timeout” feature. When creating a sandbox, we can set the idle_timeout parameter in metadata (unit: seconds, minimum is 60) to enable the “Idle Timeout” feature. Once enabled, when the system detects that the sandbox has no operations (executing commands, running code, file operations, etc.) within the specified time range, the system will kill the sandbox instance. Otherwise, the sandbox will continue to run until it reaches the maximum time limit for sandbox operation (currently default is 3600 seconds). Please refer to the following example:
import { Sandbox } from 'novita-sandbox/code-interpreter';

const sandbox = await Sandbox.create(
    {
        metadata: { "idle_timeout": "60" }
    }
);
console.log('Sandbox created', sandbox.sandboxId)

const result = await sandbox.commands.run('ls -al')
console.log('Command result', result)

await new Promise(resolve => setTimeout(resolve, 90000));

const isRunning = await sandbox.isRunning()
console.log('Sandbox is running', isRunning)

await sandbox.kill()