08.04

Streaming tokens to the browser

skillmaxingPython with AIStreaming tokens to the browser
Visual lesson

Streaming tokens to the browser

Included with Pro · 4:40
Lessons · 968 words

A three-hundred-token answer takes several seconds to generate. If Relay waits for the whole thing, the customer stares at a spinner for the whole time; if Relay forwards each token as it arrives, the first words appear in well under a second and the rest fills in as fast as the model writes. Nothing about the total time changes. What changes is when the user learns the system is working, and that is most of what "fast" means in a chat interface.

Waiting versus streaming
Figure 1Waiting versus streamingThe total is the same; the first word arrives about ten times sooner.

The pipeline has three stages: the provider streams events to your process, an async generator turns those events into server-sent events, and StreamingResponse pushes them to the browser. Lesson 07.02 built the SSE side; this lesson puts a model at the front of it.

Pro

Continue reading

Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.

Unlock full access to
Python with AIAI researchComplete system design
30% off with LAUNCH30ends in 7h 40m
Buy now
View all plans ·Already Pro? Sign in
124 online