Skip to main content
Use this pattern when an LLM produces text incrementally and you want the user to hear audio before the full answer is finished. The core idea:
  1. Open one TTS WebSocket before the LLM starts.
  2. Buffer LLM tokens into word or phrase chunks.
  3. Send each chunk as a text message.
  4. Add <flush> when the LLM finishes a sentence or the whole answer.
  5. Read audio messages concurrently and stream them to playback.

Streaming LLM Tokens to TTS

Chunking Rules

  • Send complete words, phrases, or sentences.
  • Keep punctuation attached to the preceding word.
  • Avoid sending one token per message unless tokens are already clean word chunks.
  • Add <flush> at natural boundaries, especially when the LLM has completed the response.
  • Avoid frequent flushes; they reduce the model’s context and can make prosody choppy.

Handling Interruptions

If the user interrupts the agent:
  1. Stop sending new text.
  2. Stop playback locally.
  3. Close the current WebSocket.
  4. Open a fresh WebSocket for the next answer.
For several independent replies on one connection, use Multiplexing and route audio by client_req_id.

Text-to-Speech WebSocket

Full TTS streaming guide.

WebSocket Lifecycle

Setup, ready, input, flush, end, and errors.