Sub topic 3
How HTTP Can Deliver a Response in Pieces
This chapter assumes you know basic HTTP requests and responses, can read Python and JavaScript, and understand that an LLM generates tokens one at a time. Here you will see how a server can send a response body in several parts, so a client can use text before the response ends.
The normal case: one response, known length
In a typical HTTP exchange, the server builds the whole body, then sends it. The Content-Length header tells the client how many bytes to expect. The client knows the response is complete once it has read that many bytes.
An LLM cannot work this way for long answers. The server does not know the final length until the model stops generating. It also does not want to wait for the last token before sending the first.
Chunked transfer encoding
HTTP/1.1 solves this with chunked transfer encoding. The server omits Content-Length and sends Transfer-Encoding: chunked. The body then arrives as a series of chunks. Each chunk has its own size, and a final zero-size chunk marks the end.
HTTP/1.1 200 OK
Content-Type: text/event-stream
Transfer-Encoding: chunked
12
data: Hello
In HTTP/2 and HTTP/3 there is no Transfer-Encoding: chunked. Those versions send the body as frames, and the same idea applies. Your application code rarely sees the chunk framing, because the HTTP library removes it.
What the client sees
The important point is that the client library can hand you the body before it is complete. Most libraries read the whole body by default, so you must ask for streaming explicitly.
import httpx
with httpx.stream("GET", "http://localhost:8000/stream") as response:
for chunk in response.iter_text():
print(repr(chunk))This example assumes a server like the FastAPI endpoint shown later in the tutorial. Each value of chunk is whatever bytes arrived, decoded as text.
Chunks are not messages
Network chunk boundaries do not match your application's message boundaries. One read can return half an event, or three events at once. The sizes depend on how the server flushes data, how proxies repackage it, and how TCP delivers packets.
For this reason, never assume that one chunk equals one token or one event. A later chapter covers buffering partial data so you can parse events correctly.
Things that must be true for streaming to work
- The server sends headers right away and flushes each piece instead of holding it in a buffer.
- The response has no
Content-Length, or the connection uses a protocol that frames the body itself. - Every proxy between the server and client passes data through without collecting it first.
- The client reads the body incrementally.
Exercise
A server sends an LLM answer with Transfer-Encoding: chunked. Your client reads one chunk and tries to parse it as a complete JSON object, but it sometimes fails. Why?
Show answer
Chunk boundaries come from how the data was flushed and transported, not from the structure of your data. A chunk can contain only part of a JSON object or event, so you must buffer data across reads and parse only once you have a complete unit.