|
| 1 | +--- |
| 2 | +name: "AgentDB Memory Patterns" |
| 3 | +description: "Implement 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." |
| 4 | +--- |
| 5 | + |
| 6 | +# AgentDB Memory Patterns |
| 7 | + |
| 8 | +## What This Skill Does |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +**Performance**: 150x-12,500x faster than traditional solutions with 100% backward compatibility. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Node.js 18+ |
| 17 | +- AgentDB v1.0.7+ (via agentic-flow or standalone) |
| 18 | +- Understanding of agent architectures |
| 19 | + |
| 20 | +## Quick Start with CLI |
| 21 | + |
| 22 | +### Initialize AgentDB |
| 23 | + |
| 24 | +```bash |
| 25 | +# Initialize vector database |
| 26 | +npx agentdb@latest init ./agents.db |
| 27 | + |
| 28 | +# Or with custom dimensions |
| 29 | +npx agentdb@latest init ./agents.db --dimension 768 |
| 30 | + |
| 31 | +# Use preset configurations |
| 32 | +npx agentdb@latest init ./agents.db --preset large |
| 33 | + |
| 34 | +# In-memory database for testing |
| 35 | +npx agentdb@latest init ./memory.db --in-memory |
| 36 | +``` |
| 37 | + |
| 38 | +### Start MCP Server for Claude Code |
| 39 | + |
| 40 | +```bash |
| 41 | +# Start MCP server (integrates with Claude Code) |
| 42 | +npx agentdb@latest mcp |
| 43 | + |
| 44 | +# Add to Claude Code (one-time setup) |
| 45 | +claude mcp add agentdb npx agentdb@latest mcp |
| 46 | +``` |
| 47 | + |
| 48 | +### Create Learning Plugin |
| 49 | + |
| 50 | +```bash |
| 51 | +# Interactive plugin wizard |
| 52 | +npx agentdb@latest create-plugin |
| 53 | + |
| 54 | +# Use template directly |
| 55 | +npx agentdb@latest create-plugin -t decision-transformer -n my-agent |
| 56 | + |
| 57 | +# Available templates: |
| 58 | +# - decision-transformer (sequence modeling RL) |
| 59 | +# - q-learning (value-based learning) |
| 60 | +# - sarsa (on-policy TD learning) |
| 61 | +# - actor-critic (policy gradient) |
| 62 | +# - curiosity-driven (exploration-based) |
| 63 | +``` |
| 64 | + |
| 65 | +## Quick Start with API |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { createAgentDBAdapter } from 'agentic-flow/reasoningbank'; |
| 69 | + |
| 70 | +// Initialize with default configuration |
| 71 | +const adapter = await createAgentDBAdapter({ |
| 72 | + dbPath: '.agentdb/reasoningbank.db', |
| 73 | + enableLearning: true, // Enable learning plugins |
| 74 | + enableReasoning: true, // Enable reasoning agents |
| 75 | + quantizationType: 'scalar', // binary | scalar | product | none |
| 76 | + cacheSize: 1000, // In-memory cache |
| 77 | +}); |
| 78 | + |
| 79 | +// Store interaction memory |
| 80 | +const patternId = await adapter.insertPattern({ |
| 81 | + id: '', |
| 82 | + type: 'pattern', |
| 83 | + domain: 'conversation', |
| 84 | + pattern_data: JSON.stringify({ |
| 85 | + embedding: await computeEmbedding('What is the capital of France?'), |
| 86 | + pattern: { |
| 87 | + user: 'What is the capital of France?', |
| 88 | + assistant: 'The capital of France is Paris.', |
| 89 | + timestamp: Date.now() |
| 90 | + } |
| 91 | + }), |
| 92 | + confidence: 0.95, |
| 93 | + usage_count: 1, |
| 94 | + success_count: 1, |
| 95 | + created_at: Date.now(), |
| 96 | + last_used: Date.now(), |
| 97 | +}); |
| 98 | + |
| 99 | +// Retrieve context with reasoning |
| 100 | +const context = await adapter.retrieveWithReasoning(queryEmbedding, { |
| 101 | + domain: 'conversation', |
| 102 | + k: 10, |
| 103 | + useMMR: true, // Maximal Marginal Relevance |
| 104 | + synthesizeContext: true, // Generate rich context |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +## Memory Patterns |
| 109 | + |
| 110 | +### 1. Session Memory |
| 111 | +```typescript |
| 112 | +class SessionMemory { |
| 113 | + async storeMessage(role: string, content: string) { |
| 114 | + return await db.storeMemory({ |
| 115 | + sessionId: this.sessionId, |
| 116 | + role, |
| 117 | + content, |
| 118 | + timestamp: Date.now() |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + async getSessionHistory(limit = 20) { |
| 123 | + return await db.query({ |
| 124 | + filters: { sessionId: this.sessionId }, |
| 125 | + orderBy: 'timestamp', |
| 126 | + limit |
| 127 | + }); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### 2. Long-Term Memory |
| 133 | +```typescript |
| 134 | +// Store important facts |
| 135 | +await db.storeFact({ |
| 136 | + category: 'user_preference', |
| 137 | + key: 'language', |
| 138 | + value: 'English', |
| 139 | + confidence: 1.0, |
| 140 | + source: 'explicit' |
| 141 | +}); |
| 142 | + |
| 143 | +// Retrieve facts |
| 144 | +const prefs = await db.getFacts({ |
| 145 | + category: 'user_preference' |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +### 3. Pattern Learning |
| 150 | +```typescript |
| 151 | +// Learn from successful interactions |
| 152 | +await db.storePattern({ |
| 153 | + trigger: 'user_asks_time', |
| 154 | + response: 'provide_formatted_time', |
| 155 | + success: true, |
| 156 | + context: { timezone: 'UTC' } |
| 157 | +}); |
| 158 | + |
| 159 | +// Apply learned patterns |
| 160 | +const pattern = await db.matchPattern(currentContext); |
| 161 | +``` |
| 162 | + |
| 163 | +## Advanced Patterns |
| 164 | + |
| 165 | +### Hierarchical Memory |
| 166 | +```typescript |
| 167 | +// Organize memory in hierarchy |
| 168 | +await memory.organize({ |
| 169 | + immediate: recentMessages, // Last 10 messages |
| 170 | + shortTerm: sessionContext, // Current session |
| 171 | + longTerm: importantFacts, // Persistent facts |
| 172 | + semantic: embeddedKnowledge // Vector search |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +### Memory Consolidation |
| 177 | +```typescript |
| 178 | +// Periodically consolidate memories |
| 179 | +await memory.consolidate({ |
| 180 | + strategy: 'importance', // Keep important memories |
| 181 | + maxSize: 10000, // Size limit |
| 182 | + minScore: 0.5 // Relevance threshold |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +## CLI Operations |
| 187 | + |
| 188 | +### Query Database |
| 189 | + |
| 190 | +```bash |
| 191 | +# Query with vector embedding |
| 192 | +npx agentdb@latest query ./agents.db "[0.1,0.2,0.3,...]" |
| 193 | + |
| 194 | +# Top-k results |
| 195 | +npx agentdb@latest query ./agents.db "[0.1,0.2,0.3]" -k 10 |
| 196 | + |
| 197 | +# With similarity threshold |
| 198 | +npx agentdb@latest query ./agents.db "0.1 0.2 0.3" -t 0.75 |
| 199 | + |
| 200 | +# JSON output |
| 201 | +npx agentdb@latest query ./agents.db "[...]" -f json |
| 202 | +``` |
| 203 | + |
| 204 | +### Import/Export Data |
| 205 | + |
| 206 | +```bash |
| 207 | +# Export vectors to file |
| 208 | +npx agentdb@latest export ./agents.db ./backup.json |
| 209 | + |
| 210 | +# Import vectors from file |
| 211 | +npx agentdb@latest import ./backup.json |
| 212 | + |
| 213 | +# Get database statistics |
| 214 | +npx agentdb@latest stats ./agents.db |
| 215 | +``` |
| 216 | + |
| 217 | +### Performance Benchmarks |
| 218 | + |
| 219 | +```bash |
| 220 | +# Run performance benchmarks |
| 221 | +npx agentdb@latest benchmark |
| 222 | + |
| 223 | +# Results show: |
| 224 | +# - Pattern Search: 150x faster (100µs vs 15ms) |
| 225 | +# - Batch Insert: 500x faster (2ms vs 1s) |
| 226 | +# - Large-scale Query: 12,500x faster (8ms vs 100s) |
| 227 | +``` |
| 228 | + |
| 229 | +## Integration with ReasoningBank |
| 230 | + |
| 231 | +```typescript |
| 232 | +import { createAgentDBAdapter, migrateToAgentDB } from 'agentic-flow/reasoningbank'; |
| 233 | + |
| 234 | +// Migrate from legacy ReasoningBank |
| 235 | +const result = await migrateToAgentDB( |
| 236 | + '.swarm/memory.db', // Source (legacy) |
| 237 | + '.agentdb/reasoningbank.db' // Destination (AgentDB) |
| 238 | +); |
| 239 | + |
| 240 | +console.log(`✅ Migrated ${result.patternsMigrated} patterns`); |
| 241 | + |
| 242 | +// Train learning model |
| 243 | +const adapter = await createAgentDBAdapter({ |
| 244 | + enableLearning: true, |
| 245 | +}); |
| 246 | + |
| 247 | +await adapter.train({ |
| 248 | + epochs: 50, |
| 249 | + batchSize: 32, |
| 250 | +}); |
| 251 | + |
| 252 | +// Get optimal strategy with reasoning |
| 253 | +const result = await adapter.retrieveWithReasoning(queryEmbedding, { |
| 254 | + domain: 'task-planning', |
| 255 | + synthesizeContext: true, |
| 256 | + optimizeMemory: true, |
| 257 | +}); |
| 258 | +``` |
| 259 | + |
| 260 | +## Learning Plugins |
| 261 | + |
| 262 | +### Available Algorithms (9 Total) |
| 263 | + |
| 264 | +1. **Decision Transformer** - Sequence modeling RL (recommended) |
| 265 | +2. **Q-Learning** - Value-based learning |
| 266 | +3. **SARSA** - On-policy TD learning |
| 267 | +4. **Actor-Critic** - Policy gradient with baseline |
| 268 | +5. **Active Learning** - Query selection |
| 269 | +6. **Adversarial Training** - Robustness |
| 270 | +7. **Curriculum Learning** - Progressive difficulty |
| 271 | +8. **Federated Learning** - Distributed learning |
| 272 | +9. **Multi-task Learning** - Transfer learning |
| 273 | + |
| 274 | +### List and Manage Plugins |
| 275 | + |
| 276 | +```bash |
| 277 | +# List available plugins |
| 278 | +npx agentdb@latest list-plugins |
| 279 | + |
| 280 | +# List plugin templates |
| 281 | +npx agentdb@latest list-templates |
| 282 | + |
| 283 | +# Get plugin info |
| 284 | +npx agentdb@latest plugin-info <name> |
| 285 | +``` |
| 286 | + |
| 287 | +## Reasoning Agents (4 Modules) |
| 288 | + |
| 289 | +1. **PatternMatcher** - Find similar patterns with HNSW indexing |
| 290 | +2. **ContextSynthesizer** - Generate rich context from multiple sources |
| 291 | +3. **MemoryOptimizer** - Consolidate similar patterns, prune low-quality |
| 292 | +4. **ExperienceCurator** - Quality-based experience filtering |
| 293 | + |
| 294 | +## Best Practices |
| 295 | + |
| 296 | +1. **Enable quantization**: Use scalar/binary for 4-32x memory reduction |
| 297 | +2. **Use caching**: 1000 pattern cache for <1ms retrieval |
| 298 | +3. **Batch operations**: 500x faster than individual inserts |
| 299 | +4. **Train regularly**: Update learning models with new experiences |
| 300 | +5. **Enable reasoning**: Automatic context synthesis and optimization |
| 301 | +6. **Monitor metrics**: Use `stats` command to track performance |
| 302 | + |
| 303 | +## Troubleshooting |
| 304 | + |
| 305 | +### Issue: Memory growing too large |
| 306 | +```bash |
| 307 | +# Check database size |
| 308 | +npx agentdb@latest stats ./agents.db |
| 309 | + |
| 310 | +# Enable quantization |
| 311 | +# Use 'binary' (32x smaller) or 'scalar' (4x smaller) |
| 312 | +``` |
| 313 | + |
| 314 | +### Issue: Slow search performance |
| 315 | +```bash |
| 316 | +# Enable HNSW indexing and caching |
| 317 | +# Results: <100µs search time |
| 318 | +``` |
| 319 | + |
| 320 | +### Issue: Migration from legacy ReasoningBank |
| 321 | +```bash |
| 322 | +# Automatic migration with validation |
| 323 | +npx agentdb@latest migrate --source .swarm/memory.db |
| 324 | +``` |
| 325 | + |
| 326 | +## Performance Characteristics |
| 327 | + |
| 328 | +- **Vector Search**: <100µs (HNSW indexing) |
| 329 | +- **Pattern Retrieval**: <1ms (with cache) |
| 330 | +- **Batch Insert**: 2ms for 100 patterns |
| 331 | +- **Memory Efficiency**: 4-32x reduction with quantization |
| 332 | +- **Backward Compatibility**: 100% compatible with ReasoningBank API |
| 333 | + |
| 334 | +## Learn More |
| 335 | + |
| 336 | +- GitHub: https://github.com/ruvnet/agentic-flow/tree/main/packages/agentdb |
| 337 | +- Documentation: node_modules/agentic-flow/docs/AGENTDB_INTEGRATION.md |
| 338 | +- MCP Integration: `npx agentdb@latest mcp` for Claude Code |
| 339 | +- Website: https://agentdb.ruv.io |
0 commit comments