{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# Persistence\n", "\n", "Many AI applications need memory to share context across multiple interactions. In LangGraph, memory is provided for any [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph) through [Checkpointers](https://langchain-ai.github.io/langgraph/reference/checkpoints/).\n", "\n", "When creating any LangGraph workflow, you can set them up to persist their state by doing using the following:\n", "\n", "1. A [Checkpointer](https://langchain-ai.github.io/langgraph/reference/checkpoints/#basecheckpointsaver), such as the [AsyncSqliteSaver](https://langchain-ai.github.io/langgraph/reference/checkpoints/#asyncsqlitesaver)\n", "2. Call `compile(checkpointer=my_checkpointer)` when compiling the graph.\n", "\n", "Example:\n", "```python\n", "from langgraph.graph import StateGraph\n", "from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver\n", "\n", "builder = StateGraph(....)\n", "# ... define the graph\n", "memory = AsyncSqliteSaver.from_conn_string(\":memory:\")\n", "graph = builder.compile(checkpointer=memory)\n", "...\n", "```\n", "\n", "This works for [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph) and all its subclasses, such as [MessageGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#messagegraph).\n", "\n", "Below is an example.\n", "\n", "
Note
\n", "\n",
" In this how-to, we will create our agent from scratch to be transparent (but verbose). You can accomplish similar functionality using the create_react_agent(model, tools=tool, checkpointer=checkpointer) (API doc) constructor. This may be more appropriate if you are used to LangChain’s AgentExecutor class.\n",
"
Note
\n", "\n", " These model requirements are not general requirements for using LangGraph - they are just requirements for this one example.\n", "
\n", "