Modern AI agents need more than just conversation skills β they need memory. This guide explores how to build production-grade long-term memory systems using LangGraph, enabling AI agents to learn and improve from user feedback over time.
π§© The Two-Layer Memory System
Agentic AI systems rely on two distinct memory layers:
β¦ Short-Term Memory (Thread-Level): Works within a single conversation, tracking messages, files, and interactions. Think of it as the agent's "working memory" that gets cleared after each session.
β¦ Long-Term Memory (Cross-Thread): Persists across multiple sessions, storing user preferences, decisions, and learned behaviors. This memory enables consistency and personalization over time.
π LangGraph Memory Storage Options
LangGraph provides three storage implementations suited for different development stages:
InMemoryStore: Perfect for notebooks and testing. Runs entirely in memory with no persistence β ideal for quick experiments.
Local Development Store: Automatically saves data to your file system when using langgraph dev. Provides basic persistence without external databases.
Production Store: Uses PostgreSQL with pgvector for scalable, reliable storage with built-in semantic search capabilities.
π© Building an Email Assistant with Memory
Let's build a practical example: an AI email assistant that learns from your feedback.
πΉ Core Architecture Components
β₯ Schemas: Define data structures using Pydantic models to ensure type-safety and predictable outputs. The RouterSchema classifies emails as "ignore," "respond," or "notify."
β₯ Prompts: Craft instructions for different agent roles β triage classification, email composition, and memory updates. Default preferences provide baseline behavior.
β₯ Tools: Functions like write_email, schedule_meeting, and check_calendar_availability give the agent action capabilities.
πΉ Memory Functions
Two critical functions manage the learning process:
get_memory(): Retrieves preferences from storage or initializes them with defaults on first run. This ensures the agent always has rules to follow.
update_memory(): Uses a specialized LLM to update stored knowledge based on user feedback intelligently. It follows strict rules: never overwrite entirely, only add new information or update contradicted facts, and preserve everything else.
πΉ The Learning Workflow
The system operates through a graph-based workflow:
- Triage Router: Classifies incoming emails using stored triage preferences
- Response Agent: Drafts responses following learned writing styles
- Human-in-the-Loop (HITL): Pauses for user review and captures feedback
- Memory Update: Processes feedback to refine future behavior
π₯ Capturing Feedback Effectively
The system learns from three types of user interactions:
β Accept: No memory update β simple approval provides no new information.
β Edit: Direct modifications create clear before/after comparisons. For example, changing a meeting duration from 45 to 30 minutes teaches the agent to prefer shorter meetings.
β Response: Natural language feedback like "make it less formal" gets generalized into reusable rules.
πThe Four-Step Learning Loop
Here's how feedback transforms into learned behavior:
Step 1 - Trigger: Learning only activates when you provide explicit feedback through edits or responses.
Step 2 - Manager Call: A dedicated "memory manager" LLM analyzes feedback using strict update instructions.
Step 3 - Surgical Update: The manager makes targeted changes, integrating new rules without deleting existing knowledge.
Step 4 - Injection: Updated preferences are fetched on the next run, automatically changing agent behavior.
β Practical Example: Learning from Edits
When testing the email assistant:
First Run (Accept): The user approves all actions β memory remains unchanged.
Second Run (Edit): User changes the meeting duration from 45 to 30 minutes and shortens the email subject. The system learns:
β₯ Calendar preference: "30-minute meetings are preferred."
β₯ Response preference: "Use concise subject lines and brief confirmations."
These learned preferences persist and influence all future interactions.
π Key Implementation Insights
β§ Memory Organization: Use namespaces (like tuples) to organize different types of preferences β triage rules, writing style, calendar settings.
β§ Controlled Updates: The memory manager LLM prevents corruption by following strict rules about preserving existing information.
β§ Contextual Learning: Different feedback types update different memory namespaces β meeting edits update calendar preferences, and email edits update response style.
π Why This Matters
This approach transforms generic AI agents into personalized assistants that:
β Adapt to individual preferences without manual reconfiguration
β Improve continuously through natural user interactions
β Maintain consistency across sessions and conversations
β Scale from prototypes to production systems
π Getting Started
The complete implementation is available on GitHub (search for "langgraph-long-memory"). Start with InMemoryStore for local testing, then migrate to PostgreSQL for production deployment.
By implementing long-term memory, you're not just building an AI tool β you're creating an assistant that grows smarter with every interaction, truly understanding and adapting to its users' needs.

Top comments (0)