Building & Deploying a Voice Agent with Vapi, OpenRouter & MACH-AI
Overview
Build a production-ready AI voice agent that handles real-time phone conversations. This guide combines:
- Vapi - Voice AI platform for real-time phone conversations
- OpenRouter - Access to 100+ LLMs (GPT-4, Claude, Llama, etc.)
- MACH-AI - Deploy your backend with one command
By the end, you'll have a voice agent that can answer calls, understand speech, and respond intelligently.
Architecture
Caller (Phone) --> Vapi (Voice AI) --> MACH-AI Backend --> OpenRouter (LLM)
Flow: 1. Caller dials your Vapi phone number 2. Vapi converts speech to text (STT) 3. Text is sent to your MACH-AI backend webhook 4. Backend calls OpenRouter for AI response 5. Response flows back through Vapi as speech (TTS)
Prerequisites
- MACH-AI installed (
pip install mach-ai) - A Vapi account with API key
- An OpenRouter account with API key
- A phone number (optional, Vapi provides one)
Step 1: Create Your MACH-AI Project
mkdir voice-agent
cd voice-agent
mach-ai
Switch to architect mode:
/mode architect
Step 2: Generate the Voice Agent Backend
Use MACH-AI to generate the backend by describing your app:
Build a Flask backend for a Vapi voice agent webhook with:
1. POST /webhook/vapi endpoint that handles Vapi webhook events
2. Integration with OpenRouter API for LLM responses
3. Support for function-call and end-of-call-report events
4. A function that takes user message from Vapi, sends to OpenRouter, returns AI response
5. Environment variables for VAPI_API_KEY and OPENROUTER_API_KEY
6. A /health endpoint for monitoring
7. CORS support for *.machaao.com domains
Step 3: Configure Environment Variables
Set your API keys:
/env-vars set VAPI_API_KEY=your-vapi-key
/env-vars set OPENROUTER_API_KEY=your-openrouter-key
Or in your .env file:
VAPI_API_KEY=your-vapi-api-key
OPENROUTER_API_KEY=your-openrouter-api-key
Step 4: Set Up Vapi Assistant
Create an assistant in the Vapi Dashboard:
- Go to Assistants > Create Assistant
- Set the Voice to your preferred voice (e.g., "Sara" or "Charlie")
- Under Functions, add a function:
- Name:
respond - Description: "Respond to the user's question or request"
- Parameters:
user_message(string, required) - Under Server URL, set your MACH-AI webhook URL:
https://your-app.apps.machaao.com/webhook/vapi
Step 5: Deploy to Production
Deploy your backend with one command:
/deploy
Your voice agent backend will be live at:
https://your-app-name.apps.machaao.com
Step 6: Test Your Voice Agent
- In the Vapi Dashboard, click Test Call
- Or dial the phone number assigned to your assistant
- Speak naturally - your AI will respond in real-time
Customizing the System Prompt
Edit the system prompt in your backend to customize your agent's personality:
SYSTEM_PROMPT = """You are a helpful customer support agent for Acme Corp.
Keep responses concise and conversational since they will be spoken aloud.
Avoid using markdown, code blocks, or special formatting.
Be friendly, professional, and natural in your responses.
If you don't know something, say so honestly."""
Adding Function Calling
Vapi supports function calling for advanced interactions. Define functions your agent can use:
TOOLS = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}
}
}
]
Troubleshooting
| Issue | Solution |
|---|---|
| No audio from agent | Check TTS provider is configured in Vapi |
| Webhook errors | Verify URL is correct and server is running |
| OpenRouter rate limits | Check your OpenRouter plan limits |
| Calls drop immediately | Ensure webhook returns valid response |
Next Steps
- Add call recording and transcription logging
- Implement sentiment analysis on conversations
- Build an admin dashboard to view call history
- Add multi-language support with different voices