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

# Run a coding agent in a sandbox

> [//]: # (smoke: not executed, a prompt needs the caller's own model key)

Run Claude Code or the Codex CLI inside a sandbox with a prompt, follow its events, steer or interrupt it, and resume the conversation later.

The agent runs in your sandbox, so it edits your files and runs your commands there and nowhere else.

## What you need

* A sandbox with Python and Node: the `base` template (the default) has both, and git. A sandbox without `python3` answers
  `409 agent_runtime_missing`.
* Outbound access to npm from the sandbox. The first prompt of a harness installs it into `~/.agent/npm`
  (`@anthropic-ai/claude-code` or `@openai/codex`, the current release) and says so in the stream
  (`harness.installing`, `harness.installed`). Later turns reuse it. The template does not ship either harness.
* Your own model credentials, passed with each prompt (see below). Styne does not supply a model.

## Send a prompt

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/prompt" \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "Content-Type: application/json" \
  -d '{"prompt": "Fix the failing test in /home/user/app and run it", "harness": "claude",
       "cwd": "/home/user/app", "env": {"ANTHROPIC_API_KEY": "'"$ANTHROPIC_API_KEY"'"}}'
```

```json theme={"theme":"css-variables"}
{"ok": true, "type": "prompt.queued", "sandboxId": "...", "conversationId": "cv_0123456789abcdef",
 "promptId": "pr_0123456789abcdef", "harness": "claude", "env": ["ANTHROPIC_API_KEY"]}
```

The answer is `202`: the turn runs on in the sandbox while you read its events.

| Field | Meaning |
| - | - |
| `prompt` | The task (1 byte to 256 KiB). `message` and `text` are accepted as names. |
| `harness` | `claude` (default) or `codex`. |
| `model` | The harness's own model name, passed through. |
| `new` | `true` starts a new conversation. With neither `new` nor `conversationId` the most recently used conversation continues. |
| `conversationId` | Continue this conversation. A conversation keeps its harness; naming another one is `400 harness_mismatch`. |
| `env` | Up to 32 variables (name `[A-Za-z_][A-Za-z0-9_]*`, value up to 8 KiB) handed to the harness: your model credentials, `ANTHROPIC_API_KEY` for Claude Code or `CODEX_API_KEY` / `OPENAI_API_KEY` for Codex. |
| `permissionMode` | `bypassPermissions` (default), `default`, `acceptEdits`, `auto` or `plan`. Claude Code gets it as `--permission-mode`; Codex runs with its own sandbox off for `bypassPermissions` (the microVM is the boundary) and with its default otherwise. |
| `cwd` | Working directory, under `/home/user` or `/tmp`. Default `/home/user`. |
| `partial` | Claude Code: also stream partial messages as they are generated. |

A conversation runs one turn at a time: a prompt into a conversation with a running turn is `409 conversation_busy`
(steer or interrupt it instead). Up to 8 turns run at once in one sandbox, in different conversations; more is
`429 too_many_turns`.

### Pass your credentials

The `env` values travel in the request body to the sandbox's agent launcher on its standard input. They are not written
to disk, not logged, not stored by Styne and not echoed back (the answer lists the variable names only), and every
event masks them as `***`. The harness itself can still see them in its environment, as any program you run in the
sandbox can, so give an agent a key you are willing to give that sandbox. Send `env` again with every prompt and every
steer: Styne keeps no copy to reuse.

## Follow the events

A page of events; resume from `nextCursor`:

```bash theme={"theme":"css-variables"}
curl -sS "https://api.styne.ai/sandboxes/$SANDBOX_ID/events?conversation=$CV&cursor=0" -H "Authorization: Bearer $STYNE_API_KEY"
```

A live stream of newline-delimited JSON until the turn ends:

```bash theme={"theme":"css-variables"}
curl -sSN "https://api.styne.ai/sandboxes/$SANDBOX_ID/events?conversation=$CV&follow=1&until=$PROMPT_ID" -H "Authorization: Bearer $STYNE_API_KEY"
```

Every event carries `conversationId`, `promptId`, `harness` and `ts`. `conversation` takes one id or a comma-separated
list; without it every conversation of the sandbox is read. A page answers `{"events": [...], "nextCursor": N,
"more": bool}` (`limit` 1 to 1000, default 200). `cursor` is a byte position: keep the `nextCursor` and send it back.

| `type` | When |
| - | - |
| `prompt.started` | The turn began (`text`, `model`). |
| `harness.installing`, `harness.installed` | The first turn of a harness installed it. |
| `agent.event` | One line of the harness's own JSON stream, unchanged in `event` (Claude Code `stream-json`, Codex `--json`). |
| `agent.output` | A line the harness printed that is not JSON. |
| `prompt.finished` | The turn ended: `status` is `succeeded`, `failed` or `interrupted`, with `exitCode` and, when it failed, `error` (`harness_unavailable: ...` when the harness could not be installed or started). |

With `follow=1` the stream also carries `{"type":"ping","cursor":N}` after 15 quiet seconds and ends with
`{"type":"end","cursor":N}` after 10 minutes, or earlier when the prompt named by `until` has finished: reconnect from
that cursor. Each event line is cut to 1 MiB (`truncated: true` replaces the `event`).

## Read status and conversations

* `GET /sandboxes/{id}/prompts/{promptId}`: `status` is `queued` (the launcher is starting), `running`, `succeeded`, `failed`, `interrupted` or `lost` (the
  turn's process is gone without an end, e.g. the sandbox was stopped), with `exitCode` and the times.
* `GET /sandboxes/{id}/conversations`: every conversation, most recently used first, with `harness`, `model`,
  `promptCount`, `lastPrompt`, `running`, `runningPrompt` and `current` (the one a prompt without `new` or
  `conversationId` continues).
* `GET /sandboxes/{id}/conversations/{conversationId}`: one of them.

Conversations live in the sandbox's disk (`~/.agent`), so they survive an API restart and a stop and resume of the
sandbox, and are in its snapshots. A turn running when the sandbox stops is `lost`.

## Steer or interrupt a turn

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/steer" -H "Authorization: Bearer $STYNE_API_KEY" -H "Content-Type: application/json" \
  -d '{"message": "Skip the integration tests", "env": {"ANTHROPIC_API_KEY": "..."}}'
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/interrupt" -H "Authorization: Bearer $STYNE_API_KEY" -d '{"conversation": "cv_..."}'
```

A steer interrupts the running turn and continues the same conversation, with the same harness session and so its
memory, on your message. The answer has `native: false`: neither harness takes a new message into a running
non-interactive turn, so the turn is stopped and a new one resumes the session; what the first turn had finished
stays. A conversation with nothing running answers `409 no_running_turn` (`error.details.reason`: `no_turn` or
`turn_ended`); start work with a prompt instead.

An interrupt stops every running turn of the sandbox, or only `conversation`'s. The turn ends with
`prompt.finished` `interrupted`. An interrupt with nothing running answers `200` with an empty list.

## Customise the harness

Put your rules, MCP servers and skills in the harness's ordinary config files in the sandbox home. Only sandboxes of the same account are reachable: a sandbox of another account is a `404`.

The same routes exist under `/boat/v1` for clients of that API.

## Operations

| Method | Path | What it does |
| - | - | - |
| `POST` | `/sandboxes/{id}/prompt` | Prompt a coding agent (Claude Code, Codex) in a sandbox |
| `GET` | `/sandboxes/{id}/prompts/{promptId}` | Read the status of one prompt |
| `GET` | `/sandboxes/{id}/conversations` | List the agent conversations of a sandbox |
| `GET` | `/sandboxes/{id}/conversations/{conversationId}` | Read one agent conversation |
| `GET` | `/sandboxes/{id}/events` | Read or follow the agent events of a sandbox |
| `POST` | `/sandboxes/{id}/steer` | Steer a running agent turn with a new message |
| `POST` | `/sandboxes/{id}/interrupt` | Interrupt the running agent turns of a sandbox |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.