mastra-voice
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Add voice to Mastra agents: text-to-speech with speak(), speech-to-text with listen(), realtime speech-to-speech with connect(), send(), answer(), and events, CompositeVoice to mix providers or AI SDK speech and transcription models, and LiveKit for interruptible phone and browser calls. Use this skill whenever the user wants an agent to talk or listen, transcribe audio, build a voice assistant, pick a voice provider, combine one provider for STT with another for TTS, or run a realtime voice session with tools. Triggers on Mastra voice, agent.voice, voice.speak, voice.listen, voice.connect, voice.send, voice.on('speaker'), CompositeVoice, MastraVoice, @mastra/voice-openai, @mastra/voice-openai-realtime, @mastra/voice-elevenlabs, @mastra/voice-deepgram, @mastra/voice-azure, @mastra/voice-google, @mastra/voice-google-gemini-live, @mastra/voice-xai-realtime, @mastra/voice-aws-nova-sonic, @mastra/livekit, @mastra/node-audio, TTS, STT, speech-to-speech.
- Reads credentials
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 46
Default speaker voice and language
- Very specific: line 103
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed provider package types (for …
- Very specific: line 104
Pick the mode and provider: Use references/providers.md.
- Very specific: line 109
For live calls: use @mastra/livekit with liveKitConnectionRoute() and a worker from @mastra/livekit/worker (see references/providers.md).
- Very specific: line 146
When the voice is attached to an agent, providers such as Nova Sonic add the agent's tools automatically.
- Very specific: line 161
Listeners pile up: Remove them with voice.off(event, callback) when a UI component unmounts.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Voice
Give an agent a voice provider through its voice option. Every provider implements one interface: speak() for text-to-speech, listen() for speech-to-text, and connect()/send()/on() for realtime speech-to-speech.
When to use
- Turning agent replies into audio (TTS)
- Transcribing audio files or microphone input before calling an agent (STT)
- Building a realtime voice assistant with streaming audio in and out (STS)
- Mixing providers: one for listening, another for speaking
- Using AI SDK speech or transcription models inside Mastra
- Running interruptible phone or browser calls through LiveKit
- Choosing a voice provider and its package, constructor, and env vars
When NOT to use
- Agent definition, instructions, and
generate()(usemastra-agents) - Tool schemas for realtime voice tools (use
mastra-tools) - Text streaming of agent output (use
mastra-streaming) - Chat model providers and model IDs (use
mastra-models) - Custom API routes and server config around voice endpoints (use
mastra-server) - Voice delivered through messaging channels (use
mastra-channels) - Tracing voice calls (use
mastra-observability)
Required inputs
- The mode: TTS only, STT only, both, or realtime speech-to-speech
- The provider(s) and their API keys or cloud credentials
- Runtime: Node server, CLI with microphone and speaker, browser, or phone (LiveKit)
- Audio format expectations (MP3, WAV, PCM, sample rate)
- Default speaker voice and language
- Whether the voice session needs tools and request context
The framework: one interface, three modes
import { Agent } from '@mastra/core/agent'
import { OpenAIVoice } from '@mastra/voice-openai'
import { playAudio } from '@mastra/node-audio'
import { createReadStream } from 'fs'
const voiceAgent = new Agent({
id: 'voice-agent',
name: 'Voice Agent',
instructions: 'You are a voice assistant that can help users with their tasks.',
model: 'openai/gpt-6.1-sol',
voice: new OpenAIVoice(),
})
// STT: audio in, text out
const transcript = await voiceAgent.voice.listen(createReadStream('./how_can_i_help_you.mp3'))
// Agent turn
const { text } = await voiceAgent.generate(transcript)
// TTS: text in, audio stream out
const audioStream = await voiceAgent.voice.speak(text, { speaker: 'default', responseFormat: 'wav' })
playAudio(audioStream)| Mode | Methods | Returns | Providers |
| TTS | speak(text, options) | NodeJS.ReadableStream (or void on realtime providers) | OpenAI, Azure, ElevenLabs, Deepgram, Google, Murf, Speechify, Cloudflare, Sarvam, Mistral, Inworld, ModelsLab |
| STT | listen(audio, options) | string, a text stream, or void on realtime providers | OpenAI, Azure, Deepgram, Google, Sarvam, Mistral, Inworld, Gladia, ElevenLabs |
| STS | connect(), send(), answer(), on(), close() | events: speaker, speaking, writing, error | OpenAI Realtime, Gemini Live, xAI Realtime, AWS Nova Sonic, Inworld Realtime |
Combine providers
import { CompositeVoice } from '@mastra/core/voice'
import { OpenAIVoice } from '@mastra/voice-openai'
import { ElevenLabsVoice } from '@mastra/voice-elevenlabs'
const voice = new CompositeVoice({
input: new OpenAIVoice({ listeningModel: { name: 'whisper-1', apiKey: process.env.OPENAI_API_KEY } }),
output: new ElevenLabsVoice(),
})input and output also accept AI SDK models such as openai.transcription('whisper-1') and elevenlabs.speech('eleven_turbo_v2'). A realtime provider, when set, handles speak() and listen() too.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installed provider package types (for examplenode_modules/@mastra/voice-openai). Otherwise fetchhttps://mastra.ai/reference/voice/overview.mdand the provider page underhttps://mastra.ai/integrations/voice/. Trust these over memory. - Pick the mode and provider. Use
references/providers.md. Check that the provider supports the mode (several are TTS only or STT only). - Install the provider package (
@mastra/voice-<provider>) and@mastra/node-audioif you play or record audio in Node. - Set credentials in
.envwith the variable the provider reads (for exampleOPENAI_API_KEY,ELEVENLABS_API_KEY,DEEPGRAM_API_KEY). - Attach the provider (or a
CompositeVoice) to the agent'svoiceoption, or use it standalone. - For realtime: register
on('speaker')andon('writing')listeners,await voice.connect()in atry/catch, thenspeak()/send(). Callclose()when done. - For live calls: use
@mastra/livekitwithliveKitConnectionRoute()and a worker from@mastra/livekit/worker(seereferences/providers.md). - Test with a short known audio file and a fixed phrase. Confirm format, sample rate, and latency.
Deep dive: realtime speech-to-speech
import { Agent } from '@mastra/core/agent'
import { getMicrophoneStream, playAudio } from '@mastra/node-audio'
import { XAIRealtimeVoice } from '@mastra/voice-xai-realtime'
const agent = new Agent({
id: 'voice-agent',
name: 'Voice Agent',
instructions: 'You are a helpful voice assistant.',
model: 'xai/grok-4.3',
voice: new XAIRealtimeVoice({
apiKey: process.env.XAI_API_KEY,
speaker: 'eve',
turnDetection: { type: 'server_vad' },
}),
})
await agent.voice.connect()
agent.voice.on('speaker', audioStream => playAudio(audioStream))
agent.voice.on('writing', ({ text, role }) => console.log(`${role}: ${text}`))
agent.voice.on('error', ({ message, code }) => console.error(code, message))
await agent.voice.speak('How can I help you today?')
await agent.voice.send(getMicrophoneStream())
// later
agent.voice.close()- Realtime
speak()andlisten()return nothing. Output arrives asspeaker/speakingandwritingevents. connect()must succeed beforesend()oranswer(). It rejects on failure. Anerrorlistener does not replacetry/catch.- When the voice is attached to an agent, providers such as Nova Sonic add the agent's tools automatically. Otherwise call
voice.addTools({...})andvoice.addInstructions('...')beforeconnect(). - OpenAI Realtime
connect({ requestContext })passes request context to tools.
Failure patterns
speak()returns undefined. The provider is realtime. Listen forspeakerevents instead.listen()throws on a TTS-only provider. ElevenLabs is documented both ways, and Murf, Speechify, Cloudflare, and ModelsLab are TTS only. UseCompositeVoicewith an STT provider asinput.speak()throws on Gladia. Gladia is STT only. Pair it with a TTS provider.- Azure auth fails. The package uses Azure Speech Services keys, not Azure OpenAI keys. Use
AZURE_API_KEYandAZURE_REGIONper the integration page. - Realtime call hangs silently. No
connect(), orconnect()rejected unhandled. Await it insidetry/catch. - Distorted audio. Sample rate or format mismatch with the player. Match the provider's output (for example PCM16 at 24 kHz on realtime providers).
- Leaked sessions and bills. No
close(). Close realtime sessions on hang-up, error, and process exit. CompositeVoicethrowsVOICE_COMPOSITE_NO_REALTIME_PROVIDER_SEND.send()needs arealtimeprovider.- Listeners pile up. Remove them with
voice.off(event, callback)when a UI component unmounts. - Wrong option names. Options are provider-specific.
speedis OpenAI,stabilityis ElevenLabs,propertiesis Murf. Check the provider page.
Output format
A finished voice delivery contains:
- The provider package(s) in
package.jsonand env vars in.env.example - The agent with
voiceset, or a standalone voice module - The audio path wired: file or microphone in, player, file, or HTTP response out
- For realtime: listeners,
connect()error handling, andclose()on every exit path - Evidence: a recorded transcript and an audio file produced by
speak(), or a working call
Reference files
references/api.md- Voice interface methods, events,CompositeVoice,MastraVoicebase config, agent integration.references/providers.md- Every voice provider: package, class, modes, constructor shape, env vars, defaults.references/examples.md- TTS reply, STT then agent then TTS, CompositeVoice with AI SDK models, realtime assistant with tools, LiveKit call.references/checklist.md- Voice checklist.
Live docs
- https://mastra.ai/reference/voice/overview
- https://mastra.ai/reference/voice/composite-voice
- https://mastra.ai/reference/voice/mastra-voice
- https://mastra.ai/reference/voice/voice.speak
- https://mastra.ai/reference/voice/voice.listen
- https://mastra.ai/reference/voice/voice.connect
- https://mastra.ai/reference/voice/voice.events
- https://mastra.ai/integrations/voice/openai
- https://mastra.ai/integrations/voice/livekit
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

