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.
| Subpath | Framework | Returns |
|---|---|---|
voiceai-sdk/ai | AI SDK | A tool set for generateText or streamText |
voiceai-sdk/mastra | Mastra | A tool map for a Mastra Agent |
voiceai-sdk/openai-agents | OpenAI Agents | A tool array for an OpenAI Agents Agent |
voiceai-sdk/mcp | MCP | Tools to register on an MCP server |
The tools
| Tool | Input | Output | What it does |
|---|---|---|---|
speak | text | ok, durationMs | Speaks the text into the connected transport, like session.say. |
listen | None | text, and speaker when available | Waits for the caller's next final transcript. |
transcribe | audio, base64-encoded | ok, text, language, durationMs, and reason when ok is false | Transcribes a recording with session.transcribe. |
end_call | None | ok | Closes 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:
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),
});
}import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import type { Session } from "voiceai-sdk";
import { voiceTools } from "voiceai-sdk/mastra";
export async function runAgent(session: Session) {
const agent = new Agent({
name: "receptionist",
instructions: "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.",
model: openai("gpt-4.1"),
tools: voiceTools(session),
});
await agent.generate("A caller is on the line.", { maxSteps: 50 });
}import { Agent, run } from "@openai/agents";
import type { Session } from "voiceai-sdk";
import { voiceTools } from "voiceai-sdk/openai-agents";
export async function runAgent(session: Session) {
const agent = new Agent({
name: "Receptionist",
instructions: "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.",
model: "gpt-4.1",
tools: voiceTools(session),
});
await run(agent, "A caller is on the line.", { maxTurns: 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. speakandlistenneed a transport. They use the audio attached withsession.connect. Connect it before the model runs.transcribeis for recordings. It takes base64 audio your app passes in, such as a voicemail, and doesn't use the transport.end_callcloses the session. Any tool call after it fails withclosed.
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.