Server-Sent Events
SSE
Part of: Streaming, Latency & Caching
Open the network tab while a chat app types out an answer and you will see one long-lived HTTP response, not a flood of separate requests. Each line of it starts with data:. That is Server-Sent Events (SSE) , the text-based protocol almost every streaming LLM API uses to push tokens to you. What it is Server-Sent Events is a one-way streaming channel: the server holds an HTTP connection open and keeps sending small text events down it until it is done. The client does not poll and does not send anything back over this channel, it just reads events as they arrive. It is plain HTTP, so it works through normal proxies and needs no special handshake like WebSockets do. For LLMs, each event carries one chunk of the response, usually a token or a few tokens packaged as JSON. How it works The body is a stream of text with a strict, tiny format. Each event is one or more lines, and a blank line separates events: The rules you must handle: - Lines beginning with data: carry the payload. Strip the prefix and parse the rest (often JSON). - A blank line marks the end of one event. - A sentinel like [DONE] signals the stream is finished, so you stop reading. Parsing it in Python looks like this
Challenge: The SSE Reassembler