Langmem Mongodb Notebook
Use Case: A Simple Preference-Aware Assistant
We will build a simple assistant that can remember a user's preferences (like their preferred display mode) and recall them in the same or different conversations.
Part 1: Setup and Dependencies
First, let's install the required Python libraries.
Collecting langgraph
Downloading langgraph-0.6.5-py3-none-any.whl.metadata (6.8 kB)
Collecting langmem
Downloading langmem-0.0.29-py3-none-any.whl.metadata (6.8 kB)
Collecting pymongo
Downloading pymongo-4.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB)
Requirement already satisfied: openai in /usr/local/lib/python3.11/dist-packages (1.99.9)
Collecting python-dotenv
Downloading python_dotenv-1.1.1-py3-none-any.whl.metadata (24 kB)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.11/dist-packages (4.14.1)
Collecting langgraph-checkpoint-mongodb
Downloading langgraph_checkpoint_mongodb-0.1.4-py3-none-any.whl.metadata (2.6 kB)
Collecting langchain_mongodb
Downloading langchain_mongodb-0.6.2-py3-none-any.whl.metadata (1.7 kB)
Collecting langgraph-store-mongodb
Downloading langgraph_store_mongodb-0.0.1-py3-none-any.whl.metadata (362 bytes)
Collecting langchain_voyageai
Downloading langchain_voyageai-0.1.7-py3-none-any.whl.metadata (1.2 kB)
Requirement already satisfied: langchain-core>=0.1 in /usr/local/lib/python3.11/dist-packages (from langgraph) (0.3.74)
Collecting langgraph-checkpoint<3.0.0,>=2.1.0 (from langgraph)
Downloading langgraph_checkpoint-2.1.1-py3-none-any.whl.metadata (4.2 kB)
Collecting langgraph-prebuilt<0.7.0,>=0.6.0 (from langgraph)
Downloading langgraph_prebuilt-0.6.4-py3-none-any.whl.metadata (4.5 kB)
Collecting langgraph-sdk<0.3.0,>=0.2.0 (from langgraph)
Downloading langgraph_sdk-0.2.0-py3-none-any.whl.metadata (1.5 kB)
Requirement already satisfied: pydantic>=2.7.4 in /usr/local/lib/python3.11/dist-packages (from langgraph) (2.11.7)
Requirement already satisfied: xxhash>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from langgraph) (3.5.0)
Collecting langchain-anthropic>=0.3.3 (from langmem)
Downloading langchain_anthropic-0.3.18-py3-none-any.whl.metadata (1.9 kB)
Collecting langchain-openai>=0.3.1 (from langmem)
Downloading langchain_openai-0.3.30-py3-none-any.whl.metadata (2.4 kB)
Requirement already satisfied: langchain>=0.3.15 in /usr/local/lib/python3.11/dist-packages (from langmem) (0.3.27)
Requirement already satisfied: langsmith>=0.3.8 in /usr/local/lib/python3.11/dist-packages (from langmem) (0.4.14)
Collecting trustcall>=0.0.39 (from langmem)
Downloading trustcall-0.0.39-py3-none-any.whl.metadata (29 kB)
Collecting dnspython<3.0.0,>=1.16.0 (from pymongo)
Downloading dnspython-2.7.0-py3-none-any.whl.metadata (5.8 kB)
Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from openai) (4.10.0)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from openai) (1.9.0)
Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from openai) (0.28.1)
Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from openai) (0.10.0)
Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/dist-packages (from openai) (4.67.1)
Collecting motor>3.6.0 (from langgraph-checkpoint-mongodb)
Downloading motor-3.7.1-py3-none-any.whl.metadata (21 kB)
Collecting pymongo
Downloading pymongo-4.12.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB)
Requirement already satisfied: langchain-text-splitters>=0.3 in /usr/local/lib/python3.11/dist-packages (from langchain_mongodb) (0.3.9)
Collecting lark<2.0.0,>=1.1.9 (from langchain_mongodb)
Downloading lark-1.2.2-py3-none-any.whl.metadata (1.8 kB)
Requirement already satisfied: numpy>=1.26 in /usr/local/lib/python3.11/dist-packages (from langchain_mongodb) (2.0.2)
Collecting voyageai<1,>=0.3.4 (from langchain_voyageai)
Downloading voyageai-0.3.4-py3-none-any.whl.metadata (2.9 kB)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio<5,>=3.5.0->openai) (3.10)
Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->openai) (2025.8.3)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.9)
Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.11/dist-packages (from langchain>=0.3.15->langmem) (2.0.43)
Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain>=0.3.15->langmem) (2.32.3)
Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain>=0.3.15->langmem) (6.0.2)
Collecting anthropic<1,>=0.60.0 (from langchain-anthropic>=0.3.3->langmem)
Downloading anthropic-0.64.0-py3-none-any.whl.metadata (27 kB)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-core>=0.1->langgraph) (9.1.2)
Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core>=0.1->langgraph) (1.33)
Requirement already satisfied: packaging>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core>=0.1->langgraph) (25.0)
Requirement already satisfied: tiktoken<1,>=0.7 in /usr/local/lib/python3.11/dist-packages (from langchain-openai>=0.3.1->langmem) (0.11.0)
Collecting ormsgpack>=1.10.0 (from langgraph-checkpoint<3.0.0,>=2.1.0->langgraph)
Downloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (43 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.7/43.7 kB 2.7 MB/s eta 0:00:00
Requirement already satisfied: orjson>=3.10.1 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk<0.3.0,>=0.2.0->langgraph) (3.11.2)
Requirement already satisfied: requests-toolbelt>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.3.8->langmem) (1.0.0)
Requirement already satisfied: zstandard>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.3.8->langmem) (0.23.0)
Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (0.7.0)
Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (2.33.2)
Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (0.4.1)
Collecting dydantic<1.0.0,>=0.0.8 (from trustcall>=0.0.39->langmem)
Downloading dydantic-0.0.8-py3-none-any.whl.metadata (3.6 kB)
Requirement already satisfied: aiohttp in /usr/local/lib/python3.11/dist-packages (from voyageai<1,>=0.3.4->langchain_voyageai) (3.12.15)
Collecting aiolimiter (from voyageai<1,>=0.3.4->langchain_voyageai)
Downloading aiolimiter-1.2.1-py3-none-any.whl.metadata (4.5 kB)
Requirement already satisfied: pillow in /usr/local/lib/python3.11/dist-packages (from voyageai<1,>=0.3.4->langchain_voyageai) (11.3.0)
Requirement already satisfied: tokenizers>=0.14.0 in /usr/local/lib/python3.11/dist-packages (from voyageai<1,>=0.3.4->langchain_voyageai) (0.21.4)
Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core>=0.1->langgraph) (3.0.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain>=0.3.15->langmem) (3.4.3)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain>=0.3.15->langmem) (2.5.0)
Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.11/dist-packages (from SQLAlchemy<3,>=1.4->langchain>=0.3.15->langmem) (3.2.4)
Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/dist-packages (from tiktoken<1,>=0.7->langchain-openai>=0.3.1->langmem) (2024.11.6)
Requirement already satisfied: huggingface-hub<1.0,>=0.16.4 in /usr/local/lib/python3.11/dist-packages (from tokenizers>=0.14.0->voyageai<1,>=0.3.4->langchain_voyageai) (0.34.4)
Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (2.6.1)
Requirement already satisfied: aiosignal>=1.4.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (1.4.0)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (25.3.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (1.7.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (6.6.4)
Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (0.3.2)
Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->voyageai<1,>=0.3.4->langchain_voyageai) (1.20.1)
Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.14.0->voyageai<1,>=0.3.4->langchain_voyageai) (3.18.0)
Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.14.0->voyageai<1,>=0.3.4->langchain_voyageai) (2025.3.0)
Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.14.0->voyageai<1,>=0.3.4->langchain_voyageai) (1.1.7)
Downloading langgraph-0.6.5-py3-none-any.whl (153 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 153.2/153.2 kB 6.5 MB/s eta 0:00:00
Downloading langmem-0.0.29-py3-none-any.whl (67 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 67.1/67.1 kB 4.6 MB/s eta 0:00:00
Downloading python_dotenv-1.1.1-py3-none-any.whl (20 kB)
Downloading langgraph_checkpoint_mongodb-0.1.4-py3-none-any.whl (11 kB)
Downloading pymongo-4.12.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.4/1.4 MB 36.6 MB/s eta 0:00:00
Downloading langchain_mongodb-0.6.2-py3-none-any.whl (59 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 59.1/59.1 kB 4.0 MB/s eta 0:00:00
Downloading langgraph_store_mongodb-0.0.1-py3-none-any.whl (8.6 kB)
Downloading langchain_voyageai-0.1.7-py3-none-any.whl (6.2 kB)
Downloading dnspython-2.7.0-py3-none-any.whl (313 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 313.6/313.6 kB 18.1 MB/s eta 0:00:00
Downloading langchain_anthropic-0.3.18-py3-none-any.whl (29 kB)
Downloading langchain_openai-0.3.30-py3-none-any.whl (74 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.4/74.4 kB 5.4 MB/s eta 0:00:00
Downloading langgraph_checkpoint-2.1.1-py3-none-any.whl (43 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.9/43.9 kB 2.4 MB/s eta 0:00:00
Downloading langgraph_prebuilt-0.6.4-py3-none-any.whl (28 kB)
Downloading langgraph_sdk-0.2.0-py3-none-any.whl (50 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 50.6/50.6 kB 3.5 MB/s eta 0:00:00
Downloading lark-1.2.2-py3-none-any.whl (111 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 111.0/111.0 kB 7.7 MB/s eta 0:00:00
Downloading motor-3.7.1-py3-none-any.whl (74 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.0/75.0 kB 5.9 MB/s eta 0:00:00
Downloading trustcall-0.0.39-py3-none-any.whl (30 kB)
Downloading voyageai-0.3.4-py3-none-any.whl (28 kB)
Downloading anthropic-0.64.0-py3-none-any.whl (297 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 297.2/297.2 kB 17.6 MB/s eta 0:00:00
Downloading dydantic-0.0.8-py3-none-any.whl (8.6 kB)
Downloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (216 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 216.5/216.5 kB 14.5 MB/s eta 0:00:00
Downloading aiolimiter-1.2.1-py3-none-any.whl (6.7 kB)
Installing collected packages: python-dotenv, ormsgpack, lark, dnspython, aiolimiter, pymongo, motor, langgraph-sdk, dydantic, anthropic, langgraph-checkpoint, langchain-openai, langchain-anthropic, voyageai, langgraph-prebuilt, langgraph, langchain_voyageai, langchain_mongodb, trustcall, langgraph-store-mongodb, langgraph-checkpoint-mongodb, langmem
Successfully installed aiolimiter-1.2.1 anthropic-0.64.0 dnspython-2.7.0 dydantic-0.0.8 langchain-anthropic-0.3.18 langchain-openai-0.3.30 langchain_mongodb-0.6.2 langchain_voyageai-0.1.7 langgraph-0.6.5 langgraph-checkpoint-2.1.1 langgraph-checkpoint-mongodb-0.1.4 langgraph-prebuilt-0.6.4 langgraph-sdk-0.2.0 langgraph-store-mongodb-0.0.1 langmem-0.0.29 lark-1.2.2 motor-3.7.1 ormsgpack-1.10.0 pymongo-4.12.1 python-dotenv-1.1.1 trustcall-0.0.39 voyageai-0.3.4
Setup API Keys
OpenAI API Key: ·········· MongoDB URI: ·········· Voyager AI API Key: ·········· Voyager AI API Key: ··········
Step 2: Importing Libraries and Setting Up Environment
Now, we'll import the necessary modules and set up our API keys for OpenAI and MongoDB. We'll use python-dotenv to load credentials from a .env file.
Loading Data set
We will use the "ai_shop" catalog dataset which has a product catalog with title, descripition and pricing data.
Load the data and create vector store index.
We will use a langchain vector store adapter to load and embed our catalog. This will allow the agent to search for relevant products from our database.
Database seeded successfully!
Part 2: Core Components Explained
Our agent has a few key parts:
MongoDBStore: This is where the agent's "memories" are stored for fast retrieval using vector search. It's great for semantic search with persistant nature.MongoDBSaver: This acts as a checkpointer. It saves the entire conversation state (including messages) to a MongoDB database, ensuring durability and allowing conversations to be resumed.create_manage_memory_tool: This tool, provided bylangmem, gives the agent the ability to create, update, and delete memories in itsMongoDBStore.promptfunction: This function dynamically injects relevant memories from theMongoDBStoreinto the agent's system prompt, giving it context for its responses.
Part 3: Building and Running the Agent
We will leverge MongoDBStore to form a long term memory, this component will leverge semantic (vector) search to remember and fetch memories.
Demonstration
Let's start a conversation. We use a thread_id to manage conversation state. The agent has no memories of us yet.
Here's a selection of ingredients for making lasagne, including prices: ### Lasagne Noodles - **Whole Wheat Lasagna Sheets**: $3.00 🍖 - **Gluten-Free Lasagna Sheets**: $4.00 🍚 - **Spinach Lasagna Sheets**: $3.50 🥬 - **Eggplant Slices** (for vegetarian option): $2.75 🍆 ### Ground Beef (or alternatives) - **Ground Turkey**: $4.50 🍗 - **Ground Chicken**: $4.00 🐔 - **Vegetarian Lentil Mince**: $3.50 🍲 ### Tomato Sauce - **Marinara Sauce**: $3.50 🍅 - **Arrabbiata Sauce**: $4.00 🌶️🍅 - **Tomato Basil Sauce**: $3.50 🍅🌿 ### Ricotta Cheese (or alternatives) - **Queso Fresco**: $3.00 🧀 - **Cottage Cheese**: $2.50 🧀 - **Tofu Ricotta**: $3.00 🌱 ### Mozzarella Cheese (or alternatives) - **Vegan Mozzarella**: $5.00 🧀 - **Provolone Cheese**: $3.50 🧀 - **Fontina Cheese**: $4.00 🧀 ### Parmesan Cheese (or alternatives) - **Parmesan Cheese**: $4.00 🧀 - **Grana Padano**: $5.50 🧀 - **Pecorino Romano**: $5.00 🧀 ### Garlic - **Garlic**: $0.50 🧄 - **Garlic Paste**: $3.00 🧄 - **Garlic Flakes**: $2.50 🧄 ### Onion - **Red Onion**: $1.20 🟥 - **Regular Onion**: $1.00 🧅 - **Shallots**: $2.00 🧅 ### Basil - **Pesto Sauce** (includes basil): $4.00 🌿 ### Oregano - Found in various seasoning mixes. ## Total Cost for Basic Ingredients: Assuming you select the first option for each category, the total would be approximately **$24.70**. Feel free to choose different options to suit dietary requirements or preferences!
Now, let's tell the agent our preference. The agent will use the manage_memory tool to save this information.
I've noted that you follow a vegan diet. Here is the recalculated total with vegan ingredients, assuming one option for each category: ### Vegan Ingredients Selection 1. **Lasagne Noodles:** Whole Wheat Lasagna Sheets - $3.00 2. **Ground Beef Alternative:** Vegan Soy Meat Crumbles - $4.50 3. **Tomato Sauce:** Marinara Sauce - $3.50 4. **Ricotta Cheese Alternative:** Tofu Ricotta - $3.00 5. **Mozzarella Cheese Alternative:** Vegan Mozzarella - $5.00 6. **Parmesan Cheese Alternative:** Vegan Cashew Cream Sauce - $5.00 7. **Garlic:** Garlic - $0.50 8. **Onion:** Regular Onion - $1.00 9. **Basil:** Pesto Sauce - $4.00 ### Total Cost **$29.50** This total accounts for using vegan products to ensure your dietary preferences are accommodated. Let me know if you need any further assistance!
If we ask again in the same conversation thread, it should remember.
The selection I provided aligns with a vegan diet because it excludes all animal products, including meat, dairy, and eggs. Here’s how: 1. **Lasagne Noodles:** Options include eggplant slices, zucchini slices, and whole wheat or gluten-free lasagna sheets, which are all plant-based. 2. **Ground Beef Alternative:** I selected vegan soy meat crumbles, ensuring the texture and protein source are plant-derived. 3. **Tomato Sauce:** Marinara and vegan sauces like cashew cream are plant-based and free from dairy. 4. **Ricotta Cheese Alternative:** Tofu Ricotta is used instead of traditional ricotta cheese, providing a vegan-friendly option. 5. **Mozzarella Cheese Alternative:** Vegan mozzarella, made from nuts or soy, replaces conventional mozzarella. 6. **Parmesan Cheese Alternative:** The use of vegan cashew cream sauce offers a dairy-free replacement to parmesan. 7. **Garlic and Onion:** These are naturally vegan and provide flavor without animal products. 8. **Herbs:** Basil, via vegan pesto sauce, complements the dish without any animal-derived ingredients. Overall, each ingredient chosen fits within a vegan framework by either being inherently plant-based or replaced with a vegan alternative, supporting your dietary preferences.
Now, let's start a new conversation with a different thread_id. The agent will remeber us in this new thread, because the memories are scoped not scoped to a conversation.
Yes, I remember you follow a vegan diet. If there's anything else you'd like to update or add, feel free to let me know!
⛳ Knowledge Checkpoint
You've now seen how to:
- Create a stateful agent using
langgraph. - Use
langmemto provide the agent with tools to manage its own memory. - Leverage MongoDB with
MongoDBSaverandMongoDBStoreto persist conversation history and long-term memory, making your agent durable with memory mechanisim.