Persistence means saving the agent's execution state so that it can continue from where it stopped instead of starting from scratch.
LangGraph handles this using Checkpointers.
A checkpointer automatically saves snapshots of graph state during execution.
How checkpointing works
Imagine an AI agent performing a long-running job search.
Research Agent searches for jobs.
LangGraph saves the current state.
Resume Agent analyzes the resume.
LangGraph saves another checkpoint.
Matching Agent starts processing.
If execution is interrupted, the workflow can resume from a saved checkpoint.
How to implement persistence in LangGraph: Using
InMemorySaver
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
# Define state
class AgentState(TypedDict):
query: str
result: str
# Define nodes
def research_agent(state):
return {
"result": "Found 10 AI internships"
}
def matching_agent(state):
return {
"result": state["result"] + " - Matching completed"
}
# Build graph
graph = StateGraph(AgentState)
graph.add_node("research", research_agent)
graph.add_node("matching", matching_agent)
graph.add_edge(START, "research")
graph.add_edge("research", "matching")
graph.add_edge("matching", END)
# Add checkpointer
memory = InMemorySaver()
app = graph.compile(checkpointer=memory)
# Define thread ID
config = {
"configurable": {
"thread_id": "user_123"
}
}
# Execute workflow
result = app.invoke(
{
"query": "Find AI internships",
"result": ""
},
config=config
)
print(result)Types of persistence in LangGraph
LangGraph has two main persistence systems:
checkpointersfor short-term, single-thread state.storesfor long-term, cross-thread memory.