# Commands

`POST /v1/sandboxes/:id/commands` runs one shell command (`sh -c`) as the sandbox user in `/workspace`, returns its output, bills it as one command and ends it with a checkpoint.

## Request

| Field | Type | Default | Meaning |
| --- | --- | --- | --- |
| `command` | string | required | A shell command line |
| `cwd` | string | `/workspace` | Working directory |
| `env` | object | | Extra environment variables |
| `stdin` / `stdinBase64` | string | | Data on standard input |
| `timeoutSec` | number | 30 | Wall-clock limit, up to 90 |
| `cpuBudgetSec` | number | 5 | Guest CPU limit, up to 30 |
| `changes` | boolean | false | Also list the files the command changed |
| `heavy` | `"reject"` or `"try"` | `"reject"` | What to do with a command that looks CPU-heavy |
| `outputEncoding` | `"utf8"` or `"base64"` | `"utf8"` | Use base64 for binary output |

## Response

```json
{
  "exitCode": 0,
  "stdout": "hello\n",
  "stderr": "",
  "truncated": false,
  "timedOut": false,
  "cpuLimitExceeded": false,
  "wallMs": 41,
  "cpuMs": 20,
  "backgroundCpuMs": 0,
  "egressBytes": 0,
  "backgroundProcesses": 0,
  "checkpoint": { "id": "ck_...", "kind": "files", "status": "ready" },
  "cost": { "usd": 0.00005, "commandUsd": 0.00005, "cpuOverageUsd": 0 },
  "route": { "target": "flash" },
  "timing": { "execMs": 41, "checkpointMs": 4, "totalMs": 47 }
}
```

## Limits

- **Wall time.** A command still running at `timeoutSec` is killed, together with anything it started, and `timedOut` is true.
- **CPU budget.** A watchdog stops the command once the guest has used `cpuBudgetSec` of CPU while it runs; `cpuLimitExceeded` is true. Background processes the command leaves running are not bound by it.
- **Output.** Up to 1 MiB each of stdout and stderr; beyond that `truncated` is true.

## Heavy commands

Package installs (`npm install`, `pip install`), type checkers and bundlers (`tsc`, `vite build`), test runners (`pytest`, `npm test`), compilers (`make`, `cargo`, `gcc`) and container tools are refused with `422 too_heavy`. Under emulation they run about 30 times slower, and a slow answer costs your agent more than a clear refusal. Send `"heavy": "try"` to run one anyway under the CPU budget. A program that runs over its budget is treated as heavy from then on in that sandbox.

Sending heavy commands to a full machine with the same files is coming next; the API will not change.

## Background processes

A command can leave a process running: `nohup python3 server.py > log 2>&1 &`. The response counts them in `backgroundProcesses`. While any run, checkpoints include the machine's memory, so rollback and hibernation keep them running; their CPU between commands is billed as background CPU. For processes you want to manage, use the [processes API](/docs/processes/).

## Changed files

With `"changes": true` the response lists the files the command created or modified under `/workspace`, `/tmp` and home (up to 200; `.git`, `node_modules` and caches skipped). It adds about 20 ms.
