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

# Sandbox Lifecycle

> Sandbox states, termination reasons, exit codes, timeouts, readiness probes, names, tags, and retention.

## States

```text theme={null}
queued ──► preparing ──► scheduled ──► starting ──► running ──► terminated
```

| State | Meaning |
| - | - |
| `queued` | Accepted. The image is being resolved, and the sandbox waits for a free node. |
| `preparing` | GPUs are being prepared for the sandbox. Skipped when no preparation is needed. |
| `scheduled` | A node is assigned. |
| `starting` | The image is being pulled and the sandbox's VM is starting. |
| `running` | The main process is running. Billing runs only in this state. |
| `terminated` | Final. `termination_reason` says why. |

Any non-terminal state can go straight to `terminated`. Follow a sandbox with `GET /v2/ai/sandboxes/{sandbox_id}`, or wait for it to end with [`GET …/wait`](#wait-for-a-sandbox-to-end).

## Termination Reasons

`termination_reason` is always set once `state` is `terminated`. `exit_code` is set whenever the main process ran.

| Reason | When |
| - | - |
| `exit` | The main process exited on its own. See `exit_code`. |
| `timeout` | The sandbox reached its `timeout`. |
| `idle_timeout` | The sandbox had no activity for `idle_timeout` seconds. |
| `queue_timeout` | It was still queued or preparing after `queue_timeout` seconds. |
| `terminated_by_user` | `POST …/terminate` was called. A queued or preparing sandbox is cancelled at once. |
| `image_pull_failed` | The image can't be used. `termination_message` says why when it's known. |
| `no_command` | The image has no `ENTRYPOINT` or `CMD`, and the request has no `command`. |
| `oom_killed` | The sandbox ran out of memory. |
| `disk_limit_exceeded` | The sandbox wrote more outside `/data` than its root filesystem limit (`root_disk_gib`). |
| `account_suspended` | The account lost access. |
| `killed_by_operator` | Telnyx stopped the sandbox. |
| `spend_limit_reached` | The account, or the API key that created the sandbox, reached its GPU Sandboxes spend limit. The sandbox isn't restarted when the limit is raised. |
| `node_lost` | The sandbox's node failed. |
| `internal_error` | Telnyx couldn't start or keep the sandbox running. |

A process killed by a signal reports `exit_code` 128 plus the signal number: 137 for `SIGKILL`, 143 for `SIGTERM`.

`termination_message` is set in two cases only. With `internal_error`, it holds a short reason from Telnyx, such as the sandbox's disk not being ready. With `image_pull_failed` before the sandbox ran, it holds the registry's own message or what the image checks found (the reference, the image's platform, `ENTRYPOINT`/`CMD`, `WorkingDir`, or `User`). It is text from your registry, shown as received with control characters removed.

## Timeouts

| Field | Default | Range | Counts |
| - | - | - | - |
| `timeout` | 300 s | 1 s or more; `null` for no limit | From `started_at`. Queue time, GPU preparation, and image pulls don't count. |
| `idle_timeout` | none | 1 s up to `timeout` | Time without activity, while running. |
| `queue_timeout` | 600 s | 0–86,400 s | Resolving the image, waiting for a node, and preparing GPUs. |

> [!WARNING]
>
> Send `"timeout": null` explicitly for a sandbox with no lifetime limit. A client that drops `null` fields from the JSON body gets the 300 s default.

**Activity** for `idle_timeout` is an exec running, starting, or finishing, and stdin written to an exec or to the main process. Reading output, file calls, and the main process's own work don't count. A sandbox whose main process is a server, with nothing else going on, ends after `idle_timeout`.

With `queue_timeout: 0`, a sandbox always ends `queue_timeout`, because resolving the image counts. The first sandbox of an account can also wait for its [log bucket](https://developers.telnyx.com/docs/gpu-sandboxes/logs.md#log-bucket) to be created before its queue time starts.

## Stopping

`POST /v2/ai/sandboxes/{sandbox_id}/terminate` stops a sandbox:

* A queued or preparing sandbox is cancelled at once.
* A placed sandbox is stopped asynchronously. Its processes get `SIGTERM`, then `SIGKILL` if they are still running after a grace period. It reaches `terminated` with `termination_reason: terminated_by_user` and the main process's `exit_code` if it ran.
* A sandbox that already ended on its own keeps its own reason. Terminating a terminated sandbox succeeds and changes nothing.

The response returns `202` with the sandbox as it is at that moment. [File calls](https://developers.telnyx.com/docs/gpu-sandboxes/files.md) keep working during the grace period, so results can still be fetched from a sandbox that is ending.

When `timeout` and a terminate request overlap, the earlier one wins. A stop that comes after the main process already exited changes nothing.

## Wait for a Sandbox to End

`GET /v2/ai/sandboxes/{sandbox_id}/wait?timeout=10` is a long poll. It returns `200` with the sandbox as soon as it is `terminated`, or after at most 10 seconds. Larger `timeout` values are clamped to 10; `0` returns at once. Call it again until `state` is `terminated`. This waits for a long-running sandbox without holding one connection open.

## Readiness

`ready` tells whether the sandbox can serve its purpose. Without a probe, it is `true` once the sandbox is `running`. With `readiness_probe`, set exactly one of:

| Field | Ready once |
| - | - |
| `tcp_port` | The port (1–65535) accepts connections inside the sandbox. |
| `exec` | The command (argv) exits with `0`. |

```json theme={null}
{ "readiness_probe": { "exec": ["test", "-f", "/tmp/model-loaded"] } }
```

## Names and Tags

* **`name`** (optional): 1–64 characters of `A-Z`, `a-z`, `0-9`, `.`, `_`, `-`. It is unique among the account's non-terminated sandboxes (`409 name_conflict` otherwise) and reusable once the sandbox holding it ends. Look a sandbox up with `GET /v2/ai/sandboxes?name=<name>`.
* **`tags`** (optional): up to 20 key/value labels. Keys are 1–63 characters, values up to 255. Filter with `GET /v2/ai/sandboxes?tag[project]=llm-ft`; every given tag must match.

`PATCH /v2/ai/sandboxes/{sandbox_id}` with `{"tags": {...}}` replaces the whole tag set; `{}` clears it. Tags are the only thing that can change after create, and they can change on terminated sandboxes too.

## Listing and Retention

`GET /v2/ai/sandboxes` lists sandboxes newest first. Without `state`, it lists only non-terminated sandboxes. Add `state=terminated` for finished ones, or repeat `state` for several. Sandboxes belong to the organization: every user in it sees and manages them.

Terminated sandboxes and their execs stay readable for at least 30 days. [Metrics](https://developers.telnyx.com/docs/gpu-sandboxes/metrics.md) are kept for 30 days.
