Compiling, Invoking, Runtime and Streaming
A `StateGraph` is only a builder. `compile()` turns it into a runnable graph that you invoke or stream. You need to know what compile accepts, what each stream mode emits, how config and runtime context reach nodes, and how to stream tokens and progress out of nested graphs.
Key points
- 1
compile()takescheckpointer,store,cache,interrupt_before,interrupt_after,debugandname, and it checks the graph structure. Per-run values such asrecursion_limit,thread_idand context go toinvoke, not to compile. - 2
A compiled graph is a LangChain Runnable, so
invoke/ainvoke,stream/astreamandbatch/abatchall work. In async code, useainvokeorastream. The sync calls block the event loop. - 3
stream()defaults tostream_mode="values", the full state after each step.updatesyields{node: delta}for each node,messagesyields(message_chunk, metadata)for LLM tokens,customyields whatever nodes write, andcheckpoints,tasksanddebugshow execution detail. - 4
Pass a list such as
stream_mode=["updates", "custom"]and each chunk becomes a(mode, data)tuple. Withsubgraphs=True, a namespace tuple is added in front, and it is()for the root graph. - 5
Opting in with
version="v2"gives one consistent shape. Stream chunks become dicts withtype,nsanddata, andinvokereturns aGraphOutputwith.valueand.interrupts. v1 is still the default. - 6
invoke(inputs, stream_mode="updates")returns the list of chunks for that mode instead of the final state. - 7
Inside a node,
get_stream_writer()(orruntime.stream_writer) sends any data tostream_mode="custom". If the caller does not ask forcustom, those writes are dropped. In async code it needs Python 3.11 or later, because it relies on contextvar propagation. - 8
In
messagesmode, filter bymetadata["langgraph_node"]or by tags set withmodel.with_config(tags=[...]). Tokens stream even when the node callsmodel.invoke, as long as the call happens inside the graph run. - 9
Tokens and progress events from a subgraph reach the parent's stream only with
subgraphs=True. The namespace (for example('agent:<task_id>',)) tells you which subgraph produced each chunk. - 10
A node can take
config: RunnableConfigandruntime: Runtime[Ctx]parameters.Runtimeexposescontext,store,stream_writerand execution info, and context is passed on each call withinvoke(..., context={...}). - 11
recursion_limitis a top-level config key ({"recursion_limit": 50}), not one insideconfigurable. Hitting it raisesGraphRecursionError. The default is high (the docs say 1,000 steps since 1.0.6, and installed 1.2.16 uses 10,007), so a runaway loop burns many model calls first. Set it on purpose. - 12
graph.get_graph().draw_mermaid()returns Mermaid text, anddraw_mermaid_png()renders an image. Conditional edges are drawn from the path map or the router's return type hint, so a router without either shows incomplete edges.
Common traps
Putting
recursion_limitunderconfigurable. It is silently ignored, and the default limit applies.Calling sync
invokeinside anasync defFastAPI handler. It blocks the event loop for every other request.Streaming
valuesto a browser. Every event resends the whole growing state and exposes internal keys. Stream filteredmessagesandcustomevents instead.Expecting subgraph tokens in the parent's stream without passing
subgraphs=True.Writing with
get_stream_writer()but streaming onlyupdates, so the progress events never arrive.Still unpacking
(mode, chunk)tuples after switching toversion="v2", where chunks are dicts with atypekey.
Read the source
Further reading
Suggest a resourceNo extra reading for Compiling, Invoking, Runtime and Streaming yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Compiling, Invoking, Runtime and Streaming
Ten questions, with the answer and explanation after each one.