<aside> 🎙️

Date: Wednesday, October 21, 2026 · Week 4, Day 24 · Type: Build Day + Live Session 7 (7:30 PM IST, 90 min)

</aside>

Learning Manual

24.1 The User Experience of Latency — why waiting 10 seconds for a full paragraph feels broken, while streaming words immediately feels instantaneous.

24.2 Server-Sent Events (SSE) — how HTTP streaming works over a single persistent connection.

24.3 Python Generators — using yield to stream tokens asynchronously from an LLM API to a web browser.

The Daily Build

Add a streaming endpoint GET /stream?prompt=... to your FastAPI application that streams tokens in real time using Server-Sent Events (SSE) so words appear one by one as they are generated.

Architecture Diagram

+---------------+     +--------------------+     +-------------------+     +------------------+
| Web Browser   | <-- | SSE Event Stream   | <-- | FastAPI Generator | <-- | LLM Token Stream |
+---------------+     +--------------------+     +-------------------+     +------------------+

Interview Questions

  1. When would you use Server-Sent Events (SSE) instead of WebSockets for an AI chat application?
  2. What is Time-to-First-Token (TTFT), and why does streaming drastically improve the user's perceived latency?
  3. How do you safely close an upstream LLM generation stream if a user abruptly closes their browser tab?

Problem Statement

Problem 24. Write an asynchronous Python generator function fake_token_stream(text: str) that splits a string into words and yields them one by one formatted as Server-Sent Events (data: {"token": "word"}\n\n) with a 50ms simulated delay between tokens.

AI-DSA Primitive

Data Structure: Async Generator Stream Buffer.

Mechanics: Coroutine-based iterator yielding data chunks without blocking the main event loop.

Real-World Production Use: Standard streaming response architecture used in Vercel AI SDK, OpenAI API, and FastAPI streaming responses.

Software Design Pattern