# Sandboxes

A sandbox is a microVM with its own kernel, filesystem and network, made for one agent, one task or one rollout. You make it, run things in it, and delete it, or let its deadline delete it for you.

**CLI**

```
# Create: deleted on its own after 30 minutes, asleep after 5 idle
ID=$(kosa8 cloud sandbox create --template python --ttl 30m --sleep-after 5m --json | jq -r .sandbox.id)

kosa8 cloud sandbox get $ID          # state, size, region, timers
kosa8 cloud sandbox exec $ID -- python3 --version
kosa8 cloud sandbox rm $ID           # its disk is wiped
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"

# Create: deleted on its own after 30 minutes, asleep after 5 idle
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H 'Content-Type: application/json' \
  -d '{"template":"python","ttl_s":1800,"sleep_after_s":300}' | jq -r .sandbox.id)

curl -s $API/sandboxes/$SBX -H "$AUTH" | jq '.sandbox | {id, state, cpus, mem_mb, region}'

# Push the deadline out to two hours from now
curl -s -X PATCH $API/sandboxes/$SBX -H "$AUTH" -H 'Content-Type: application/json' \
  -d '{"ttl_s":7200}' | jq '.sandbox.delete_after'

curl -s -X DELETE $API/sandboxes/$SBX -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

box = cloud.sandboxes.create(template="python", ttl_s=1800, sleep_after_s=300)
print(box.id, box.state)             # sbx_… running

box.update(ttl_s=7200)               # push the deadline out to two hours
same = cloud.sandboxes.get(box.id)   # look it up again by id
print(same.state)                    # running

box.delete()
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();

const box = await cloud.sandboxes.create({ template: 'python', ttlS: 1800, sleepAfterS: 300 });
console.log(box.id, box.state);            // sbx_… running

await box.update({ ttlS: 7200 });          // push the deadline out to two hours
const same = await cloud.sandboxes.get(box.id);
console.log(same.state);                   // running

await box.delete();
```

## Create

`POST /v1/sandboxes` with an empty body gives you a `base` sandbox (Alpine, 1 vCPU, 256 MiB). The fields you will use most:

| Field | What it does |
|---|---|
| `template` | `base`, `python`, `node`, a `universal-*` size, or your own. See [sizes and templates](https://docs.kosa8.com/concepts/sizes/) |
| `image` | Any public OCI image instead, such as `python:3.12-slim` |
| `size` | `0.25x0.5` up to `16x32` (vCPUs x GiB) |
| `ttl_s` | Delete it this many seconds from now: 60 s to 90 days, or `null` for never |
| `sleep_after_s` | Put it to [sleep](https://docs.kosa8.com/concepts/sleep/) after this many idle seconds (0 = never, up to 7 days) |
| `name`, `labels` | Find it again later: `GET /v1/sandboxes?label=team=ml` |
| `env` | Environment for every command |
| `persistent` | Keep one container across execs, so installed packages and running processes stay |
| `snapshot` | Start from a [snapshot](https://docs.kosa8.com/concepts/snapshots/) instead |

Send an `Idempotency-Key` header and a retried create returns the first sandbox instead of a second one. Every field is in the [API reference](https://docs.kosa8.com/api/sandboxes/).

## States

**creating**The microVM is booting.

**running**Ready for exec, files, fork and checkpoints.

**sleeping**Saved and stopped. The next exec wakes it, memory included.

**deleted**Gone, with its disk. Its record stays in the audit log.

You may also see `expired` (its `ttl_s` ran out), `error`, and `paused` or `stopped`; the [API reference](https://docs.kosa8.com/api/sandboxes/) lists them all.

## What lives where

Each exec runs in the sandbox's container. Files under `/workspace` are kept for the whole life of the sandbox, and are what checkpoints, forks and snapshots carry. For processes that keep running between commands, create with `persistent: true` or use a [background command](https://docs.kosa8.com/concepts/exec/#background).

## Limits

How many sandboxes can run at once, and how big, depends on your plan. A create past a limit is refused with `402 plan_limit` and an `upgrade_url`, never silently queued. See [limits](https://docs.kosa8.com/limits/).

---
Source: https://docs.kosa8.com/concepts/sandboxes/
