$npx -y skills add spencermarx/open-code-review --skill agentdb-memory-patternsImplement persistent memory patterns for AI agents using AgentDB. Includes session memory, long-term storage, pattern learning, and context management. Use when building stateful agents, chat systems, or intelligent assistants.
| 1 | # AgentDB Memory Patterns |
| 2 | |
| 3 | ## What This Skill Does |
| 4 | |
| 5 | Provides memory management patterns for AI agents using AgentDB's persistent storage and ReasoningBank integration. Enables agents to remember conversations, learn from interactions, and maintain context across sessions. |
| 6 | |
| 7 | **Performance**: 150x-12,500x faster than traditional solutions with 100% backward compatibility. |
| 8 | |
| 9 | ## Prerequisites |
| 10 | |
| 11 | - Node.js 18+ |
| 12 | - AgentDB v1.0.7+ (via agentic-flow or standalone) |
| 13 | - Understanding of agent architectures |
| 14 | |
| 15 | ## Quick Start with CLI |
| 16 | |
| 17 | ### Initialize AgentDB |
| 18 | |
| 19 | ```bash |
| 20 | # Initialize vector database |
| 21 | npx agentdb@latest init ./agents.db |
| 22 | |
| 23 | # Or with custom dimensions |
| 24 | npx agentdb@latest init ./agents.db --dimension 768 |
| 25 | |
| 26 | # Use preset configurations |
| 27 | npx agentdb@latest init ./agents.db --preset large |
| 28 | |
| 29 | # In-memory database for testing |
| 30 | npx agentdb@latest init ./memory.db --in-memory |
| 31 | ``` |
| 32 | |
| 33 | ### Start MCP Server for Claude Code |
| 34 | |
| 35 | ```bash |
| 36 | # Start MCP server (integrates with Claude Code) |
| 37 | npx agentdb@latest mcp |
| 38 | |
| 39 | # Add to Claude Code (one-time setup) |
| 40 | claude mcp add agentdb npx agentdb@latest mcp |
| 41 | ``` |
| 42 | |
| 43 | ### Create Learning Plugin |
| 44 | |
| 45 | ```bash |
| 46 | # Interactive plugin wizard |
| 47 | npx agentdb@latest create-plugin |
| 48 | |
| 49 | # Use template directly |
| 50 | npx agentdb@latest create-plugin -t decision-transformer -n my-agent |
| 51 | |
| 52 | # Available templates: |
| 53 | # - decision-transformer (sequence modeling RL) |
| 54 | # - q-learning (value-based learning) |
| 55 | # - sarsa (on-policy TD learning) |
| 56 | # - actor-critic (policy gradient) |
| 57 | # - curiosity-driven (exploration-based) |
| 58 | ``` |
| 59 | |
| 60 | ## Quick Start with API |
| 61 | |
| 62 | ```typescript |
| 63 | import { createAgentDBAdapter } from 'agentic-flow/reasoningbank'; |
| 64 | |
| 65 | // Initialize with default configuration |
| 66 | const adapter = await createAgentDBAdapter({ |
| 67 | dbPath: '.agentdb/reasoningbank.db', |
| 68 | enableLearning: true, // Enable learning plugins |
| 69 | enableReasoning: true, // Enable reasoning agents |
| 70 | quantizationType: 'scalar', // binary | scalar | product | none |
| 71 | cacheSize: 1000, // In-memory cache |
| 72 | }); |
| 73 | |
| 74 | // Store interaction memory |
| 75 | const patternId = await adapter.insertPattern({ |
| 76 | id: '', |
| 77 | type: 'pattern', |
| 78 | domain: 'conversation', |
| 79 | pattern_data: JSON.stringify({ |
| 80 | embedding: await computeEmbedding('What is the capital of France?'), |
| 81 | pattern: { |
| 82 | user: 'What is the capital of France?', |
| 83 | assistant: 'The capital of France is Paris.', |
| 84 | timestamp: Date.now() |
| 85 | } |
| 86 | }), |
| 87 | confidence: 0.95, |
| 88 | usage_count: 1, |
| 89 | success_count: 1, |
| 90 | created_at: Date.now(), |
| 91 | last_used: Date.now(), |
| 92 | }); |
| 93 | |
| 94 | // Retrieve context with reasoning |
| 95 | const context = await adapter.retrieveWithReasoning(queryEmbedding, { |
| 96 | domain: 'conversation', |
| 97 | k: 10, |
| 98 | useMMR: true, // Maximal Marginal Relevance |
| 99 | synthesizeContext: true, // Generate rich context |
| 100 | }); |
| 101 | ``` |
| 102 | |
| 103 | ## Memory Patterns |
| 104 | |
| 105 | ### 1. Session Memory |
| 106 | ```typescript |
| 107 | class SessionMemory { |
| 108 | async storeMessage(role: string, content: string) { |
| 109 | return await db.storeMemory({ |
| 110 | sessionId: this.sessionId, |
| 111 | role, |
| 112 | content, |
| 113 | timestamp: Date.now() |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | async getSessionHistory(limit = 20) { |
| 118 | return await db.query({ |
| 119 | filters: { sessionId: this.sessionId }, |
| 120 | orderBy: 'timestamp', |
| 121 | limit |
| 122 | }); |
| 123 | } |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | ### 2. Long-Term Memory |
| 128 | ```typescript |
| 129 | // Store important facts |
| 130 | await db.storeFact({ |
| 131 | category: 'user_preference', |
| 132 | key: 'language', |
| 133 | value: 'English', |
| 134 | confidence: 1.0, |
| 135 | source: 'explicit' |
| 136 | }); |
| 137 | |
| 138 | // Retrieve facts |
| 139 | const prefs = await db.getFacts({ |
| 140 | category: 'user_preference' |
| 141 | }); |
| 142 | ``` |
| 143 | |
| 144 | ### 3. Pattern Learning |
| 145 | ```typescript |
| 146 | // Learn from successful interactions |
| 147 | await db.storePattern({ |
| 148 | trigger: 'user_asks_time', |
| 149 | response: 'provide_formatted_time', |
| 150 | success: true, |
| 151 | context: { timezone: 'UTC' } |
| 152 | }); |
| 153 | |
| 154 | // Apply learned patterns |
| 155 | const pattern = await db.matchPattern(currentContext); |
| 156 | ``` |
| 157 | |
| 158 | ## Advanced Patterns |
| 159 | |
| 160 | ### Hierarchical Memory |
| 161 | ```typescript |
| 162 | // Organize memory in hierarchy |
| 163 | await memory.organize({ |
| 164 | immediate: recentMessages, // Last 10 messages |
| 165 | shortTerm: sessionContext, // Current session |
| 166 | longTerm: importantFacts, // Persistent facts |
| 167 | semantic: embeddedKnowledge // Vector search |
| 168 | }); |
| 169 | ``` |
| 170 | |
| 171 | ### Memory Consolidation |
| 172 | ```typescript |
| 173 | // Periodically consolidate memories |
| 174 | await memory.consolidate({ |
| 175 | strategy: 'importance', // Keep important memories |
| 176 | maxSize: 10000, // Size limit |
| 177 | minScore: 0.5 // Relevance threshold |
| 178 | }); |
| 179 | ``` |
| 180 | |
| 181 | ## CLI Operations |
| 182 | |
| 183 | ### Query Database |
| 184 | |
| 185 | ```bash |
| 186 | # Query with vector embedding |
| 187 | npx agentdb@latest query ./agents.db "[0.1,0.2,0.3,...]" |
| 188 | |
| 189 | # Top-k results |
| 190 | npx agentdb@latest query ./agents.db "[0.1,0.2,0.3]" -k 10 |
| 191 | |
| 192 | # With similarity threshold |
| 193 | npx agentdb@latest query ./agents.db "0.1 0.2 0.3" -t 0.75 |
| 194 | |
| 195 | # JSON output |
| 196 | npx agentdb@latest query ./agents.db "[...]" - |