{ "cells": [ { "cell_type": "markdown", "id": "aad4e28d", "metadata": {}, "source": [ "# Persistence\n", "\n", "Many AI applications need memory to share context across multiple interactions.\n", "In LangGraph, memory is provided for any\n", "[StateGraph](https://langchain-ai.github.io/langgraphjs/reference/classes/index.StateGraph.html)\n", "through\n", "[Checkpointers](https://langchain-ai.github.io/langgraphjs/reference/interfaces/index.Checkpoint.html).\n", "\n", "When creating any LangGraph workflow, you can set them up to persist their state\n", "by doing using the following:\n", "\n", "1. A\n", " [Checkpointer](https://langchain-ai.github.io/langgraphjs/reference/classes/index.BaseCheckpointSaver.html),\n", " such as the\n", " [MemorySaver](https://langchain-ai.github.io/langgraphjs/reference/classes/index.MemorySaver.html)\n", "2. Call `compile(checkpointer=myCheckpointer)` when compiling the graph.\n", "\n", "Example:\n", "\n", "```javascript\n", "import { MemorySaver } from \"@langchain/langgraph\";\n", "\n", "const workflow = new StateGraph({\n", " channels: graphState,\n", "});\n", "\n", "/// ... Add nodes and edges\n", "// Initialize any compatible CheckPointSaver\n", "const memory = new MemorySaver();\n", "const persistentGraph = workflow.compile({ checkpointer: memory });\n", "```\n", "\n", "This works for\n", "[StateGraph](https://langchain-ai.github.io/langgraphjs/reference/classes/index.StateGraph.html)\n", "and all its subclasses, such as\n", "[MessageGraph](https://langchain-ai.github.io/langgraphjs/reference/classes/index.MessageGraph.html).\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 createReactAgent(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", "