> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Sandbox Lifecycle

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

A sandbox keeps running until its timeout expires or until you stop it manually. By default, a new sandbox stays alive for 5 minutes. You can set a different timeout when creating the sandbox, update the timeout while it is running, inspect sandbox information, or kill it when you are done.

<SandboxConfigHint />

## Create a sandbox with a timeout

Set the timeout at creation time when you know how long the work should run.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Timeout is in milliseconds.
  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Timeout is in seconds.
  sandbox = Sandbox.create(timeout=60)

  sandbox.kill()
  ```
</CodeGroup>

## Update the timeout

You can use the `setTimeout` method to reset how much longer the sandbox should stay alive. This is useful when user activity continues after the original timeout would have expired.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  await sandbox.setTimeout(300_000)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create(timeout=60)

  sandbox.set_timeout(300)

  sandbox.kill()
  ```
</CodeGroup>

## Get sandbox information

You can use the `getInfo` method in JavaScript or `get_info` method in Python to inspect the sandbox ID, template, metadata, current state, and expiration time.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  const info = await sandbox.getInfo()

  console.log(info.sandboxId)
  console.log(info.templateId)
  console.log(info.state)
  console.log(info.startedAt)
  console.log(info.endAt)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create(timeout=60)

  info = sandbox.get_info()

  print(info.sandbox_id)
  print(info.template_id)
  print(info.state)
  print(info.started_at)
  print(info.end_at)

  sandbox.kill()
  ```
</CodeGroup>

## Kill a sandbox

You can use the `kill` method to shut down a sandbox before its timeout expires when you no longer need it.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })
  const sandboxId = sandbox.sandboxId

  await sandbox.kill()

  // Or kill by ID.
  await Sandbox.kill(sandboxId)
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create(timeout=60)
  sandbox_id = sandbox.sandbox_id

  sandbox.kill()

  # Or kill by ID.
  Sandbox.kill(sandbox_id)
  ```
</CodeGroup>

## Next steps

* To keep a sandbox state for later use, see [Sandbox Persistence](/guides/sandbox-persistence).
* To create reusable saved states, see [Sandbox Snapshot](/guides/sandbox-snapshot).
