Course contentsShow
← AI Engineering
Lesson 108 of 1,8863. API-First AI EngineeringFree lesson

Server-Sent Events (SSE) Fundamentals

Learn how SSE works for streaming AI responses and how to parse event streams from LLM APIs.

Server-Sent Events (SSE) Fundamentals

What you'll learn: How SSE enables real-time streaming of AI responses from server to client, making chatbot experiences feel instant and interactive.

The Problem SSE Solves

When you use ChatGPT or Claude, you see words appearing one by one rather than waiting for the entire response. That's streaming in action. Without SSE, your app would make a request and wait (sometimes 30+ seconds) for the complete answer. SSE lets the server push data to your client continuously over a single, long-lived HTTP connection.

How SSE Works

Think of SSE like a news ticker on TV. Once you tune in (open the connection), updates keep flowing to you without asking for each one. The server keeps the connection open and sends chunks of data as they become available.

SSE uses a simple text-based protocol. The server sends messages in this format:

data: {"choices":[{"delta":{"content":"Hello"}}]}

data: {"choices":[{"delta":{"content":" world"}}]}

data: [DONE]

Each message starts with data:, followed by the content. A blank line separates messages. The client listens and processes each chunk as it arrives.

Parsing LLM API Event Streams

When you set stream=true in your LLM API request (from lessons 85-89), you receive SSE responses. Your client needs to:

  1. Keep the connection open
  2. Read each data: line
  3. Parse the JSON payload
  4. Extract the text fragment (usually in delta.content)
  5. Accumulate fragments into the full response
  6. Stop when receiving the [DONE] signal

Key Takeaway: SSE transforms slow, blocking API calls into responsive, real-time experiences by streaming text chunks as the LLM generates them, using a simple event-based protocol your client parses line-by-line.