# AI Agent Memory Patterns — Build Agents That Remember > Design patterns for adding persistent memory to AI agents. Covers conversation memory, entity extraction, knowledge graphs, tiered memory, and memory management strategies. ## Install Paste the prompt below into your AI tool: ## Quick Use ### Simplest Memory: Conversation Buffer ```python from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() memory.save_context({"input": "My name is Alice"}, {"output": "Hello Alice!"}) memory.save_context({"input": "I like Python"}, {"output": "Python is great!"}) # Later... memory.load_memory_variables({}) # Returns full conversation history ``` ## What are Agent Memory Patterns? Memory patterns define how AI agents remember information across conversations and sessions. Without memory, every conversation starts from scratch. With the right memory architecture, agents can remember user preferences, track ongoing tasks, accumulate knowledge, and provide personalized responses over time. **Answer-Ready**: Agent memory patterns enable AI agents to remember across conversations. Key patterns: conversation buffer, summary memory, entity extraction, knowledge graphs, and tiered memory (core/recall/archival). Used by Mem0, Letta, Zep, and Claude Code's own memory system. Essential for production agents. **Best for**: Developers building AI agents that need to remember. **Works with**: Any LLM, LangChain, LlamaIndex, custom agent frameworks. ## Memory Patterns ### 1. Conversation Buffer ``` Store: Full conversation history Best for: Short conversations Limit: Context window fills up ``` ### 2. Conversation Summary ``` Store: LLM-generated summary of conversation Best for: Long conversations Trade-off: Loses detail, keeps gist ``` ```python from langchain.memory import ConversationSummaryMemory memory = ConversationSummaryMemory(llm=llm) # Automatically summarizes older messages ``` ### 3. Entity Memory ``` Store: Facts about entities (people, projects, tools) Best for: CRM-like agents, personal assistants ``` ```python # Extracts and stores entity facts entities = { "Alice": ["Python developer", "works at Acme", "prefers dark mode"], "Project X": ["deadline March 15", "uses FastAPI", "3 team members"], } ``` ### 4. Knowledge Graph Memory ``` Store: Entity relationships as graph Best for: Complex domain knowledge ``` ``` Alice --[works_at]--> Acme Corp Alice --[leads]--> Project X Project X --[uses]--> FastAPI Project X --[deadline]--> 2026-03-15 ``` ### 5. Tiered Memory (Letta/MemGPT Pattern) ``` Core Memory (always in context): - User name, preferences, current task - ~2K tokens, editable by agent Recall Memory (searchable): - Full conversation history - Vector-indexed, retrieved on demand Archival Memory (long-term): - Facts, documents, accumulated knowledge - Unlimited size, agent decides what to store ``` ### 6. Sliding Window + Summary ``` Recent: Last 10 messages (full detail) Older: Summarized into 1-2 paragraphs Ancient: Key facts only ``` ## Memory Tools | Tool | Pattern | Best For | |------|---------|----------| | Mem0 | Entity + vector | Simple persistent memory | | Letta (MemGPT) | Tiered (core/recall/archival) | Self-managing memory | | Zep | Session + entity + graph | Production chat apps | | Graphiti | Knowledge graph | Complex relationships | | LangChain Memory | All patterns | Framework integration | ## Implementation Guide ### Step 1: Choose Your Pattern ``` Short conversations? → Buffer Long single-session? → Summary Multi-session personal? → Entity + Vector Complex domain? → Knowledge Graph Full autonomy? → Tiered (Letta-style) ``` ### Step 2: Storage Backend | Backend | Best For | |---------|---------| | In-memory | Prototyping | | SQLite | Single-user apps | | PostgreSQL + pgvector | Production | | Redis | High-throughput | | Vector DB (Qdrant, Pinecone) | Semantic retrieval | ### Step 3: Memory Management ```python # Agent decides what to remember def should_memorize(message): # Memorize: preferences, facts, decisions # Skip: greetings, confirmations, small talk triggers = ["my name", "i prefer", "remember", "important"] return any(t in message.lower() for t in triggers) ``` ## Best Practices 1. **Let the agent manage its own memory** — Don't hard-code what to remember 2. **Separate facts from opinions** — Store verified facts, not guesses 3. **Add timestamps** — Memory without time context becomes stale 4. **Implement forgetting** — Remove outdated or contradicted memories 5. **Test memory retrieval** — Wrong memory is worse than no memory ## FAQ **Q: Does Claude Code have built-in memory?** A: Yes, Claude Code's auto-memory system stores user/project/feedback memories in `~/.claude/projects/*/memory/`. It follows the entity + file-based pattern. **Q: Which pattern should I start with?** A: Start with conversation buffer. Add summary when context fills up. Add entity extraction when you need cross-session memory. **Q: How do I prevent memory conflicts?** A: Newer information overrides older. Always check timestamps. Let the agent update or delete stale memories. ## Source & Thanks > References: > - [Mem0](https://github.com/mem0ai/mem0) — 25k+ stars > - [Letta](https://github.com/letta-ai/letta) — 12k+ stars > - [Zep](https://github.com/getzep/zep) — 3k+ stars > - [LangChain Memory](https://python.langchain.com/docs/modules/memory/) ## 快速使用 从对话缓冲开始,逐步升级到实体记忆和分层记忆。 ## 什么是 Agent 记忆模式? 定义 AI Agent 如何跨对话和会话记住信息的设计模式。 **一句话总结**:AI Agent 记忆设计模式,对话缓冲/摘要记忆/实体提取/知识图谱/分层记忆(Letta 模式),Mem0/Letta/Zep 等工具实现,生产 Agent 必备。 ## 6 种模式 1. 对话缓冲 — 最简单,存全部历史 2. 对话摘要 — LLM 自动总结 3. 实体记忆 — 提取人物/项目事实 4. 知识图谱 — 实体关系图 5. 分层记忆 — 核心/回忆/归档三层 6. 滑动窗口+摘要 — 近期详细,远期总结 ## 选择指南 短对话 → 缓冲;长对话 → 摘要;多会话 → 实体+向量;复杂领域 → 知识图谱;全自主 → 分层。 ## 常见问题 **Q: Claude Code 有记忆吗?** A: 有,auto-memory 系统在 ~/.claude/projects/*/memory/ 存储用户/项目/反馈记忆。 ## 来源与致谢 > [Mem0](https://github.com/mem0ai/mem0) | [Letta](https://github.com/letta-ai/letta) | [Zep](https://github.com/getzep/zep) | [LangChain Memory](https://python.langchain.com/docs/modules/memory/) --- Source: https://tokrepo.com/en/workflows/b52189f9-e04a-4425-89db-e16fa7e81eec Author: Agent Toolkit