{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# How to add thread-level persistence (functional API)\n", "\n", "!!! info \"Prerequisites\"\n", "\n", " This guide assumes familiarity with the following:\n", " \n", " - [Functional API](../../concepts/functional_api/)\n", " - [Persistence](../../concepts/persistence/)\n", " - [Memory](../../concepts/memory/)\n", " - [Chat Models](https://python.langchain.com/docs/concepts/chat_models/)\n", "\n", "!!! info \"Not needed for LangGraph API users\"\n", "\n", " If you're using the LangGraph API, you needn't manually implement a checkpointer. The API automatically handles checkpointing for you. This guide is relevant when implementing LangGraph in your own custom server.\n", "\n", "Many AI applications need memory to share context across multiple interactions on the same [thread](../../concepts/persistence#threads) (e.g., multiple turns of a conversation). In LangGraph functional API, this kind of memory can be added to any [entrypoint()][langgraph.func.entrypoint] workflow using [thread-level persistence](https://langchain-ai.github.io/langgraph/concepts/persistence).\n", "\n", "When creating a LangGraph workflow, you can set it up to persist its results by using a [checkpointer](https://langchain-ai.github.io/langgraph/reference/checkpoints/#basecheckpointsaver):\n", "\n", "\n", "1. Create an instance of a checkpointer:\n", "\n", " ```python\n", " from langgraph.checkpoint.memory import InMemorySaver\n", " \n", " checkpointer = InMemorySaver() \n", " ```\n", "\n", "2. Pass `checkpointer` instance to the `entrypoint()` decorator:\n", "\n", " ```python\n", " from langgraph.func import entrypoint\n", " \n", " @entrypoint(checkpointer=checkpointer)\n", " def workflow(inputs)\n", " ...\n", " ```\n", "\n", "3. Optionally expose `previous` parameter in the workflow function signature:\n", "\n", " ```python\n", " @entrypoint(checkpointer=checkpointer)\n", " def workflow(\n", " inputs,\n", " *,\n", " # you can optionally specify `previous` in the workflow function signature\n", " # to access the return value from the workflow as of the last execution\n", " previous\n", " ):\n", " previous = previous or []\n", " combined_inputs = previous + inputs\n", " result = do_something(combined_inputs)\n", " ...\n", " ```\n", "\n", "4. Optionally choose which values will be returned from the workflow and which will be saved by the checkpointer as `previous`:\n", "\n", " ```python\n", " @entrypoint(checkpointer=checkpointer)\n", " def workflow(inputs, *, previous):\n", " ...\n", " result = do_something(...)\n", " return entrypoint.final(value=result, save=combine(inputs, result))\n", " ```\n", "\n", "This guide shows how you can add thread-level persistence to your workflow.\n", "\n", "!!! tip \"Note\"\n", "\n", " If you need memory that is __shared__ across multiple conversations or users (cross-thread persistence), check out this [how-to guide](../cross-thread-persistence-functional).\n", "\n", "!!! tip \"Note\"\n", "\n", " If you need to add thread-level persistence to a `StateGraph`, check out this [how-to guide](../persistence)." ] }, { "cell_type": "markdown", "id": "7cbd446a-808f-4394-be92-d45ab818953c", "metadata": {}, "source": [ "## Setup\n", "\n", "First we need to install the packages required" ] }, { "cell_type": "code", "execution_count": 1, "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install --quiet -U langgraph langchain_anthropic" ] }, { "cell_type": "markdown", "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", "metadata": {}, "source": [ "Next, we need to set API key for Anthropic (the LLM we will use)." ] }, { "cell_type": "code", "execution_count": null, "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "\n", "def _set_env(var: str):\n", " if not os.environ.get(var):\n", " os.environ[var] = getpass.getpass(f\"{var}: \")\n", "\n", "\n", "_set_env(\"ANTHROPIC_API_KEY\")" ] }, { "cell_type": "markdown", "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", "metadata": {}, "source": [ "
Set up LangSmith for LangGraph development
\n", "\n", " 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. \n", "
\n", "