Chapter 4: Streaming and SSE
A request/response cycle assumes the answer exists all at once. A generated answer does not: it comes into being token by token, and a long job makes progress step by step. The result is a sequence over time, not a value. Streaming transports keep one connection open and push the pieces as they are ready, so the user sees the first token in tens of milliseconds instead of waiting seconds for the last one.
Concepts from other sections (10)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- inference engine: software that loads weights, schedules requests, manages the KV cache, and generates tokens (vLLM, SGLang, TensorRT-LLM, llama.cpp). Read more: 20.5 LLM Serving Platforms: How They Work and When to Use Them in AI Engineering
- Server streaming: a gRPC call shape with one request and a stream of responses. Read more: Chapter 3: RPC and Protocols
- continuous batching: token-level scheduling that admits and evicts sequences every step to keep the GPU full. Read more: Chapter 20: Inference Engines and GPU Deployment in AI Engineering
- KV cache: the stored keys and values of earlier tokens, bytes per token. Read more: 11.1 Counting: parameters, FLOPs, KV bytes, and memory plans in Mathematics
- Content delivery network (CDN): a network of edge servers that serves static content close to users, pulling from or pushed by the origin and controlled by
Cache-Controlheaders. Read more: Chapter 1: System Design - Load balancer: a component that spreads traffic across instances of a service by round-robin, least connections, or consistent hashing, at layer 4 (TCP) or layer 7 (HTTP). Read more: Chapter 1: System Design
- graceful shutdown: stop accepting, let in-flight work finish within a bound, then exit 0. Read more: 1.9 Async Rust and tokio: futures, tasks, channels, cancellation, hyper in Programming and Algorithms
- Fan-in / fan-out: the number of inputs feeding one output, and the number of outputs fed by one input. Read more: 13.4 Variance propagation and initialization in Mathematics
- AI gateway / LLM gateway: the single front door to a fleet of model servers, where authentication, rate limits, usage policy, caching, routing, and metering live, so the engines behind it stay simple and keyless; its defining constraint is forwarding a token stream without delaying the first token. Read more: Chapter 25: The AI Gateway in AI Engineering
- SSE event: one
data: ...block terminated by a blank line (\n\n) in atext/event-streamresponse. Read more: 25.3 The SSE streaming proxy in AI Engineering
Server-Sent Events (SSE) is the minimal version of this idea: a normal HTTP response that simply never ends, framed as a list of text events. Most “AI streaming” is exactly this. SSE is one-way (server to client), plain text, and ordinary HTTP, and those constraints are features rather than limitations. They are why it survives proxies, why the browser can reconnect it for free, and why it is the default transport for LLM responses.
This chapter first makes the case for streaming at all, then specifies the SSE protocol, compares it with WebSockets, long-polling, and gRPC streaming, and traces the canonical end-to-end path for streaming LLM tokens. It closes with the operational concerns that make streaming hard in production: buffering proxies, heartbeats, resumption, backpressure, and scaling long-lived connections.
Learning objectives
- Explain why streaming generation improves perceived latency, and why time-to-first-token is the metric that matters.
- Read and write the SSE wire format:
data:,event:,id:, andretry:fields, and blank-line dispatch.- Choose between SSE, WebSockets, long-polling, and gRPC streaming for a given hop.
- Trace an LLM token stream from the inference engine through the gateway to the browser, including the
[DONE]sentinel and cancellation.- Handle the operational hazards of streaming: proxy buffering, idle timeouts,
Last-Event-IDresumption, and backpressure.
Before you begin
Section titled “Before you begin”This chapter builds on RPC and Protocols, especially gRPC server streaming and HTTP/2, and assumes basic HTTP. It also helps to know why generation is token by token, which LLM Systems & Inference in AI Engineering explains along with continuous batching and the KV cache.
In this chapter
Section titled “In this chapter”- Why stream at all: perceived latency, cancellation, and memory.
- Server-Sent Events: the wire format and the browser’s
EventSource. - SSE, WebSockets, long-polling, and gRPC streaming: choosing a transport by direction and format.
- Streaming LLM tokens end to end: the canonical AI-platform path, framing, and cancellation.
- Operational concerns: buffering, heartbeats, resumption, backpressure, and scaling.
Why stream at all
Section titled “Why stream at all”The first reason is perceived latency. Autoregressive decoding produces about one token per forward pass. If the server buffers the whole completion, the user waits for all of it; if it streams, the user reads along with generation. Making the user wait for the full completion throws away the latency win, and time-to-first-token (TTFT) becomes the experience you are optimizing.
The second reason is progress and cancellation. A stream lets the user stop a bad generation early, and that lets the platform free the GPU immediately instead of finishing wasted work. The third is memory: streaming a large result, or a long-running job’s logs, avoids buffering it all on the server.
Streaming is the last mile of the inference path, which runs from the model to the serving engine to the gateway and then, as a stream, to the client. The rest of this chapter is about that final hop.
Server-Sent Events
Section titled “Server-Sent Events”SSE is a never-ending HTTP response of text events, served with Content-Type: text/event-stream. Here is a stream of three events:
data: Hello
data: worldid: 42event: token
data: {"token": "!", "done": false}The format has four fields. data: carries the payload and can repeat to send multiple lines. event: names an event type the client can listen for. id: is the event’s id, which the browser echoes back as Last-Event-ID when it reconnects. retry: sets the reconnect delay in milliseconds. A blank line dispatches the event. That is the whole wire format, and it is trivial on purpose: the simplicity is why it survives proxies and reconnects cleanly.
On the client, the browser does the work:
const es = new EventSource("/stream");es.onmessage = e => append(e.data);EventSource handles the connection, the parsing, and automatic reconnection.
Three constraints define what SSE is good for. It is one-way: server to client only. The client opens it with a normal GET, and to send data back it makes a separate request. It is plain HTTP, so it works through proxies, CDNs, and firewalls that mishandle WebSocket upgrades, and over HTTP/2 it is not limited by the old six-connections-per-domain cap. And it is text only: SSE carries UTF-8, so binary must be base64-encoded, which is one reason to prefer WebSockets for binary data.
SSE, WebSockets, long-polling, and gRPC streaming
Section titled “SSE, WebSockets, long-polling, and gRPC streaming”SSE is not the only way to push data, so it helps to place it among the alternatives. The four transports differ in direction, format, and who handles reconnection:
| Transport | Direction | Format | Reconnect | Best for |
|---|---|---|---|---|
| SSE | Server → client | Text | Built-in (EventSource) | LLM tokens, notifications, progress, live feeds |
| WebSockets | Bidirectional | Text or binary | Manual | Chat, collaboration, games, trading |
| Long-polling | Server → client (faked) | Any | Per-request | Legacy fallback when SSE/WS unavailable |
| gRPC streaming | Uni/bidi | Binary (Protobuf) | Manual | Service-to-service streams (not browsers) |
The decision between SSE and WebSockets is a one-way versus two-way decision. SSE is simpler (it is just HTTP), one-way, auto-reconnecting, and text. WebSockets are bidirectional, binary-capable, and lower-overhead per message, but you manage reconnection yourself and they need a protocol upgrade that some infrastructure mishandles. If the server just pushes and the client just listens (tokens, progress, notifications), SSE is simpler, cheaper, and firewall-friendly. Use WebSockets only when you truly need bidirectional, low-latency, binary frames, as in collaborative editing or games. For streaming model output, SSE is almost always the right default.
gRPC server streaming is the same idea as SSE for backend-to-backend traffic: binary, typed, and over HTTP/2. Browsers cannot speak it directly, so the common pattern is a gRPC stream internally and SSE to the browser, converted at the gateway. Pick the transport per hop.
Long-polling is the fallback: the client sends a request, the server holds it until data is available, responds, and the client immediately requests again. It has higher latency and overhead, so use it only when neither SSE nor WebSockets is available.
Streaming LLM tokens end to end
Section titled “Streaming LLM tokens end to end”With the transport chosen, you can trace the canonical streaming path of an AI platform. The client opens an SSE request to the gateway. The gateway calls the inference engine (vLLM or TGI), which yields tokens through its own streaming API, often a gRPC server stream or an async generator. The gateway re-emits each token as an SSE data: event, and a final data: [DONE] sentinel closes the stream.
Framing the payload follows a de facto convention set by OpenAI-style APIs: each event carries a JSON delta such as {"choices":[{"delta":{"content":"Hel"}}]}, and the stream ends with the [DONE] marker. When the stream carries more than tokens, such as tool calls or errors, use named SSE event: types (token, tool_call, error, done) so the client can branch on the event type without sniffing payloads.
Warning: Cancellation is the part teams forget. When the client disconnects (closes the tab, presses stop), propagate the cancellation down to the serving engine so it evicts the request and frees its KV cache and GPU slot immediately. If you do not, the GPU keeps generating for nobody, and you leak work. This ties directly to continuous batching in LLM Systems & Inference.
Operational concerns
Section titled “Operational concerns”The protocol is easy; running it is where the difficulty lies. Backpressure and disconnects are the hard parts: a client that stops reading, a proxy that buffers, a dropped mobile connection.
Buffering proxies are the most common way streaming silently breaks. nginx and load balancers may buffer the whole response, which defeats streaming. Disable buffering (for nginx, the X-Accel-Buffering: no response header), set appropriate timeouts, and use HTTP/1.1 chunked transfer or HTTP/2.
Heartbeats keep idle connections alive. Send an SSE comment line (: ping) periodically so that neither the client nor any intermediary times the connection out while the model is thinking.
Resumption uses the id: field. On reconnect, the browser sends Last-Event-ID, and the server can replay from that id if it kept a buffer. That is often unnecessary for a fresh generation but essential for durable event feeds.
Connection limits and backpressure follow from the fact that each stream is a held-open connection and, for generation, a pinned GPU slot. Cap concurrency, apply timeouts, and shed load; a slow client should not pin a GPU forever.
Scaling long-lived connections complicates load balancing and graceful shutdown: drain connections rather than cutting them. Use sticky routing or a stateless re-attach, and lean on HTTP/2 multiplexing for fan-out.
Choosing a streaming transport
Section titled “Choosing a streaming transport”The chapter’s decisions condense into a short lookup:
| Situation | Use |
|---|---|
| Stream LLM tokens to a browser | SSE |
| Server-push notifications / progress / live feed | SSE |
| Bidirectional, low-latency, binary (chat/collab/games) | WebSockets |
| Backend-to-backend streaming | gRPC server-streaming |
| Infra blocks SSE and WS | Long-polling (fallback) |
| Need to cancel a generation | Propagate disconnect → evict from serving engine |
You can see these choices across the industry. OpenAI and Anthropic expose SSE token streaming as their public API surface, and nearly every generative-AI product team uses SSE with a stop-generation control to drive a TTFT-focused experience. Vercel and Cloudflare stream SSE at the edge and the CDN, in their AI gateways. Slack and Figma are the contrast case: their real-time features are genuinely bidirectional, so they use WebSockets.
Where this leads
Section titled “Where this leads”This book builds the streaming path for real in the gateway chapter: SSE streaming proxy implements the gateway’s half of the path described here, relaying an engine’s token stream to the client. Outside this book, LLM Systems & Inference explains token-by-token decode, TTFT, and continuous batching, which is why output is a stream and why cancellation frees the GPU. System Design covers load balancing, timeouts, and connection handling for long-lived connections; Cloud Native covers SSE through Kubernetes ingress, draining, and graceful shutdown; Observability covers measuring TTFT and tail latency; and Batch & Streaming Processing in Data Engineering contrasts durable event feeds, with replay and delivery semantics, with ephemeral generation streams.
Further reading
Section titled “Further reading”- The HTML Server-Sent Events spec and MDN Server-Sent Events (free): the primary references.
- MDN EventSource.
- WebSocket (RFC 6455) and HTTP/2 (RFC 9113).
- OpenAI streaming docs, for the de facto delta-and-
[DONE]framing. - WHATWG Fetch and ReadableStream, for consuming streams without
EventSource.
Key terms
Section titled “Key terms”- Server-Sent Events (SSE): a one-way, text-only stream of events over a single long-lived HTTP response with
Content-Type: text/event-stream. EventSource: the browser API that opens an SSE stream, parses events, and reconnects automatically.- Time-to-first-token (TTFT): the delay between sending a request and receiving the first generated token.
Last-Event-ID: the header a reconnecting client sends with the id of the last event it received, enabling replay.[DONE]sentinel: the finaldata:event that marks the end of an OpenAI-style token stream.- Heartbeat: a periodic SSE comment line (
: ping) that keeps idle connections from timing out. - Backpressure: limiting what a producer sends or holds when a consumer reads slowly.
Summary
Section titled “Summary”- Generation is inherently a stream; streaming tokens as they are produced makes time-to-first-token the metric that matters.
- SSE is the default transport for LLM responses: one-way, plain text over one long-lived HTTP response, with
EventSourcereconnecting for free. - SSE versus WebSockets is a one-way versus two-way decision; use gRPC streaming between backend services and SSE at the last mile.
- The wire format is deliberately trivial:
data:lines, blank-line-delimited events, optionalevent:,id:, andretry:. - Cancellation and backpressure are the hard parts: a disconnect must free the upstream GPU slot, and held-open connections must be bounded, timed out, and shed.
Review questions
Section titled “Review questions”-
Why does streaming improve the user’s experience even though total generation time is unchanged?
Answer
The model produces about one token per forward pass, so the first token exists long before the last. Streaming shows it immediately, so the user starts reading at time-to-first-token instead of waiting for the whole completion. -
What does a blank line do in an SSE stream, and what does
id:enable?Answer
A blank line dispatches the accumulated fields as one event. `id:` sets the event's id, which the browser sends back as `Last-Event-ID` on reconnect so the server can replay from that point. -
Why is SSE usually a better choice than WebSockets for streaming model output?
Answer
Model output is one-way text. SSE is plain HTTP, so it passes through proxies and firewalls that mishandle WebSocket upgrades, and the browser reconnects it automatically. WebSockets add bidirectional, binary capability that this use case does not need, at the cost of manual reconnection. -
A user closes the tab halfway through a long generation. What should the platform do, and what happens if it does not?
Answer
The gateway should detect the disconnect and propagate cancellation to the serving engine, which evicts the request and frees its KV cache and GPU slot. Otherwise the engine keeps generating tokens nobody will read, wasting GPU capacity that other requests could use. -
Your SSE endpoint works locally but clients behind your load balancer receive the whole response at once. What is the likely cause and fix?
Answer
An intermediary (nginx or the load balancer) is buffering the response. Disable buffering, for example with `X-Accel-Buffering: no` for nginx, check its timeouts, and use HTTP/1.1 chunked transfer or HTTP/2.
Exercises
Section titled “Exercises”- Build a
/streamendpoint that emits one SSE event per word of a sentence, consume it in the browser withEventSource, and watch the events arrive incrementally. - Wire it to a real model: stream vLLM or OpenAI tokens through your server to the client, and measure TTFT against total time.
- Kill the connection mid-stream, confirm that
EventSourcereconnects automatically, and implementLast-Event-IDresumption. - Re-implement the same feature over WebSockets and over gRPC server streaming, and list what each gained and lost.