LangGraph 1 cheatsheet
LangGraph 1: Running and streaming
5 common LangGraph 1 tasks.
Run the graph
Run it to completion and get the final state.
result = graph.invoke({"messages": [{"role": "user", "content": "Hello"}]})
Stream each node's update
Print what every node returns as it finishes.
for update in graph.stream(inputs, stream_mode="updates"):
print(update)
Stream model tokens
Stream the model's output token by token, with the metadata about where it came from.
for token, metadata in graph.stream(inputs, stream_mode="messages"):
print(token.content, end="", flush=True)
Limit the number of steps
Stop loops from running forever. LangGraph raises an error when the limit is hit.
graph.invoke(inputs, {"recursion_limit": 50})
Draw the graph
Print a Mermaid diagram you can paste into a viewer.
print(graph.get_graph().draw_mermaid())