Back to Hub Directory
Step 2.2: Build the Callback Server (
How to Build an AI Phone Agent That Answers Customer Calls 24/7
By Weavecode Team
Published 2026-07-14
2 min read

How to Build an AI Phone Agent That Answers Customer Calls 24/7
Deploying an interactive voice agent requires orchestrating a fast, real-time pipeline. Any latency over 1 second during conversations destroys user experience, making the agent feel robotic and laggy.
This guide provides a step-by-step developer tutorial showing how to connect Twilio (telephony), Vapi/Retell (voice orchestration), ElevenLabs (voice generation), and Weavecode API (LLM brain) to handle customer support calls autonomously.
1. System Integration Flow
Customer Call ──► Twilio Phone Number
│
▼
[ Retell/Vapi ] ◄──► [ ElevenLabs TTS ]
│ (Stream Audio)
▼
Weavecode API (Fast LLM Brain)- Twilio: Provisions the phone number and handles routing.
- Retell AI / Vapi: Orchestrates the incoming call, manages Speech-to-Text (STT) transcription, handles customer interruption logic, and streams outputs.
- Weavecode API: Processes the transcription and generates immediate conversational text.
2. API Implementation Guide
We will create a FastAPI backend endpoint that handles Retell AI's dynamic system webhook callbacks.
Step 2.1: Implement the FastAPI Server
Install FastAPI and dependencies:
pip install fastapi uvicorn openaiStep 2.2: Build the Callback Server (main.py)
from fastapi import FastAPI, Request
from openai import OpenAI
import os
app = FastAPI()
# Initialize Weavecode OpenAI-compatible client
client = OpenAI(
base_url="https://api.weavecode.ai/v1",
api_key=os.getenv("WEAVECODE_KEY")
)
@app.post("/voice/callback")
async def voice_callback(request: Request):
payload = await request.json()
transcript = payload.get("transcript", "")
# 1. Evaluate user transcript using high-speed model
response = client.chat.completions.create(
model="google/gemini-1.5-flash",
messages=[
{"role": "system", "content": "You are a customer support agent. Give short, direct answers under 30 words."},
{"role": "user", "content": transcript}
]
)
answer = response.choices[0].message.content
# 2. Return text output to Vapi/Retell to convert to speech stream
return {
"response_text": answer,
"actions": []
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)3. Configuring Telephony (Twilio integration)
- Navigate to Retell AI Dashboard → Phone Numbers → Buy/Import Number.
- Connect your Twilio Account SID and Auth Token.
- Set the Inbound Webhook URL to your deployed FastAPI callback endpoint:
https://your-server.com/voice/callback. - Call the phone number to start interacting with your autonomous agent.
Related Articles
Business Automation
2 min readHow AI Voice Agents Save Call Centers Millions of Dollars
3 min read
3 min read