On this page

AI SDK

Answer each turn with streamText, or give a model speak, listen and end_call tools with voiceTools from voiceai-sdk/ai.

The AI SDK writes the words; the session hears the caller and speaks. There are two ways to connect them. session.converse runs the turn loop and calls streamText once per turn. voiceTools(session) from voiceai-sdk/ai hands the model tools instead, so it decides when to speak, listen and hang up.

Install

npm install voiceai-sdk ai

The voiceai-sdk/ai subpath ships inside voiceai-sdk; ai is the AI SDK itself. The examples use the model string "openai/gpt-5", which the AI SDK resolves with its own credentials. Set those up as the AI SDK describes.

Quickstart

This server accepts audio over a WebSocket. Deepgram transcribes each turn, streamText writes the reply, and Deepgram speaks it.

TypeScript
import { streamText } from "ai";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { webSocketAudio } from "voiceai-sdk/ws";

Bun.serve({
  port: 3000,
  fetch: (req, server) => (server.upgrade(req) ? undefined : new Response("upgrade required", { status: 426 })),
  websocket: {
    async open(ws) {
      await using session = await createSession(deepgram());
      session.connect(webSocketAudio(ws));

      await session.say`Hi, how can I help?`;
      await session.converse((text, { signal }) => {
        const result = streamText({
          model: "openai/gpt-5",
          system: "You are a voice assistant. Answer in one or two short sentences.",
          prompt: text,
          abortSignal: signal,
        });
        return result.textStream;
      });
    },
  },
});

converse calls the handler with each final transcript. The handler returns textStream, and the session passes it to speak as the text arrives, so speech starts before the model has finished the reply.

  • signal aborts when the caller talks over the reply. Passing it as abortSignal stops the model as well as the speech.
  • history in the same context holds the earlier turns. This handler sends only the latest one.
  • When the socket closes, converse resolves and await using closes the session.

Let the model drive with voice tools

voiceTools(session) returns a tool set in the AI SDK's format. Pass it to generateText or streamText, and the model calls the session itself. Inside the same open handler, replace converse with:

TypeScript
import { generateText, stepCountIs } from "ai";
import { voiceTools } from "voiceai-sdk/ai";

// After session.connect(webSocketAudio(ws))
await generateText({
  model: "openai/gpt-5",
  system: "You take table bookings by phone. Use speak to talk and listen to hear the caller. Call end_call once the booking is confirmed.",
  prompt: "A caller just connected.",
  tools: voiceTools(session),
  stopWhen: stepCountIs(50),
});

generateText stops after one step by default, so the model would greet the caller and return. stopWhen: stepCountIs(50) lets it keep calling tools until it calls end_call or reaches 50 steps.

ToolWhat the model gets
speakSays text to the caller through the connected transport.
listenWaits for the caller's next final transcript and returns its text.
transcribeTurns a piece of audio into text.
end_callEnds the conversation and closes the session.

Use converse when every turn gets an answer and your code owns the loop. Use the tools when the model should decide what happens next, such as filling in a form or ending the call. Agent tools lists each tool's input and output.

Where keys stay

Everything on this page runs on your server. Two sets of credentials are involved, and neither reaches the browser:

  • The voice provider's key is read by the adapter, here DEEPGRAM_API_KEY for deepgram().
  • The model's credentials are read by the AI SDK when it resolves "openai/gpt-5". The session never sees them.
  • The browser streams audio over the WebSocket and plays what comes back. It holds no key.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview