Learn AI · Foundations
Agent Memory Explained
How AI agents remember context across sessions — short-term window, long-term stores, vector memory, and when you do not need memory at all.
Key takeaways
- 1Short-term memory is the chat window and summarization of older turns.
- 2Long-term memory is stored facts/preferences retrieved per session — not magic.
- 3RAG over user documents is not agent memory — it is external knowledge.
- 4Memory increases cost and privacy surface — justify before adding.
- 5Explicit user preferences beat inferred memory for product trust.
Three layers
1. Short-term (conversation)
The last N messages in context. Summarize older turns when the window fills.
Cost: tokens every request.
Implementation: message array + rolling summary system prompt.
2. Long-term (user memory)
Facts extracted from past sessions: "prefers bullet points", "uses Python", "company is Acme Corp".
Cost: extraction LLM call + storage + retrieval embedding.
Implementation: Mem0, custom Postgres JSONB, or vector store per user.
3. External knowledge (RAG)
Not memory — documents, tickets, codebase. Retrieved by query relevance.
Implementation: RAG Explained, vector DB.
When to add long-term memory
Add when users say:
- "Remember that I..."
- "Like last time..."
- "You already know my stack"
Skip when:
- Single-session tasks (classify this email)
- RAG on docs covers the knowledge need
- Privacy policy forbids storing inferred traits
Memory extraction pattern
After each session:
→ LLM extracts candidate facts (structured JSON)
→ Deduplicate against existing memory
→ Store with source session ID and timestamp
On new session:
→ Embed user query
→ Retrieve top-k memory facts
→ Inject into system prompt (labeled as user memory)
Show users what is stored. Let them delete.
Production concerns
| Risk | Mitigation |
|---|---|
| Wrong fact stored | Confidence threshold + user confirm |
| Stale preferences | TTL + last_verified date |
| Cross-user leak | Strict tenant isolation in DB |
| Token bloat | Cap retrieved memories at 5-10 facts |
Tools (2026)
| Tool | Role |
|---|---|
| Mem0 | OSS memory layer |
| Postgres + pgvector | Roll your own with full control |
| LangGraph checkpoint | Session state, not user preferences |
| Redis | Fast session cache |
Coming to directory: Mem0. For now see Top GitHub Repos.
Practical default
v1: 20-turn history + RAG on user docs
v2: Summarize sessions older than 1 hour
v3: Long-term memory for power users who opt in
Memory is a product feature with privacy cost — not an infrastructure requirement on launch day.
Common misconceptions
The wrong-but-common takes worth correcting.
Myth
Agents need Mem0 or a memory layer from day one.
Reality
Most v1 chatbots need RAG on docs plus 10-turn history. Memory layers add when users expect continuity across days.
Myth
Long context windows replace memory.
Reality
200k tokens still cost money and dilute attention. Summarize and retrieve selectively.
Real-world use cases
Frequently asked questions
Related on AIKnowHub
Concept
Top GitHub Repos Every AI Builder Should Know (2026)
Fifteen open-source projects that cover 90% of what AI engineers actually install — agents, RAG, observability, routing, and local inference.
Concept
AI Agents Explained
Agents are LLMs that can take actions in a loop. Here's what that actually means, where they shine, and where they fall over.
Concept
Context Engineering Explained
Prompt engineering is one input. Context engineering is everything around it — what you fetch, what you trim, what you remember, what you let the model see.
Tool Guide
Dify Guide
The definitive guide to Dify for low-code AI apps — RAG chatbots, agent workflows, MCP import, self-host vs cloud, and when to pick it over Flowise or n8n.
Comparison
RAG vs Fine-Tuning vs Long Context
Three ways to make an LLM work with your data. When to retrieve, when to train, when to paste — and when to use none of them.
Tool Guide
Flowise Guide
The definitive guide to Flowise for visual RAG and agent prototyping — drag-and-drop chains, 2.0 templates, MCP nodes, and when to pick it over Dify or n8n.