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
connecting ──▶ open ──▶ closing ──▶ closed| State | What is happening |
|---|---|
connecting | createSession is opening the provider connection and checking your options. |
open | The session is live. It can listen and speak, and can be found by id or key. |
closing | Something asked the session to close. Current speech stops and the provider connection is released. |
closed | The 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.
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 throwsWithout 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.
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:
| Option | Closes the session |
|---|---|
timeoutMs | After this long in total, whatever is happening. |
idleTimeoutMs | After this long with no audio in or out. |
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.
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
Speechyou are awaiting or iterating rejects withVoiceErrorcodeclosed. listenloops end, andconverseresolves.- Any later call, such as
speakortranscribe, throwsVoiceErrorwith codeclosed.
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:
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
- Clean up sessions: patterns for servers that hold many sessions.
- Errors: every
VoiceErrorcode. - Sessions and keys: find a live session again by id or key.