On this page

Interruptions and barge-in

Stop speaking when the caller talks, stop the model with it, and choose when a turn ends.

Callers don't wait for an agent to finish. When they talk over a reply, the agent should stop, listen, and answer what they just said. This is called barge-in. session.converse handles it for you; this page shows what happens and which parts you control.

What happens when the caller talks over a reply

  1. The session is speaking a reply your handler returned.
  2. Voice activity detection hears the caller start to speak.
  3. The session stops the speech, emits interrupt, and aborts ctx.signal for that turn.
  4. Once the caller has been silent for vad.silenceMs, the session calls your handler with the new text.
ControlWhat it does
ctx.signalAborts when the current turn is interrupted
vad.silenceMsHow long the caller must be silent before a turn ends
session.on("interrupt", fn)Runs fn each time speech is cut off
session.interrupt()Stops the current speech from your own code
session.capabilities.interruptionsWhether the session can stop speech partway

Stop the model with ctx.signal

A reply that was cut off is no longer needed, but the model keeps writing it unless you stop it. Pass ctx.signal to the model call and to anything else the handler waits on.

TypeScript
import { streamText } from "ai";

await session.converse(async (text, ctx) => {
  const res = await fetch("https://api.example.com/orders/latest", { signal: ctx.signal });
  const order = await res.json();

  const result = streamText({
    model: "openai/gpt-5",
    system: `Answer questions about this order: ${JSON.stringify(order)}`,
    messages: [...ctx.history, { role: "user", content: text }],
    abortSignal: ctx.signal,
  });
  return result.textStream;
});

When the caller interrupts, the fetch and the model stream both stop, and the model stops producing a reply nobody will hear. The next turn starts from the caller's new words.

Choose when a turn ends

vad.silenceMs is how long the caller must be silent before the session treats the turn as finished and calls your handler.

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

await using session = await createSession(
  { stt: deepgram(), tts: elevenlabs() },
  { vad: { silenceMs: 800 } },
);
  • A shorter value answers sooner, but can cut in when the caller pauses mid-sentence.
  • A longer value lets the caller pause to think, but every reply starts later.

Tune it on recordings of real calls. The right value depends on your callers and on what they are asked.

Pick up after an interruption

The interrupt event fires each time speech is cut off. Use it to tell the model on the next turn, so it doesn't repeat the whole reply or lose the thread.

TypeScript
import { streamText } from "ai";

let interrupted = false;
session.on("interrupt", () => {
  interrupted = true;
});

await session.converse((text, ctx) => {
  const note = interrupted ? " The caller interrupted your last reply. Don't repeat it in full." : "";
  interrupted = false;

  const result = streamText({
    model: "openai/gpt-5",
    system: "You are a helpful voice assistant. Keep answers short." + note,
    messages: [...ctx.history, { role: "user", content: text }],
    abortSignal: ctx.signal,
  });
  return result.textStream;
});

Stop speech from your own code

session.interrupt() stops the current speech right away. Use it when something outside the conversation decides, such as a supervisor pressing Stop in a dashboard.

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

const provider = { stt: deepgram(), tts: elevenlabs() };

export async function stopSpeaking(sessionId: string) {
  const session = await getSession(provider, sessionId);
  session.interrupt();
}

This example doesn't use await using. The session belongs to the call, and ending the scope would close it and hang up on the caller.

Check that the provider supports it

session.capabilities.interruptions is true when the session can stop speech partway. It depends on the provider; see Capabilities.

TypeScript
if (!session.capabilities.interruptions) {
  console.warn(`${session.provider} can't be interrupted. Keep replies short.`);
}

Where it is false, ask the model for short replies, so the caller doesn't wait long for a turn to speak.

Next steps

Esc

Loading…

↑↓ navigate openCtrl J preview