From short-term recall to deep personalization โ let's explore how AI remembers like humans!
Imagine you're chatting with your AI travel assistant Lexi and planning a trip to Shimla. Wouldnโt it be amazing if she remembered your favorite hotel, travel dates, and even your preference for toy trains โ just like a human would?
That's exactly what AI Memory in LangChain is all about.
๐ง What is Memory in AI?
In human terms, memory helps us recall:
- What was said
- What we like
- What we did
- What weโve learned
For AI agents, memory helps them act smarter, carry over context, and improve over time. LangChain and LangGraph offer robust ways to manage both short-term and long-term memory โ just like a human brain.
๐ Two Types of Memory in LangChain
1. โจ Short-Term Memory (Thread-Scoped)
This memory lives within a single conversation.
๐งณ Example: You told Lexi, โBook a trip to Shimla in December.โ
Lexi remembers:
- Destination: Shimla
- Timing: December
And that memory stays as long as you're in the same thread โ thanks to LangGraphโs checkpointer.
Key Highlights:
- Thread-specific
- Stored in agentโs state
- Loaded at every step
- Temporary
๐ก Like sticky notes on Lexiโs desk โ perfect for the current chat.
2. ๐ง Long-Term Memory (Cross-Thread)
This memory survives across sessions.
โPlan something like last time.โ
Lexi remembers you meant Shimla in December. Why? Because that was stored in long-term memory, scoped to your user profile.
Key Highlights:
- Works across conversations
- Persisted using vector stores or DBs
- Supports personalization
๐ Like Lexiโs personal diary โ useful for lifelong relationships.
๐งฌ AI Memory Types: Inspired by Human Brain
LangChainโs memory also resembles human memory types:
1. ๐ Episodic Memory
Stores specific events โ like your Dec 10 hotel booking in Shimla.
- Chat logs and user actions
- Enables time-stamped recall
โBook a toy train to Shimla on the 14thโ โ remembered exactly as said.
2. ๐ Semantic Memory
Stores general knowledge โ like facts about Shimla.
- Snowfall in Kufri
- Best time to visit
- Toy train info
Even if you donโt say "Shimla", Lexi might recommend it if you say โsnowy hills in North India.โ
3. โ๏ธ Procedural Memory
Learns routines or behaviors โ like always booking a hotel after a train.
- Learns booking patterns
- Automates tasks
Lexi starts suggesting your travel steps without being told โ like muscle memory.
๐ง When Should AI Create Memories?
Unlike humans, AI doesnโt sleep. So when do they store new memories?
LangChain offers two approaches:
๐ฅ 1. Hot Path (Real-Time Writing)
- Happens during the conversation
- Fast recall, but slower response time
Lexi notes: "User prefers mountain-facing roomsโ while chatting.
๐ 2. Background (Post-Task Writing)
- Happens after the task
- Batched or summarized memory
After your session, Lexi reflects: โUser loves snowy cafes in Himachal.โ
๐ง Pro Strategy: Combine Both
- Use hot path for bookings/preferences
- Use background for session summarization
๐๏ธ Tagging Makes Memory Smarter
To make memory usable, tag it by:
- Thread ID
- Location (e.g., Shimla)
- User ID
Right memory, right moment โ just like a thoughtful friend.
๐ ๏ธ Memory Management in LangGraph
Many AI applications need memory to share context across multiple interactions. LangGraph provides built-in support for managing memory effectively, enabling agents to stay within the LLM's context window while remaining aware of the conversation history.
LangGraph supports two main types of memory:
๐ Short-Term Memory
- Tracks the ongoing conversation within a session
- Maintains message history during the current flow
- Critical for contextual follow-ups
๐ง Long-Term Memory
- Stores user-specific or app-level data across sessions
- Used for persistent personalization and historic recall
๐ Handling Context Window Limits
With short-term memory enabled, long conversations can exceed the LLMโs token limit. LangGraph offers the following strategies:
โ๏ธ Trimming
- Remove the first or last N messages
- Keeps the most relevant and recent messages
- Ensures LLM receives a manageable context
๐ Summarization
- Earlier parts of the conversation are summarized
- Summaries replace full message logs
- Helps maintain continuity while reducing tokens
๐๏ธ Deletion
- Permanently remove messages from LangGraph state
- Useful for stateless workflows
๐ ๏ธ Custom Strategies
- Filter messages based on importance
- Retain specific types (e.g., user queries only)
- Fully customizable to fit app needs
๐ฏ Why It Matters
These memory management strategies allow your AI agent to:
- Operate within LLM limits
- Stay context-aware
- Provide coherent responses
- Enhance long-form conversations
๐งพ Summary Table
Memory Layer | Type | Scope | Shimla Example | Management Strategy |
---|---|---|---|---|
Short-Term | Episodic | One conversation | "Shimla trip in December" | Trimming, Summarization, Deletion |
Long-Term | Episodic/Semantic | Multiple chats | Remembers previous trip to Shimla | Stored in DB or vector store |
Semantic | Knowledge-based | General facts | Knows Shimla is snowy in winter | Stored as knowledge base |
Procedural | Habitual recall | Behavior patterns | Always books train โ hotel โ cafe | Pattern learning over time |
Hot Path | Real-time save | Immediate | Saves hotel preference mid-convo | Stored instantly |
Background | Post-processing | Deferred | Summarizes entire trip memory | Summarized after conversation |
๐งญ Why This Matters for AI Agents
Without memory:
- AI feels robotic, forgetful, and cold
With memory:
- AI becomes personal, smart, and useful
Next time you plan a winter trip, Lexi might say:
โShall I book that toy train and hillside hotel you liked last December?โ
Thatโs the power of AI memory. ๐ง โจ
๐ Credits
This article is inspired by and references the official LangChain documentation. Special thanks to the LangChain team for making advanced memory handling so intuitive.
๐จโ๐ป About Me
Hi, Iโm Utkarsh Rastogi, an AWS Community Builder passionate about:
- ๐ฉ๏ธ Cloud-native apps on AWS
- ๐ค Building intelligent AI assistants
- ๐งฑ Infrastructure-as-Code with Terraform & CloudFormation
- ๐ Blogging real-world AWS & AI projects on awslearner.hashnode.dev
Letโs connect on LinkedIn!
Happy building! ๐
Top comments (3)
pretty cool seeing memory stuff actually explained in plain words - honestly, i always wonder if real progress comes from better tech or just from making things more human-like
Love the Shimla analogy! Have you found any tradeoffs in real apps when deciding between hot path and background memory updates?
I am just trying to learn langchain concepts so will be implementing based on this after days10 series ends