DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

Create an AI Voice Agent for Customer Support

Why Voice AI for Customer Support?

Customer support is one of the most repetitive yet critical parts of any business. The same questions—“Where is my order?” “How do I reset my password?” “What��s my billing history?”—are asked over and over again. If you can replace a human agent with a smart, natural‑speaking voice assistant, you free up your team for complex problems while delivering instant, 24/7 help to your users.

Voice AI combines a speech‑to‑text (STT) engine, a natural‑language‑understanding (NLU) model, and a text‑to‑speech (TTS) service. The biggest bottleneck in most DIY stacks is the voice component: how do you make the bot sound human? That’s where modern TTS and voice‑cloning platforms come in.


The Building Blocks

Component What It Does Typical Libraries / Services
STT Converts spoken words into text Google Cloud Speech‑to‑Text, OpenAI Whisper, Amazon Transcribe
NLU Interprets intent and extracts entities Rasa NLU, Dialogflow, LUIS, OpenAI GPT‑4
TTS Turns bot replies into spoken audio ElevenLabs, Google Cloud TTS, Amazon Polly
Voice Cloning Makes the voice match your brand or a specific persona ElevenLabs, Resemble AI, Descript Overdub
Orchestration Routes audio streams, keeps context WebSocket, gRPC, serverless functions

The most time‑saving step is picking a TTS provider that offers high‑quality, controllable voice output and optional cloning. ElevenLabs is a standout because it gives you:

  • Real‑time streaming – no need to wait for a full audio file.
  • Voice cloning – upload a few minutes of audio and get a custom voice model.
  • Developer‑friendly API – simple REST and WebSocket endpoints, great SDKs.

Step‑by‑Step: Building a Simple Voice Agent

Below is a minimal Python example that stitches together:

  1. Speech‑to‑Text with Whisper (open‑source).
  2. NLU with OpenAI GPT‑4 (or your own Rasa model).
  3. Text‑to‑Speech with ElevenLabs.

Tip: This example runs locally. For production, consider containerizing the agent and scaling with Kubernetes or a serverless platform.

1. Install Dependencies

pip install openai whisper requests python-socketio
Enter fullscreen mode Exit fullscreen mode

If you’re on Windows, you may need ffmpeg for Whisper to work.

2. STT: Capture and Transcribe

We’ll use Whisper’s audio_to_text helper. In a real app, you’d stream the microphone audio instead of a static file.

import whisper

model = whisper.load_model("base")  # 0.4GB, good balance

def transcribe(audio_path: str) -> str:
    result = model.transcribe(audio_path)
    return result["text"]
Enter fullscreen mode Exit fullscreen mode

3. NLU: Get Intent & Generate Response

Using OpenAI’s GPT���4 to generate a response based on the transcribed text.

import openai

openai.api_key = "YOUR_OPENAI_KEY"

def generate_reply(user_text: str) -> str:
    prompt = f"You are a friendly customer support bot. Customer says: '{user_text}'. Respond concisely."
    completion = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.2,
        max_tokens=150,
    )
    return completion.choices[0].message.content.strip()
Enter fullscreen mode Exit fullscreen mode

4. TTS: Speak with ElevenLabs

ElevenLabs offers a WebSocket endpoint for streaming audio. The example below uses their REST API for simplicity.

import requests
import json

ELEVEN_API_KEY = "YOUR_ELEVEN_API_KEY"
ELEVEN_URL = "https://api.elevenlabs.io/v1/text-to-speech"

def text_to_speech(text: str, voice_id: str = "your-voice-id") -> bytes:
    headers = {
        "xi-api-key": ELEVEN_API_KEY,
        "Content-Type": "application/json",
    }
    payload = {
        "text": text,
        "voice_settings": {
            "stability": 0.5,
            "similarity_boost": 0.75
        }
    }
    response = requests.post(f"{ELEVEN_URL}/{voice_id}", headers=headers, data=json.dumps(payload))
    response.raise_for_status()
    return response.content  # raw audio bytes (mp3)
Enter fullscreen mode Exit fullscreen mode

Pro tip: Replace "your-voice-id" with the ID of a cloned voice from ElevenLabs. If you don’t have one yet, create it with the voice‑cloning dashboard.

5. Glue It All Together

import wave
import pyaudio

def play_audio(audio_bytes: bytes):
    wf = wave.open(io.BytesIO(audio_bytes), 'rb')
    pa = pyaudio.PyAudio()
    stream = pa.open(
        format=pa.get_format_from_width(wf.getsampwidth()),
        channels=wf.getnchannels(),
        rate=wf.getframerate(),
        output=True
    )
    data = wf.readframes(1024)
    while data:
        stream.write(data)
        data = wf.readframes(1024)
    stream.stop_stream()
    stream.close()
    pa.terminate()

def main():
    audio_path = "customer_question.wav"  # replace with live mic capture
    user_text = transcribe(audio_path)
    reply_text = generate_reply(user_text)
    audio_bytes = text_to_speech(reply_text)
    play_audio(audio_bytes)

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Run python voice_agent.py and you’ll hear a natural‑sounding reply. The whole pipeline can be wrapped in an async WebSocket server for real‑time interaction.


Voice Cloning in Action

Cloning lets you keep brand consistency. For example, if you have a recorded CEO voice that customers love, you can clone it and use it for the entire support bot.

  1. Upload a sample – a few minutes of clear speech.
  2. Generate a voice ID – ElevenLabs processes the audio and returns an ID.
  3. Use that ID – pass it to the text_to_speech function.
# Example: Creating a clone
clone_url = "https://api.elevenlabs.io/v1/voices"

headers = {
    "xi-api-key": ELEVEN_API_KEY,
    "Content-Type": "application/json"
}
payload = {
    "name": "CEO Clone",
    "file_url": "https://mycdn.com/ceo_sample.wav"
}
response = requests.post(clone_url, headers=headers, json=payload)
clone_id = response.json()["voice_id"]
print("Clone ID:", clone_id)
Enter fullscreen mode Exit fullscreen mode

Once you have the clone_id, you can replace "your-voice-id" in the TTS call. The resulting audio will sound like your CEO, creating a personal touch that static TTS voices can’t match.


Best Practices & Tips

Area Recommendation
Latency Use ElevenLabs’ streaming endpoint to pipe audio as it’s generated, reducing perceived wait times.
Fallback If STT fails, prompt the user to repeat or provide a text alternative.
Privacy Store audio only temporarily; delete after transcription.
Rate Limits ElevenLabs offers generous limits for development. For heavy traffic, request higher quotas.
Testing Build unit tests for each module (STT, NLU, TTS) to catch API changes early.
Accessibility Provide captions or transcripts for users who prefer text.
Cost TTS costs scale with usage. Monitor usage dashboards and set alerts.

Scaling the Agent

When you’re ready to move from a prototype to a production system:

  1. Containerize the agent with Docker. Use the official python:3.12-slim base image.
  2. Deploy to a cloud provider (AWS ECS, GKE, or Azure Container Apps).
  3. Orchestrate with Kubernetes and use a service mesh (Istio) for secure, observable traffic.
  4. Add a load balancer and horizontal pod autoscaling based on CPU or request queue length.
  5. Integrate with your existing ticketing system via webhooks.

Security & Compliance

Voice data is often regulated under GDPR, CCPA, or HIPAA. Make sure:

  • Encryption: All API calls use HTTPS.
  • Token protection: Store API keys in a secrets manager (AWS Secrets Manager, Vault).
  • Data residency: Choose a TTS provider that offers data centers in your region if required.
  • User consent: Explicitly ask users before recording or storing their voice.

Wrap‑Up

Building a voice‑enabled customer support agent is now more accessible than ever. By leveraging a powerful STT engine, a flexible NLU model, and a high‑quality TTS service like ElevenLabs, you can deliver a seamless, human‑like experience for your customers—all while freeing up human agents for the hard stuff.

If you’re ready to bring your brand’s voice to life, give ElevenLabs a try. Their platform makes voice cloning a breeze, and the real‑time TTS capabilities will keep your support bot sounding fresh and responsive.

👉 Try ElevenLabs today: https://try.elevenlabs.io/kr07zfuqn1bp

Happy coding—and happy speaking!

Top comments (0)