Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here.
To add persistence to a graph with subgraphs, all you need to do is pass a checkpointer when compiling the parent graph. LangGraph will automatically propagate the checkpointer to the child subgraphs.
Note
You shouldn't provide a checkpointer when compiling a subgraph. Instead, you must define a **single** checkpointer that you pass to parentGraph.compile(), and LangGraph will automatically propagate the checkpointer to the child subgraphs. If you pass the checkpointer to the subgraph.compile(), it will simply be ignored. This also applies when you add a node that invokes the subgraph explicitly.
Let's define a simple graph with a single subgraph node to show how to do this.
import{StateGraph,Annotation}from"@langchain/langgraph";// subgraphconstSubgraphStateAnnotation=Annotation.Root({foo:Annotation<string>,bar:Annotation<string>,});constsubgraphNode1=async(state:typeofSubgraphStateAnnotation.State)=>{return{bar:"bar"};};constsubgraphNode2=async(state:typeofSubgraphStateAnnotation.State)=>{// note that this node is using a state key ('bar') that is only available in the subgraph// and is sending update on the shared state key ('foo')return{foo:state.foo+state.bar};};constsubgraph=newStateGraph(SubgraphStateAnnotation).addNode("subgraphNode1",subgraphNode1).addNode("subgraphNode2",subgraphNode2).addEdge("__start__","subgraphNode1").addEdge("subgraphNode1","subgraphNode2").compile();// parent graphconstStateAnnotation=Annotation.Root({foo:Annotation<string>,});constnode1=async(state:typeofStateAnnotation.State)=>{return{foo:"hi! "+state.foo,};};constbuilder=newStateGraph(StateAnnotation).addNode("node1",node1)// note that we're adding the compiled subgraph as a node to the parent graph.addNode("node2",subgraph).addEdge("__start__","node1").addEdge("node1","node2");
We can now compile the graph with an in-memory checkpointer (MemorySaver).
import{MemorySaver}from"@langchain/langgraph-checkpoint";constcheckpointer=newMemorySaver();// You must only pass checkpointer when compiling the parent graph.// LangGraph will automatically propagate the checkpointer to the child subgraphs.constgraph=builder.compile({checkpointer:checkpointer});
Let's now run the graph and inspect the persisted state for both the parent graph and the subgraph to verify that persistence works. We should expect to see the final execution results for both the parent and subgraph in state.values.
We can now view the parent graph state by calling graph.get_state() with the same config that we used to invoke the graph.
(awaitgraph.getState(config)).values;
{ foo: 'hi! foobar' }
To view the subgraph state, we need to do two things:
Find the most recent config value for the subgraph
Use graph.getState() to retrieve that value for the most recent subgraph config.
To find the correct config, we can examine the state history from the parent graph and find the state snapshot before we return results from node2 (the node with subgraph):
The state snapshot will include the list of tasks to be executed next. When using subgraphs, the tasks will contain the config that we can use to retrieve the subgraph state: