Skip to main content

Streaming & Latency

Latency is the enemy of natural conversation. Streaming is how LLMRTC minimizes it—starting each step before the previous one completes.


The Latency Problem​

Without streaming, each step waits for the previous one to finish:

Total latency: 3300ms from speech end to first audio.


Streaming Solution​

With streaming, steps overlap:

First audio at 700ms—a 4.7x improvement.


How Streaming Works​

Each component produces output incrementally:

The key insight: TTS can start synthesizing the first sentence while the LLM is still generating the rest.


STT Streaming​

Speech-to-text can provide partial results as audio arrives:

Partial transcripts enable:

  • Real-time captions
  • Early abort if user changes direction
  • UI responsiveness

LLM Streaming​

Language models can stream tokens as they're generated:

Benefits:

  • Time to first token (TTFT) is much lower than full completion
  • TTS can start immediately
  • Users see/hear responses sooner

LLMRTC tracks llm.ttft_ms (time to first token) as a key metric.


TTS Streaming​

Text-to-speech synthesizes audio in chunks:

The orchestrator buffers LLM output and sends complete sentences to TTS for natural-sounding output.


Sentence Chunking​

Text is split into sentence-sized chunks for TTS:

InputChunks
"Hello. How are you?"["Hello.", "How are you?"]
"The weather is sunny and warm today."["The weather is sunny and warm today."]

The default chunker splits on .!? followed by whitespace. For languages without these markers (like Chinese or Japanese), you can provide a custom sentenceChunker function.


Audio Format​

TTS produces PCM audio:

PropertyValue
Sample rate24kHz
Bit depth16-bit signed
EndiannessLittle-endian
ChannelsMono

This format is then encoded to Opus for WebRTC transport.


Pipeline Timing​

A complete turn has these timing components:

Key metrics:

MetricDescriptionTarget
stt.duration_msSpeech-to-text time< 300ms
llm.ttft_msTime to first LLM token< 200ms
llm.duration_msTotal LLM timevaries
tts.duration_msTTS synthesis time< 500ms
turn.duration_msComplete turn timevaries

Latency Factors​

Several factors affect end-to-end latency:

Network​

  • Physical distance to AI providers
  • WebRTC connection quality
  • TURN relay overhead (when needed)

Model​

  • Model size (larger = slower)
  • Max tokens setting
  • Conversation history length

Configuration​

  • Streaming enabled/disabled
  • History limit
  • TTS voice complexity

Streaming Configuration​

Enable streaming in the server:

const server = new LLMRTCServer({
streamingTTS: true, // Enable TTS streaming
// ...
});

LLM streaming is typically enabled by default in providers. TTS streaming requires FFmpeg for audio chunk processing.


Non-Streaming Fallback​

When streaming isn't available:

This is simpler but has higher latency. Useful for:

  • Providers without streaming support
  • Environments without FFmpeg
  • Debugging