TwilioAmanda Lange, Matthew SetterTutorial
How to Orchestrate Multi-Call Conversations with an LLM and Twilio Conversation Memory with PHP
Summary
This tutorial shows how to build a PHP voice agent that retains caller context, preferences, and action history across separate inbound calls. It uses Twilio Conversation Memory with a Memory Store, an Open Swoole service, Twilio ConversationRelay, and an OpenAI integration using gpt-4o-mini; the example models an auto-repair shop assistant. The OpenAI service appends supplied prior customer context to its system prompt, while streamed responses are sent through a WebSocket and environment variables provide API credentials and the Memory Store ID. After running the service on port 9501, exposing it with ngrok, and configuring the phone-number webhook, the tutorial tests two calls from the same number and says the second call should remember details from the first.
Context
The tutorial addresses voice-agent interruptions caused by hangups or disconnections. Twilio Conversation Memory is presented as a way to persist context between calls, although it is not intended for sensitive information.
Approach / What changed
Create a PHP project with Composer dependencies, configure a Twilio Memory Store and environment variables, then connect an Open Swoole WebSocket server to Twilio ConversationRelay and OpenAI. The OpenAI service uses gpt-4o-mini, incorporates prior memory context into its system prompt, streams response tokens, and logs the interaction. The application is run locally and exposed to Twilio through ngrok.
Takeaways
Twilio Conversation Memory persists context between separate calls, allowing a returning caller’s previous discussion to be used without repeating details.
The OpenAI integration uses gpt-4o-mini with a 400-token limit and streams generated content through ConversationRelayHandler over a WebSocket.
Conversation Memory is not intended for sensitive information, and testing requires two calls from the same phone number after configuring the Twilio webhook.
FAQ
How does the agent remember details after a caller hangs up?
Twilio Conversation Memory persists context between calls. On a later call, the prior customer context is appended to the OpenAI assistant’s system prompt.
How are OpenAI responses delivered to the phone agent?
The PHP service creates a streamed gpt-4o-mini response. Each returned token is sent as JSON through the WebSocket handler, followed by a final message marking the stream complete.
What is required to test the multi-call memory flow?
The tutorial requires a Twilio account and voice-capable number, PHP 8.5 or later, Composer, an OpenAI API key, and ngrok. After running the service and pointing the incoming-call webhook to the ngrok URL followed by /voice, make two calls from the same number.
Source and freshness
- Source
- twilio.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.