$npx -y skills add spencermarx/open-code-review --skill reasoningbank-agentdbImplement ReasoningBank adaptive learning with AgentDB's 150x faster vector database. Includes trajectory tracking, verdict judgment, memory distillation, and pattern recognition. Use when building self-learning agents, optimizing decision-making, or implementing experience repla
| 1 | # ReasoningBank with AgentDB |
| 2 | |
| 3 | ## What This Skill Does |
| 4 | |
| 5 | Provides ReasoningBank adaptive learning patterns using AgentDB's high-performance backend (150x-12,500x faster). Enables agents to learn from experiences, judge outcomes, distill memories, and improve decision-making over time with 100% backward compatibility. |
| 6 | |
| 7 | **Performance**: 150x faster pattern retrieval, 500x faster batch operations, <1ms memory access. |
| 8 | |
| 9 | ## Prerequisites |
| 10 | |
| 11 | - Node.js 18+ |
| 12 | - AgentDB v1.0.7+ (via agentic-flow) |
| 13 | - Understanding of reinforcement learning concepts (optional) |
| 14 | |
| 15 | --- |
| 16 | |
| 17 | ## Quick Start with CLI |
| 18 | |
| 19 | ### Initialize ReasoningBank Database |
| 20 | |
| 21 | ```bash |
| 22 | # Initialize AgentDB for ReasoningBank |
| 23 | npx agentdb@latest init ./.agentdb/reasoningbank.db --dimension 1536 |
| 24 | |
| 25 | # Start MCP server for Claude Code integration |
| 26 | npx agentdb@latest mcp |
| 27 | claude mcp add agentdb npx agentdb@latest mcp |
| 28 | ``` |
| 29 | |
| 30 | ### Migrate from Legacy ReasoningBank |
| 31 | |
| 32 | ```bash |
| 33 | # Automatic migration with validation |
| 34 | npx agentdb@latest migrate --source .swarm/memory.db |
| 35 | |
| 36 | # Verify migration |
| 37 | npx agentdb@latest stats ./.agentdb/reasoningbank.db |
| 38 | ``` |
| 39 | |
| 40 | --- |
| 41 | |
| 42 | ## Quick Start with API |
| 43 | |
| 44 | ```typescript |
| 45 | import { createAgentDBAdapter, computeEmbedding } from 'agentic-flow/reasoningbank'; |
| 46 | |
| 47 | // Initialize ReasoningBank with AgentDB |
| 48 | const rb = await createAgentDBAdapter({ |
| 49 | dbPath: '.agentdb/reasoningbank.db', |
| 50 | enableLearning: true, // Enable learning plugins |
| 51 | enableReasoning: true, // Enable reasoning agents |
| 52 | cacheSize: 1000, // 1000 pattern cache |
| 53 | }); |
| 54 | |
| 55 | // Store successful experience |
| 56 | const query = "How to optimize database queries?"; |
| 57 | const embedding = await computeEmbedding(query); |
| 58 | |
| 59 | await rb.insertPattern({ |
| 60 | id: '', |
| 61 | type: 'experience', |
| 62 | domain: 'database-optimization', |
| 63 | pattern_data: JSON.stringify({ |
| 64 | embedding, |
| 65 | pattern: { |
| 66 | query, |
| 67 | approach: 'indexing + query optimization', |
| 68 | outcome: 'success', |
| 69 | metrics: { latency_reduction: 0.85 } |
| 70 | } |
| 71 | }), |
| 72 | confidence: 0.95, |
| 73 | usage_count: 1, |
| 74 | success_count: 1, |
| 75 | created_at: Date.now(), |
| 76 | last_used: Date.now(), |
| 77 | }); |
| 78 | |
| 79 | // Retrieve similar experiences with reasoning |
| 80 | const result = await rb.retrieveWithReasoning(embedding, { |
| 81 | domain: 'database-optimization', |
| 82 | k: 5, |
| 83 | useMMR: true, // Diverse results |
| 84 | synthesizeContext: true, // Rich context synthesis |
| 85 | }); |
| 86 | |
| 87 | console.log('Memories:', result.memories); |
| 88 | console.log('Context:', result.context); |
| 89 | console.log('Patterns:', result.patterns); |
| 90 | ``` |
| 91 | |
| 92 | --- |
| 93 | |
| 94 | ## Core ReasoningBank Concepts |
| 95 | |
| 96 | ### 1. Trajectory Tracking |
| 97 | |
| 98 | Track agent execution paths and outcomes: |
| 99 | |
| 100 | ```typescript |
| 101 | // Record trajectory (sequence of actions) |
| 102 | const trajectory = { |
| 103 | task: 'optimize-api-endpoint', |
| 104 | steps: [ |
| 105 | { action: 'analyze-bottleneck', result: 'found N+1 query' }, |
| 106 | { action: 'add-eager-loading', result: 'reduced queries' }, |
| 107 | { action: 'add-caching', result: 'improved latency' } |
| 108 | ], |
| 109 | outcome: 'success', |
| 110 | metrics: { latency_before: 2500, latency_after: 150 } |
| 111 | }; |
| 112 | |
| 113 | const embedding = await computeEmbedding(JSON.stringify(trajectory)); |
| 114 | |
| 115 | await rb.insertPattern({ |
| 116 | id: '', |
| 117 | type: 'trajectory', |
| 118 | domain: 'api-optimization', |
| 119 | pattern_data: JSON.stringify({ embedding, pattern: trajectory }), |
| 120 | confidence: 0.9, |
| 121 | usage_count: 1, |
| 122 | success_count: 1, |
| 123 | created_at: Date.now(), |
| 124 | last_used: Date.now(), |
| 125 | }); |
| 126 | ``` |
| 127 | |
| 128 | ### 2. Verdict Judgment |
| 129 | |
| 130 | Judge whether a trajectory was successful: |
| 131 | |
| 132 | ```typescript |
| 133 | // Retrieve similar past trajectories |
| 134 | const similar = await rb.retrieveWithReasoning(queryEmbedding, { |
| 135 | domain: 'api-optimization', |
| 136 | k: 10, |
| 137 | }); |
| 138 | |
| 139 | // Judge based on similarity to successful patterns |
| 140 | const verdict = similar.memories.filter(m => |
| 141 | m.pattern.outcome === 'success' && |
| 142 | m.similarity > 0.8 |
| 143 | ).length > 5 ? 'likely_success' : 'needs_review'; |
| 144 | |
| 145 | console.log('Verdict:', verdict); |
| 146 | console.log('Confidence:', similar.memories[0]?.similarity || 0); |
| 147 | ``` |
| 148 | |
| 149 | ### 3. Memory Distillation |
| 150 | |
| 151 | Consolidate similar experiences into patterns: |
| 152 | |
| 153 | ```typescript |
| 154 | // Get all experiences in domain |
| 155 | const experiences = await rb.retrieveWithReasoning(embedding, { |
| 156 | domain: 'api-optimization', |
| 157 | k: 100, |
| 158 | optimizeMemory: true, // Automatic consolidation |
| 159 | }); |
| 160 | |
| 161 | // Distill into high-level pattern |
| 162 | const distilledPattern = { |
| 163 | domain: 'api-optimization', |
| 164 | pattern: 'For N+1 queries: add eager loading, then cache', |
| 165 | success_rate: 0.92, |
| 166 | sample_size: experiences.memories.length, |
| 167 | confidence: 0.95 |
| 168 | }; |
| 169 | |
| 170 | await rb.insertPattern({ |
| 171 | id: '', |
| 172 | type: 'distilled-pattern', |
| 173 | domain: 'api-optimization', |
| 174 | pattern_data: JSON.stringify({ |
| 175 | embedding: await computeEmbedding(JSON.stringify(distilledPattern)), |
| 176 | pattern: distilledPattern |
| 177 | }), |
| 178 | confidence: 0.95, |
| 179 | usage_count: 0, |
| 180 | success_count: 0, |
| 181 | created_at: Da |