A production-grade real-time voice AI agent for clinical appointment scheduling. Patients call a Twilio number, speak naturally in English, Hindi, or Tamil, and the agent books, cancels, or reschedules appointments — no IVR menus, no human in the loop.
Target latency: ≤ 450 ms from speech end → first audio byte (measured and logged per turn).
Caller ──► Twilio ──► WebSocket (/ws/call)
│
┌────────────▼─────────────┐
│ VAD (webrtcvad) │ mulaw 8 kHz → PCM 8 kHz
└────────────┬─────────────┘
│ on_speech_end (500 ms silence)
┌────────────▼─────────────┐
│ Deepgram Nova-2 STT │ PCM → transcript (multi-lang)
└────────────┬─────────────┘
│ text
┌────────────▼─────────────┐
│ LangGraph Agent │
│ Groq llama-3.3-70b │◄── system prompt (Redis + Postgres)
│ + 6 tools │
└────────────┬─────────────┘
│ sentence stream
┌────────────▼─────────────┐
│ ElevenLabs TTS │ ulaw_8000 stream → Twilio
└──────────────────────────┘
PostgreSQL (Neon) Redis (Upstash) Celery Worker
patients / doctors session turns reminders
slots / appointments language pref outbound calls
campaign logs pending confirm
cp .env.example .env # fill in all credentials (see table below)
docker compose up --buildAPI is live at http://localhost:8000. For Twilio to reach it locally:
ngrok http 8000
# copy the https URL, e.g. https://abc123.ngrok-free.appThen in .env:
WS_BASE_URL=wss://abc123.ngrok-free.app
API_BASE_URL=https://abc123.ngrok-free.app
Set your Twilio phone number's voice webhook to:
POST https://abc123.ngrok-free.app/api/twilio/voice
| Variable | Description |
|---|---|
DATABASE_URL |
Neon PostgreSQL connection string |
REDIS_URL |
Upstash Redis rediss:// URL |
GROQ_API_KEY |
Groq Cloud API key |
DEEPGRAM_API_KEY |
Deepgram API key |
ELEVENLABS_API_KEY |
ElevenLabs API key |
TWILIO_ACCOUNT_SID |
Twilio Account SID |
TWILIO_AUTH_TOKEN |
Twilio Auth Token |
TWILIO_PHONE_NUMBER |
Your Twilio number (E.164) |
LANGCHAIN_API_KEY |
LangSmith key (for tracing) |
WS_BASE_URL |
wss:// base for Twilio Media Streams |
API_BASE_URL |
https:// base for outbound TwiML |
Open http://localhost:8000/api/test in Chrome.
The page provides:
- Text chat with the full LangGraph agent (no audio hardware needed)
- Web Speech API mic — speak in Chrome, transcript sent to agent
- Language switcher — EN / हिंदी / தமிழ்
- "Call Me" button — triggers Twilio to call a verified number (reverse call)
- Browser TTS — agent responses spoken back via SpeechSynthesis
REST smoke tests:
# health
curl http://localhost:8000/api/health
# text chat with agent
curl -X POST http://localhost:8000/api/test/chat \
-H "Content-Type: application/json" \
-d '{"text": "Book me an appointment with a cardiologist"}'
# list doctors
curl http://localhost:8000/api/doctors
# list available slots
curl "http://localhost:8000/api/slots?specialty=Cardiology"
# latency log (last 20 turns)
curl http://localhost:8000/api/latency| Layer | Choice | Reason |
|---|---|---|
| Framework | FastAPI + Uvicorn | Async-native; WebSocket + REST in one process; auto-OpenAPI |
| Database | PostgreSQL on Neon | Serverless, scales to zero, asyncpg for async ORM |
| ORM | SQLAlchemy 2 (async) | Row-level locking (SELECT FOR UPDATE) prevents double-booking |
| Cache / Session | Redis on Upstash | Sub-ms reads, TTL auto-cleanup, serverless billing |
| STT | Deepgram Nova-2 (multi) |
Single model for EN/HI/TA; lowest WER on Indian accents |
| TTS | ElevenLabs (ulaw_8000) |
Native Twilio format — no FFmpeg re-encode saves ~50 ms/sentence |
| LLM | Groq llama-3.3-70b-versatile |
~50–100 ms TTFT (fastest available inference); strong tool calling |
| Agent | LangGraph StateGraph | Explicit topology, deterministic tool routing, streaming-compatible |
| Tracing | LangSmith | Full chain trace per call; every tool input/output visible |
| VAD | webrtcvad | C extension, ~0 ms overhead; no GPU; adequate quality at 8 kHz |
| Background jobs | Celery + Redis | Durable retry, Celery beat for hourly reminder scheduling |
┌─────────────────────────────────────────────────────────┐
│ SHORT-TERM Redis TTL=30 min │
│ key: session:{id}:{field} │
│ │
│ turns list[str] last 10 exchanges (FIFO) │
│ language str en / hi / ta detected │
│ pending JSON booking awaiting confirmation │
│ patient_id int caller identity │
│ agent_state JSON LangGraph checkpoint │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│ LONG-TERM PostgreSQL │
│ │
│ Patient profile, phone, language preference │
│ Doctor specialty, availability flag │
│ Slot FK→Doctor, start/end, is_booked │
│ Appointment FK→Patient+Slot, status, notes │
│ CampaignLog outbound call outcomes │
└─────────────────────────────────────────────────────────┘
system prompt = role instructions (~200 tok)
+ language rule (1 line)
+ patient context — name, last 3 appts (~100 tok)
+ last 6 session turns (~400 tok)
─────────────────────────────────
≈ 700 tokens total budget
The system prompt is rebuilt from scratch on every agent node call so stale context never leaks between turns.
- Reads are ~0.2 ms — keeping prompt-build off the hot path
- TTL cleanup is automatic (no cron needed)
- Each field updates independently (no read-modify-write of a full object)
When detect_and_set_language tool fires, it immediately:
- Updates
session:{id}:languagein Redis (instant for current call) - Writes
Patient.language_preferenceto Postgres (persists across calls)
The system was designed with a theoretical target latency of ≤ 450 ms. However, due to reliance on free-tier cloud services and rate limits across multiple APIs, real-world latency is significantly higher (often 2-4 seconds or more).
| Stage | Theoretical Budget | Real-World Observed | Why it's slow (Free-Tier Bottlenecks) |
|---|---|---|---|
| VAD silence detection | ~0 ms | 500 ms | Operates locally, but limits how fast we can even start processing. |
| Deepgram STT (Nova-2) | ≤ 250 ms | 500ms - 1.5s+ | Occasional API rate-limiting delays or cold starts on the pre-recorded endpoint. |
| Groq TTFT (Llama-3.3-70b) | ≤ 100 ms | 200ms - 1s+ | Free-tier token per minute (TPM) limits and occasional queueing/cold starts. |
| First sentence buffer | ≤ 50 ms | 50ms - 100ms | Depends entirely on LLM token generation speed reaching the first punctuation mark. |
| ElevenLabs TTS | ≤ 100 ms | 1s - 2s+ | Major Bottleneck: Free-tier accounts have tight concurrency limits and processing queues. |
| Postgres DB (Neon Free) | ~10-20 ms | 100ms - 2s+ | Major Bottleneck: Neon suspends free databases after 5 minutes of inactivity. Cold starts take several seconds. |
| Redis (Upstash Free) | ~1-5 ms | 50ms - 200ms | Free tier introduces network hops and occasional latency spikes. |
| Total (speech end → audio) | ≤ 450 ms | 2.5s - 5s+ | Free-tier reality across 5 different external services compounding delays. |
Every turn writes one JSON line to /app/latency_logs.jsonl:
{
"session_id": "...",
"vad_end_ms": 0,
"stt_complete_ms": 231,
"llm_first_token_ms": 308,
"tts_first_chunk_ms": 401,
"total_e2e_ms": 401
}All timestamps are offset from vad_end_ms = 0 (VAD fire time).
Read live: GET /api/latency returns the last 20 records.
- Groq inference — dedicated LPU hardware gives consistent TTFT vs cloud GPU, though we are capped by free limits.
- ElevenLabs
ulaw_8000— skips FFmpeg; Twilio receives mulaw directly. Highly optimized payload natively natively compatible with Twilio. - Sentence-level streaming — TTS starts on the first completed sentence; user hears audio before LLM finishes generating.
- Barge-in — active pipeline task is cancelled the moment VAD detects new speech; no waiting for current response to finish.
- Async throughout — FastAPI, SQLAlchemy, Deepgram, Redis all use async I/O; no thread blocking.
Reality Check: While architecturally optimized, stringing 5 different free-tier remote services (Neon, Upstash, Deepgram, Groq, ElevenLabs) together over public internet means network RTT + cold starts dictate actual latency.
1. Twilio dials → webhook POST /api/twilio/voice
2. TwiML returned: <Connect><Stream url="wss://.../ws/call"/></Connect>
3. WebSocket opens; "start" event received
4. Patient's phone number looked up → language loaded from Postgres
5. Greeting TTS played in patient's language
── per-turn loop ──────────────────────────────────────────
6. Twilio sends base64 mulaw 8kHz chunks as "media" events
7. VAD accumulates 20ms frames; detects speech start/end
8. on_speech_end puts (pcm_bytes, fired_at) on asyncio.Queue
9. Background processor:
a. If another pipeline task is running → cancel it (barge-in)
b. Send {"event":"clear"} to Twilio (flush its audio buffer)
c. Deepgram STT → transcript text
d. build_system_prompt() pulls Redis turns + Postgres context
e. set_tool_context() injects db/session_id/patient_id via contextvars
f. agent.astream_events() → stream LLM tokens
g. Sentence buffer: flush at [.!?।] → ElevenLabs → mulaw chunks → Twilio
h. Log latency checkpoints
i. Store turn in Redis session
──────────────────────────────────────────────────────────
10. On "stop" event: put sentinel on queue, cancel processor, cleanup Redis
| Tool | Description |
|---|---|
check_availability |
List open slots (by doctor or specialty, optional date filter) |
book_appointment |
Book a slot with full conflict resolution (4 checks + locking) |
cancel_appointment |
Cancel an existing appointment by ID |
reschedule_appointment |
Cancel old + book new atomically; rolls back on failure |
get_patient_context |
Fetch patient profile + last 3 appointments from Postgres |
detect_and_set_language |
Detect EN/HI/TA from Unicode ranges; persist to Redis + Postgres |
- Slot in the past → offer next available slot for that doctor
- Doctor is unavailable → offer slots from same specialty
- Slot already booked → offer nearest open slots for that doctor
- Patient time overlap → inform patient of their existing appointment
All booking uses SELECT FOR UPDATE row-level locking so concurrent calls cannot double-book the same slot.
Celery beat (hourly)
└─► schedule_reminders task
└─► query appointments due in next 24 h
└─► for each: OutboundCallService.initiate_call()
└─► Twilio REST API creates outbound call
└─► call connects to same /ws/call WebSocket
└─► agent greets in patient's language
└─► books / reschedules / logs outcome
Campaign outcomes (booked / rescheduled / rejected / no_answer) are written to CampaignLog in Postgres.
| Decision | Alternative considered | Why this choice |
|---|---|---|
| Groq (Llama 3.3 70B) | Gemini 2.0 Flash, GPT-4o-mini | Lowest TTFT of any provider; no quota issues on free tier |
| webrtcvad over Silero | Silero VAD (PyTorch) | C extension = ~0 ms overhead; +1 GB Docker image avoided |
| ElevenLabs ulaw_8000 | Deepgram TTS, Google TTS | Only provider with native mulaw output; saves FFmpeg step |
| Sentence-level TTS flush | Full-response TTS | TTFA ≈ time-to-first-sentence (~0.3 s) instead of full response |
| LangGraph over raw function calling | OpenAI function-calling loop | Explicit graph = traceable, deterministic, streaming-compatible |
| Single Uvicorn process | nginx + gunicorn + Uvicorn | Fewer moving parts; async event loop handles 100s of WS connections |
| Celery + Redis (not RabbitMQ) | RabbitMQ | Redis already in stack; avoids another managed service |
| contextvars for tool state | Global state / thread-locals | Safe in async context; no cross-request leakage |
-
Free-Tier API Latency & Limits — Stringing multiple free-tier cloud APIs (Neon DB cold starts, Upstash Redis network hops, Groq rate-limits, ElevenLabs queueing) results in compounded latency, heavily inflating the time from patient speech to first TTS response.
-
Tamil ASR accuracy — Deepgram Nova-2
multimodel has lower WER on Tamil than EN/HI as of 2025. A per-language model selector would improve this at the cost of one extra API round-trip. -
webrtcvad + background noise — aggressiveness=2 works well in quiet environments. Noisy call centers may need aggressiveness=3, which increases false negatives on soft speech.
-
mulaw conversion is lossy — audioop
ulaw2linis optimized for voice; not an issue for clinical calls, but limits the fidelity. -
No speaker diarization — assumes one speaker per call. Three-way calls could confuse the agent.
-
ElevenLabs rate limits — free tier: 10,000 characters/month. Production requires a paid plan to function consistently.
-
Database Cold Starts — Neon Postgres free tier suspends databases after 5 minutes of inactivity. When a call comes in after a period of quiet, the database takes several seconds to wake up, severely impacting the first interaction.
-
Celery beat not in docker-compose — the
workercontainer runs the Celery worker. A separatecelery beatprocess is needed for hourly reminders. Omitted from compose to prevent duplicate scheduler instances. -
ngrok URL changes on restart — free ngrok plan assigns a new URL each session. Update
WS_BASE_URL+ Twilio webhook URL after each ngrok session restart.
- Barge-in / interrupt handling — incoming speech cancels the active pipeline task mid-stream and clears the Twilio audio buffer
- Redis-backed memory with TTL — 30-minute sliding TTL on all session keys
- Background job queue — Celery + Redis for durable outbound reminder scheduling
- Horizontal scalability — stateless API containers; session state in Redis; multiple
apireplicas can run behind a load balancer; theworkerservice scales independently
Voice Agent/
├── docker-compose.yml
├── .env.example
├── architecture.png
└── backend/
├── main.py # FastAPI app, lifespan hooks, router mounts
├── config.py # Pydantic Settings (all env vars)
├── models.py # SQLAlchemy ORM — 5 tables with Integer PKs
├── database.py # Async engine, connection pool, Seeder
├── requirements.txt
├── Dockerfile
├── scheduling/
│ └── slots.py # SlotService, ConflictError, 4-check booking
├── agent/
│ ├── tools.py # 6 @tool functions + set_tool_context (contextvars)
│ └── graph.py # AgentState TypedDict, LangGraph workflow
├── memory/
│ ├── session.py # SessionMemory (Redis, TTL 30 min)
│ └── longterm.py # Postgres patient context, build_system_prompt
├── voice/
│ ├── vad.py # VAD wrapper (webrtcvad, 500 ms silence)
│ ├── stt.py # DeepgramSTT async client
│ ├── tts.py # ElevenLabsTTS streaming (ulaw_8000)
│ └── pipeline.py # VoicePipeline.handle_turn, latency logger
├── api/
│ ├── routes.py # REST + browser demo + test endpoints
│ └── websocket.py # Twilio Media Streams handler, barge-in logic
└── campaigns/
├── tasks.py # Celery app, send_reminder, schedule_reminders
└── outbound.py # OutboundCallService (Twilio REST API)
| Method | Path | Description |
|---|---|---|
| GET | /api/health |
Server health check |
| GET | /api/doctors |
All doctors with availability |
| GET | /api/slots |
Open slots (?doctor_id=, ?specialty=, ?date=) |
| GET | /api/patients/phone/{phone} |
Look up patient by phone number |
| POST | /api/patients |
Register new patient |
| GET | /api/appointments/{patient_id} |
Appointment history |
| GET | /api/latency |
Last 20 per-turn latency records |
| POST | /api/twilio/voice |
Twilio inbound call webhook (returns TwiML) |
| GET | /api/test |
Browser demo page (no phone needed) |
| POST | /api/test/chat |
Text chat with full LangGraph agent |
| POST | /api/test/call |
Trigger Twilio to call a verified number |
| WS | /ws/call |
Twilio Media Streams WebSocket |
