A large language model generates a response one token at a time. A 600-token answer might take 8 to 10 seconds to finish, even if the first token is ready in less than a second. Streaming lets users start reading as tokens arrive, without waiting for the full response.
Sending text to a browser is straightforward, but production systems need to handle more than the normal flow. Connections can drop halfway through an answer. A user might click "Stop" while the model keeps generating and consuming tokens. Tool calls can arrive as incomplete JSON. Realtime voice adds tighter constraints: the assistant needs to respond quickly, recognize interruptions, and stop speaking when the user starts talking.
In this chapter, we'll cover how to stream LLM responses, recover from disconnects, cancel generation, and handle structured output and tool calls.