Lesson 20 / 29
Streaming Updates, Values and Tokens
Show progress to users while a graph runs.
Do not make users stare at a spinner
Agent runs can take many seconds. app.stream(input, stream_mode=...) yields progress as it happens. "updates" emits only what each node changed (good for step-by-step progress), "values" emits the full state after every step, "messages" streams model tokens as they are produced, and "custom" lets nodes emit their own events (for example "searching the docs..."). You can combine modes. Streaming improves perceived latency, lets users cancel early, and gives you real-time logs. Remember that a streamed answer may later fail validation, so decide what the UI does if the final result is rejected.
Updates versus values, run
I ran this offline with langgraph 1.2.12 and langchain-core 1.6.6 in a Python virtual environment. No API key or model is needed because plain Python functions stand in for the model, so the output is repeatable. With stream_mode="updates" each chunk is the change one node made. With "values" each chunk is the whole state: first the input (5), then 10 after double, then 11 after plus_one.
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
n: int
def double(state): return {"n": state["n"] * 2}
def plus_one(state): return {"n": state["n"] + 1}
g = StateGraph(State)
g.add_node("double", double); g.add_node("plus_one", plus_one)
g.add_edge(START, "double"); g.add_edge("double", "plus_one"); g.add_edge("plus_one", END)
app = g.compile()
print("updates:")
for chunk in app.stream({"n": 5}, stream_mode="updates"):
print(" ", chunk)
print("values:")
for chunk in app.stream({"n": 5}, stream_mode="values"):
print(" ", chunk)
Output:
updates:
{'double': {'n': 10}}
{'plus_one': {'n': 11}}
values:
{'n': 5}
{'n': 10}
{'n': 11}Quick check: Which stream mode emits only what each node changed?
- values
- updates
- none
- checkpoint
Answer
updates — "updates" gives per-node deltas; "values" gives the full state each step.