Live protocol
What goes over the wire between the client and the engine during a session: how it connects, how audio travels, and every message each way. Read it when you write your own client (a native app, another language, a phone bridge) instead of using the browser client, which does all of this for you.
A session goes like this:
- Your server starts the session and passes
session_id,client_token,engine_urlandice_serversto the client. - The client connects over WebRTC, or over a WebSocket when WebRTC can't connect.
- Audio flows both ways, and JSON messages travel beside it.
- The session ends: the client gets
session.endingandsession.ended, and the engine closes the connection.
The client token
The client token is the only credential the client holds. It works for any number of connection attempts until the
session's audio first flows, and for at most 5 minutes after the session started (client_token_expires_at). After
that it is spent: there is no reconnecting to a session.
- An attempt that never got audio flowing (WebRTC that didn't connect, say) gives the session back, so the next attempt, or the WebSocket fallback, can use the same token.
- A session the client never connects to ends with
expiredonce the token has run out. - Never put the token in a URL: the WebSocket takes it as a subprotocol (below).
WebRTC
The preferred transport. One exchange of offer and answer, with no trickle ICE.
The offer
The client makes an RTCPeerConnection with ice_servers as its ICE servers (STUN, and TURN with credentials good for
an hour), then adds:
- one audio track,
sendrecv, carrying the mic. Put Opus first in its codec preferences: the engine takes Opus only. - one data channel labelled
events, ordered, which the client creates.
An offer without both is refused. Wait for ICE gathering to finish (the browser client waits up to 2 seconds) so the offer carries every candidate, then send it:
POST {engine_url}/v1/sessions/{session_id}/connect
Authorization: Bearer {client_token}
Content-Type: application/json
{ "type": "offer", "sdp": "v=0…" }
The route answers any origin, so a page on your own domain can call it.
200 OK carries the engine's answer, with all its candidates:
{ "type": "answer", "sdp": "v=0…" }
Set it as the remote description. The engine's audio arrives as one remote track.
| Status | code | Why |
|---|---|---|
| 400 | bad_request | The body isn't { "type": "offer", "sdp": "…" }. |
| 401 | unauthorized | The token is missing, or isn't this session's. |
| 404 | not_found | The session ID isn't one. |
| 409 | already_connected | Another connection holds the session: a second tab, or a client that connected twice. |
| 410 | expired | The token ran out before the session connected. |
| 410 | session_ended | The session has ended. |
| 429 | rate_limited | Too many connection attempts from this IP (about 60 a minute, WebRTC and WebSocket together). Wait Retry-After seconds. |
| 503 | at_capacity / draining | The engine instance that took the request can't run another session. Trying again may reach another one. |
Errors have the same shape as the rest of the API: { "error": { "code", "message" } }.
Audio on WebRTC
- Opus, mono, in 20 ms frames, both ways. The engine decodes and encodes at 24 kHz; the RTP clock is Opus's 48 kHz.
- The engine sends the agent's voice in real time, as it plays, and marks the first packet of each stretch of speech. When the participant cuts in, it stops sending: nothing is queued on your side.
- Send the mic all the time. A muted mic should send silence (a disabled track does). Short gaps are filled with silence on the engine's side, so packets lost or not sent while quiet do no harm.
Connected, and hanging up
The session counts as connected once ICE and DTLS are up and the events channel is open. An attempt that gets there
within 20 seconds is the session's connection; one that doesn't is dropped, and the token still works.
To hang up, end the session first, then close the data channel. Closing the channel (or the peer connection)
on purpose ends the session as participant_ended. A connection that drops ends it as disconnected: when ICE stays
disconnected for 5 seconds, or fails.
WebSocket
The fallback, for networks that block WebRTC. Audio and messages share one socket.
GET {engine_url}/v1/sessions/{session_id}/ws (wss:// for an https engine_url)
Sec-WebSocket-Protocol: ve.v1, ve.token.{client_token}
Offer both subprotocols: the engine selects ve.v1 and never echoes the token back. Browsers can't set headers on a
WebSocket, and the subprotocol keeps the token out of URLs and access logs.
The engine checks the token before it upgrades. A refused upgrade is an HTTP answer with the same statuses as connecting over WebRTC (401, 404, 409, 410, 429), which a browser only sees as a close with code 1006. Once upgraded, a session that can't start closes the socket with:
| Close code | Why |
|---|---|
| 4409 | already_connected: another connection holds the session. |
| 1013 | The engine instance can't run another session (at_capacity or draining). Trying again may reach another one. |
| 1011 | The session couldn't start. |
Frames
- Text frames carry the JSON messages, one per frame.
- Binary frames carry audio: PCM16, little-endian, mono, 24 kHz, both ways.
- The engine sends the agent's voice in 20 ms frames (960 bytes), in real time.
- Send the mic continuously, in blocks of any length, in real time. Send silence while the mic is muted.
- Queue the agent's audio yourself and play it in order. When
agent.speech.endedcomes withinterrupted: true, the participant cut in: drop whatever is queued and not yet played.
Keeping the socket alive, and hanging up
The engine pings every 15 seconds and drops a socket that doesn't answer a ping before the next one, so a vanished client is noticed within 30 seconds. WebSocket libraries and browsers answer pings by themselves.
To hang up, end the session first, then close the socket. A close with code 1000 or 1001 ends the session as
participant_ended; any other close, or a missed ping, ends it as disconnected.
Falling back
Try WebRTC first. If it fails, or hasn't connected within a few seconds (the browser client waits 8), abort it and open
the WebSocket with the same token. If WebRTC connects after you gave up on it, close it: only one connection gets the
session, and the other is refused with 409.
Once connected, a dropped connection ends the session. To carry on, your server starts a new one.
Messages
Every message is a JSON object with a type. Fields are snake_case. Times in messages are numbers, milliseconds since
the epoch (unlike the HTTP API's ISO strings).
- Ignore a
typeyou don't know: new ones may be added. - The engine drops a message it can't read, silently. Nothing is sent back.
- Until the session is live, the engine reads only
session.end.
Chunks
A data channel can't carry very large messages, so any message over 15,000 characters travels as chunk envelopes:
| Field | Type | Meaning |
|---|---|---|
type | "chunk" | |
id | string | The same for every chunk of one message. |
seq | integer | The chunk's place, from 0. |
total | integer | How many chunks the message has (at most 1,000; the engine takes at most 128 from a client). |
data | string | Up to 15,000 characters of the message's JSON. |
To read one, collect the chunks of an id, join their data in seq order and parse the result as a message. Drop a
message whose chunks haven't all come within 30 seconds. To send one, cut the JSON into 15,000-character pieces.
The engine chunks only on WebRTC. On a WebSocket it sends whole messages, and takes either.
From the engine
type | Fields | When |
|---|---|---|
session.started | session_id; agent: { slug, version } (the version's number, or null); push_to_talk: present only on a push-to-talk session | The session is live. Sent once the agent's model is ready; the agent may speak right after. |
participant.speech.started | turn | The participant started speaking, or pressed the talk button. |
participant.speech.ended | turn | They stopped speaking, or released the button. |
transcript.delta | turn, role, delta | More of a turn's text, as it comes. |
transcript.done | turn, role, text, inaudible? | A turn's whole text. inaudible: true marks a participant turn the engine couldn't hear (blank, or words its transcription invents from noise): show a placeholder instead of text. |
transcript.removed | turn, reason | A turn was taken back, for example a second question in one reply, or a press of the talk button with nothing said. Take it off the screen. |
agent.speech.started | turn | The agent's line started playing. |
agent.speech.ended | turn, interrupted, heard_ms | It stopped: played to the end, or cut off when the participant cut in (interrupted: true). heard_ms is how much of it played. |
metrics | turn, timings | How long an agent line took to start, once it plays. timings is the transcript's line timings. For debugging; ignore it otherwise. |
tool.called | call_id, name, args, builtin, await_result | The agent called a tool that goes to the client. See Tools. |
frame.request | request_id, max_width? | The agent wants a camera or screen frame. See Camera frames. |
sidecar.result | key, handler, turn, data, captured_at | A helper model's result: key is the helper, handler vision_signal (what it read from a frame, captured_at the frame's time) or note (data.text, and data.answers from a helper that runs as a decision). turn may be null. |
recording.upload / recording.refused | Only for recordings, which Cullman's tests keep. See Recordings. | |
session.ending | reason | The session is closing, with its end reason. |
session.ended | reason | It has closed. The engine closes the connection next. |
error | code, message, fatal | A problem the engine reports. fatal: true means the session is over. Failures today end the session instead (session.ending and session.ended with error), so handle both. |
turn numbers the conversation's turns, the participant's and the agent's in one sequence, as the session's
transcript does afterwards. role is participant, agent or system.
A typical exchange, on an open-mic session where the agent speaks first:
session.started
agent.speech.started turn 0
transcript.delta … turn 0, agent
transcript.done turn 0, agent
agent.speech.ended turn 0, interrupted: false
participant.speech.started turn 1
participant.speech.ended turn 1
transcript.done turn 1, participant
agent.speech.started turn 2
…
session.ending reason: agent_ended
session.ended reason: agent_ended
The order of a turn's transcript and its speech events isn't fixed: text can come before, during or after the audio.
Place text by turn, not by arrival.
From the client
type | Fields | Does |
|---|---|---|
session.end | finished? (boolean) | Ends the session. See Ending. |
ptt.start | The talk button went down. See Push-to-talk. | |
ptt.stop | The talk button came up. | |
text.input | text (1 to 4,000 characters) | A participant turn typed instead of spoken. It cuts the agent off, as speech does, and the agent replies to it. |
tool.result | call_id, result (any JSON), failed? (boolean) | The answer to a tool.called. See Tools. |
frame | request_id, data_url, captured_at | The answer to a frame.request. See Camera frames. |
recording.start / recording.chunk | Only for recordings. See Recordings. |
Push-to-talk
A session is push-to-talk when its version says so; session.started then carries push_to_talk, and an open-mic
session's doesn't. Nothing the client sends changes which it is.
On a push-to-talk session:
- Send
ptt.startwhen the talk button goes down. The agent stops speaking and a participant turn starts (participant.speech.started). - Send
ptt.stopwhen it comes up (participant.speech.ended), and mute the mic at the same moment: keep sending frames, but silent ones (a disabled WebRTC track sends silence; over the WebSocket send zeros). The engine keeps the audio from before the release that is still on the way, and takes the turn at the first muted frame. If none comes, it takes the turn 300 ms afterptt.stop. - Keep sending frames all the time, before and after presses too. The engine only hears what comes during a press and until that first muted frame, and ignores the rest. Never send the open mic after the release: whatever arrives before the first muted frame is part of the answer.
- A press with nothing said is dropped (
transcript.removed). A new press before the engine has taken the turn replaces the earlier one.
The browser client does all of this itself: it opens the mic on the press and mutes it on the release. push_to_talk
holds nothing yet; that it's there is what says the session is push-to-talk. An open-mic session ignores ptt.start
and ptt.stop.
Tools
A tool set to go to the Participant's browser comes as tool.called:
| Field | Meaning |
|---|---|
call_id | The call's ID. Send it back with the answer. |
name | The tool's name. |
args | The arguments the agent called it with, a JSON object. |
builtin | Set for a tool the engine handles itself, such as ending the session: nothing to answer. Otherwise null. |
await_result | true: the agent waits for your answer. |
When await_result is true, send tool.result with the same call_id before the tool's timeout (set on the tool,
8 seconds unless changed). A late answer, or one for a call nobody waits for, is ignored, and the agent hears that the
tool is unavailable. When the tool couldn't do its work, send failed: true (result may be null): the agent hears
what the tool says for a failure.
{ "type": "tool.result", "call_id": "call_4f…", "result": { "shown": true } }
Where else a tool's calls can go is in Tools.
Camera frames
When the session was started with options.vision: true and its version has a helper that reads the camera, the
engine asks for frames with frame.request. Answer within 5 seconds, or the helper goes without:
| Field | Meaning |
|---|---|
request_id | The request's ID, from frame.request. |
data_url | The image as a data:image/… URL, at most 1,500,000 characters. JPEG is best, no wider than max_width when the request gives one. |
captured_at | When the frame was grabbed, in milliseconds since the epoch. |
On WebRTC a frame usually goes as chunks. The frames are read and not kept.
Recordings
Cullman's test calls and testers' sessions record the participant's camera and mic with recording.start,
recording.chunk and the engine's recording.upload answers. A session your server starts keeps no recording: the
engine answers recording.start with recording.refused. Your client needs none of these messages.
Ending
| Who ends it | What happens | end_reason |
|---|---|---|
| The agent | Its goodbye plays to the end, then session.ending and session.ended come, and the engine closes the connection. | agent_ended |
| The client | Send session.end. The engine answers session.ending and session.ended and closes the connection. Closing the connection on purpose without it ends the session the same way. | participant_ended |
| Your server | End a session. | client_command |
| The connection | It dropped (see WebRTC and WebSocket). | disconnected |
| Time | The session reached its longest length. | max_duration |
Add finished: true to session.end when the conversation had reached its natural end (the agent wrapped up and the
participant moved on), so the session counts as completed:
{ "type": "session.end", "finished": true }
Only the client can say that: closing the connection never marks a session finished.