# API reference: agent

The hub's cloud agent. A session works toward a goal with the hub's own tools and a sandboxed shell. It acts as a short-lived key scoped below its creator's. Spends past a pre-approved budget wait for a person. Everything it does is recorded as an append-only event stream.

## GET /v1/agent/sessions

`listAgentSessions` · scope `key:read` · List agent sessions

Sessions visible to the caller (owner, creator, or a member of the owning org), newest first. No session is public.

Parameters:

- `status` (query)
- `limit` (query): Page size.
- `cursor` (query): Opaque cursor from the previous page's `next_cursor`.

Responses:

- `200`: Page of sessions.
- `401`: Missing or invalid credentials.
- `422`: Request failed validation.

## POST /v1/agent/sessions

`createAgentSession` · scope `key:train` · Start an agent session (the hub's cloud agent works toward a goal)

`{goal, robot?, campaign?, budget_usd, cap_usd?, scopes?, owner?}` → a session whose first turn is queued at once. The agent loop runs on the hub (one arq job per turn, state in Postgres). A frontier model sits behind the coach's model interface, extended with tool use. It calls the hub's MCP tools in-process, as the session's own short-lived key. Where the deployment configures one, it also calls a Modal sandbox shell whose network reaches only the hub.

**Keys.** The hub mints a key for every turn, bound to the session (`api_keys.agent_session_id`). The key expires on its own, and is revoked when the turn yields or the session ends. Its scope is the one the session was granted: `scopes`, which is never above the creator's own. No agent key can call a `session`-scoped operation or approve its own spend (`resolveAgentApproval` is `403` to every agent key). No agent key can create another session or approve a device handoff either.

**Money.** Two numbers.

`budget_usd` is the spend a person pre-approves for the tools that cost money. Those are `submit_training` on a priced executor, a Modal `rollout_in_sim`, `serve_model` on Modal, `plan_task` and the coach's two paid tools. Inside the budget, the tool runs and says so. Above it, the session pauses with an `approval_requested` event (`waiting_on_you`) until a person answers.

`cap_usd` (default $10) caps the session's own model + sandbox spend. The model is billed at its vendor's list price, plus 5% on a hub that takes payments. Each call reserves `count_tokens` + `max_tokens` first, and settles on the reported usage, cache fields included. The next call is refused at the cap. The sandbox is billed at Modal's sandbox rate × 1.1, only while active. Every call and every sandbox interval is one `ledger_entries` row.

The model is billed to the Anthropic key of the session owner's workspace (`listModelKeys`). A workspace with no key answers `402` with `type: model_key_required`, unless the hub serves it with its own key.

Until a payment system exists, every account (an org shares one) gets a **$5 monthly allowance** for what the hub pays. That is the sandbox, and model calls on the hub's own key. There is nothing beyond it: `402` when it is spent. Only `claude-sonnet-5` is offered.

An account may run **up to `AGENT_MAX_LIVE_SESSIONS` (3) working sessions at once**. A working session is running, waiting on an event or waiting on you. A sleeping session does not count. A fourth is `409` naming the three.

**Plan first.** Nothing billable runs before a person approves a plan. On a task that needs compute, the agent's first move is a plan (`listAgentPlans`). Until one is approved, every training run, round, rollout, render, endpoint and orchestration plan the agent asks for is refused. Its tools refuse them, and the hub's own routes refuse them for its key. Model turns may run, and a session that only answers a question needs no plan. A person who starts the session with `plan_first: false` lets the hub approve a plan for them. The hub does so when the plan is free or fits what is left of the free allowance. A plan that costs more still waits for them.

**Context.** `context[]` names the hub objects a session starts with ("Ask the agent about this model"). Each one is checked as the caller. Missing, private and the wrong kind are one `422` at `context[i]`. Each object is stored on the session. The model gets them on its first turn as a compact summary built from the hub's own reads. That summary is a `notice` event (`code: context`), which the page shows too.

Spends model dollars: `key:train`.

Request body: `application/json`, required.

Responses:

- `201`: Session created; its first turn is queued.
- `401`: Missing or invalid credentials.
- `402`: The account's monthly allowance is used up. Or the workspace needs an Anthropic key (`model_key_required`) or reached its monthly model cap (`model_cap_reached`). `settings_url` says where a person fixes the last two.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).
- `422`: Request failed validation.
- `503`: A service this endpoint needs is not configured in this deployment. The API boots with zero secrets. An endpoint that needs one says so instead of returning a stack trace.

## GET /v1/agent/sessions/{session_id}

`getAgentSession` · scope `key:read` · Get an agent session (status, spend, what it waits on, pending approvals)

Everything a page needs beside the event stream:

- status and why;
- the three spend lines (model at the vendor's price, sandbox × 1.1, GPU committed from the budget);
- the allowance;
- what the agent waits on;
- the pending approvals;
- the sandbox's state;
- `last_event_id`, where `streamAgentEvents` resumes.

A session you may not see is `404`.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Responses:

- `200`: The session.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).

## POST /v1/agent/sessions/{session_id}/messages

`postAgentMessage` · scope `key:train` · Send the agent a message (a user turn)

Appends a user message and queues a turn. **A message wakes any session that is not `canceled` or `failed`:**

- A `sleeping` one.
- A `waiting_on_you` one. A reply in words is how a person changes a proposed plan. The agent answers with a new version.
- A `waiting_on_event` one, whose wait it interrupts.
- A `done` one, which it reopens.

On a `running` one, the message is handed to the model at its next step. `409` only on a `canceled` or `failed` session. A message never counts against the working-session cap. It spends model dollars, so it needs `key:train`. An agent's own key is `403`. A workspace that has no Anthropic key the hub may use answers `402`, and no turn is queued.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Request body: `application/json`, required.

Responses:

- `202`: Message recorded; a turn is queued.
- `401`: Missing or invalid credentials.
- `402`: This workspace has no key the hub may use for the model's vendor, or a limit is reached: its own key's monthly cap, or the workspace's limit. `type` and `errors[].type` say which: `model_key_required`, `model_cap_reached` or `billing.cap_reached`. `settings_url` is where a person adds a key, raises the cap or opens Billing. Nothing was spent.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).
- `422`: Request failed validation.

## GET /v1/agent/sessions/{session_id}/events

`streamAgentEvents` · scope `key:read` · Stream a session's events (SSE, resumable)

Server-Sent Events from the session's append-only event table, in the order they were written. Every frame carries `id: <event id>` (a ULID), `event: <kind>` and `data: <AgentEvent JSON>`. A reconnecting `EventSource` sends `Last-Event-ID`, and the stream resumes strictly after it. The `after` query parameter does the same. With `follow=true` the stream stays open until the session ends. While nothing happens, it sends `: keepalive` comments. `event: end` always closes it, with the session's status. Same shape as `streamRunLogs`. The kinds are `message`, `tool_use`, `tool_result`, `terminal`, `approval_requested`, `approval_resolved`, `status`, `usage`, `sandbox`, `notice`, `error`, `plan`, `decision` and `round`. Each kind and its `data` are as `AgentEvent` defines them. Replaying the stream plus `getAgentSession` (and the digest, objects and decisions) is everything the agent page needs.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `follow` (query): Keep the stream open until the session ends.
- `after` (query): Resume strictly after this event id (the `Last-Event-ID` header wins when both are sent).
- `Last-Event-ID` (header): The id of the last event the client saw (sent by `EventSource` on reconnect).

Responses:

- `200`: SSE event stream.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).
- `422`: Request failed validation.

## POST /v1/agent/sessions/{session_id}/approvals/{approval_id}

`resolveAgentApproval` · scope `key:train` · Approve or deny a spend the agent asked for

Answers a `spend` decision (`approval_requested`). A spend the rules do not allow becomes one:

- **any compute call outside the approved plan, whatever it costs**;
- a coach call over the per-run limit or the budget.

The wait is non-blocking: the agent keeps working while it waits. On `approve`, its next turn runs the held call **exactly as it was asked for** before anything else. The held call is no longer a dry run. On `deny`, the agent is told the call did not run and why. `raise_budget_to` approves this call and grants the session a standing allowance for the rest of its life. Later compute calls whose caps fit in what is left of it run without asking (the rule reads `session_grant`). The session's budget is raised to cover the allowance.

**A call outside the approved plan is decided only by a signed-in person's session**. Such a call asks a person, and a key is not a person. So every API key is `403`, the owner's `train` key included. The owner's `train` key may still decide a coach call over the per-run limit or the budget. **Every agent session key is `403`** for everything, including this session's own. An agent never approves its own spend. `409` once the approval is decided or expired (`expires_at`, the session's window). `409` also when the session is `canceled` / `failed`.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `approval_id` (path, required): Agent spend approval id (`aappr_...`).

Request body: `application/json`, required.

Responses:

- `200`: The decided approval.
- `401`: Missing or invalid credentials.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).
- `422`: Request failed validation.

## POST /v1/agent/sessions/{session_id}/cancel

`cancelAgentSession` · scope `key:train` · Cancel an agent session

Ends the session now (`canceled`). Pending approvals are withdrawn and the session's keys are revoked. The sandbox is terminated and its last interval is billed. A model call already in flight finishes and is billed (the ledger records it). Nothing the model asked for after that call runs. Runs, rollouts or plans the agent started are not touched. Cancel them on their own pages. `409` when the session already ended.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Responses:

- `200`: The canceled session.
- `401`: Missing or invalid credentials.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).

## POST /v1/agent/sessions/{session_id}/end

`endAgentSession` · scope `key:train` · End an agent session (done; a reply reopens it)

The person ends the session: status `done`, `stop_code: ended`. Unlike `cancelAgentSession`, this is not final. Its sandbox snapshot is kept and **a message reopens it** (`postAgentMessage` wakes a `done` session). Pending decisions are withdrawn: a spend or a plan nobody answered does not survive the end. The session's keys are revoked. Runs, rollouts or rounds it started are not touched. `409` on a `canceled` or `failed` session, or one that is already `done`. An agent's own key is `403`.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Responses:

- `200`: The ended session.
- `401`: Missing or invalid credentials.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).

## GET /v1/agent/sessions/{session_id}/plans

`listAgentPlans` · scope `key:read` · The plans the agent proposed in this session, newest version first

Every version of the session's plan. The agent proposes a plan before anything that costs compute. Nothing billable happens before a person approves a plan. A person approves the latest version (`approveAgentPlan`, session-only), or replies in words. After a reply, the agent proposes the next version, and the version it replaces reads `edited`. Only the latest version can be approved.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Responses:

- `200`: The plans.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).

## GET /v1/agent/sessions/{session_id}/plans/{plan_id}

`getAgentPlan` · scope `key:read` · One version of the session's plan

The plan as the agent page draws it. It holds:

- its rounds;
- contingent rounds and their condition;
- "then" and "not doing";
- the total cap.

Each round is what will become a campaign round's locked spec. It holds one change and its keys, and gates with the prediction written beside each. The hypothesis is shown beside the gates and is never counted. A round also holds the gate while training, the stop rules, the estimate and the cap. Estimates are the hub's own (`estimateRun`'s function). Nothing in a plan is spent until a person approves it.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `plan_id` (path, required): Agent plan id (`aplan_...`), one version of the session's plan.

Responses:

- `200`: The plan.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).

## POST /v1/agent/sessions/{session_id}/plans/{plan_id}/approve

`approveAgentPlan` · scope `session` · Approve the agent's plan (a signed-in person only)

**Session-only, like a device approval: no API key of any scope approves a plan**. So no agent can approve its own. The plan is the contract: approving it pre-approves each round's cap. A round can start now when it has no `contingent_on` and no `parent_from`. In a session with a campaign, this request **creates and locks every such round through `createRound`'s own path**. The three rules and the campaign's budget are checked. A refusal (`422` with the rule reasons, `409` on the budget) records nothing and leaves the plan proposed. In a session without a campaign, each such round's run is created through `createRun`'s path. The later rounds start when the agent asks for them by key (`start_planned_round`), exactly as approved. Anything the agent then wants that the plan does not name asks a person again, whatever it costs. `409` when the plan is not the latest version or is not `proposed`. `409` also when the session is `canceled` or `failed`.

**Beyond the free allowance.** When the plan can cost more than the owner's free monthly allowance covers (`allowance.needs_confirmation`), the person must confirm it with `confirm_beyond_allowance: true`. Without it the answer is `409` with the amount, and nothing is created.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `plan_id` (path, required): Agent plan id (`aplan_...`), one version of the session's plan.

Request body: `application/json`.

Responses:

- `200`: The approved plan, with the rounds it created.
- `401`: Missing or invalid credentials.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
- `409`: State conflict (duplicate handle/slug, wrong repo kind, terminal job, ...).
- `422`: Request failed validation.

## GET /v1/agent/sessions/{session_id}/decisions

`listAgentDecisions` · scope `key:read` · The session's decisions: plan, spend, shortlist, handoff

The typed decision cards, all in one shape:

- a **plan**, answered by `approveAgentPlan` or a reply in words;
- a **spend** outside the approved plan, answered by `resolveAgentApproval` (with `expires_at` and `raise_budget_to`);
- a **shortlist** of checkpoints for the real robot, answered by `decideRound {action: shortlist}`. The 20-seed scan runs first, then one consent request per checkpoint;
- a **handoff**: a device consent request. It only links to the consent page and is never approved here.

`answer_via` names the operation that answers each. Newest first. `status` filters the list.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `status` (query)

Responses:

- `200`: The decisions.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).
- `422`: Request failed validation.

## GET /v1/agent/sessions/{session_id}/digest

`getAgentDigest` · scope `key:read` · "Since you left": what happened in the session since the viewer last looked

**The hub generates it from the session's events and the objects they link.** No AI model writes it. It costs nothing, reads the same every time, and cannot state a fact the rows do not hold. The agent's own words appear only as quotes (a round card's "Next", a spend's proposal). The lines come in this order, each with the ids to open:

- runs that finished or failed;
- the gates their checkpoints got;
- the round's verdict by its locked gates;
- the tripwire;
- videos rendered or rendering;
- handoffs;
- what needs you.

`since` defaults to the viewer's `last_seen_at` (else the session's start). Unless `mark=false`, reading it moves the viewer's `last_seen_at` to now. An agent's own key never marks.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).
- `since` (query): Lines about what happened after this instant (default the viewer's `last_seen_at`).
- `mark` (query): Move the viewer's `last_seen_at` to now after reading (default true).

Responses:

- `200`: The digest.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).
- `422`: Request failed validation.

## GET /v1/agent/sessions/{session_id}/objects

`listAgentObjects` · scope `key:read` · The session's objects grouped by state: Needs you, Running, Ready for review, Done

The overview column of the session page. It lists every object the session touched, derived from its events' links and its plan. Each object gets one line of facts, and the lines are grouped. **Needs you**: pending decisions, and the spends an older session still holds the old way. Those are pending `AgentApproval`s with no decision card (`kind: approval`), so a page never has to patch them in. A render's line says how long is left, from the hub's measured render time (`AgentRenderRef.expected_s`). **Running**: queued or running runs, renders and the shortlist's scans. **Ready for review**: checkpoints that PASS the round's locked gates (a shortlist's candidates once one is proposed). **Done**: finished runs and renders, and plan rounds that were not needed. The groups always come back in that order, empty ones included.

Parameters:

- `session_id` (path, required): Agent session id (`asess_...`).

Responses:

- `200`: The objects.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).

## GET /v1/tasks

`listTasks` · scope `key:read` · List the agent's tasks, sessions and robot plans in one list

Everything the hub's agent works on for the caller, newest first. These are its sessions, which train and test policies, and its plans on a connected robot. No task is public.

Each task has a `stage`. The hub derives it on every read from what exists: the task's plan, its runs, its sim tests and its deploy requests. Nothing about a stage is stored. The stages run `planning`, `awaiting_approval`, `training`, `testing_in_sim`, `ready_for_review`, `deploy_pending_approval`, then `deployed`, `failed` or `stopped`. A plan on a connected robot has no training stages.

Parameters:

- `limit` (query): Page size.
- `cursor` (query): Opaque cursor from the previous page's `next_cursor`.

Responses:

- `200`: Page of tasks.
- `401`: Missing or invalid credentials.
- `422`: Request failed validation.

## GET /v1/tasks/{task_id}

`getTask` · scope `key:read` · Get one task with its stages, evidence and deploy requests

One task. It shows the task's stage and why, the stages it has reached and when, and the runs it started. It also shows its sim tests with their verdicts, its deploy requests and what it has cost so far. A task you may not see is `404`.

Parameters:

- `task_id` (path, required): A task's id, the id of its agent session (`asess_...`) or of its plan on a robot (`plan_...`).

Responses:

- `200`: The task.
- `401`: Missing or invalid credentials.
- `404`: Resource not found (or hidden from the caller).

## POST /v1/tasks/{task_id}/stop

`stopTask` · scope `key:train` · Stop a task and the work it started

The task page's Stop. For a session the agent stops (`endAgentSession`): a message reopens it. Each of its training runs still queued is canceled, and each one running stops early and keeps its checkpoints (`stopRun`). For a plan on a connected robot the plan is canceled (`cancelPlan`). A deployment already approved keeps running: stop it from the robot's approval page. Stopping a task that has already stopped changes nothing and answers the task.

Parameters:

- `task_id` (path, required): A task's id, the id of its agent session (`asess_...`) or of its plan on a robot (`plan_...`).

Responses:

- `200`: The task after the stop.
- `401`: Missing or invalid credentials.
- `403`: Authenticated but not allowed (visibility, membership or scope).
- `404`: Resource not found (or hidden from the caller).
