TwilioPaul KampTutorial
Add Token Streaming and Interruption Handling to a Twilio Voice Anthropic Integration
Summary
This tutorial extends a basic Twilio Conversation Relay voice assistant that uses Anthropic Claude in Node.js by adding token streaming and interruption-aware conversation tracking. The author replaces the aiResponse() function with aiResponseStream(), which uses anthropic.messages.stream() to receive content_block_delta text chunks and forwards each token to Conversation Relay with last: false before sending an empty final token with last: true. The prompt handler now awaits the streamed response and pushes the assistant role and content into local session storage. To handle interruptions, the app listens for Conversation Relay interrupt messages and uses utteranceUntilInterrupt to find the last assistant message containing the heard text, truncating that message and removing later assistant turns so the model knows how far the caller heard. Testing requires an ngrok tunnel, a Twilio number configured with a /twiml webhook, and a phone call to verify the changes.
Context
Builds on a previous post that integrated Anthropic Claude with Twilio Voice using Conversation Relay in Node.js. The basic app waited for the full AI response before sending text to speech, causing noticeable latency on long prompts, and it did not track how much of a response a caller heard when interrupting.
Takeaways
Anthropic’s messages.stream() (or create with stream:true) emits content_block_delta chunks; when delta.type is text_delta, delta.text is a token that can be forwarded incrementally.
Conversation Relay text messages carry a last attribute: tokens sent with last:false tell it more text is coming, and an empty token with last:true signals the response is complete.
When a caller interrupts, Conversation Relay sends utteranceUntilInterrupt; the app truncates the last matching assistant message at that utterance and removes later assistant turns so the next prompt reflects what was actually heard.
FAQ
How do I reduce voice latency in the basic Twilio-Anthropic app?
Switch from waiting for a complete Anthropic response to streaming tokens with anthropic.messages.stream() (or stream:true). Forward each text_delta token to Conversation Relay with last:false, then send an empty token with last:true when done, so the caller hears the response before it finishes.
How does the app know how much of a response the caller heard?
Conversation Relay sends an interrupt message with utteranceUntilInterrupt, the text the caller heard. handleInterrupt() finds the last assistant message containing that utterance, truncates it at that point, and removes any later assistant messages before the conversation is sent back to Anthropic.
What changes are needed in the prompt handler for streaming?
The 'prompt' case must await aiResponseStream(conversation, ws) instead of calling aiResponse(), then push response.role and response.content into the local conversation storage. This ensures the assistant turn is recorded after the streamed response completes.
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.