All guides

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:

  1. Your server starts the session and passes session_id, client_token, engine_url and ice_servers to the client.
  2. The client connects over WebRTC, or over a WebSocket when WebRTC can't connect.
  3. Audio flows both ways, and JSON messages travel beside it.
  4. The session ends: the client gets session.ending and session.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 expired once 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.

StatuscodeWhy
400bad_requestThe body isn't { "type": "offer", "sdp": "…" }.
401unauthorizedThe token is missing, or isn't this session's.
404not_foundThe session ID isn't one.
409already_connectedAnother connection holds the session: a second tab, or a client that connected twice.
410expiredThe token ran out before the session connected.
410session_endedThe session has ended.
429rate_limitedToo many connection attempts from this IP (about 60 a minute, WebRTC and WebSocket together). Wait Retry-After seconds.
503at_capacity / drainingThe 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 codeWhy
4409already_connected: another connection holds the session.
1013The engine instance can't run another session (at_capacity or draining). Trying again may reach another one.
1011The 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.ended comes with interrupted: 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 type you 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:

FieldTypeMeaning
type"chunk"
idstringThe same for every chunk of one message.
seqintegerThe chunk's place, from 0.
totalintegerHow many chunks the message has (at most 1,000; the engine takes at most 128 from a client).
datastringUp 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

typeFieldsWhen
session.startedsession_id; agent: { slug, version } (the version's number, or null); push_to_talk: present only on a push-to-talk sessionThe session is live. Sent once the agent's model is ready; the agent may speak right after.
participant.speech.startedturnThe participant started speaking, or pressed the talk button.
participant.speech.endedturnThey stopped speaking, or released the button.
transcript.deltaturn, role, deltaMore of a turn's text, as it comes.
transcript.doneturn, 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.removedturn, reasonA 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.startedturnThe agent's line started playing.
agent.speech.endedturn, interrupted, heard_msIt stopped: played to the end, or cut off when the participant cut in (interrupted: true). heard_ms is how much of it played.
metricsturn, timingsHow long an agent line took to start, once it plays. timings is the transcript's line timings. For debugging; ignore it otherwise.
tool.calledcall_id, name, args, builtin, await_resultThe agent called a tool that goes to the client. See Tools.
frame.requestrequest_id, max_width?The agent wants a camera or screen frame. See Camera frames.
sidecar.resultkey, handler, turn, data, captured_atA 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.refusedOnly for recordings, which Cullman's tests keep. See Recordings.
session.endingreasonThe session is closing, with its end reason.
session.endedreasonIt has closed. The engine closes the connection next.
errorcode, message, fatalA 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

typeFieldsDoes
session.endfinished? (boolean)Ends the session. See Ending.
ptt.startThe talk button went down. See Push-to-talk.
ptt.stopThe talk button came up.
text.inputtext (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.resultcall_id, result (any JSON), failed? (boolean)The answer to a tool.called. See Tools.
framerequest_id, data_url, captured_atThe answer to a frame.request. See Camera frames.
recording.start / recording.chunkOnly 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.start when the talk button goes down. The agent stops speaking and a participant turn starts (participant.speech.started).
  • Send ptt.stop when 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 after ptt.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:

FieldMeaning
call_idThe call's ID. Send it back with the answer.
nameThe tool's name.
argsThe arguments the agent called it with, a JSON object.
builtinSet for a tool the engine handles itself, such as ending the session: nothing to answer. Otherwise null.
await_resulttrue: 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:

FieldMeaning
request_idThe request's ID, from frame.request.
data_urlThe 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_atWhen 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 itWhat happensend_reason
The agentIts goodbye plays to the end, then session.ending and session.ended come, and the engine closes the connection.agent_ended
The clientSend 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 serverEnd a session.client_command
The connectionIt dropped (see WebRTC and WebSocket).disconnected
TimeThe 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.