Cancellation and lifecycle
Closing sessions, cancelling requests with AbortSignal, and how timeouts are applied.
Closing a session
A realtime session owns a network connection. Close it when the work it belongs to finishes, otherwise the process stays alive and the provider keeps billing.
const session = await voice.openSTTSession();
try {
// ...
} finally {
await session.close();
}close() waits for the provider to acknowledge and returns after five seconds
if it does not, so shutdown cannot block indefinitely.
There is no client-level close
Voice and the provider classes do not have a close() method. They hold
configuration, not connections. Every connection belongs to a session, and each
session closes itself.
Cancelling with a signal
Pass an AbortSignal to cancel one or more operations:
const call = new AbortController();
const listening = await voice.openSTTSession({ signal: call.signal });
const speaking = await voice.openTTSSession({ signal: call.signal });
call.abort();Aborting the controller closes both sessions. The same signal works on one-shot calls through the request context:
await voice.speak({ text: "Hello there." }, { signal: call.signal });Two behaviours are guaranteed across every provider:
- Aborting the signal closes the session.
- Opening a session with a signal that has already aborted rejects immediately,
matching how
fetchbehaves.
Timeouts
A timeout bounds the time until response headers arrive. Once headers are
received, the timer is cleared and the body streams without a deadline.
const voice = new Voice({
provider: new DeepgramProvider(),
options: { timeout: 30_000 },
});
// A per-call context overrides the client default.
await voice.speak({ text: "Hello there." }, { timeout: 5_000 });Streaming responses stay open for as long as generation takes, so a deadline
that covered the body would cancel valid requests. To bound the total duration
of an operation, use an AbortSignal.
Timeouts and retries are applied in the provider's HTTP transport. See Middleware for the order the layers run in.