Prompt
How do I set up a telephony AI stack for an IVR replacement with call routing, recording consent, and fallback to a human agent?
Latest observation
A practical telephony AI stack for replacing an IVR usually has 5 layers:
- Telephony provider – handles inbound/outbound calls and transfers
- Conversation layer – speech-to-text, text-to-speech, and dialog orchestration
- Business logic / routing – decides where the caller goes
- Human fallback / contact center integration – queues or transfers to an agent
- Recording / compliance / observability – consent, storage, logs, analytics
1) Reference architecture
Inbound call flow
- Caller dials your number
- Telephony provider answers and streams audio to your AI app
- AI greets caller and requests recording consent if needed
- AI collects intent and any required fields
- Routing logic:
- resolve self-service if possible
- else transfer to human queue/agent
- else offer callback or voicemail
- Recordings/transcripts are stored according to policy
Typical stack
- Telephony: Twilio, Vonage, Plivo, SignalWire, or SIP trunk + Asterisk/FreeSWITCH
- Media streaming: Twilio Media Streams or SIP RTP media server
- ASR (speech-to-text): Deepgram, Google STT, Azure Speech, AssemblyAI
- LLM / dialog engine: OpenAI / Anthropic / hosted rules engine
- TTS: ElevenLabs, Azure TTS, Google TTS, Amazon Polly
- Routing/CRM: Salesforce, Zendesk, Genesys, Five9, Dialpad, custom REST API
- Storage: S3/GCS/Azure Blob for recordings + DB for metadata
- Observability: logs, traces, call transcripts, sentiment, QA tools
2) Core call flow design
A. Greeting and consent
You generally want the bot to:
- identify itself
- disclose recording/AI usage if required by your jurisdiction and policy
- ask for consent if needed
- provide an opt-out path to a human
Example:
“Hi, you’ve reached Acme Support. This call may be recorded for quality and training. I’m the virtual assistant. How can I help today?”
If you need explicit consent:
“Before we continue, do I have your consent to record this call?”
If the caller declines:
- stop recording
- continue without recording if allowed, or
- transfer to human / end call based on policy
B. Intent capture and routing
Common routing buckets:
- billing
- technical support
- sales
- account changes
- emergencies / urgent escalations
- unknown / low-confidence
Use both:
- rules for deterministic cases
- LLM classification for flexible intent extraction
Example routing logic:
- if intent = “lost card” or “fraud” → urgent queue
- if confidence < threshold → human fallback
- if caller requests agent → transfer immediately
- if after-hours → voicemail/callback or async ticket
C. Fallback to human agent
You need a reliable handoff mechanism:
- warm transfer if possible
- cold transfer if simple
- pass transcript, caller info, reason for transfer, and collected fields
Transfer targets:
- SIP extension
- contact center queue
- PSTN number
- webhook to agent-assist workflow
3) Recommended implementation pattern
Option 1: Fastest to production
Use:
- Twilio Programmable Voice
- Media Streams
- Your AI service for dialog
- Deepgram/Google for STT
- OpenAI/Anthropic for intent + response generation
- Twilio
<Dial>or contact center integration for transfer
This is usually the quickest path.
Why
- minimal telephony infrastructure to manage
- easy webhook/control flow
- built-in call transfer and recording support
4) Architecture components
Telephony layer
Responsibilities:
- answer inbound calls
- start/stop recording
- stream audio to AI
- transfer or bridge to human
- detect voicemail/AMD if needed
Key capabilities to look for:
- call control via webhooks
- bidirectional media streaming
- call status callbacks
- recording APIs
- conference bridges / warm transfer
Conversation engine
Responsibilities:
- maintain session state
- detect intent
- ask follow-up questions
- execute actions via tools/APIs
- decide when to escalate
Best practice:
- keep a structured state machine for routing
- use an LLM for natural language, not for everything
- use strict confidence thresholds before self-service actions
Consent and compliance module
Responsibilities:
- determine whether recording consent is required
- play disclosures at the correct time
- store consent outcome
- disable recording when required
- produce audit logs
Agent handoff module
Responsibilities:
- create a ticket or screen-pop
- attach transcript, summary, caller metadata
- route to correct queue
- preserve context in transfer
5) Recording consent handling
Recording consent is jurisdiction-dependent, so implement a policy engine rather than hardcoding one behavior.
Suggested policy fields
- caller country/state
- business region
- whether the line is inbound/outbound
- whether AI is speaking
- whether call recording is enabled
- whether explicit consent is required
Simple flow
- Before recording starts, play disclosure
- Ask for consent
- If yes, start recording
- If no, either:
- continue unrecorded, or
- transfer to a human who can continue without recording, or
- end call politely if policy requires
Important
- log consent decision and timestamp
- make sure recordings are not started before consent if your policy requires prior consent
- if you use transcription, treat that as a form of recording in many policies
6) Human fallback design
Use fallback when:
- user explicitly asks for a person
- confidence is low
- task is unsupported
- compliance or risk rules trigger escalation
- SLA requires human assistance
Fallback implementation pattern
- AI says:
“I’m going to connect you with a specialist.”
- Create an internal case:
- caller number
- reason for escalation
- summary
- transcript excerpt
- priority
- Transfer call:
- warm transfer to queue
- or conference in an agent
- If no agent available:
- offer callback
- collect message
- create ticket
7) Suggested data model
Store:
call_idcaller_numberstart_timeend_timeconsent_statusrecording_enabledintentconfidencerouting_decisionescalated_to_humanagent_idsummarytranscript_locationrecording_location
This helps with compliance, analytics, and QA.
8) Operational safeguards
Reliability
- set strict timeouts on AI responses
- have a “please hold while I connect you” fallback
- cache prompts and TTS where possible
- keep a dead-simple IVR fallback if AI fails
Safety / quality
- allow callers to say “agent” at any time
- don't let the AI improvise on regulated topics
- use a whitelist of actions the bot can take
- validate account info before making changes
Observability
- log every turn
- track containment rate
- transfer rate
- average handle time
- drop-off rate
- consent acceptance rate
- misroute rate
9) Minimal practical stack example
If you want something concrete:
- Twilio Voice for calls
- Twilio Media Streams for real-time audio
- Deepgram for streaming STT
- OpenAI for intent/routing and response generation
- ElevenLabs for TTS
- PostgreSQL for session state
- S3 for recordings
- Zendesk or Salesforce for human handoff
- Redis for low-latency session cache
10) Example call flow logic
Pseudo-logic:
on incoming call:
answer
if recording required:
play disclosure
ask consent
if no consent:
disable recording
greet caller
while call active:
listen
classify intent
if caller asks for agent or confidence low:
create handoff record
transfer to human
exit
if intent is self-service and allowed:
perform action
else:
clarify or escalate
11) What to build first
Start with an MVP:
- inbound answering
- consent disclosure
- speech-to-text
- intent routing
- transfer to human
- call logging/transcripts
- recording storage
Then add:
- CRM integration
- callback scheduling
- analytics dashboards
- QA review tooling
- multilingual support
- outbound follow-up calls
12) Common mistakes to avoid
- recording before consent when consent is required
- trying to make the LLM do all routing without rules
- no fallback if ASR/LLM/TTS fails
- losing transcript context on transfer
- not handling silence, barge-in, or interruptions
- unclear disclosure that callers are speaking to AI
- not testing transfers to real agents
If you want, I can give you one of these next:
- a specific Twilio-based architecture
- a sample webhook + state machine design
- a production readiness checklist
- a reference implementation in Python/Node.js