# One-call run

Send code or a command, get its output. kosa8 takes a fresh sandbox from the warm pool, writes your files, runs it, streams stdout and stderr as it prints, sends back the files you ask for, and deletes the sandbox. There is nothing to create or clean up. It is billed per second, like any sandbox.

## From the terminal

New here? `kosa8 init` signs you in, runs a first sandbox and prints what to try next.

```
kosa8 run "python3 -c 'print(6 * 7)'"
# ▸ kosa8 run · python3 -c 'print(6 * 7)'
# 42
# ✓ exit 0 · 640 ms · python
#   View run → https://app.kosa8.com/runs/sr_…

kosa8 run train.py --epochs 3                 # the language comes from the extension
kosa8 run --upload data.csv --out chart.png plot.py
kosa8 run --timeout 10m --size m bench.sh
kosa8 run --share "pytest -q"                 # a public read-only run card
kosa8 run --json "echo hi" | jq .status        # one JSON object, no live output
```

- A single quoted argument with a space in it is a shell command. A file that exists with a `.py`, `.js`, `.mjs`, `.ts`, `.sh` or `.bash` extension is a script; anything after it goes to the script. `kosa8 run IMAGE COMMAND` is still the local engine's docker-style run.

- `-e KEY=VALUE` sets env, `--upload` sends a local file into `/workspace`, `--out` brings a file back into the current directory (never over an existing file), `--template` and `--size` pick the sandbox, `--timeout` stops it (default 60 s, at most 1 h), `-q` prints only the command's own output.

- It exits with the command's exit code, and 124 when it ran past `--timeout`. Flags go before the command or script.

- Not signed in, it runs `kosa8 login` first.

## From the API

```
curl -s https://api.kosa8.com/v1/run \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"code": "import sys\nprint(sys.version)", "language": "python"}'
# {"run":{"id":"sr_…","status":"ok","exit":0,"stdout":"3.12…\n","stderr":"",
#   "duration_ms":612,"timing":{"sandbox_ms":180,"exec_ms":410,"total_ms":612},
#   "template":"python","files":[],"view_url":"https://app.kosa8.com/runs/sr_…"}}

curl -s https://api.kosa8.com/v1/run \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"command": "wc -l data.csv && sort data.csv > sorted.csv",
       "files": {"data.csv": "b\na\nc\n"}, "outputs": ["sorted.csv"]}'
# "files":[{"path":"/workspace/sorted.csv","bytes":6,"content_base64":"YQpiCmMK"}]
```

| Field | What it does |
|---|---|
| `code` | Source to run. It is written to `filename` (default `main.py`, `main.js`, …) in `/workspace` and run with the language's interpreter. |
| `command` | A shell string, or a list of strings. Without `code`, it is what runs. With `code`, it runs instead of the interpreter (for example `pytest -q` on the file you sent). |
| `language` | `python`, `node`, `typescript`, `bash` or `sh`. Default: from `filename`, else python. |
| `template` | Default: the language's own (`python`, `node` or `base`); for a bare command, `node` when it starts with node, npm or npx, else `python`. |
| `files` | `{"path": "text"}`, or `[{"path", "content", "encoding": "base64"}]` for bytes. Paths are under `/workspace`. The whole body is at most 6 MiB. |
| `env` | `{"NAME": "value"}`. Values are hidden (`•••`) in the saved output and on run cards. |
| `outputs` | Up to 16 paths to send back, base64, 4 MiB together. A missing one comes back with `error: "not_found"`. |
| `timeout_s` | Default 60, at most 3600. Past it the run stops with `status: "timeout"`. |
| `size` | A size name (`GET /v1/sizes`). Default: the template's own. |
| `stream` | `true` for NDJSON as it prints, `"sse"` for server-sent events. See below. |
| `share` | `true` publishes the run card. `{"public": true, "code": true}` shows the code on it too. |

A command that exits non-zero is still a `200`, with `status: "failed"` and its `exit`: it ran, and it failed. A `4xx` or `5xx` means it could not run. Every error carries `hint`, the next step, and `docs`, a link here. When the sandbox could not be made, `stage` is `create` and the status is the create's own (402 without a plan, 429 at the running limit, 503 when there is no room for a moment).

## Streaming

With `"stream": true` the answer is NDJSON. The first line names the run, so a caller can show its link at once. Then come the exec stream's own frames, `{"stream": "stdout" | "stderr", "data": "<base64>"}`, with an empty `heartbeat` frame every 15 s while the command prints nothing. The last line is `{"done": true, "exit": N, "run": {…}}`.

```
curl -sN https://api.kosa8.com/v1/run \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"command": "for i in 1 2 3; do echo $i; sleep 1; done", "stream": true}'
# {"run":{"id":"sr_…","sandbox_id":"sbx_…","status":"running","view_url":"…"}}
# {"stream":"stdout","data":"MQo="}
# {"stream":"stdout","data":"Mgo="}
# {"stream":"stdout","data":"Mwo="}
# {"done":true,"exit":0,"run":{"status":"ok","duration_ms":3420,…}}
```

Send `Accept: text/event-stream` (or `"stream": "sse"`) for server-sent events instead, with the output already decoded as UTF-8 text. That is what a browser's `fetch` reader or an `EventSource` proxy wants.

```
event: run
data: {"id":"sr_…","view_url":"…"}

event: stdout
data: {"text":"1\n"}

: heartbeat

event: done
data: {"id":"sr_…","status":"ok","exit":0,"duration_ms":3420,…}
```

The same `Accept: text/event-stream` works on `POST /v1/sandboxes/{id}/exec` with `"stream": true`, for a sandbox you keep. A streamed run or exec is not held to the 90 s limit of a buffered exec. A run of more than 85 s without `stream` is still one JSON answer at the end; kosa8 streams it internally.

## Run links and run cards

Every run answers with a `view_url`, and the CLI prints `View run →` with it. The page shows the command, its output, the exit code and how long it took.

- It is private by default: only people signed in to your organization can open it.

- Make one public with `share: true` on the run, `kosa8 run --share`, `POST /v1/runs/{id}/share {"public": true}`, or the Share button on the page. Make every new run public with the organization setting `public_runs` (`PATCH /v1/settings {"public_runs": true}`). It is never on by default.

- A public card shows the command, the output and the duration. It never shows env or the files you sent. The code is shown only when you publish it with `{"public": true, "code": true}`. The values of the run's env, and strings shaped like kosa8, OpenAI, Anthropic, GitHub, AWS and Slack keys, are replaced by `•••` before the output is saved.

- Hosted jobs and RL runs have cards too: their answers carry `view_url`, and `kosa8 job submit` and `kosa8 cloud rl runs create` print it. Publish one with `POST /v1/jobs/{id}/share` or `POST /v1/rl/runs/{id}/share` `{"public": true}`. A job with a grader, or an RL run with rollouts the sealed grader scored, shows a **graded by sealed grader** badge: the score came from a sealed copy of the sandbox the solver could not touch. A job or RL run card shows names, counts, reward and duration, never task inputs, output, env or files.

- An exec answer carries `view_url` too: the sandbox's page in the dashboard.

- `GET /v1/runs` lists the organization's runs; `GET /v1/runs/{id}` has the saved output (the last 32 KiB of stdout and 16 KiB of stderr). Runs are kept 30 days.

## The Run page

[Run, in the kosa8 Cloud app](https://app.kosa8.com/app#run) (sign in first; app.kosa8.com/run takes you there): paste code, pick a language, press Run (or ⌘⏎), and watch the output stream in. Share publishes the card and copies its link. Below it, **Run it from your terminal** makes an API key for you and fills it into the install, sign-in, `kosa8 run` and `curl` commands, masked on screen and whole on the clipboard.

## How it works

- A run is an ordinary sandbox: the same plan, payment, spending-cap and running-limit checks as `POST /v1/sandboxes`, the same per-second bill, and an audit record (`run.create`) with its language, template and exit, never its code or env.

- Small files and code ride inside the command, so a run is two calls to the host: the create from the warm pool and the exec. Files over about 44 KiB in all are uploaded first.

- The sandbox is deleted as soon as the output is read. It also carries a `ttl_s` past the run's timeout, so it goes even if that delete were lost.

- For state that lasts between calls, use a sandbox (`POST /v1/sandboxes` and exec), the [code interpreter](https://docs.kosa8.com/guides/cloud-code-interpreter/), or a [background command](https://docs.kosa8.com/guides/cloud-snapshots-background/). For an event handler called again and again, deploy a function.

---
Source: https://docs.kosa8.com/guides/cloud-run/
