memory-design-patterns
Best practices for memory architecture design including user vs agent vs session memory patterns, vector vs graph memory tradeoffs, retention strategies, and performance optimization. Use when designing memory systems, architecting AI memory layers, choosing memory types, planning retention strategies, or when user mentions memory architecture, user memory, agent memory, session memory, memory patterns, vector storage, graph memory, or Mem0 architecture.
What this skill does
# Memory Design Patterns
Production-ready memory architecture patterns for AI applications using Mem0. This skill provides comprehensive guidance on designing scalable, performant memory systems with proper isolation, retention strategies, and optimization techniques.
## Instructions
### Phase 1: Understand Memory Types
Mem0 provides three distinct memory scopes, each serving different purposes:
#### 1. User Memory (Persistent Preferences & Profile)
**Purpose**: Long-term personal preferences, profile data, and user characteristics that persist across all interactions.
**Use Cases**:
- User preferences (dietary restrictions, communication style, language preferences)
- Personal information (location, occupation, family details)
- Long-term goals and interests
- Historical context that should persist indefinitely
**Implementation**:
```python
# Add user-level memory
memory.add(
"User prefers concise responses without technical jargon"
user_id="customer_bob"
)
# Search user memories
user_context = memory.search(
"communication style"
user_id="customer_bob"
)
```
**Key Characteristics**:
- Persists indefinitely (or until explicitly deleted)
- Shared across all agents interacting with this user
- Should contain stable, long-term information
- Typically 10-50 memories per user
#### 2. Agent Memory (Agent-Specific Context)
**Purpose**: Agent-specific knowledge, behaviors, and learned patterns that apply across all users interacting with this agent.
**Use Cases**:
- Agent capabilities and limitations
- Domain-specific knowledge
- Learned behaviors and patterns
- Agent-specific instructions and protocols
**Implementation**:
```python
# Add agent-level memory
memory.add(
"When handling refund requests, always check order date first"
agent_id="support_agent_v2"
)
# Search agent memories
agent_context = memory.search(
"refund process"
agent_id="support_agent_v2"
)
```
**Key Characteristics**:
- Shared across all users interacting with this agent
- Contains agent-specific procedures and knowledge
- Moderate retention (days to months)
- Typically 50-200 memories per agent
#### 3. Session/Run Memory (Temporary Conversation Context)
**Purpose**: Ephemeral context specific to a single conversation or task session.
**Use Cases**:
- Current conversation topic
- Temporary task context
- Session-specific state
- Short-term working memory
**Implementation**:
```python
# Add session-level memory
memory.add(
"Current issue: payment failed with error code 402"
run_id="session_12345_20250115"
)
# Search session memories
session_context = memory.search(
"current issue"
run_id="session_12345_20250115"
)
```
**Key Characteristics**:
- Short-lived (minutes to hours)
- Isolated to specific conversation or task
- Should be cleaned up after session ends
- Typically 5-20 memories per session
### Phase 2: Choose Storage Backend (Vector vs Graph)
#### Vector Memory (Default)
**How It Works**: Embeddings stored in vector database, semantic similarity search using cosine distance.
**Strengths**:
- Fast semantic search
- Excellent for unstructured data
- Low setup complexity
- Works out-of-the-box with Mem0
**Weaknesses**:
- Cannot query relationships
- No explicit entity connections
- Limited reasoning about connections
**Best For**:
- Simple preference storage
- Document/chunk retrieval
- Semantic search use cases
- Quick prototyping
**Configuration**:
```python
from mem0 import Memory
# Default vector-only configuration
memory = Memory()
```
#### Graph Memory (Advanced)
**How It Works**: Entities and relationships stored in graph database (Neo4j/Memgraph), enables relationship traversal and complex queries.
**Strengths**:
- Explicit entity relationships
- Complex query capabilities
- Relationship reasoning
- Multi-hop traversal
**Weaknesses**:
- Requires graph database setup
- Higher infrastructure complexity
- Slower for pure semantic search
- More storage overhead
**Best For**:
- Multi-entity systems
- Relationship-heavy domains
- Complex reasoning requirements
- Enterprise knowledge graphs
**Configuration**:
```python
from mem0 import Memory
from mem0.configs.base import MemoryConfig
config = MemoryConfig(
graph_store={
"provider": "neo4j"
"config": {
"url": "bolt://localhost:7687"
"username": "neo4j"
"password": "password"
}
}
)
memory = Memory(config)
```
**Decision Matrix**:
| Use Case | Vector | Graph |
|----------|--------|-------|
| User preferences | ✅ Best | ⚠️ Overkill |
| Product recommendations | ✅ Best | ⚠️ Overkill |
| Customer support | ✅ Good | ✅ Better |
| Knowledge management | ⚠️ Limited | ✅ Best |
| Multi-tenant systems | ✅ Good | ✅ Best |
| Team collaboration | ⚠️ Limited | ✅ Best |
### Phase 3: Design Retention Strategy
Use the retention strategy template:
```bash
bash scripts/generate-retention-policy.sh <memory-type> <retention-days>
```
#### Retention Guidelines
**User Memory**:
- Retention: Indefinite (with user control)
- Cleanup: User-initiated deletion only
- Archival: After 1 year of inactivity
- GDPR: Must support right to deletion
**Agent Memory**:
- Retention: 90-180 days typical
- Cleanup: Automatic based on relevance score
- Versioning: Keep agent version history
- Deprecation: Clear old agent memories on major updates
**Session Memory**:
- Retention: 1-24 hours
- Cleanup: Automatic after session end
- Conversion: Promote important memories to user/agent level
- Storage: Consider in-memory for very short sessions
#### Retention Implementation
Run the retention analyzer:
```bash
bash scripts/analyze-retention.sh <user_id_or_agent_id>
```
This script:
1. Analyzes memory age and access patterns
2. Identifies stale memories
3. Suggests cleanup actions
4. Generates retention reports
### Phase 4: Implement Multi-Level Memory Pattern
**Pattern**: Combine all three memory types for comprehensive context.
**Template**: Use `templates/multi-level-memory-pattern.py`
**Architecture**:
```
Query Processing Flow:
1. Retrieve session context (immediate)
2. Retrieve user context (preferences)
3. Retrieve agent context (capabilities)
4. Merge contexts with priority weighting
5. Generate response with full context
```
**Priority Weighting**:
- Session: 40% weight (most relevant to current task)
- User: 35% weight (personalizes response)
- Agent: 25% weight (ensures consistent behavior)
**Implementation**:
```python
# Retrieve all context levels
session_memories = memory.search(query, run_id=run_id)
user_memories = memory.search(query, user_id=user_id)
agent_memories = memory.search(query, agent_id=agent_id)
# Weighted merge
context = merge_contexts(
session=session_memories
user=user_memories
agent=agent_memories
weights={"session": 0.4, "user": 0.35, "agent": 0.25}
)
```
### Phase 5: Optimize Performance
#### Vector Search Optimization
Run the performance analyzer:
```bash
bash scripts/analyze-memory-performance.sh <project_name>
```
**Optimization Techniques**:
1. **Limit Search Results**:
```python
memories = memory.search(query, user_id=user_id, limit=5)
```
- Default: 10 results
- Recommended: 3-5 for chat, 10-20 for RAG
2. **Use Filters to Reduce Search Space**:
```python
memories = memory.search(
query
filters={
"AND": [
{"user_id": "alex"}
{"agent_id": "support_agent"}
]
}
)
```
3. **Cache Frequently Accessed Memories**:
- Cache user preferences (rarely change)
- Refresh cache every 5-10 minutes
- Invalidate on explicit memory updates
4. **Batch Operations**:
```python
# Add multiple memories in one call
memory.add(messages, user_id=user_id)
```
#### Graph Query Optimization
For graph memory:
1. **Limit Traversal Depth**: Max 2-3 hops
2. **Index Key Properties**: user_id, agent_id, timestamps
3. **Use Relationship Filters**: Reduce unnecessary traveRelated in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.