Part 1 of 7 · 1 min read
Overview
In this module you will
- Explain what streaming changes (the wait for the first text) and what it does not change (total time and cost).
- Consume a stream, cancel it, and handle a dropped connection without losing what already arrived.
- Parse Server-Sent Events that arrive in arbitrary chunks, including line endings split across chunks.
- Show half-finished JSON safely for display, and never act on it.
An agent step can take many seconds, and a whole run can take minutes. A person staring at a blank screen assumes the program is broken. Streaming lets you show the answer as it is written, and also show the agent's progress (which tool it is calling right now).
Streaming also adds failure cases that a normal call does not have. A stream can stop halfway, and a half-written answer is not a valid answer. Most of this module is about those cases.
About the examples
The code reads from a fake stream so that everything runs offline and can be tested. It copies the pattern of real streaming APIs (many small text events, then a final event with token usage). The SDK calls you will write against a real provider use different names, so read your provider's streaming reference. No real SDK or network was used here.