# 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/) ## Quick Use Start with a conversation buffer, then gradually upgrade to entity memory and hierarchical memory. ## What Are Agent Memory Patterns? Design patterns that define how an AI agent remembers information across conversations and sessions. **TL;DR**: AI agent memory design patterns. Conversation buffer / summary memory / entity extraction / knowledge graph / hierarchical memory (Letta-style). Implemented by tools like Mem0/Letta/Zep. Essential for production agents. ## 6 Patterns 1. Conversation buffer — simplest; store all history 2. Conversation summary — LLM auto-summarizes 3. Entity memory — extract facts about people/projects 4. Knowledge graph — entity-relation graph 5. Hierarchical memory — core / recall / archive tiers 6. Sliding window + summary — recent in detail, older summarized ## Selection Guide Short conversations → buffer; long conversations → summary; multi-session → entity + vector; complex domain → knowledge graph; fully autonomous → hierarchical. ## FAQ **Q: Does Claude Code have memory?** A: Yes — the auto-memory system stores user/project/feedback memories under ~/.claude/projects/*/memory/. ## Source & Thanks > [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/ai-agent-memory-patterns-build-agents-remember-b52189f9 Author: Agent Toolkit