Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Bijit-Mondal/VoiceAgent/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Voice Agent SDK provides robust interruption handling to support natural conversational flow. Users can interrupt the agent at any time (“barge-in”), and the agent immediately cancels in-flight work to save tokens and reduce latency.Interruption Types
The SDK supports two levels of interruption:1. Speech-Only Interruption
Cancels ongoing speech generation and playback, but allows the LLM stream to continue.- User starts speaking while agent is playing audio
- You want to stop audio but let the LLM finish generating text (e.g., for display purposes)
- Aborts current TTS generation
- Clears pending TTS chunks from the queue
- Emits
speech_interruptedevent - LLM stream continues running
2. Full Interruption (Barge-In)
Cancels both the LLM stream and speech generation.- User starts speaking (true barge-in scenario)
- You want to completely cancel the current response to process new input
- Save tokens and API costs by stopping unnecessary generation
- Aborts the LLM stream using
AbortController - Cancels all speech generation
- Clears pending TTS chunks
- Emits
speech_interruptedevent - Agent immediately ready for new input
Implementation Details
AbortController for LLM Streams
The agent usesAbortController to cancel in-flight LLM streams:
streamText():
Speech Queue Clearing
TheSpeechManager clears all pending chunks:
Automatic Barge-In
Server-Side (VoiceAgent)
The agent automatically interrupts when receiving user input:Client-Side (Browser)
The browser client detects speech and interrupts automatically:Manual Interruption
From Application Code
From Browser Client
Send an interrupt message via WebSocket:Interruption Events
Listen for interruption events to update UI or log analytics:user_speaking— User started speaking (barge-in)user_clicked_stop— User clicked stop/interrupt buttonclient_request— Generic client-side interruptiontimeout— Response took too longpriority_message— Higher priority input receivedinterrupted— Default reason if none provided
Cleanup on Disconnect
The agent automatically cleans up when disconnecting:- LLM stream is cancelled
- Speech queue is cleared
- Pending inputs are rejected
- No lingering resources
Interruption in Multi-Step Tool Execution
When tools are executing, interruption cancels the entire operation:Best Practices
Use full interruption for barge-in
Use full interruption for barge-in
When a user starts speaking, call
interruptCurrentResponse() to cancel both LLM and speech. This saves tokens and provides the fastest response to new input.Provide visual feedback
Provide visual feedback
Show users when interruption occurs:
Pass meaningful reasons
Pass meaningful reasons
Use descriptive reasons for interruption to help with debugging and analytics:
Handle interruption in long-running tools
Handle interruption in long-running tools
If your tools do external API calls or long operations, check for cancellation:
Test barge-in latency
Test barge-in latency
Measure time from user speech to interruption:
Common Patterns
Debounced Interruption
Prevent accidental interruptions from brief pauses:Conditional Interruption
Only interrupt in certain states:Interrupt and Resume
Save state before interruption for potential resumption:Troubleshooting
Interruption seems slow
Interruption seems slow
Check:
- Are you calling
interruptCurrentResponse()instead of justinterruptSpeech()? - Is there network latency between client and server?
- Are you using WebSocket for real-time communication?
- Is the browser’s audio queue too long?
LLM keeps generating after interrupt
LLM keeps generating after interrupt
Ensure you’re using
interruptCurrentResponse() (not interruptSpeech()). Verify the AbortController is being properly set and aborted.Tool execution continues after interrupt
Tool execution continues after interrupt
The currently executing tool may complete even after abort signal. This is expected. Design tools to be cancellable if needed.
Speech interruption not working
Speech interruption not working
Check that:
speechModelis configured- Speech generation is actually active (
agent.speaking === true) - You’re calling
interruptSpeech()orinterruptCurrentResponse()
Next Steps
Browser Client
See interruption handling in the complete browser implementation
History Management
Learn how to manage conversation state across interruptions