Skip to lesson
Exit
Building AI-Native Workflows1 / 2

1 min lesson

Streaming and backpressure

Walk through the important items in "Streaming and backpressure" and give the practical point of each.

Step 1 of 2

Streaming and backpressuremake partial results usable

  • Stream tokens as they generate so the user sees motion in hundreds of milliseconds instead of waiting for the whole response.
  • Make partial output usable: render the diff as it forms so a reviewer can start reading before it finishes.
  • Apply backpressure when the consumer is slower than the stream. The consumer tells the stream to slow down, so a fast model can't flood a slow UI or a downstream tool.
Stream and apply backpressure so the consumer sets the pace.
async def stream_edit(req):
    async for token in model.stream(req):       # tokens as they arrive
        await ui.send(token)                    # render incrementally
        if ui.buffer_full():                    # consumer can't keep up
            await ui.drain()                    # wait before pulling more