Skip to content

Sessions

A session is the unit of everything: one running avatar in one LiveKit room, from creation to teardown. This page covers the full lifecycle beyond the happy path in the Quickstart.

  1. Create: POST /v1/sessions with avatar_id and mode. transport defaults to "livekit", the only transport. Returns session_id, transport, livekit_url, room_name, participant_token, agent_identity, expires_at and chat. Add "verbatim": true to make the avatar say your exact words instead (see Bring your own brain).
  2. Connect: the client joins the LiveKit room using livekit_url and participant_token. The agent appears as the other participant in the room (the first remote participant, usually within about a second); subscribe to its video and audio tracks. agent_identity in the response is the identity the token gives your client, not the agent’s. See Streaming into your app.
  3. Chat: the client sends each user turn to the agent as a text stream on chat.send_topic (lk.chat), as plain text. The avatar’s own brain and voice reply on its tracks, with the reply text on chat.reply_topic (lk.transcription). Strip bracketed expression tags such as [curious] from that text before display.
  4. Reconnect (automatic): LiveKit SDKs reconnect to the same room after short network blips. Do not mint a new session to reconnect.
  5. End: DELETE /v1/sessions/{id} when the conversation is over, releasing the session’s resources and its concurrency slot.
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"
}'

avatar_id comes from the avatar_id field of catalog hydrate. For a developer-portal key it is a gen id, gen- plus 26 lowercase letters and digits.

"verbatim": true asks for verbatim speech (bring your own brain). It is granted only on an avatar 2wai has enabled for your account. Otherwise the create answers 403 verbatim_not_enabled. When it is granted, the response carries "verbatim": true, and chat gains say_topic (lk.say) and say_status_topic (lk.say.status).

Sending "transport": "livekit" is allowed but unnecessary. "transport": "mse-ws" is refused with 410 transport_retired, and any other value with 422 unknown_transport. A voice_id is accepted and ignored: the avatar speaks in the voice set on it in 2wai Studio.

List sessions on your account, optionally filtered by status:

Terminal window
curl "https://api.2wai.ai/v1/sessions?status=active" \
-H "Authorization: Bearer $TWAI_SECRET_KEY"

Fetch a single session:

Terminal window
curl https://api.2wai.ai/v1/sessions/$SESSION_ID \
-H "Authorization: Bearer $TWAI_SECRET_KEY"

LiveKit SDKs handle short network blips automatically: the SDK reconnects to the same room with the same participant_token, with no help from your mint proxy.

Do not call POST /v1/sessions again to reconnect. Every create is a new session in a new room, with a new agent. The old session does not close when you do this: it stays open, counting against your concurrency cap, until you end it or it is reaped.

If the connection is truly lost and you want to start fresh, end the old session first, then mint a new one:

async function startOver(oldSessionId) {
await fetch('/end', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session_id: oldSessionId }),
}); // your proxy calls DELETE /v1/sessions/{id}
await room.disconnect();
const session = await (await fetch('/mint', { method: 'POST' })).json();
await room.connect(session.livekit_url, session.participant_token);
return session;
}

Always end sessions you’re done with. This frees the concurrency slot immediately instead of waiting for the reaper:

Terminal window
curl -X DELETE https://api.2wai.ai/v1/sessions/$SESSION_ID \
-H "Authorization: Bearer $TWAI_SECRET_KEY"

Good times to call this: the user closes the chat or tab (a pagehide handler that fires navigator.sendBeacon to your backend), your app’s own idle timeout fires, or the conversation is explicitly ended.

  • A session with no activity is reaped after 30 minutes.
  • When your key is at its concurrency cap, idle LiveKit sessions can be reaped after 120 seconds to free a slot.
  • Beyond your key’s cap there is a platform-wide cap. When the platform is full, POST /v1/sessions returns 429 platform_busy even if you are under your own cap.

See Limits & concurrency.

  • End sessions when a user leaves; don’t rely on the reaper.
  • Don’t create a new session per message. One session covers an entire conversation: send every turn over the same room.
  • Let your LiveKit SDK handle reconnect. Never re-mint to reconnect; if you must start over, end the old session first.
  • Watch x-twai-concurrent-sessions on responses so you can back off before you’re throttled. See Limits & concurrency.