Recipe: a cancel-safe consumer with time to first token
Time is simulated. The same consumer ends as finished, stopped or broken, and cleans up exactly once each time.
Three recipes to copy: an SSE parser, a stream assembler, and a cancel-safe consumer that always ends in a known state and always cleans up. Plus the checklist to run before you ship a streaming feature.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write chunked(text, size) that cuts a string into pieces of size characters (the last one may be shorter), and use it to check that a parser gives the same events for sizes 1, 3 and 1000. Use the SseParser below.
Write summarise(result) that turns a result dictionary with text, finish and usage (or None) into one line such as text=[Hi] finish=stop tokens=15, writing tokens=- when usage is None.
Explain it without notes
Walk through what happens to one chunk of bytes from the network until it becomes text on the screen.
Which three states can a cancel-safe consumer end in, and what must always happen?
Give five items you would put on a pre-flight checklist for a streaming feature.
Expected output
finished: text=[Hello world] first=450 ms, last=600 ms, cleanups=1
stopped : text=[Hello] first=450 ms, last=520 ms, cleanups=1
broken : text=[Hello] first=450 ms, last=500 ms, cleanups=1