Why WebSocket?
There is no REST endpoint for sending a text message and getting a reply — the WebSocket is the only way to exchange conversation turns in text mode. Opening one persistent, authenticated connection avoids repeating auth and connection setup on every turn, and lets the server push events proactively (like a session-expiry warning) without the client polling for them.This is a persistent connection, not token-by-token streaming. Each turn gets exactly one
response frame containing the AI’s complete reply — there is no chunk frame or partial-text
event. If you want a “typing” or token-reveal effect in your UI, animate the complete response
client-side (see Building a streaming UI) rather than expecting the server
to send partial tokens.Wire protocol
The WebSocket endpoint isGET {agent_url}/v1/sessions/{sid}/ws — served on the agent_url
returned by POST /v1/sessions, not necessarily the host you called to open the session.
Authentication
Non-browser clients (native apps, servers, embedded devices) send the same bearer token used to open the session, in theAuthorization header on the upgrade request.
Browsers cannot set custom headers on a WebSocket handshake, so first mint a short-lived,
single-use ticket:
?ticket= on the WS URL instead of the raw token:
Sending a message
Send one UTF-8 JSON text frame per turn:Receiving a response
message_id is a fresh UUID per response — use it to deduplicate if you retry sending logic on
your end.
Session expiry warning
Sent once, 60 seconds (configurable) before the session’s hard time limit:SESSION_ENDED error/close.
Frame types
One of
message (client → server), response, error, or session_expiring (server → client).Present only on
error frames. One of TURN_ERROR, SESSION_ENDED, or SERVICE_UNAVAILABLE
— see Error frames below.On
error frames, a human-readable description — for TURN_ERROR, this string is prefixed
with the real machine-readable reason (e.g. "INPUT_REJECTED: Contains a blocked phrase.").
On session_expiring, a display-ready warning string.Error frames
Errors during a turn arrive as a JSON frame on the same open connection — the WebSocket is not closed with a 4xxx code the way some APIs do it.code is one of:
To get the specific guardrail/FraudGuard reason out of a
TURN_ERROR, split message on the
first ": ":
Browser integration
Reconnection strategy
The session is not ended when the connection drops — the same session can be reconnected to (potentially served by a different agent process). WebSocket connections can drop due to network interruptions; implement reconnection with exponential back-off:When reconnecting mid-session, the server still has the conversation history in its own store —
you do not need to resend previous turns. Send the next user input directly once the new
connection is open.