Building a Voice Agent: Speech-to-Text, LLM Replies and Natural TTS

Build a voice agent in three stages: Whisper transcribes speech, a language model writes a reply, and ElevenLabs speaks it back. A step-by-step tutorial.

By Galactic News Editorial · Published 2026-11-26T04:49:45+00:00 · Updated 2026-11-26T04:49:45+00:00

Building voice agent speech text TTS pipeline with Whisper and ElevenLabs

Building a Voice Agent: Speech-to-Text, LLM Replies and Natural TTS

A voice agent listens, thinks and speaks. A user talks. The agent turns speech into text. A language model writes a reply. Text-to-speech turns the reply back into voice. The whole loop should feel like a conversation, not a form. This tutorial builds that loop step by step. It uses OpenAI Whisper for speech-to-text, a language model for replies and ElevenLabs for natural text-to-speech. No step needs a paid plan to try.

Building voice agent speech text TTS pipeline

OpenAI's voice agent docs describe three architectures. The first is a single speech-to-speech model, such as the Realtime API. One model hears, reasons and speaks. The second is GPT-Live, a full-duplex session with a separate backend. The third is a chained pipeline. The app controls each stage: speech-to-text, then the agent, then text-to-speech. The chained pipeline is the best starting point. Each stage is visible and replaceable. You can store the transcript. You can run policy checks before the agent replies. You can generate speech only after the answer is approved. That control matters in production. The pipeline has four parts. A microphone captures audio. Whisper transcribes it into text. The language model generates a reply. ElevenLabs speaks the reply. Each part is a separate API call. The app connects them in a loop. Latency is the main challenge. A slow loop feels broken. A fast loop feels natural. The usual target is under one second from the end of speech to the start of the reply. Three tricks help. Stream the audio in chunks. Start the language model as soon as the transcript is ready. Stream the speech output as it is generated. Do not wait for the full reply before speaking. Speak the first sentence while the rest is still generating.

Step-by-step build

  1. Set up the project. Create a Python project. Install the OpenAI and ElevenLabs client libraries with pip. Store both API keys in environment variables. Never hard-code a key in the source.
  2. Capture audio from the microphone. Record short utterances, around 3 to 5 seconds each. Use a sample rate of 16000 Hz. Whisper works best at that rate. Save the audio as a WAV buffer in memory.
  3. Transcribe with Whisper. Send the audio buffer to the Whisper API. Use the whisper-1 model. Pass the language if you know it. The API returns a transcript. Keep the transcript for the conversation history.
  4. Generate the reply. Send the transcript to the language model with a short system prompt. Tell the model to answer briefly. Spoken replies should be short. One or two sentences work best. Long answers sound like a lecture. Pass the conversation history so the agent remembers context.
  5. Convert the reply to speech. Send the reply text to ElevenLabs. Pick a voice from the dashboard. Use the low-latency model for real-time use. The API returns an audio stream. Play it as it arrives.
  6. Close the loop. After the reply finishes playing, listen again. Repeat the cycle. Add a wake word or a push-to-talk button so the agent does not listen all the time.
  7. Add voice activity detection. Detect when the user stops speaking. A silence threshold of around half a second works well. Without it, the agent either cuts the user off or waits too long.
  8. Handle interruptions. Let the user speak while the agent is talking. Stop the audio playback when new speech is detected. This makes the conversation feel natural. The OpenAI Realtime API handles this natively. In a chained pipeline, you build it yourself.

Making replies sound natural

Natural speech is more than a good voice. The words matter first. Write the system prompt for spoken answers. Ask for short sentences. Ask for plain language. Forbid lists and bullet points in replies. A list read aloud sounds wrong. The model should answer the question, then stop. Voice settings come second. ElevenLabs lets you adjust stability, similarity and style. A higher stability setting sounds calmer. A lower one sounds more expressive. Test a few settings with real sentences. Pick the one that fits the use case. A support agent sounds different from a storyteller. Latency is the third factor. ElevenLabs offers a low-latency model built for real-time use. It returns audio in around 200 to 300 milliseconds for short sentences. That keeps the conversation flowing. The OpenAI Realtime API offers a different path. It runs speech-to-speech in one session. A session lasts up to 60 minutes. The model handles interruptions and turn-taking itself. That is simpler to build. The trade-off is control. You cannot inspect the text between stages. For many apps, that trade is worth it. For apps that need policy checks or logging, the chained pipeline stays the better choice. Phone calls need one more piece. Twilio Media Streams sends live call audio to your server over a WebSocket. Your pipeline processes it. The reply audio streams back to the caller. The same loop works. The transport changes, not the logic.

Common mistakes to avoid

Bottom line

A voice agent is three stages in a loop: Whisper turns speech into text, a language model writes a short reply, and ElevenLabs speaks it back. The chained pipeline gives the most control. The Realtime API gives the simplest build. Either way, keep replies short, stream every stage, and handle interruptions. That is what makes an agent feel like a conversation instead of a recording.