Skip to main content
The Prompt WebSocket gives you real-time, bidirectional access to an agent’s work - tool calls, file edits, text output, and completion status as they happen. This guide covers the connection flow, event types, and patterns for robust integration. For the full protocol specification, see the Prompt WebSocket reference. For the simpler REST alternative, see Run Prompt.

When to use this

  • You want live agent output as the prompt executes (not just the final result)
  • You are building a UI that displays tool calls, file changes, or streaming text
  • You want to monitor an agent’s progress in real time from your terminal, a local script, or a backend service
If you only need the final result and do not need streaming, use the REST POST /api/sessions/{id}/prompt endpoint with GET /api/sessions/{id}/events (SSE) instead.

Prerequisites

  • An API key with sessions:prompt scope
  • A running session (state: running)
  • A WebSocket client library (websockets for Python, native WebSocket for JS/browsers)

Connection flow

The Prompt WebSocket uses a three-step flow: mint a token, connect, send the prompt.

Step 1: Mint a token

WebSocket connections cannot carry Authorization headers in the browser, so you exchange your API key for a short-lived, single-purpose token over HTTPS:
Tokens are short-lived and single-use. Mint a new one for every WebSocket connection. Never reuse tokens.

Step 2: Connect

Open the WebSocket using the token as a query parameter:

Step 3: Send the prompt

After the connection is accepted, send exactly one JSON text frame within 10 seconds:

Event types

After the prompt is accepted, the server streams events until a terminal done or error frame. Here are the event types in the order you will typically see them:

Full example

Resume a conversation

To continue a prior conversation on the same tab, reuse the same tab_id and set resume: true:
You must mint a new token for the same tab_id each time you reconnect.

Close codes

If the connection closes unexpectedly, check the close code:
Disconnecting the WebSocket does not cancel the agent. The prompt continues running in the background. To stop it, call POST /api/sessions/{id}/prompt/cancel. To recover the output, reconnect with resume: true or read the Prompt History.

Next steps

Handle long-running prompts

Cancel, rewind, and replay prompt history.

Manage sessions at scale

Lifecycle management, polling, and error recovery.

WebSocket reference

Terminal, prompt, and collaboration WebSocket specs.

Token Exchange reference

Full token-exchange endpoint specification.