TwilioPaul KampTutorial
Integrate Claude, Anthropic’s AI Assistant, with Twilio Voice Using Conversation Relay
Summary
Twilio’s Conversation Relay enables real-time voice applications with AI LLMs over a WebSocket connection. This tutorial demonstrates integrating Claude, Anthropic’s AI model, with Twilio Voice using Conversation Relay. The setup uses a Node.js server with Fastify, the Anthropic SDK, and a system prompt that instructs the model to spell out numbers and avoid special characters for better text-to-speech. The server exposes two endpoints: /twiml for TwiML instructions and /ws for the WebSocket relay. The tutorial tests the integration with Claude Sonnet 5.5 and Haiku 5.5 at low effort, both handling real-time voice well. The result is a functioning voice assistant that can be called via a Twilio phone number.
Approach / What changed
The tutorial sets up a Node.js server using Fastify with WebSocket support, installs the Anthropic SDK, and defines a system prompt that instructs the model to spell out numbers and avoid special characters. It creates two endpoints: /twiml returns TwiML that connects an incoming call to a Conversation Relay WebSocket, and /ws handles the WebSocket messages, managing sessions per call and sending the assistant’s text response back to Twilio. The server is exposed via ngrok, and the Twilio phone number is configured to point to the /twiml endpoint.
Takeaways
Conversation Relay connects to a WebSocket you control, allowing integration with any LLM provider over a fast two-way connection.
The system prompt should instruct the LLM to spell out numbers and avoid special characters to improve text-to-speech output.
Claude Sonnet 5.5 and Haiku 5.5 both handle real-time voice well at low effort, so the model choice depends on the use case.
FAQ
Why does the system prompt ask Claude to spell out numbers?
The system prompt instructs the model to spell out numbers (e.g., 'twenty' not '20') and avoid emojis, bullet points, asterisks, or special symbols, which makes the text easier for the Text-to-Speech step to handle.
Which Claude model is used and why?
The tutorial uses claude-sonnet-5-5 at low effort, but it was also tested with Claude Haiku 5.5. Both handled real-time voice well at low effort, and the choice depends on the use case.
How do the /twiml and /ws endpoints work together?
The /twiml endpoint returns TwiML that instructs Twilio to connect the call to a Conversation Relay WebSocket at the /ws endpoint. The /ws endpoint handles the WebSocket messages, including setup, prompt, and interrupt events.
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.