On this page

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, and session.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 converse passes to your handler as ctx.history.
  • Usage: session.usage(), with audio in and out in milliseconds and characters spoken.
TypeScript
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.

idkey
Set byThe SDK, when the session is createdYou, in the createSession options
Looks likeAn opaque stringAnything you choose, such as caller:+15551234567
Find a session withgetSession(provider, id)createSession(provider, { key })
Use it forReaching a session you already know aboutResuming 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.

TypeScript
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.

TypeScript
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 }.

TypeScript
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);
}
stateMeaning
connectingcreateSession is opening the provider connection.
openThe session is live. You can reach it by id or by key.
closingThe session was asked to close and is releasing its provider connection.
closedThe session has ended. It can't be resumed.

The CLI prints the same list:

Bash
npx voiceai-sdk sessions list --provider openai

Attach 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.

TypeScript
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

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview