Token Streaming
A delivery pattern that pushes LLM output tokens incrementally via Server-Sent Events (SSE) as they are decoded, rather than blocking until the entire response completes
Table of Contents
Structure Diagram
What is Token Streaming?
A delivery pattern that pushes LLM output tokens incrementally via Server-Sent Events (SSE) as they are decoded, rather than blocking until the entire response completes. It is a core concept in LLM Fundamentals within AI / ML.
Core Principles
Generating a 1,000-token response can take several seconds. Instead of blocking on a blank spinner until completion, Token Streaming streams decoded token deltas over Server-Sent Events (SSE) or WebSockets the instant they are produced.
Users begin reading as soon as TTFT elapses, and they can cancel runaway or off-track generations mid-flight to save both time and output token costs.
Practical Takeaways
- Disable proxy buffering: Reverse proxies like Nginx buffer small chunks by default. Set
Content-Type: text/event-stream,Cache-Control: no-cache, andX-Accel-Buffering: no. - Propagate client aborts upstream: Wire browser
AbortControllerdisconnects to cancel the upstream LLM API call so you do not pay for discarded tokens. - Handle partial markdown & JSON cleanly: Use streaming-safe markdown renderers or partial JSON parsers when updating structured UI components incrementally.
Notable Examples
- ChatGPT & Claude Interfaces: Stream markdown paragraphs and syntax-highlighted code blocks token by token.
- Agent Event Streams: Multiplex token deltas alongside structured
tool_call,tool_result, andcitationevents on one SSE channel. - Streaming TTS Voice Bots: Flush each completed sentence from the token stream directly into text-to-speech for low-latency audio.