Primer llms engineering

Why the answer trickles

Call a model API without streaming and the connection sits silent while the model writes the whole answer, then delivers it in one object. Flip one flag in the same request and the same answer arrives as a river: each token flushed down the wire the moment it exists. Nothing about the model changed. The tokens are identical, the last one is ready at the same moment, and the bill is the same to the cent. Streaming is a change in delivery, not in intelligence, and yet it is the difference between a ten-second blank screen and words in a third of a second, which is why it quietly became an architectural commitment for everyone who serves these models.

Ink illustration of a dammed reservoir with a closed gate on the left, and on the right the same water released as a stream turning a waterwheel and filling a row of cups one by one
Same water, two deliveries. The reservoir: the whole answer held back until it is complete, then released at once. The stream: released as it flows, the wheel turning from the first cup.
The one sentence to keep

Streaming changes when you see the answer, not when the answer exists: same request, same tokens, same finish time, same price; the only thing that moves is the wait, from the full generation time down to the time of the first token.

Researched October 2026. The worked example is arithmetic, labeled as such; wire formats and prices are from the providers' own documentation pages.

How it works

One request two ways, then the serving machinery that decides the river's rhythm.

Figure 1: one request, two deliveries

Follow a single call both ways. The worked example throughout: a 500-token answer generated at 50 tokens per second, so generation itself takes 10.0 seconds no matter what. Watch where the waiting happens in each mode.

Tap the arrows to step through

Figure 2: why your stream stutters

Your tokens are not made alone. They are produced inside a shared machine whose scheduling choices you can literally watch as rhythm, pauses, and bursts in the arriving text. Four mechanisms, in the order they touch your stream.

Tap the arrows to step through

Figure 3: the fine print, four panels

What the chunks actually look like from the two big APIs, which transport carries them and why, and the cases where the right answer is not to stream at all.

The road here

Landmarks only: from request-and-response to streaming as the default transport.

Tap the arrows to step through

Key voices

The contracts, the serving papers, and one interested dissent.

OpenAI's API documentation
The chunk contract

The streamed form of a chat completion: a sequence of chat.completion.chunk objects, each carrying choices delta content (or delta tool_calls fragments), all sharing one completion id, closed by a literal data: [DONE] line. The docs present streaming as a display option on the same endpoint, which is the whole thesis in miniature.

Anthropic's API documentation
The event ladder

The typed alternative: message_start, content_block_start, a run of content_block_delta events (text_delta for prose, input_json_delta carrying partial JSON strings for tool input), content_block_stop, message_delta with stop reason and usage, message_stop, with ping keepalives and an error event that can arrive at any point. More ceremony, more information, same river.

Gyeong-In Yu and co-authors
Orca, OSDI 2022

The serving insight underneath everything: schedule per iteration, not per request, so finished sequences exit and waiting ones join after every single decode step. They named it iteration-level scheduling; the field renamed it continuous batching, a citation drift the literature itself now documents.

Woosuk Kwon and the vLLM team
PagedAttention, SOSP 2023

Their framing made streaming performance a systems property: the bottleneck was KV-cache memory management, solved by paging the cache like an operating system pages RAM. The launch blog claimed up to 24 times the throughput of Hugging Face Transformers in their benchmarks; the batching itself was inherited from Orca, a credit assignment the retellings often scramble.

Amey Agrawal and co-authors
Sarathi-Serve, OSDI 2024

The stall hunters: a long prompt's prefill, run as one block, freezes everyone else's decode. Split prefill into budget-sized chunks piggybacked on decode iterations and the freezes go away; they report 2.6 to 5.6 times the serving capacity of vLLM under tail-latency constraints. Your stream's smoothness is partly their chunk budget.

Yaniv Leviathan, Matan Kalman, Yossi Matias; and Charlie Chen et al.
Speculative decoding, 2022 to 2023

The pair of papers behind the bursts: a small draft model proposes several tokens, the big model verifies them in one parallel pass, and the output distribution is provably unchanged (2 to 3 times faster on T5-XXL; 2 to 2.5 times on Chinchilla 70B). Because the guarantee is mathematical, labs deploy it silently, and clients meet it only as rhythm: several tokens at once, then a pause.

Ably
The WebSocket dissent, 2026 (interested party)

The realtime-infrastructure vendor argues WebSockets beat server-sent events for AI streaming at scale: reconnection, replay, and last-mile delivery are the client's problem under SSE, and lab session APIs do not replay missed events. Fair points, commercially convenient ones: Ably sells the alternative. The labs' revealed preference so far is SSE for one-shot text and sockets only where the client must also talk (realtime audio).

Where the field agrees

Streaming is presentation, not compute

No serious source prices or benchmarks streaming as a different product: rate cards list one per-token price with streaming as a supported feature, the forward passes are identical, and the last token lands at the same time either way. Claims that streaming "makes the model faster" are confusing time-to-first-token with total latency, and the field's own definitions keep them separate.

The scheduler is part of the product

Since Orca, serving quality is understood to live in batching, cache management, and prefill policy as much as in the model. TTFT and inter-token latency are the two numbers every serious serving stack now reports, because users can feel both.

Where it splits

Split 1, Side A: SSE is enough for text

The labs' revealed preference. For one-shot generation the client speaks first and then only listens, which is exactly what server-sent events do, over ordinary HTTP that every proxy and firewall already understands. OpenAI and Anthropic both standardized on it; simplicity won.

Split 1, Side B: sockets for real scale

The Ably position. At scale, SSE leaves reconnection, replay of missed events, and fan-out to the client and the edge. WebSockets give a managed, bidirectional channel with delivery semantics. The disagreement is really about who should own reliability, the protocol or the application, and it stays open because both sides are describing real costs.

Split 2, Side A: protect the decode stream

The decode-first schedulers. Keep batches purely decode so inter-token latency stays glassy; let new prompts wait for a clean slot. The user already reading should never feel someone else's prompt arrive.

Split 2, Side B: mix prefill in, in chunks

The Sarathi position. Pure decode batches strand prefill capacity and punish new arrivals' first tokens. Chunked prefill with a token budget keeps both promises at once, at measurably higher total capacity (their 2.6 to 5.6 times figure), accepting tiny decode jitter as the price.

Takeaways

So, streaming, in one paragraph: it is the same HTTP request with one flag flipped, answered as a sequence of server-sent events instead of one held JSON object. Generation is autoregressive, so a long answer takes linear time either way; streaming simply spends none of that time hiding finished tokens. The two numbers that describe the experience are time to first token (dominated by queueing and prefill) and inter-token latency (dominated by the shared batch your request rides in), and every serving advance of the last four years, continuous batching, paged KV cache, chunked prefill, speculative decoding, is a lever on one of those two numbers.

  • Same everything, earlier. Same request, model, tokens, finish time, and per-token price. If a vendor or a post implies streaming itself speeds up the model, they are selling you time-to-first-token as total latency.
  • Learn the two numbers. TTFT is the wait to feel; ITL is the rhythm after. Long prompts hurt the first (prefill), crowded servers hurt the second (shared batches), and the fixes are different.
  • Your stream's rhythm is a crowd signal. Stutters usually mean someone else's huge prefill landed in your batch; bursts usually mean speculative decoding is accepting runs of drafted tokens. Neither is your client misbehaving.
  • Partial is the normal case. Chunks split mid-word and mid-JSON; tool arguments arrive as fragments to assemble, never to parse per chunk; and a 200 status was sent long before any mid-stream error can occur, so errors are events, not status codes.
  • Cancellation is the one real cost lever. A stopped stream stops generation, and ungenerated tokens are unbilled: stopping at token 80 of a planned 800 is a genuine tenfold saving, the only place streaming changes expected cost rather than perceived cost.
  • Do not stream to machines. Offline work belongs on the Batch APIs at half price; a programmatic consumer that needs the complete object gains nothing from partial delivery except assembly bugs. Stream for eyes, batch for pipelines.

Keep exploring

Sources

Every load-bearing claim traces to one of these.

DocsStreaming responses (OpenAI developer docs)
The streamed delivery model and the chat.completion.chunk contract: delta content and tool-call fragments under one completion id, terminated by data: [DONE].
DocsStreaming messages (Anthropic docs)
The event ladder (message_start through message_stop), input_json_delta for tool input, ping keepalives, and the mid-stream error event.
PaperOrca: A Distributed Serving System for Transformer-Based Generative Models (arXiv, 2022)
Iteration-level scheduling: sequences join and leave the batch after every decode iteration, the mechanism the field renamed continuous batching.
PaperEfficient Memory Management for LLM Serving with PagedAttention (vLLM, arXiv, 2023)
Paged KV-cache management; the launch blog's up-to-24-times throughput claim versus Hugging Face Transformers is the project's own benchmark framing.
PaperTaming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve (arXiv, 2024)
Chunked prefill and stall-free batching, with the 2.6 to 5.6 times serving-capacity improvement over vLLM under tail-latency constraints.
PaperFast Inference from Transformers via Speculative Decoding (Leviathan et al., arXiv, 2022)
Draft-and-verify decoding with an unchanged output distribution, 2 to 3 times faster on T5-XXL.
PaperAccelerating Large Language Model Decoding with Speculative Sampling (Chen et al., arXiv, 2023)
The companion result on Chinchilla 70B (2 to 2.5 times), with the distribution-preservation proof that makes silent deployment defensible.
DocsGPT-4.1 model page (OpenAI)
One per-token price for the model ($2 input, $8 output per million) with streaming listed as a supported feature: the rate-card structure behind the same-price conclusion.