v2/docs/reasoningbank/tutorial-advanced.md
This advanced tutorial covers SAFLA (Self-Aware Feedback Loop Algorithm), pattern linking, cognitive diversity patterns, and optimization strategies for building intelligent, self-improving AI systems.
Prerequisites: Complete Basic Tutorial first Time Required: 60 minutes Level: Advanced
SAFLA is the core intelligence system that makes ReasoningBank self-aware and continuously improving.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SAFLA Cycle β
β β
β 1. OBSERVE β
β ββ Track pattern usage and outcomes β
β β
β 2. ANALYZE β
β ββ Evaluate success/failure of patterns β
β β
β 3. LEARN β
β ββ Update confidence scores (Bayesian) β
β β
β 4. ADAPT β
β ββ Adjust rankings and recommendations β
β β
β 5. FEEDBACK β
β ββ Return to step 1 (continuous loop) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Example: Finding related patterns without exact keywords
# Store security patterns
npx claude-flow@alpha memory store sql_injection \
"Use parameterized queries to prevent SQL injection attacks" \
--namespace security --reasoningbank
npx claude-flow@alpha memory store xss_prevention \
"Sanitize user input and escape HTML to prevent XSS" \
--namespace security --reasoningbank
npx claude-flow@alpha memory store csrf_protection \
"Use CSRF tokens for state-changing operations" \
--namespace security --reasoningbank
# Query with broad term
npx claude-flow@alpha memory query "prevent attacks" \
--namespace security --reasoningbank
Output:
β
Found 3 results (semantic search)
1. sql_injection
Match Score: 82%
Confidence: 50%
β System understands "prevent attacks" relates to SQL injection
2. xss_prevention
Match Score: 79%
Confidence: 50%
β Also finds XSS without exact keyword match
3. csrf_protection
Match Score: 71%
Confidence: 50%
β Finds CSRF through semantic similarity
SAFLA tracks every pattern access to identify valuable knowledge:
# Check pattern statistics
npx claude-flow@alpha memory status --detailed --reasoningbank
Output:
Usage Statistics (Last 30 Days):
Most Accessed Patterns:
1. jwt_authentication (42 uses) β
2. error_handling (31 uses)
3. database_pooling (28 uses)
4. cache_strategy (15 uses)
5. rate_limiting (8 uses)
Least Accessed:
- deprecated_api_v1 (0 uses) β οΈ
- old_auth_method (1 use) β οΈ
π‘ High usage patterns get priority in search results
Every pattern has a confidence score that evolves based on usage:
# Track confidence evolution
npx claude-flow@alpha memory info jwt_authentication --reasoningbank
Output:
Pattern: jwt_authentication
Content: JWT with refresh token rotation and 15-min expiration
Confidence History:
ββ Initial: 50% (2025-10-01)
ββ After 5 uses: 58% (2025-10-05)
ββ After 10 uses: 65% (2025-10-08)
ββ After 20 uses: 73% (2025-10-12)
ββ Current: 78% (2025-10-14) β
Usage: 42 times
Last Success: 2025-10-14
Last Failure: None
Trend: βοΈ Increasing (High reliability)
SAFLA builds a knowledge graph connecting related concepts:
# Store related patterns across domains
npx claude-flow@alpha memory store jwt_auth \
"JWT tokens with HMAC SHA256 signing" \
--namespace backend --reasoningbank
npx claude-flow@alpha memory store token_storage \
"Store tokens in httpOnly cookies, not localStorage" \
--namespace frontend --reasoningbank
npx claude-flow@alpha memory store token_refresh \
"Refresh tokens before expiration using interceptors" \
--namespace frontend --reasoningbank
# Query finds related patterns across namespaces!
npx claude-flow@alpha memory query "token management" --reasoningbank
Output:
β
Found 3 results across 2 namespaces
Backend:
1. jwt_auth (88% match)
Frontend:
2. token_storage (82% match)
3. token_refresh (79% match)
π‘ SAFLA discovered that backend JWT and frontend storage are related!
SAFLA uses MMR (Maximal Marginal Relevance) scoring:
Score = 0.4 * Semantic_Similarity +
0.3 * Reliability_Confidence +
0.2 * Recency +
0.1 * Diversity
Example:
npx claude-flow@alpha memory query "authentication" \
--namespace backend --reasoningbank --verbose
Output:
β
Found 2 results (with scoring breakdown)
1. jwt_auth_v2
Total Score: 87%
ββ Semantic: 35% (0.4 * 87.5% similarity)
ββ Reliability: 23% (0.3 * 78% confidence)
ββ Recency: 18% (0.2 * 92% - used 2 days ago)
ββ Diversity: 11% (0.1 * 100% - unique result)
2. basic_auth_deprecated
Total Score: 42%
ββ Semantic: 30% (0.4 * 75% similarity)
ββ Reliability: 9% (0.3 * 31% confidence) β οΈ Low!
ββ Recency: 2% (0.2 * 12% - used 180 days ago) β οΈ Old!
ββ Diversity: 1% (0.1 * 5% - similar to result 1)
Pattern linking enables causal reasoning - understanding how patterns relate to each other.
| Type | Meaning | Example |
|---|---|---|
| causes | A leads to B | Poor error handling β Production crashes |
| requires | A needs B first | JWT auth β Secret key management |
| conflicts | A incompatible with B | Stateless auth β Server sessions |
| enhances | A improves B | Caching β Database performance |
| alternative | A substitutes B | OAuth2 vs Basic auth |
# Method 1: Explicit linking (future feature)
npx claude-flow@alpha memory link \
jwt_authentication requires secret_key_rotation \
--strength 0.9 --reasoningbank
# Method 2: Automatic discovery (current)
# SAFLA automatically discovers links based on:
# - Co-occurrence in queries
# - Semantic similarity
# - Usage patterns
# Store patterns
npx claude-flow@alpha memory store postgres_db \
"PostgreSQL with connection pooling" \
--namespace database --reasoningbank
npx claude-flow@alpha memory store db_indexing \
"B-tree indexes on foreign keys and WHERE clause columns" \
--namespace database --reasoningbank
npx claude-flow@alpha memory store query_optimization \
"Use EXPLAIN ANALYZE to identify slow queries" \
--namespace database --reasoningbank
# Query multiple times - SAFLA learns relationships
npx claude-flow@alpha memory query "database performance" --reasoningbank
npx claude-flow@alpha memory query "slow queries" --reasoningbank
npx claude-flow@alpha memory query "PostgreSQL optimization" --reasoningbank
# After usage, SAFLA discovers links:
# db_indexing β enhances β postgres_db
# query_optimization β requires β db_indexing
# Find all patterns that REQUIRE a specific pattern
npx claude-flow@alpha memory links jwt_authentication --type requires
Expected Output:
Patterns that REQUIRE jwt_authentication:
1. token_refresh (strength: 0.95)
β Can't refresh tokens without JWT system
2. api_authorization (strength: 0.88)
β API auth depends on JWT validation
3. user_session_management (strength: 0.75)
β Sessions need JWT for state
ReasoningBank supports 6 cognitive reasoning strategies inspired by cognitive science research.
| Pattern | Description | Use Case |
|---|---|---|
| Convergent | Focus on single best solution | Optimization, debugging |
| Divergent | Explore multiple possibilities | Brainstorming, architecture |
| Lateral | Creative indirect approaches | Innovation, problem-solving |
| Systems | Holistic interconnected thinking | Complex systems, architecture |
| Critical | Evaluate and challenge assumptions | Code review, security |
| Adaptive | Learn and evolve strategies | Self-improving agents |
# Store pattern with cognitive type
npx claude-flow@alpha memory store debug_approach \
"Use binary search to isolate bugs: test middle, eliminate half" \
--namespace debugging \
--cognitive-pattern convergent \
--reasoningbank
npx claude-flow@alpha memory store architecture_brainstorm \
"Consider microservices, monolith, serverless, hybrid approaches" \
--namespace architecture \
--cognitive-pattern divergent \
--reasoningbank
npx claude-flow@alpha memory store security_audit \
"Question all assumptions: Is this input validated? Can this be spoofed?" \
--namespace security \
--cognitive-pattern critical \
--reasoningbank
# Find convergent (focused) approaches
npx claude-flow@alpha memory query "optimization" \
--cognitive-pattern convergent --reasoningbank
Output:
β
Found convergent thinking patterns:
1. debug_approach
Binary search to isolate bugs
Pattern: Convergent (narrowing down)
2. performance_profiling
Profile first, optimize bottleneck, measure again
Pattern: Convergent (focused optimization)
Scenario: Building a new authentication system
# Phase 1: Divergent (explore options)
npx claude-flow@alpha memory query "authentication approaches" \
--cognitive-pattern divergent --reasoningbank
# Returns: JWT, OAuth2, Session-based, API keys, certificates
# Phase 2: Critical (evaluate each)
npx claude-flow@alpha memory query "JWT security concerns" \
--cognitive-pattern critical --reasoningbank
# Returns: Token theft, XSS risks, CSRF considerations
# Phase 3: Convergent (pick best solution)
npx claude-flow@alpha memory query "production JWT implementation" \
--cognitive-pattern convergent --reasoningbank
# Returns: JWT with refresh rotation (best practice)
# Phase 4: Systems (understand full picture)
npx claude-flow@alpha memory query "JWT integration points" \
--cognitive-pattern systems --reasoningbank
# Returns: Frontend storage, backend validation, refresh flow, logout
# Phase 5: Adaptive (learn from implementation)
# After implementation, confidence scores update based on success/failure
Track sequential reasoning steps to learn complete workflows.
# Start a task trajectory
npx claude-flow@alpha memory trajectory start build_api \
"Building RESTful API with authentication" \
--reasoningbank
# Record each step
npx claude-flow@alpha memory trajectory step build_api \
"Designed database schema with users, tokens tables" \
--reasoningbank
npx claude-flow@alpha memory trajectory step build_api \
"Implemented JWT token generation with HMAC SHA256" \
--reasoningbank
npx claude-flow@alpha memory trajectory step build_api \
"Added refresh token rotation with secure cookies" \
--reasoningbank
npx claude-flow@alpha memory trajectory step build_api \
"Tested with Postman: 100% pass rate" \
--reasoningbank
# Complete trajectory
npx claude-flow@alpha memory trajectory end build_api \
--outcome success --reasoningbank
# Get full trajectory
npx claude-flow@alpha memory trajectory get build_api --reasoningbank
Output:
Trajectory: build_api
Task: Building RESTful API with authentication
Outcome: β
Success
Steps: 4
Step 1: Designed database schema with users, tokens tables
Duration: 15 minutes
Step 2: Implemented JWT token generation with HMAC SHA256
Duration: 30 minutes
Step 3: Added refresh token rotation with secure cookies
Duration: 20 minutes
Step 4: Tested with Postman: 100% pass rate
Duration: 10 minutes
Total Time: 75 minutes
Confidence: 75% (successful trajectory)
SAFLA uses trajectories to:
Example:
# Query for similar task
npx claude-flow@alpha memory query "build authentication API" \
--include-trajectories --reasoningbank
Output:
β
Found 1 matching trajectory: build_api
Recommended Steps (based on successful past execution):
1. Design database schema (est. 15 min)
2. Implement JWT generation (est. 30 min)
3. Add refresh rotation (est. 20 min)
4. Test thoroughly (est. 10 min)
Total Estimated Time: 75 minutes
Success Rate: 100% (1/1 trajectories succeeded)
Confidence: 75%
SAFLA uses Bayesian updating to learn from outcomes.
P(pattern works | evidence) = P(evidence | pattern works) * P(pattern works)
βββββββββββββββββββββββββββββββββββββββββββββ
P(evidence)
Simplified:
new_confidence = old_confidence + learning_rate * (outcome - old_confidence)
# Day 1: Store new pattern
npx claude-flow@alpha memory store async_pattern \
"Use async/await with try-catch for error handling" \
--namespace javascript --reasoningbank
# Confidence: 50% (prior belief: "probably works")
# Day 5: First use β Success
# Confidence: 50% β 60% (+20% of remaining 50%)
# Day 10: Second use β Success
# Confidence: 60% β 68% (+20% of remaining 40%)
# Day 15: Third use β Failure!
# Confidence: 68% β 58% (-15% of current 68%)
# Day 20: Fourth use β Success
# Confidence: 58% β 66% (+20% of remaining 42%)
# Day 30: Fifth use β Success
# Confidence: 66% β 73% (+20% of remaining 34%)
# Report success (future feature)
npx claude-flow@alpha memory feedback async_pattern \
--outcome success --notes "Worked perfectly in production" \
--reasoningbank
# Report failure (future feature)
npx claude-flow@alpha memory feedback async_pattern \
--outcome failure --notes "Didn't handle promise rejection properly" \
--reasoningbank
# Query with confidence threshold
npx claude-flow@alpha memory query "error handling" \
--min-confidence 0.7 --reasoningbank
Output:
β
Found 3 results (confidence >= 70%)
1. try_catch_async (confidence: 87%) βββ
Proven approach, highly reliable
2. error_middleware (confidence: 79%) ββ
Good approach, moderately tested
3. logging_errors (confidence: 72%) β
Acceptable approach, some validation
Excluded (low confidence):
- manual_error_check (31%) β οΈ Unreliable
- custom_error_class (45%) β οΈ Untested
# Prioritize recent, reliable patterns
npx claude-flow@alpha memory query "database optimization" \
--min-confidence 0.6 \
--max-age 90 \
--namespace backend \
--reasoningbank
# Only return highly relevant results
npx claude-flow@alpha memory query "authentication" \
--min-similarity 0.75 \
--reasoningbank
# Search all namespaces, group by domain
npx claude-flow@alpha memory query "performance" \
--all-namespaces \
--group-by namespace \
--reasoningbank
Output:
β
Found 8 results across 3 namespaces
Backend (4 results):
1. db_connection_pooling (89% match)
2. query_optimization (82% match)
3. caching_strategy (78% match)
4. api_rate_limiting (71% match)
Frontend (3 results):
5. lazy_loading (85% match)
6. code_splitting (79% match)
7. image_optimization (74% match)
DevOps (1 result):
8. cdn_configuration (81% match)
# Find patterns used recently
npx claude-flow@alpha memory query "API design" \
--since "2025-10-01" \
--reasoningbank
# Find patterns from specific time period
npx claude-flow@alpha memory query "security" \
--between "2025-09-01" "2025-10-01" \
--reasoningbank
Integrate ReasoningBank with AI agents for autonomous learning.
class SelfLearningAgent {
constructor(namespace) {
this.namespace = namespace;
this.reasoningBank = new ReasoningBankClient();
}
async solveTask(task) {
// 1. Query ReasoningBank for relevant patterns
const patterns = await this.reasoningBank.query(task, {
namespace: this.namespace,
minConfidence: 0.5
});
// 2. Apply highest-confidence pattern
const bestPattern = patterns[0];
const result = await this.applyPattern(bestPattern, task);
// 3. Record outcome
await this.reasoningBank.recordOutcome(bestPattern.id, {
success: result.success,
duration: result.duration,
notes: result.notes
});
// 4. Update confidence (SAFLA learning)
// Happens automatically in ReasoningBank
// 5. Store trajectory for complex tasks
if (task.complex) {
await this.reasoningBank.recordTrajectory({
task: task.description,
steps: result.steps,
outcome: result.success ? 'success' : 'failure'
});
}
return result;
}
async learnFromExperience() {
// Analyze recent patterns
const stats = await this.reasoningBank.getStatistics(this.namespace);
// Identify low-confidence patterns
const unreliable = stats.patterns.filter(p => p.confidence < 0.4);
// Suggest improvements or alternatives
for (const pattern of unreliable) {
const alternatives = await this.reasoningBank.query(pattern.title, {
excludeIds: [pattern.id],
minConfidence: 0.6
});
if (alternatives.length > 0) {
console.log(`Consider replacing ${pattern.title} with ${alternatives[0].title}`);
}
}
}
}
// Usage
const agent = new SelfLearningAgent('backend');
await agent.solveTask({ description: 'Implement API authentication' });
await agent.learnFromExperience();
# Agent learns from code reviews over time
# Week 1: Store initial patterns
npx claude-flow@alpha memory store code_review_001 \
"Check for SQL injection vulnerabilities in raw queries" \
--namespace code_review --reasoningbank
# Week 2: Agent finds SQL injection β Confidence increases
# Confidence: 50% β 65%
# Week 5: Agent misses XSS vulnerability β Need new pattern
npx claude-flow@alpha memory store code_review_002 \
"Validate and escape user input to prevent XSS attacks" \
--namespace code_review --reasoningbank
# Week 10: Agent now catches both SQL injection AND XSS
# Pattern 1 confidence: 65% β 82%
# Pattern 2 confidence: 50% β 74%
# Week 20: Query for code review patterns
npx claude-flow@alpha memory query "security vulnerabilities" \
--namespace code_review --reasoningbank
# Returns both patterns, ranked by reliability!
# β Slow: Sequential stores
for file in *.md; do
npx claude-flow@alpha memory store "$file" "$(cat $file)" --reasoningbank
done
# β
Fast: Batch import (future feature)
npx claude-flow@alpha memory import patterns.json --reasoningbank
# For large databases, partition by namespace
# Each namespace becomes separate table/index
npx claude-flow@alpha memory optimize --partition-namespaces --reasoningbank
# Cache embeddings for frequently queried terms
npx claude-flow@alpha memory cache-embeddings \
--queries "authentication,performance,security" \
--reasoningbank
# For > 10,000 patterns, use ANN (future feature)
npx claude-flow@alpha memory config \
--search-algorithm ann \
--ann-neighbors 100 \
--reasoningbank
// Client-side caching
const cache = new Map();
async function queryWithCache(query, options) {
const cacheKey = JSON.stringify({ query, options });
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
const results = await reasoningBank.query(query, options);
cache.set(cacheKey, results);
return results;
}
Create a self-improving bug tracking system:
# 1. Store 10 bug solutions
# 2. Query for similar bugs
# 3. Track which solutions work
# 4. Observe confidence scores improving
Build a linked knowledge base:
# 1. Store related patterns
# 2. Document relationships (requires, enhances, conflicts)
# 3. Query and observe cross-domain recommendations
Apply different cognitive patterns to the same problem:
# 1. Use divergent thinking to explore solutions
# 2. Use critical thinking to evaluate each
# 3. Use convergent thinking to select best
# 4. Use systems thinking to understand integration
You've mastered:
Key Insight: ReasoningBank is not just a databaseβit's a self-aware learning system that gets smarter with every use.
Last Updated: 2025-10-14 Version: v2.7.0-alpha.10