
Twilio Voice AI development just got a whole lot more interesting with the integration of Langflow and OpenAI. This powerful combination enables developers to build voice AI applications that can understand and respond to human language in a more natural way.
Langflow's visual programming interface makes it easy to connect Twilio Voice AI to OpenAI's powerful language models. According to the article, this integration allows developers to build voice AI applications that can perform tasks such as text-to-speech, speech-to-text, and natural language processing.
With Langflow and OpenAI integrated with Twilio Voice AI, developers can create more sophisticated voice AI applications that can understand and respond to complex queries. This opens up a wide range of possibilities for voice AI development, from virtual assistants to customer service chatbots.
Expand your knowledge: Twilio Slack Integration
Setting Up Twilio
To set up Twilio, first log in to your Twilio account and purchase a voice-capable number if you don't already have one.
You'll need to configure the number by opening its configuration page, which is where the magic happens.
Under the voice configuration, you'll set up a webhook that will trigger when a call comes in. This is where you'll enter your tunnel URL, plus the /voice endpoint, as an example: https://this-is-an-example.ngrok-free.app/voice.
Save the configuration, then dial your number to test it out.
You should now be talking to your Langflow flow over the phone, and that's a big deal.
On a similar theme: Twilio - Sms/mms-svr
Adding TwiML
To add TwiML to your number, you must first navigate to the Twilio Console and click on your Twilio number in the Phone Numbers > Manage > Active numbers section. This will take you to the Voice Configuration section for your phone number.
Set Configure with to TwiML App, select your TwiML app from the dropdown menu, and then click Save. This will ensure that Twilio executes your code when a call is made to or from your phone number.
You might like: Twilio Texting App
You can test your voice assistant by calling your Twilio phone number from your verified phone number. A voice prompt will ask for your request and the system will intelligently respond to it.
You can also implement a feature that allows your voice assistant to remember previous interactions and extract context from it, allowing for more tailored and relevant responses. This can be achieved by integrating with external APIs, such as e-commerce platforms, to enable voice-based shopping experiences.
Take a look at this: Verify Twilio Number for Dev
Building AI Voice Assistant
You can build a real-time voice AI assistant with Twilio's ConversationRelay, LiteLLM, and Python. This project integrates Twilio's ConversationRelay with LiteLLM, allowing you to pick from multiple large language model (LLM) providers with a standardized API.
To get started, ensure you have Python 3.8+, API keys for the LLM providers you'd like to test, and a Twilio account with a registered phone number. You can sign up for a free Twilio account and purchase a Twilio phone number with Voice support.
Consider reading: Twilio Phone Number Validation
The Voice AI assistant is designed for quick integration with Twilio Voice. Here are its features:
- Real-time streaming responses via a WebSocket server (using the FastAPI framework)
- Multi-provider LLM support using LiteLLM
- Smoother voice interactions with our prompt letting the LLM know it's a voice assistant
- A straightforward Twilio Voice integration through ConversationRelay, letting you call the AI and chat at any hour!
To build this setup, you'll need to clone the repository, create and enter a virtual environment, install dependencies, and configure API keys. You can then pick which LLM provider to use, improve or customize the system prompt, and start the WebSocket server.
To integrate your server with Twilio Voice using ConversationRelay, you'll need to expose the server with ngrok, copy the provided HTTPS URL, and set up a TwiML bin with the ngrok URL. You'll then link your Twilio phone number to the TwiML bin and make a test call to your Twilio number to interact with the AI assistant in real-time.
Building AI-enabled voice applications is also easy with Twilio ConversationRelay and Langflow. You can connect a voice call to Langflow by building an application that can handle HTTP requests and WebSocket connections, and then direct ConversationRelay to set up a WebSocket connection to your application.
If this caught your attention, see: Twilio Phone Number
Essentials and Tools
To build a Twilio Voice AI application, you'll need a few essentials and tools. You'll need an installation of Langflow and an API key for an LLM to use within Langflow.
A free Twilio account with ConversationRelay enabled is also required, along with a voice-capable Twilio phone number. This will allow you to connect ConversationRelay to Langflow.
Here's a list of the tools you'll need to get started:
- Langflow with an API key
- Twilio account with ConversationRelay enabled
- Twilio voice-capable phone number
- Tunnelling service (e.g., ngrok, Cloudflare Tunnel, or Tailscale)
- Node.js (version 22 or the latest LTS)
Store Credentials as Environment Variables
Storing credentials as environment variables is a secure way to manage sensitive information. Environment variables are a built-in feature in most operating systems that allow you to store and retrieve sensitive information securely.
This approach is particularly useful when working with databases, APIs, or other services that require authentication. By storing credentials as environment variables, you can avoid hardcoding them in your code.
Some popular tools, such as AWS Secrets Manager and Hashicorp's Vault, also support storing credentials as environment variables. These tools provide additional security features, such as encryption and access controls, to protect sensitive information.
You might enjoy: Twilio Test Credentials
Essentials

To get started, you'll need a few essentials in your toolkit. You'll need an installation of Langflow and an API key for an LLM to use within Langflow.
You'll also need a free Twilio account with ConversationRelay enabled. This will allow you to connect your application to the ConversationRelay service.
A voice-capable Twilio phone number is also required. This will be used to receive and send voice calls.
To enable secure access to your application, you'll need a tunnelling service like ngrok, Cloudflare Tunnel, or Tailscale, or even VS Code.
Lastly, you'll need Node.js, specifically version 22, which was the latest LTS at the time of writing.
Suggestion: Twilio Application
Handling Voice Calls
Handling voice calls with Twilio Voice AI requires a server that can handle HTTP requests and WebSocket connections. This is because Twilio makes a webhook request to your application when a call is made to a Twilio phone number.
To build this server, you'll need to use a framework like Node.js and Fastify, which has great support for both HTTP and WebSocket connections. Fastify is a great choice because it's lightweight and easy to use.
To get started, create a directory for your application and initialize it with npm. Then, install the dependencies you'll need, including the Langflow Client and Twilio's API helper library, as well as Fastify's dependencies.
Connect Voice Calls to Langflow
To connect voice calls to Langflow, you'll need to build an application that can handle HTTP requests and WebSocket connections. This application will be built using Node.js and Fastify, which has great support for both HTTP and WebSocket connections.
Twilio makes a webhook request to your application when a call is made to a Twilio phone number. Your application needs to return an XML response, known as TwiML, that will direct ConversationRelay to set up a WebSocket connection to your application.
The Langflow Client and Twilio's API helper library are among the dependencies you'll need to install. Fastify dependencies are also required to build the server.
ConversationRelay and Langflow are a powerful combination for building AI-enabled voice applications. With this combination, you can handle different languages, hand off to a human, deal with interrupts, and prompt your model for the best results.
You can check out the code for this application on GitHub, and for more information on building AI applications and agents with Langflow, check out how to use web search in your flows or how to build a fashion recommendation app in Langflow.
Handling Webhooks
Handling webhooks is a crucial part of handling voice calls.
To start, we need to define a route for the initial webhook that will be made by Twilio when a phone call is received. This route should be an HTTP POST request.
Twilio will make a request to this endpoint and set up the ConversationRelay connection to the WebSocket. We need to set up that WebSocket endpoint.
To do this, we'll use the Twilio library to generate the TwiML response that we need. This response will be used to set up the connection.
We now need to run the application as well as a tunnel so that Twilio can connect to it. This tunnel should point to localhost:3000.
By following these steps, we can successfully handle the webhooks sent by Twilio and set up the necessary connections for voice calls.
Streaming
Handling voice calls can be a bit tricky, but there are ways to make it smoother. Interacting with the flow over the phone can be quick, but you might notice if the flow returns a long response you have to wait a while to hear it.
This isn't a great experience in a synchronous voice conversation. To optimize for this, you can request a streaming response from Langflow and start sending tokens to the ConversationRelay as you receive them. Twilio can then decide when to convert the tokens to speech as they arrive.
To set up streaming, you'll need to open your flow in Langflow again. Select the model component and toggle streaming to on. You might need to open the controls for the model to find the streaming option.
Once you've done that, you can replace the code that calls the model with code that sends the chunks straight to Twilio, setting the last property to false until the stream is complete. This will allow Twilio to buffer and turn the stream into audio as it is received.
Here are the key things to consider when choosing between streaming and waiting for the full response:
Restart your application and call your phone again, and you should find that the audio starts sooner, even if you get long responses from Langflow.
Featured Images: pexels.com


