Sub topic 1
Why stream LLM output, and how HTTP streaming works
Prerequisites
This chapter assumes you know the following:
- Basic Python, including
asyncandawait. - How HTTP requests and responses work: methods, status codes, headers, and a body.
- A basic FastAPI route and Pydantic model.
- What an LLM chat API is: you send a prompt and get generated text back.
Why stream LLM output
A language model produces text one token at a time. A token is a small piece of text, often a word or part of a word. A long answer can take 10 to 30 seconds to finish.
Without streaming, the server waits for the whole answer and sends it in one response. The user sees nothing until the last token is ready. With streaming, the server sends each piece as soon as the model produces it. The total time is about the same. The time to the first visible text drops from seconds to a fraction of a second.
This is called perceived latency, which is how long the user feels they waited. A chat box that starts filling in at once feels fast, even if the full answer takes as long as before.
Streaming also helps in other ways:
- The user can start reading while the rest is still being generated.
- The client can stop early if the answer is going the wrong way.
- The server does not need to hold the whole answer in memory before it replies.
The cost is more complexity. You must handle errors that happen after the response has started, clients that leave, and proxies that hold output back. Later chapters cover each of these.
How HTTP streaming works
A normal HTTP response has a status line, headers, and a body. The server usually knows the body before it sends anything, so it can state the size in a Content-Length header.
A streamed response does not know its size in advance. The server sends the status line and headers first. Then it writes the body in pieces and keeps the connection open between pieces. In HTTP/1.1 this uses chunked transfer encoding, which is marked with the Transfer-Encoding: chunked header. Each chunk carries its own length, and a final empty chunk marks the end. HTTP/2 and HTTP/3 have their own framing, but the idea is the same. Your application code does not manage this framing. The server software does.
Two consequences matter for your code:
- The status code and headers are sent before the body. If something fails halfway, you can no longer change the status to 500. You can only send an error message inside the stream or close the connection.
- Every layer between your code and the client can hold data back. That includes the server, a reverse proxy, and the client library. If any layer buffers, the user sees nothing until the buffer fills or the response ends.
Streaming compared with a normal response
| Aspect | Normal response | Streamed response |
|---|---|---|
| Time to first byte | After all work is done | As soon as the first piece is ready |
| `Content-Length` | Known | Not known |
| Failure after start | Can return a 500 status | Status is already sent |
| Proxy buffering | Rarely a problem | Can hide the stream |
What you will build
In this tutorial you build a FastAPI endpoint that returns a generator through StreamingResponse. Each token from the model is wrapped in a Server-Sent Events message. Server-Sent Events (SSE) is a simple text format for sending events from a server to a client over one HTTP response. The next chapters build this one step at a time.
Exercise
A streamed response fails halfway through generation. Why can't the server return a 500 status code, and what can it do instead?
Show answer
The status code and headers were already sent before the body started, so they can't be changed. The server can send an error message as part of the stream, for example an SSE error event, or close the connection.
Exercise
A user reports that your streaming endpoint works locally but shows the whole answer at once in production, behind a reverse proxy. What is the likely cause?
Show answer
A layer between the server and the client, most likely the proxy, is buffering the response. It holds the chunks until the response ends or its buffer fills, so the client gets everything at once.