One session per caller
Give each caller a keyed session that is still there, with its history, when they reconnect.
Connections drop. A phone line breaks up, a browser tab reloads, a laptop changes networks. Without a key, each new connection starts a new session, and the agent forgets the conversation. With a key, the caller gets the same session back.
Give the session a key
Pass key to createSession. It is any string that identifies the caller.
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
const provider = { stt: deepgram(), tts: elevenlabs() };
await using first = await createSession(provider, { key: "caller:+15551234567" });
const again = await createSession(provider, { key: "caller:+15551234567" });
console.log(first.id === again.id); // trueWith a key, createSession first looks for a live session with that key. If there is one, you get it back. If there isn't, it starts a new one. A caller who reconnects, to this server or another, picks up where they left off.
Choose and store the key
A good key is one your server can compute again, from the same caller, on every connection:
- Phone calls: the caller's number, as in
caller:+15551234567. - Signed-in users: your user id from the authenticated request, as in
user:42. - Anonymous clients: a random id from
crypto.randomUUID(), created on first connect and stored by the client, for example in a cookie, then sent again when it reconnects. - Any of them: a prefix such as
caller:oruser:, so keys from different sources can't collide.
If you would rather keep session.id, store it where your server can read it on reconnect, such as your database, and use getSession (below).
Reconnect the caller
This server keys each session by the signed-in user. Each new socket from the same user gets the same session.
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
import { webSocketAudio } from "voiceai-sdk/ws";
import { answer } from "./agent";
import { getUserId } from "./auth";
const provider = { stt: deepgram(), tts: elevenlabs() };
Bun.serve({
port: 3000,
async fetch(req, server) {
const userId = await getUserId(req);
if (userId && server.upgrade(req, { data: { userId } })) return;
return new Response("unauthorized", { status: 401 });
},
websocket: {
async open(ws) {
const session = await createSession(provider, {
key: `user:${ws.data.userId}`,
idleTimeoutMs: 120_000,
});
session.connect(webSocketAudio(ws));
await session.converse(answer);
},
},
});When the socket closes, converse resolves and the session stays open. When the same user connects again, createSession returns that session, and session.connect attaches the new socket.
| After a reconnect | |
|---|---|
session.id and session.key | The same. |
ctx.history | The same turns, so a handler that passes it to the model continues the conversation. |
| The audio connection | Gone. Call session.connect with the new socket. |
Your converse call | Resolved when the old socket closed. Call converse again. |
You don't need storage of your own to remember the conversation. It comes back with the session.
Set how long a session waits
A keyed session doesn't close when its socket does, so something else has to close it. idleTimeoutMs closes the session once it has been idle for that long. A session with no socket connected is idle, so this is the window a caller has to come back in.
This is why the server above doesn't use await using. It would close the session when open returns, which is when the socket closes, and the caller would have nothing to come back to.
When the conversation is really over, such as when the model calls end_call, close the session then instead of waiting for the timeout. See Clean up sessions.
Find sessions by id or key
listSessions returns { id, key, startedAt, state } for each live session. getSession reconnects to one by id. Neither starts a new session.
import { getSession, listSessions } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
const provider = { stt: deepgram(), tts: elevenlabs() };
const live = await listSessions(provider);
const found = live.find((s) => s.key === "user:42");
if (found) {
const session = await getSession(provider, found.id);
console.log(session.key, session.usage());
}Don't use await using with getSession for a session a caller is still in. When the scope ends, it would close their session.
Next steps
- Sessions and keys: how keyed sessions work.
- Clean up sessions: make sure keyed sessions close when callers don't come back.
- Build a phone agent: key sessions by the caller's phone number.