Sessions and keys
What a session holds, how an id or a key identifies it, and how a reconnecting caller gets the same session back.
Everything in VoiceAI SDK happens on a session. This page covers what a session holds, the two ways to identify one, and how to find a live session again.
What a session is
A session is one open connection to a provider, or to two providers in a pipeline, plus the state the SDK keeps for it. createSession opens it, and it lives until it closes.
- Identity:
session.id,session.key, andsession.provider, a name like"deepgram". - Options: the language, voice, audio format and timeouts you passed to
createSession. - Capabilities:
session.capabilities, what this provider can do. - History: the turns of the conversation, which
conversepasses to your handler asctx.history. - Usage:
session.usage(), with audio in and out in milliseconds and characters spoken.
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram(), { language: "en" });
console.log(session.id, session.provider, session.capabilities);
console.log(session.usage()); // { audioInMs, audioOutMs, characters }Ids and keys
Every session has an id. A session has a key only if you give it one.
id | key | |
|---|---|---|
| Set by | The SDK, when the session is created | You, in the createSession options |
| Looks like | An opaque string | Anything you choose, such as caller:+15551234567 |
| Find a session with | getSession(provider, id) | createSession(provider, { key }) |
| Use it for | Reaching a session you already know about | Resuming the same conversation for the same caller |
Build keys from something stable about the conversation, such as the caller's phone number or your own user id. A prefix like caller: keeps keys from different parts of your app apart.
Resume a session with a key
Pass key to createSession. If a live session already has that key, you get that session back, with its history. If not, a new session starts with the key.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
const caller = "+15551234567";
await using session = await createSession(openai(), { key: `caller:${caller}` });
console.log(session.key); // "caller:+15551234567"This is how a caller who reconnects resumes the same session, even when the new connection reaches a different server. One session per caller walks through a full phone setup.
Reach a live session by id
getSession(provider, id) connects to a session that is already open, for example from an admin endpoint that needs to stop a reply. Pass the same kind of provider the session was created with.
import { getSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
export async function stopSpeaking(id: string) {
const session = await getSession(openai(), id);
session.interrupt();
}This example uses const, not await using. await using would close the session when the function returns, which ends the conversation for the code that owns it. Only the owner of a session should close it.
List sessions
listSessions(provider) returns each session for a provider as { id, key, startedAt, state }.
import { listSessions } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
for (const s of await listSessions(openai())) {
console.log(s.id, s.key, s.startedAt, s.state);
}state | Meaning |
|---|---|
connecting | createSession is opening the provider connection. |
open | The session is live. You can reach it by id or by key. |
closing | The session was asked to close and is releasing its provider connection. |
closed | The session has ended. It can't be resumed. |
The CLI prints the same list:
npx voiceai-sdk sessions list --provider openaiAttach metadata
metadata attaches your own values to a session, such as the tenant it belongs to or the id of the call in your system. Keys and values are both strings.
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram(), {
key: "caller:+15551234567",
metadata: { tenant: "acme", ticket: "4821" },
});Keep metadata to identifiers. Where it is visible beyond your own code depends on the provider, so don't put credentials or personal data in it.
Next steps
- Lifecycle and cleanup: the states a session moves through and how it closes.
- One session per caller: keys in a real phone app.
- Session API: every member of a session.