View Example on GitHub
Both
outbound_phone_example.py and inbound_phone_and_rag_example.py live in this folderVision Agents requires a Stream account
for real-time transport.
What You Will Build
- Make an outbound call programmatically (e.g. call your cell to test audio)
- Answer inbound calls on your Twilio number with a voice AI agent
- Handle bidirectional audio via Twilio Media Streams over WebSocket
- Bridge phone audio into a Stream call with
attach_phone_to_call
Prerequisites
Create a.env file at the Vision Agents repo root:
Run the example
Start ngrok
Expose port 8000 so Twilio can reach your local server:Copy the HTTPS hostname (without
https://) — you’ll use it as NGROK_URL.Configure your Twilio number
In the Twilio Console:
- Go to Phone Numbers → Manage → Active numbers
- Select your number (or buy one)
- Under Voice Configuration, set A call comes in to Webhook
- Enter
https://<NGROK_URL>/twilio/voicewith method HTTP POST
Make an outbound call
In a new terminal, from the example directory:Replace with your Twilio number (
--from) and a destination you can answer (--to, often your cell). This starts the HTTP server and initiates the outbound call.Run the inbound agent
With ngrok and your Twilio webhook still configured, start the inbound server:RAG is optional at this stage — the agent runs with Gemini even without extra RAG configuration.
How it works
Twilio uses TwiML to control calls. The voice webhook returns a<Connect><Stream> response that pipes audio to your WebSocket:
POST /twilio/voice— validates the Twilio signature, registers the call inTwilioCallRegistry, returns TwiML with a media stream URLWS /twilio/media/{call_id}/{token}— accepts the WebSocket, runsTwilioMediaStream, validates the tokenattach_phone_to_call— bridges Twilio mulaw audio ↔ the Stream call where your agent runs
ProxyHeadersMiddleware so signature validation works when ngrok terminates HTTPS.
Next Steps
Phone Support Agent (RAG)
Add Gemini FileSearch or TurboPuffer knowledge retrieval
Twilio Integration
Plugin API reference and components
Phone Calling
Provider overview and learning path
Telnyx
Alternative telephony provider