On this page

Lifecycle and cleanup

How a session opens, stays open and closes, and how to make sure it always releases its provider connection.

A session holds an open connection to a provider, and often a transport. Closing it releases both. This page covers the states a session moves through, the ways it closes, and what happens to work still in progress.

States

Text
connecting ──▶ open ──▶ closing ──▶ closed
StateWhat is happening
connectingcreateSession is opening the provider connection and checking your options.
openThe session is live. It can listen and speak, and can be found by id or key.
closingSomething asked the session to close. Current speech stops and the provider connection is released.
closedThe session is finished. Calls on it throw VoiceError with code closed.

createSession resolves once the session is open, so code that holds a session rarely sees connecting. listSessions reports all four states; see Sessions and keys.

Close with await using

await using closes the session when the enclosing block ends, whether the block returns or throws. Use it whenever a session's lifetime fits one block, such as one request or one call.

TypeScript
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

export async function transcribeUpload(audio: Uint8Array) {
  await using session = await createSession(deepgram());
  const result = await session.transcribe(audio);
  return result.text;
} // the session closes here, even if transcribe throws

Without it, an error can skip your cleanup and leave the provider connection open.

Close by hand

When a session outlives one block, for example when you keep it in a map between requests, call session.close(). It is idempotent: calling it twice, or after a timeout already closed the session, does nothing.

TypeScript
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";

const calls = new Map();

const session = await createSession(openai(), { key: "caller:+15551234567" });
calls.set(session.key, session);

// later, when the call ends
await session.close();
calls.delete(session.key);

Timeouts

Two options close a session for you, so a session you forgot doesn't stay open:

OptionCloses the session
timeoutMsAfter this long in total, whatever is happening.
idleTimeoutMsAfter this long with no audio in or out.
TypeScript
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";

await using session = await createSession(openai(), {
  timeoutMs: 30 * 60_000, // never longer than 30 minutes
  idleTimeoutMs: 2 * 60_000, // or after 2 minutes without audio
});

timeoutMs is a hard limit, for a call left on hold or a loop that never ends. idleTimeoutMs ends a session whose caller has gone quiet. Set both on long-lived sessions.

The close event

close fires once, whichever way the session closed: await using, close(), a timeout, or the transport hanging up.

TypeScript
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

await using session = await createSession(deepgram(), { idleTimeoutMs: 60_000 });

session.on("close", () => {
  console.log(`session ${session.id} closed`, session.usage());
});

Use it to record usage, update your own call records, or remove the session from a map. Failures are reported separately, on the error event.

In-flight speech and the closed error

Closing a session stops it right away. It doesn't wait for speech to finish.

  • Speech that is still playing stops, as if you called session.interrupt().
  • A Speech you are awaiting or iterating rejects with VoiceError code closed.
  • listen loops end, and converse resolves.
  • Any later call, such as speak or transcribe, throws VoiceError with code closed.

To make sure the caller hears a last sentence, await it before the session closes. Treat closed as the normal end of a call, not a failure:

TypeScript
import { createSession, VoiceError } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

await using session = await createSession(elevenlabs());

try {
  await session.say`Thanks for calling. Goodbye.`;
} catch (err) {
  if (!(err instanceof VoiceError && err.code === "closed")) throw err;
}

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview