On this page

Agent tools

Give a model speak, listen, transcribe and end_call tools that act on a voice session, in the format your agent framework expects.

voiceTools(session) turns a session into tools a model can call. The model decides when to listen, what to say and when to hang up. The tools do the audio work on the session you pass in.

Import

Each framework has its own subpath. Every subpath exports voiceTools, which returns the same four tools in that framework's tool format.

SubpathFrameworkReturns
voiceai-sdk/aiAI SDKA tool set for generateText or streamText
voiceai-sdk/mastraMastraA tool map for a Mastra Agent
voiceai-sdk/openai-agentsOpenAI AgentsA tool array for an OpenAI Agents Agent
voiceai-sdk/mcpMCPTools to register on an MCP server

The tools

ToolInputOutputWhat it does
speaktextok, durationMsSpeaks the text into the connected transport, like session.say.
listenNonetext, and speaker when availableWaits for the caller's next final transcript.
transcribeaudio, base64-encodedok, text, language, durationMs, and reason when ok is falseTranscribes a recording with session.transcribe.
end_callNoneokCloses the session. converse resolves and the close event fires.

Use them with a framework

speak and listen act on the session's transport, so connect one first. This server opens a session for each WebSocket connection and hands it to runAgent:

server.ts
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
import { webSocketAudio } from "voiceai-sdk/ws";
import { runAgent } from "./agent.js";

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({ stt: deepgram(), tts: elevenlabs() });
      session.connect(webSocketAudio(ws));
      await runAgent(session);
    },
  },
});

agent.ts is the only file that changes between frameworks:

import { openai } from "@ai-sdk/openai";
import { generateText, stepCountIs } from "ai";
import type { Session } from "voiceai-sdk";
import { voiceTools } from "voiceai-sdk/ai";

export async function runAgent(session: Session) {
  await generateText({
    model: openai("gpt-4.1"),
    system: "You answer the phone for a bike shop. Use listen to hear the caller and speak to reply. Call end_call when they are done.",
    prompt: "A caller is on the line.",
    tools: voiceTools(session),
    stopWhen: stepCountIs(50),
  });
}

The instructions tell the model what each tool is for. The step or turn limit caps how many tool calls one run can make, so set it high enough for a whole call.

How the tools use the session

  • They call the session. A tool call does what the matching method does, so the session's options, capabilities and usage() apply as usual.
  • speak and listen need a transport. They use the audio attached with session.connect. Connect it before the model runs.
  • transcribe is for recordings. It takes base64 audio your app passes in, such as a voicemail, and doesn't use the transport.
  • end_call closes the session. Any tool call after it fails with closed.

Results and errors

transcribe passes ok: false and the reason back to the model instead of throwing, so the model can ask the caller to try again. A VoiceError, such as rate_limited or closed, fails the tool call. Each framework then reports the failure to the model or ends the run, in its own way.

See Errors for every code and whether retrying helps.

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview