DEV Community

Syed Mehrab
Syed Mehrab

Posted on

Stop Building Static Chatbots: A Beginner’s Guide to LangGraph with Persistence 🚀

Building a chatbot that just responds to prompts is easy. Building an Agent that can think, use tools, and remember conversations across restarts? That’s where it gets tricky.

Enter LangGraph. It’s the evolution of LangChain, designed to give you total control over the flow of your AI.

In this post, I’ll break down the core concepts and show you how to implement a persistent agent using MongoDB as your "brain."

1. The Core Concepts (The "Mental Model")

Before we code, you need to understand the four pillars of LangGraph:

State: The "Source of Truth." It's a shared dictionary or object that every part of your graph can see and update.

Nodes: These are just regular Python functions. They take the State, do some work (like calling Claude or GPT), and return an update.

Edges: These are the "traffic lights" They tell the graph where to go next. Conditional Edges are the most powerful. they decide if the agent should call a tool or talk to the human.

Checkpointer: The "Save Game" button. It stores your state in a database (like MongoDB) so your agent doesn't suffer from amnesia if the server restarts.

2. Setting Up Your State

In LangGraph, you define what your agent needs to remember. Usually, this is a list of messages.

from typing import Annotated, TypedDict
from langgraph.graph.message import add_messages

class AgentState(TypedDict):
    # 'add_messages' is a Reducer. 
    # It tells LangGraph: "Don't overwrite the list, just append new messages!"
    messages: Annotated[list, add_messages]
    user_id: str
Enter fullscreen mode Exit fullscreen mode

3. Creating Your First Node

A node is where the logic happens. Here is a simple node that calls an LLM:

from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(model="claude-3-5-sonnet-20240620")

def chatbot_node(state: AgentState):
    # The node receives the state, calls the LLM
    response = model.invoke(state["messages"])
    # It returns a dictionary updating the 'messages' list
    return {"messages": [response]}
Enter fullscreen mode Exit fullscreen mode

4. Adding "Memory" with MongoDB Checkpointers

If you want your agent to remember "Ali" from yesterday's conversation, you need a Checkpointer. Since many of us use MongoDB, the MongoDBSaver is a perfect choice.

Why use a Checkpointer?
Fault Tolerance: If a node fails, it resumes from the last save.

Multi-User: Use a thread_id to keep 1,000 different user conversations separate.

Audit Trails: You can literally see the "thinking" process saved in your collections.

5. Putting it all together: The Implementation

Here is how you compile your graph with persistence:

from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.mongodb import MongoDBSaver
from pymongo import MongoClient

# 1. Setup Database
client = MongoClient("mongodb://localhost:27017")
checkpointer = MongoDBSaver(client, db_name="agent_memory")

# 2. Build the Graph
workflow = StateGraph(AgentState)
workflow.add_node("chatbot", chatbot_node)

# 3. Define the Flow
workflow.add_edge(START, "chatbot")
workflow.add_edge("chatbot", END)

# 4. Compile with Checkpointer!
app = workflow.compile(checkpointer=checkpointer)

# 5. Run it with a Thread ID
config = {"configurable": {"thread_id": "user_42"}}
inputs = {"messages": [("user", "Hi, my name is Ali!")], "user_id": "ali_01"}

for event in app.stream(inputs, config):
    print(event)
Enter fullscreen mode Exit fullscreen mode

6. Pro-Tip: Debugging in MongoDB Compass

When you run this code, open MongoDB Compass. You will see a collection named checkpoints.

Each document is a "Snapshot" of your agent's brain at a specific moment. If your agent starts hallucinating, you can look at the channel_values in the database to see exactly what "State" the agent was in when it made the mistake.

Summary
LangGraph turns AI development from "prompt engineering" into "system engineering."

  • Nodes do the work.

  • Edges manage the flow.

  • Checkpointers give it a soul (memory).

Top comments (0)