Streaming, batching, and async basics
Notebook· 01.05· 90–120 min· Lesson 14 of 129
The notebook opens in your lab, in a new tab. If your server is stopped, it starts first (a few seconds).
By the end you can
- Stream tokens to the notebook, separate reasoning from answer text, and aggregate
AIMessageChunks into a full message (+,chunk_position). - Measure time-to-first-token vs total time, and explain why streaming improves perceived speed but not total speed.
- Render a live-updating Markdown answer with an IPython display handle (rich display).
- Batch requests with
max_concurrency, keep going when one fails (return_exceptions=True), and process results as they complete. - Write your first async code: top-level
awaitin Jupyter,ainvoke,astream,abatch,asyncio.gatherwith a semaphore, andastream_events.
Before you start
Lessons 01.01–01.04. Python functions and loops; no async experience needed.
Python in this lesson
Python guideNew hereraiseawaitawait at the top of a notebook cellasync defChanging a dict (update, setdefault, pop)async withasyncioasync for
