0tokens

Topic / how to build a voice agent

How to Build a Voice Agent: A Complete Technical Guide

Learn how to build a voice agent from scratch. This technical guide covers ASR, LLMs, TTS, and latency optimization for creating human-like, real-time AI voice assistants.


Building a voice agent is no longer a project reserved for monolithic tech giants. With the advent of Large Language Models (LLMs), high-fidelity Text-to-Speech (TTS), and ultra-low latency Automatic Speech Recognition (ASR), developers can now build voice-first applications that feel indistinguishable from human conversation.

In this guide, we will break down the architectural components, the technical stack, and the step-by-step process of building a modern, low-latency voice agent capable of handling complex reasoning and natural interaction.

The Architecture of a Modern Voice Agent

Before diving into code, it is essential to understand the three-tier hierarchy that governs a voice agent's performance. Traditionally, this is known as the "Speech-to-Text-to-Speech" (STTS) pipeline.

1. Automatic Speech Recognition (ASR): This is the "ears" of the agent. It converts the user's spoken audio into text.
2. The LLM (Brain): The text is sent to an LLM (like GPT-4o, Claude 3.5, or a fine-tuned Llama 3) to generate a text response.
3. Text-to-Speech (TTS): This is the "voice." It converts the LLM's text output back into natural-sounding audio.

The primary challenge in voice interface design is latency. Humans expect a response within 500ms to 800ms. If your pipeline takes 3 seconds, the "magic" of the interaction disappears.

Step 1: Choosing Your Technical Stack

To build a production-grade voice agent, you need to select tools that prioritize speed and API stability.

The ASR Layer

  • Deepgram: Optimized for real-time transcription with sub-300ms latency.
  • OpenAI Whisper: Extremely accurate but requires optimization (like Whisper-large-v3-turbo) for real-time use.
  • AssemblyAI: Great for features like speaker diarization and sentiment analysis.

The Brain (LLM)

  • OpenAI GPT-4o / GPT-4o-mini: The gold standard for reasoning and JSON output.
  • Groq: Utilizing LPU (Language Processing Unit) technology, Groq provides the fastest inference for open-source models like Llama 3, which is critical for reducing "Time to First Token."

The TTS Layer

  • ElevenLabs: Widely considered the highest quality for emotive, human-like voices.
  • Cartesia: A newer player focused on ultra-low latency (Sonic model).
  • Play.ht: Excellent for long-form content and diverse regional accents, including Indian English.

Step 2: Setting Up the Orchestration Layer

You need a way to connect these three components. While you can build this from scratch using WebSockets (FastAPI or Node.js), many developers are now using orchestration frameworks to simplify the process.

  • LiveKit: An open-source framework specifically built for real-time audio/video. They provide a "Voice Pipeline Agent" SDK that handles the heavy lifting of audio buffering and VAD (Voice Activity Detection).
  • Vapi / Retell AI: These are "Voice-as-a-Service" platforms. They abstract the entire pipeline into a single API call, allowing you to focus on the prompt engineering rather than the infrastructure.

Step 3: Implementing Voice Activity Detection (VAD)

One of the hardest parts of building a voice agent is knowing when the user has finished speaking. If your agent interrupts the user, it’s annoying. If it waits too long, the silence feels awkward.

Effective VAD involves:

  • Silence Thresholds: Detecting a specific millisecond count of silence.
  • Interruption Handling: Designing the system to immediately stop the TTS stream the moment the ASR detects new incoming audio from the user.

Step 4: Crafting the System Prompt

A voice agent's prompt differs from a chatbot's prompt. Since the output will be spoken, you must instruct the LLM to:

  • Be Concise: Long paragraphs are tedious to listen to.
  • Use Verbal Cues: Include "umms," "ahhs," or transitional phrases like "Let me see..." to make the agent feel human.
  • Avoid Special Characters: Ensure the LLM doesn't output markdown, emojis, or complex tables that a TTS engine might struggle to read.

Example Prompt Snippet:
> "You are an AI assistant for a clinic in Bangalore. Keep responses under 20 words. Use a friendly tone. When asked about pricing, give a range rather than a list. Do not use asterisks or bold text."

Step 5: Handling Latency with Streaming

To get the best performance, you must stream everything.
1. ASR Streaming: Stream audio chunks via WebSockets to the ASR provider.
2. LLM Streaming: Use "stream=True" in your LLM configuration. As the first few words are generated, send them immediately to the TTS engine.
3. TTS Streaming: Use a TTS provider that supports chunked audio output. This allows the user's speakers to start playing the first word while the LLM is still finishing the last sentence.

Integration in the Indian Context

If you are building a voice agent for the Indian market, there are specific considerations:

  • Code-Switching: Many Indians mix English with Hindi, Kannada, Tamil, etc. (Hinglish). Using an ASR like Bhashini (an Indian government initiative) or sophisticated providers that support Indian accents is vital.
  • Network Stability: Unlike high-speed fiber everywhere, mobile data can fluctuate. Your agent needs robust reconnection logic and small audio packet sizes.

Testing and Iteration

Once your agent is live, you need to monitor "Turn-around Time" (TAT). Tools like LangSmith or Helicone can help you track how long each component of your pipeline takes. If the TTS is taking 1.2 seconds, you might need to switch to a faster model or reduce the output length of the LLM.

FAQ

Q: Can I build a voice agent for free?
A: You can build a local version using OpenAI’s Whisper (running locally), Llama 3 (via Ollama), and Coqui TTS. However, for a production-grade agent with low latency, you will likely need paid APIs.

Q: What is the best language for building voice agents?
A: Python is the industry standard due to its rich ecosystem of AI libraries (FastAPI, PyTorch, LangChain). However, Node.js is also popular for its non-blocking I/O, which is excellent for WebSocket-heavy applications.

Q: How do I prevent my agent from hallucinating?
A: Use RAG (Retrieval-Augmented Generation). By connecting your agent to a verified database of information, the LLM will provide answers based on facts rather than its training data.

Q: How do I handle interruptions?
A: Most modern orchestration layers (like LiveKit) provide a "can_interrupt" flag. When the ASR detects speech while the TTS is playing, the system sends a "clear" signal to the audio buffer, stopping the agent's voice immediately.

Building in AI? Start free.

AIGI funds Indian teams shipping AI products with credits across compute, models, and tooling.

Apply for AIGI →