Skip to content

Bring your own brain

With verbatim speech, you decide what the avatar says. Your code writes the line and the avatar speaks it, in its own voice and with lip-sync, without its 2wai brain. Everything upstream of the line is yours: understanding the user, choosing a reply, retrieval and tools. You can use any language model, or none.

  1. Your server creates the session with "verbatim": true.
  2. The response carries "verbatim": true, and its chat object gains say_topic (lk.say) and say_status_topic (lk.say.status).
  3. The client that joined the room sends each line as a text stream on lk.say. One stream is one line, from 1 to 2,000 characters.
  4. 2wai checks the line (see Moderation and retention), then the avatar speaks it on its audio and video tracks. The text is echoed on lk.transcription, like any reply.
  5. The agent answers the sender on the data topic lk.say.status: accepted, or refused with a reason, then done when the line has finished playing.

The line goes from the client to the avatar over the room; there is no REST call per line. A server-side POST /v1/sessions/{id}/speak is planned and still answers 409 not_available_on_livekit today. If your model runs on your server, send its text to your own client (over your existing socket, for example) and let the client put it on lk.say.

Terminal window
curl -X POST https://api.2wai.ai/v1/sessions \
-H "Authorization: Bearer $TWAI_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"avatar_id": "gen-xxxxxxxxxxxxxxxxxxxxxxxxxx",
"mode": "video",
"verbatim": true
}'
{
"session_id": "ses_...",
"transport": "livekit",
"livekit_url": "wss://...",
"room_name": "ppstg-...",
"participant_token": "eyJ...",
"agent_identity": "viewer-...",
"verbatim": true,
"chat": {
"send_topic": "lk.chat",
"format": "text",
"reply_topic": "lk.transcription",
"say_topic": "lk.say",
"say_status_topic": "lk.say.status"
},
"expires_at": null
}
import { Room, RoomEvent } from 'livekit-client';
const room = new Room();
await room.connect(session.livekit_url, session.participant_token);
// Status for each line you send: accepted, refused (with a reason), then done.
room.on(RoomEvent.DataReceived, (payload, participant, kind, topic) => {
if (topic !== session.chat.say_status_topic) return;
const status = JSON.parse(new TextDecoder().decode(payload));
console.log(status.id, status.status, status.reason ?? '');
});
async function say(line) {
// One text stream per line. `info.id` matches `status.id` above.
const info = await room.localParticipant.sendText(line, {
topic: session.chat.say_topic,
});
return info.id;
}
await say('Hi! I can say exactly what you tell me to.');

Wait for the agent to join the room (the first remote participant) before you send the first line.

// On your server: decide what to say, with any model or none.
const completion = await openai.chat.completions.create({
model: '<your-model>',
messages: [{ role: 'user', content: userMessage }],
});
const text = completion.choices[0].message.content;
// ...send `text` to your client, which calls say(text) on the room.

If your model streams tokens, don’t wait for the whole reply. Cut it at sentence boundaries and send each sentence as its own line, so the avatar starts talking while the rest is still being written. Lines are spoken in the order you sent them.

let buffer = '';
for await (const chunk of stream) {
buffer += chunk.choices[0]?.delta?.content ?? '';
const match = buffer.match(/^(.*?[.!?])\s+/);
if (match) {
buffer = buffer.slice(match[0].length);
await say(match[1]);
}
}
if (buffer.trim()) await say(buffer.trim());
LimitValueWhen you pass it
Line length1 to 2,000 charactersrefused, too_long or empty
Lines per minute, per session20refused, rate_limited
Lines waiting or playing at once3refused, queue_full. Wait for a done before you send more.
Lines being received at once8refused, busy

A verbatim line counts as activity, so a session in use is not reaped as idle.

Each message on lk.say.status is JSON with a v (version, 1), the stream id, and a status:

statusMeaning
acceptedThe line passed its checks and is queued to speak.
doneThe line finished playing. interrupted is true if it was cut off.
refusedThe line will not be spoken. reason says why.
reasonMeaning
moderationThe moderation check flagged the line.
moderation_unavailableThe moderation check could not run. The line is refused rather than spoken unchecked. Retry.
not_enabledVerbatim speech was turned off for this avatar during the session.
too_long, emptyThe line is over 2,000 characters, or blank.
rate_limited, queue_full, busySee Limits.
read_timeout, read_errorThe text stream did not arrive whole within 10 seconds.
audit_unavailable, speak_failedSomething went wrong on our side. Retry.

No model rewrites your words. Two things still apply, as they do to every reply: Markdown and emoji are not read aloud, and the avatar’s pronunciation fixes, if it has any, are applied.

lk.chat still works in a verbatim session: a user turn sent there gets a reply from the avatar’s own 2wai brain. That brain never sees your lk.say lines, so it will not remember them. A turn on lk.chat also interrupts the avatar, including a verbatim line it is speaking. There is no separate interrupt for verbatim lines yet.

  • Every line is checked before it is spoken, by an automated moderation service (OpenAI’s moderation API). A flagged line is refused, and so is a line the check could not run on.
  • 2wai keeps the text of every checked line for at least 30 days, whether it was spoken or refused, for abuse review, and deletes it within about a day after that. Your avatar’s brain never sees it.
  • On its way to the avatar, each line also passes through our service providers for that step: OpenAI (the moderation check), Soniox (speech synthesis) and LiveKit (the room).
  • You remain responsible for what you make the avatar say, under the developer terms.
HTTPerror.codeMeaning
403verbatim_not_enabledThis avatar is not enabled for verbatim speech for your account.
422invalid_verbatimverbatim is not true or false, or the transport is not LiveKit.
503verbatim_unavailableThe session service could not grant verbatim speech right now. Retry, or create the session without it.