{ "cells": [ { "cell_type": "markdown", "id": "7240d5b5-9dac-4070-8a9e-2350fb01e0be", "metadata": {}, "source": [ "# How to share state between threads\n", "\n", "By default, state in a graph is scoped to that thread.\n", "LangGraph also allows you to specify a \"scope\" for a given key/value pair that exists between threads. This can be useful for storing information that is shared between threads. For instance, you may want to store information about a user's preferences expressed in one thread, and then use that information in another thread.\n", "\n", "In this notebook we will go through an example of how to construct and use such a graph.\n", "\n", "## Setup\n", "\n", "First, let's install the required packages and set our API keys\n", "\n", "```bash\n", "npm install @langchain/openai @langchain/langgraph @langchain/core zod uuid\n", "```\n", "\n", "Then set your enviroment variables for OpenAI:\n", "\n", "```typescript\n", "process.env.OPENAI_API_KEY = \"your-openai-api-key\";\n", "```" ] }, { "cell_type": "markdown", "id": "51b6817d", "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", "Typing shared state keys
\n", "\n",
" Shared state channels (keys) MUST be objects (see info
channel in the AgentState example below)\n",
"