Build ยท 6 min read
Streaming
Stream tokens, intermediate steps and tool events to the client.
Streaming is the difference between an application that feels instant and one that feels broken. Every runnable supports it out of the box.
Token streaming
python
async for chunk in chain.astream({"topic": "agents"}):
await websocket.send_text(chunk)Event streaming
When you need to show progress for retrieval or tool calls, stream events instead of raw tokens and filter by event type.
python
async for event in chain.astream_events({"question": q}, version="v2"):
if event["event"] == "on_retriever_end":
show_sources(event["data"]["output"])