Skip to content

An open-source, faster Devin alternative

TypeScript

docs: management API and session events reference for Serverless Agents

Igor Zalutski1bcf5ae862db
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
new file mode 100644
index 00000000..c024e554
--- /dev/null
+++ b/docs/agents/api.mdx
@@ -0,0 +1,254 @@
+---
+title: "Management API"
+description: "Create sessions, send turns, read the event log and manage projects over HTTP"
+---
+
+The management API is the HTTP surface behind the CLI, the dashboard, the
+SDK helpers and the React hook's proxy routes. Use it from trusted server
+code to run agents from your own application.
+
+## Base URL and authentication
+
+```text
+https://app.opencomputer.dev/api/managed-agents
+```
+
+Every request carries an API key in the `x-api-key` header. A key belongs to
+an organization and reaches every project, agent and session in it. Keep it
+on the server; the [React integration](/agents/react) shows how a browser
+attaches through your own routes without seeing the key.
+
+Request and response bodies are JSON. Paths below are relative to the base
+URL; `<p>` is a project ID and `<r>` a memory resource ID.
+
+## Errors
+
+Error bodies are `{ error: { code, message } }`. The `code` is stable and is
+what to branch on; the `message` is short and safe to show.
+
+| Status | Meaning |
+| --- | --- |
+| `400` | Invalid body, query or header |
+| `401` | Missing or invalid API key |
+| `402` | `insufficient_credits`: prepaid credits are exhausted |
+| `403` | The key may not perform this operation |
+| `404` | The route or the target does not exist |
+| `409` | The request conflicts with current state, for example a reused `Idempotency-Key` |
+| `429` | Too many requests; retry after `Retry-After` |
+| `5xx` | Temporary failure; retry with the same idempotency key |
+
+A missing key and an unknown route return `{ "error": "<text>" }` without a
+code.
+
+## Sessions
+
+A session is a durable conversation with one deployed agent. It pins the
+deployment it was created on; see [Sessions and turns](/agents/sessions).
+
+| Operation | Method and path | Success |
+| --- | --- | --- |
+| Create | `POST /sessions` | `201` with the session, `200` when the key had already created it |
+| Get | `GET /sessions/<id>` | `200` with the session |
+| List | `GET /sessions` | `200` with `{ sessions }` |
+| End | `POST /sessions/<id>/end` | `200` with the ended session |
+| Interrupt | `POST /sessions/<id>/interrupt` | `200` with the session |
+
+### Create
+
+```bash
+curl -X POST 'https://app.opencomputer.dev/api/managed-agents/sessions' \
+ -H 'x-api-key: <api-key>' \
+ -H 'Content-Type: application/json' \
+ -H 'Idempotency-Key: topic/workshop/1' \
+ -d '{
+ "agentId": "<agent-id>@development",
+ "memory": {
+ "notes": { "scope": "document", "id": "workshop", "access": "read-write" }
+ }
+ }'
+```
+
+| Field | Meaning |
+| --- | --- |
+| `agentId` | `<agent-id>@development` or `<agent-id>@production`. The alias selects the environment and the deployment active in it. A bare agent ID means `production`. |
+| `deploymentId` | Pin one deployment instead of resolving an alias. Send `environment` with it; it may be omitted only when the deployment is promoted to exactly one environment. |
+| `environment` | `development` or `production`. Optional with `agentId`; it must agree with the alias. |
+| `memory` | Bindings keyed by resource ID, at most eight. Needs a deployed agent and an environment. Shapes on [Document memory](/agents/document-memory#session-bindings). |
+| `source` | `api` (default), `playground`, `channel` or `webhook`. The dashboard groups sessions by it. |
+
+The response is `{ session: { id, executionMode, status, createdAt }, deployment }`.
+The session starts without a turn; send one with the turns route.
+
+`Idempotency-Key` (at most 256 characters) makes a retry safe. The key
+identifies the session within your organization:
+
+- The same key with the same agent, deployment, environment and memory
+ bindings returns the existing session with `200`.
+- Anything else under that key is `409 idempotency_conflict`. The deployment
+ is part of the identity, so after a redeploy the same key conflicts:
+ a session pins its deployment, and new code needs a new session under a
+ new key.
+- `503 memory_admission_unconfirmed` means the memory grants were not
+ confirmed in time. Retry with the same key; the retry resumes the wait.
+
+Other codes: `400 invalid_environment`, `400 invalid_memory_binding`,
+`404 deployment_not_found`, `409 deployment_not_promoted` (a pinned
+deployment is not active in the named environment), `402
+insufficient_credits`.
+
+### Get and list
+
+`GET /sessions/<id>` returns the session:
+
+| Field | Meaning |
+| --- | --- |
+| `id`, `agentId`, `deploymentId` | The session and the deployment it pins |
+| `environment` | `development` or `production`, when the request named one |
+| `status` | `new`, `connecting`, `idle`, `running`, `waiting_runtime`, `suspending`, `suspended`, `resuming`, `failed` or `ended` |
+| `source` | The `source` given at creation |
+| `memory` | Bindings with `resource`, `scope`, `id`, `access` and `writable`; present when the session was created with bindings |
+| `turns` | Every turn: `id`, `input`, `mode`, `status`, `createdAt`, `updatedAt` |
+| `createdAt`, `updatedAt` | Timestamps |
+
+`GET /sessions` returns `{ sessions }`: the fifty most recently updated
+sessions of the organization, newest first, in the same shape. There are no
+filters or paging parameters; keep your own index of session IDs.
+
+### End and interrupt
+
+`POST /sessions/<id>/end` ends the session. Queued and running turns are
+cancelled and memory write access is revoked; do not send further turns to
+it. `503 session_end_unconfirmed` means the session is ended but the memory
+revocation was not acknowledged; retry, or
+[freeze the document](/agents/document-memory#disable-agent-writes). Ending an
+ended session returns it unchanged.
+
+`POST /sessions/<id>/interrupt` stops the running turn without spending a
+model turn. The turn settles as `cancelled` with reason `interrupted`, the
+runtime is told to stop, and the next queued turn starts. An idle session is
+returned unchanged.
+
+## Turns
+
+```bash
+curl -X POST 'https://app.opencomputer.dev/api/managed-agents/sessions/<session-id>/turns' \
+ -H 'x-api-key: <api-key>' \
+ -H 'Content-Type: application/json' \
+ -d '{
+ "input": "Plan the workshop.",
+ "idempotencyKey": "message-42",
+ "mode": "queue"
+ }'
+```
+
+| Field | Meaning |
+| --- | --- |
+| `input` | The user text for this turn. Required, not empty. |
+| `idempotencyKey` | Optional. The same key returns the existing turn; without one every request starts a turn. |
+| `mode` | `queue` (default): run after earlier turns. `steer`: deliver into the running turn when the runtime supports it, otherwise queue. `interrupt`: cancel running turns and run next. |
+
+The response is `202 { turnId, status, duplicate }` for a new turn and
+`200` with `duplicate: true` when the key had already created one. `status`
+is `queued` or `running`. Follow the turn in the event log; the response
+does not wait for it.
+
+Codes: `400 invalid_turn`, `402 insufficient_credits`,
+`409 memory_admission_pending` (retry the session creation with its key
+first), `409 memory_admission_rejected` (the session is ended; create a new
+one).
+
+## Events
+
+```bash
+curl 'https://app.opencomputer.dev/api/managed-agents/sessions/<session-id>/events?after=0' \
+ -H 'x-api-key: <api-key>'
+```
+
+`GET /sessions/<id>/events?after=<seq>` returns `{ events }`: up to 500
+events with `seq` greater than `after`, in ascending order. Each event is
+`{ id, seq, timestamp, sessionId, turnId, type, data }`; `turnId` is absent
+on session-level events.
+
+To read a whole log, start at `after=0` and repeat with the last `seq` you
+received until a page is empty. A full page means more may follow; read on
+without waiting. To follow a live session, keep polling from the last
+`seq`; the CLI's `sessions tail` and `useAgent` do exactly this. The log is
+durable, so a consumer that stops can resume from its cursor without gaps.
+Event types and their `data` are listed on [Session events](/agents/events).
+
+## Memory
+
+Document memory belongs to a project and an environment. Every route takes
+`?environment=development` or `?environment=production`. Bodies, conditional
+headers, the document object and the error codes are on
+[Document memory](/agents/document-memory#management-api).
+
+| Operation | Method and path | Success |
+| --- | --- | --- |
+| Resource inventory | `GET /projects/<p>/memory` | `200` with `{ resources }` |
+| List documents | `GET /projects/<p>/memory/<r>/documents` | `200` with `{ documents, nextCursor }` |
+| Read | `GET /projects/<p>/memory/<r>/documents/<id>` | `200` with the document and `ETag` |
+| Create | `PUT /projects/<p>/memory/<r>/documents/<id>` with `If-None-Match: *` | `201` |
+| Replace text or summary | `PUT /projects/<p>/memory/<r>/documents/<id>` with `If-Match` | `200` |
+| Change title or policy | `PATCH /projects/<p>/memory/<r>/documents/<id>` with `If-Match` | `200` |
+| Delete | `DELETE /projects/<p>/memory/<r>/documents/<id>` with `If-Match` | `204` |
+
+```bash
+curl -X PUT 'https://app.opencomputer.dev/api/managed-agents/projects/<project-id>/memory/notes/documents/workshop?environment=development' \
+ -H 'x-api-key: <api-key>' \
+ -H 'Content-Type: application/json' \
+ -H 'If-None-Match: *' \
+ -d '{ "title": "Workshop notes" }'
+```
+
+`startSessionOnDocument` in the
+[TypeScript SDK](/reference/typescript-sdk#serverless-agents-helpers) does
+this create and the session create in one call.
+
+## Webhooks
+
+Webhook configuration lives under the project; invocation uses the separate
+`/api/agent-webhooks/<id>/<token>` URL described on
+[Agent webhooks](/agents/webhooks).
+
+| Operation | Method and path | Body or query | Success |
+| --- | --- | --- | --- |
+| List | `GET /projects/<p>/webhooks` | Optional `?environment=` and `?agentId=` | `200` with `{ webhooks }`, URLs without tokens |
+| Create | `POST /projects/<p>/webhooks` | `{ name, agentId, environment, identity? }` | `201` with `{ webhook }`; `token` and the full `invocationUrl` appear once |
+| Update | `PATCH /projects/<p>/webhooks/<id>` | Any of `name`, `enabled`, `identity` (`null` removes it) | `200` with `{ webhook }` |
+| Rotate token | `POST /projects/<p>/webhooks/<id>/rotate-token` | None | `200` with `{ webhook }` carrying the new token |
+| Delete | `DELETE /projects/<p>/webhooks/<id>` | None | `204` |
+| Request ledger | `GET /projects/<p>/webhooks/<id>/requests` | None | `200` with `{ requests }` |
+
+`identity` is `header:<name>` or `body:<json-pointer>`. Creating a webhook
+for an agent that is not deployed in the environment is
+`409 webhook_target_unavailable`.
+
+```bash
+curl 'https://app.opencomputer.dev/api/managed-agents/projects/<project-id>/webhooks/<webhook-id>/requests' \
+ -H 'x-api-key: <api-key>'
+```
+
+## Projects, agents and deployments
+
+| Operation | Method and path | Success |
+| --- | --- | --- |
+| List projects | `GET /projects` | `200` with `{ projects }` |
+| Create project | `POST /projects` with `{ name, slug? }` | `201` with the project |
+| Get project | `GET /projects/<p>` | `200` with `{ project, deployments, sessions, connections, channels, schedules }` |
+| List agents | `GET /agents` | `200` with `{ agents }`: `id`, `name`, `activeAlias`, `activeDeploymentId`, `deploymentCount` |
+| List deployments | `GET /deployments?agentId=<agent-id>` | `200` with `{ deployments }` |
+| Get deployment | `GET /deployments/<id>` | `200` with `id`, `agentId`, `alias`, `memory` declarations, `createdAt` |
+
+A project is `{ id, slug, name, environments, agents, createdAt, updatedAt }`;
+`agents` carries each agent's `id` and `name`. See
+[Projects and agents](/agents/projects) for how projects are created and
+linked from the CLI.
+
+```bash
+curl 'https://app.opencomputer.dev/api/managed-agents/projects' \
+ -H 'x-api-key: <api-key>'
+```
+
+Secrets, runtime variables, schedules, channels, outboxes and logs are managed
+with the CLI and the dashboard; see their pages under Concepts.
diff --git a/docs/agents/events.mdx b/docs/agents/events.mdx
new file mode 100644
index 00000000..dfad24e2
--- /dev/null
+++ b/docs/agents/events.mdx
@@ -0,0 +1,151 @@
+---
+title: "Session events"
+description: "The durable event log every session keeps and every consumer reads"
+---
+
+Every session appends what happens to one durable log: the turns it
+accepted, the text the model produced, the tools it called, the memory it
+saved and the runtime that ran it. The log is what `opencomputer sessions
+tail --json` prints, what [`useAgent`](/agents/react) reduces to messages,
+what the dashboard's session view shows, and what
+[`GET /sessions/<id>/events`](/agents/api#events) returns.
+
+## Shape and ordering
+
+Each event is one JSON object:
+
+| Field | Meaning |
+| --- | --- |
+| `seq` | Position in the log, starting at 1 and increasing with every event |
+| `id` | A unique event ID |
+| `timestamp` | When the event was recorded |
+| `sessionId` | The session |
+| `turnId` | The turn the event belongs to; absent on session-level events |
+| `type` | One of the types below |
+| `data` | Fields specific to the type |
+
+`seq` is the cursor. A read with `after=<seq>` returns events with a greater
+`seq`, in ascending order, up to 500 at a time; repeat from the last `seq`
+you received until a page is empty, and keep polling from there to follow a
+live session. Because the log is durable and `seq` only grows, a consumer
+that stops can resume from its cursor without missing anything, and a page
+that overlaps one already read is harmless: apply events whose `seq` is
+greater than what you have applied. The React hook and the CLI work this
+way.
+
+Events at or below the cursor never change. New types can appear; treat an
+unknown type as informational and keep reading.
+
+## Event types
+
+### Session lifecycle
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `session.created` | The session exists. Always the first event. | `agentId`, `deploymentId` |
+| `session.status_changed` | The session moved between `new`, `connecting`, `idle`, `running`, `waiting_runtime`, `suspending`, `suspended`, `resuming`, `failed` and `ended` | `from`, `to` |
+| `session.ended` | The owner ended the session. Queued and running turns were cancelled and memory writes revoked. | none |
+| `session.failed` | The session cannot continue | `message`: a sanitized reason |
+
+### Turns
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `message.received` | A turn was accepted; its input is recorded before anything runs | `input`: the user text; `mode`: `queue`, `steer` or `interrupt` |
+| `turn.queued` | The turn waits for earlier turns | `mode` |
+| `turn.steered` | The input was delivered into the running turn | `activeTurnId` |
+| `turn.interrupted` | This turn cancelled the turns that were running | `interruptedTurnIds` |
+| `turn.started` | The runtime began the turn | none |
+| `turn.completed` | The turn finished and the session is idle again | none |
+| `turn.failed` | The turn stopped with an error | `message`: the runtime's reason, sanitized |
+| `turn.cancelled` | The turn was stopped by an interrupt | `reason`: `interrupted`; `replacementTurnId` when an interrupt-mode turn replaced it |
+
+A failed turn's `message` is the runtime's own error, for example a rejected
+model or a tool that is not available. Stack frames, URLs, file paths and
+credential-shaped values are removed and the text is capped at 500
+characters; when nothing readable remains, a generic sentence is sent.
+
+### Messages
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `message.delta` | The model produced a fragment of its reply | `text`: the fragment; concatenate deltas of one turn |
+| `message.completed` | The reply is complete | `text`: the whole reply |
+| `reasoning.delta` | The model produced a fragment of reasoning, when the model exposes it | `text` |
+| `reasoning.completed` | The reasoning is complete | `text` |
+
+### Tools
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `tool.started` | The model called a tool | `tool`: its name; `callId`; `title`; `input` |
+| `tool.progress` | The tool reported progress | runtime-defined |
+| `tool.completed` | The tool returned | `tool`, `callId`, `title`, `output` |
+| `tool.failed` | The tool raised an error | `tool`, `callId`, `message` |
+
+The exact fields come from the runtime's tool record; read them as optional
+and key a tool call on `callId` when it is present. The memory tools
+(`memory_save`, `memory_read`, `memory_list`) appear here like any other
+tool; the save itself is reported separately.
+
+### Memory
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `memory.saved` | A `memory_save` the session observed succeeding | `resource`, `documentId`, `revision`, `bytes` |
+
+Delivery is best-effort. A save commits independently of the event: a
+runtime that loses its connection can commit without ever reporting it, and
+the same event can be reported once or twice after a reconnect. Treat the
+event as a hint to re-read the document, and also re-read when attaching,
+reconnecting and completing work. See
+[Document memory](/agents/document-memory#saving).
+
+### Model and usage
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `model.route_resolved` | A model call is about to be sent | `providerCallId`; `requested` and `effective` as `{ provider, model }`; `runtime`; `access` |
+| `model.access_fallback` | A call planned on a connected model account fell back to managed access | `providerCallId`, `requested`, `from`, `reason` |
+| `usage.recorded` | A model call finished | `provider`, `model`, `inputTokens`, `outputTokens`, `reasoningTokens`, `cachedTokens`, `cacheWriteTokens`, `costUsd`, `payer` |
+
+### Outbound requests
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `egress.request` | A [declared connection](/agents/secrets) sent a request | `connectionId`, `method`, `path` |
+| `egress.response` | The destination answered | `connectionId`, `method`, `path`, `status`, `durationMs` |
+| `egress.failed` | The request did not complete | `connectionId`, `method`, `path`, `message` |
+
+### Runtime
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `runtime.connected` | An agent runtime attached to the session | none |
+| `runtime.disconnected` | The runtime went away; a running turn is queued again and resumes on the next runtime | none |
+| `runtime.suspended` | The runtime was suspended between turns | none |
+| `runtime.resumed` | The runtime was resumed | none |
+| `runtime.log` | A line of runtime output or a platform milestone | `level`, `stream`, `message` for output; `phase` and `message` for milestones such as the runtime starting |
+
+### Agent renders
+
+| Type | When | `data` |
+| --- | --- | --- |
+| `agent.rendered` | Your agent function was rendered for a model step | `renderId`, `instructions`, `model`, `enabledTools`, `enabledSubagents`, `enabledMcpServers`, `requiredConnections`, `input`, `tools`, `renderedAt` |
+
+One turn can carry several renders, one per model step. The debug inspector
+in the [playground](/agents/playground) shows the same data.
+
+## Reading the log
+
+From the CLI, with one NDJSON record per event:
+
+```bash
+opencomputer sessions tail <session-id> --after 0 --json
+```
+
+From an application, poll the [events route](/agents/api#events) or attach
+[`useAgent`](/agents/react), which turns `message.received`,
+`message.delta` and `message.completed` into messages, `turn.*` and
+`session.*` into `isRunning` and `ended`, and `memory.saved` into
+`memorySaves`; every event, these included, also reaches `onEvent`.
diff --git a/docs/docs.json b/docs/docs.json
index 22458231..e8d1cc11 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -40,6 +40,8 @@
"pages": [
"agents/reactive-agents",
"agents/sessions",
+ "agents/api",
+ "agents/events",
"agents/react",
"agents/memory",
"agents/schedules",
diff --git a/docs/llms.txt b/docs/llms.txt
index daf01b43..1dc78931 100644
--- a/docs/llms.txt
+++ b/docs/llms.txt
@@ -11,6 +11,8 @@
- [Deployments and environments](https://docs.opencomputer.dev/agents/deployments.md): Understand development, production, immutable deployments, and aliases.
- [Reactive agents](https://docs.opencomputer.dev/agents/reactive-agents.md): Render instructions, models, tools, subagents, and MCP servers from code.
- [Sessions and turns](https://docs.opencomputer.dev/agents/sessions.md): Create and inspect durable conversations.
+- [Management API](https://docs.opencomputer.dev/agents/api.md): Create sessions, send turns, read the event log and manage memory, webhooks and projects over HTTP with an API key.
+- [Session events](https://docs.opencomputer.dev/agents/events.md): Every event type in the durable session log, its data fields, and the seq cursor rules.
- [React integration](https://docs.opencomputer.dev/agents/react.md): Attach a React application to server-created sessions with useAgent, proxying three routes under your own authentication.
- [Agent capabilities](https://docs.opencomputer.dev/agents/capabilities.md): Choose between tools, MCP servers, skills, subagents, and outbound connections.
- [Secrets and outbound requests](https://docs.opencomputer.dev/agents/secrets.md): Store scoped secrets and inject them only into declared outbound requests.