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.
Lifecycle
Section titled “Lifecycle”- Create:
POST /v1/sessionswithavatar_idandmode.transportdefaults to"livekit", the only transport. Returnssession_id,transport,livekit_url,room_name,participant_token,agent_identity,expires_atandchat. Add"verbatim": trueto make the avatar say your exact words instead (see Bring your own brain). - Connect: the client joins the LiveKit room using
livekit_urlandparticipant_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_identityin the response is the identity the token gives your client, not the agent’s. See Streaming into your app. - 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 onchat.reply_topic(lk.transcription). Strip bracketed expression tags such as[curious]from that text before display. - Reconnect (automatic): LiveKit SDKs reconnect to the same room after short network blips. Do not mint a new session to reconnect.
- End:
DELETE /v1/sessions/{id}when the conversation is over, releasing the session’s resources and its concurrency slot.
Creating a session
Section titled “Creating a session”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.
Listing and inspecting sessions
Section titled “Listing and inspecting sessions”List sessions on your account, optionally filtered by status:
curl "https://api.2wai.ai/v1/sessions?status=active" \ -H "Authorization: Bearer $TWAI_SECRET_KEY"Fetch a single session:
curl https://api.2wai.ai/v1/sessions/$SESSION_ID \ -H "Authorization: Bearer $TWAI_SECRET_KEY"Reconnecting after a dropped connection
Section titled “Reconnecting after a dropped connection”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;}Ending a session
Section titled “Ending a session”Always end sessions you’re done with. This frees the concurrency slot immediately instead of waiting for the reaper:
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.
Idle reaping and caps
Section titled “Idle reaping and caps”- 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/sessionsreturns429 platform_busyeven if you are under your own cap.
See Limits & concurrency.
Session hygiene checklist
Section titled “Session hygiene checklist”- 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-sessionson responses so you can back off before you’re throttled. See Limits & concurrency.
- Streaming into your app for the client side of steps 2 to 4.
- Limits & concurrency for how many sessions you can run at once.