Agent Memory Integration¶
Status: ✅ Implemented Date: 2025-11-03 Version: 1.0
Overview¶
The agent memory integration enables amplihack agents to learn from past experiences and share knowledge across agent instances. This system:
- Injects relevant memories into agent prompts before they run
- Extracts learnings from agent outputs after they complete
- Shares knowledge across agent types for cross-pollination
- Maintains quality through scoring and validation
Architecture¶
┌─────────────────────────────────────────────────────────┐
│ Agent Invocation │
├─────────────────────────────────────────────────────────┤
│ │
│ 1. Before Agent Runs: │
│ inject_memory_context() │
│ ├─ Query Neo4j for relevant memories │
│ ├─ Filter by agent type + category │
│ ├─ Include cross-agent learnings │
│ └─ Format memory context for injection │
│ │
│ 2. Agent Executes: │
│ (with memory context in prompt) │
│ │
│ 3. After Agent Completes: │
│ extract_and_store_learnings() │
│ ├─ Parse output for patterns │
│ ├─ Extract decisions, recommendations, etc. │
│ ├─ Assess quality scores │
│ └─ Store in Neo4j │
│ │
└─────────────────────────────────────────────────────────┘
Components¶
1. agent_integration.py¶
Main integration module providing:
inject_memory_context(): Load and format memories for agent promptsextract_and_store_learnings(): Parse and store agent learningsdetect_agent_type(): Map agent identifiers to typesdetect_task_category(): Classify tasks by category
2. extraction_patterns.py¶
Pattern matching for learning extraction:
- Decision patterns: Structured decisions with reasoning
- Recommendation patterns: Best practices and advice
- Anti-pattern patterns: Things to avoid
- Error-solution patterns: Problem-solution pairs
- Implementation patterns: Code patterns and approaches
3. agent_memory.py¶
Low-level memory storage and retrieval (already implemented in phases 1-6).
Usage¶
For Agent Developers¶
To add memory capabilities to an agent:
from amplihack.memory.neo4j.agent_integration import (
inject_memory_context,
extract_and_store_learnings
)
# Before agent runs
def run_agent(agent_type: str, task: str):
# 1. Inject memory context
memory_context = inject_memory_context(
agent_type=agent_type,
task=task
)
# 2. Build agent prompt with memory
prompt = f"{memory_context}\n\n{standard_agent_prompt}\n\nTask: {task}"
# 3. Run agent
output = agent.run(prompt)
# 4. Extract and store learnings
memory_ids = extract_and_store_learnings(
agent_type=agent_type,
output=output,
task=task,
success=True
)
return output
For Agent Users¶
Memory integration is automatic once enabled. No user action required.
Enable memory system:
# Memory system is enabled by default when Neo4j is running
# Neo4j starts automatically in launcher (see core.py line 88)
Check memory status:
# Verify Neo4j is running
docker ps | grep amplihack-neo4j
# View memory logs
tail -f .claude/runtime/logs/session_start.log
Memory Context Format¶
When an agent runs, it sees:
## 🧠 Memory Context (Relevant Past Learnings)
_Based on previous architect work in category: system_design_
### Past Architect Learnings
**1. system_design** (quality: 0.85)
Always separate authentication from authorization logic
_Outcome: Reduced coupling, easier testing_
**2. api_design** (quality: 0.78)
Use token-based auth for stateless APIs
_Outcome: Better scalability_
### Learnings from Other Agents
**1. From builder**: error_handling
Auth token validation must happen before business logic
---
[Normal agent prompt continues...]
Learning Extraction Patterns¶
Decision Pattern¶
Input:
## Decision: Token-Based Authentication
**What**: Use JWT tokens for stateless authentication
**Why**: Enables horizontal scaling
Extracted:
- Type:
decision - Content: "Token-Based Authentication: Use JWT tokens for stateless authentication"
- Reasoning: "Enables horizontal scaling"
- Confidence: 0.85
Recommendation Pattern¶
Input:
Extracted:
- Type:
recommendation - Content: "Always use bcrypt for password hashing"
- Confidence: 0.75
Anti-Pattern Pattern¶
Input:
Extracted:
- Type:
anti_pattern - Content: "Never store JWT tokens in localStorage"
- Confidence: 0.85
Error-Solution Pattern¶
Input:
Extracted:
- Type:
error_solution - Content: "Error: Database connection timeout | Solution: Increased timeout to 30 seconds"
- Confidence: 0.90
Configuration¶
Memory Settings¶
Configured via Neo4j config (.claude/runtime/memory/.config):
{
"enabled": true,
"min_quality_threshold": 0.6,
"max_context_memories": 5,
"auto_cross_agent": true
}
Agent Type Mapping¶
Supported agent types:
architect- System architecture and designbuilder- Implementation and codingreviewer- Code review and qualitytester- Testing strategiesoptimizer- Performance optimizationsecurity- Security analysisdatabase- Database designapi-designer- API designintegration- External integrationsanalyzer- Code analysiscleanup- Code cleanupfix-agent- Error fixingpre-commit-diagnostic- Pre-commit checksci-diagnostic- CI diagnostics
Task Categories¶
Auto-detected categories:
system_design- Architecture, patternssecurity- Auth, permissions, vulnerabilitiesdatabase- Schema, queries, migrationsoptimization- Performance, cachingtesting- Tests, validation, coverageerror_handling- Bugs, fixes, exceptionsimplementation- Building, codingapi- Endpoints, interfacesintegration- External services
Testing¶
Run Integration Tests¶
This runs 10 tests:
- Prerequisites check
- Container management
- Agent type detection
- Task category detection
- Memory injection (empty)
- Learning extraction and storage
- Memory injection (with context)
- Cross-agent learning
- Error-solution patterns
- Memory retrieval by category
Expected Output¶
================================================================================
AGENT MEMORY INTEGRATION TEST SUITE
================================================================================
============================================================
TEST 1: Neo4j Prerequisites
============================================================
Docker installed: ✓
Docker running: ✓
Docker Compose available: ✓
Compose file exists: ✓
✅ All prerequisites passed!
...
============================================================
TEST 6: Learning Extraction and Storage
============================================================
Extracted and stored 7 learnings:
1. mem_abc123
2. mem_def456
...
✅ Learning extraction test passed! (7 learnings stored)
...
================================================================================
TEST SUMMARY
================================================================================
✅ PASS: Prerequisites
✅ PASS: Container Management
✅ PASS: Agent Type Detection
✅ PASS: Task Category Detection
✅ PASS: Memory Injection (Empty)
✅ PASS: Learning Extraction
✅ PASS: Memory Injection (With Context)
✅ PASS: Cross-Agent Learning
✅ PASS: Error-Solution Patterns
✅ PASS: Memory Retrieval by Category
================================================================================
Total: 10 tests | Passed: 10 | Failed: 0
================================================================================
Performance¶
Memory Injection¶
- Query time: < 50ms (p95)
- Context size: ~1KB (5 memories × 200 chars avg)
- Impact on agent: Minimal (<1% of prompt size)
Learning Extraction¶
- Pattern matching: < 100ms (p95)
- Storage time: < 100ms per learning (p95)
- Total overhead: < 500ms per agent invocation
Caching¶
- Connector connection pooling: Reuses connections
- No caching of memories (always fresh from Neo4j)
Observability¶
Logs¶
All operations logged to:
.claude/runtime/logs/session_start.log- Memory initialization- Agent-specific logs (if available)
Metrics¶
Stored in .claude/runtime/metrics/:
memories_injected- Count per agent invocationlearnings_extracted- Count per agent completionmemory_query_time_ms- Query latencylearning_extraction_time_ms- Extraction latency
Neo4j Browser¶
View memories directly:
# Open Neo4j browser
open http://localhost:7474
# Login
# Username: neo4j
# Password: amplihack_neo4j
Useful queries:
// All memories by agent type
MATCH (m:Memory)-[:CREATED_BY]->(a:Agent)
RETURN a.agent_type, count(m) as memory_count
ORDER BY memory_count DESC
// High-quality learnings
MATCH (m:Memory)
WHERE m.quality_score > 0.8
RETURN m.content, m.quality_score, m.agent_type
ORDER BY m.quality_score DESC
LIMIT 10
// Cross-agent learning patterns
MATCH (m:Memory)-[:CREATED_BY]->(a1:Agent)
WHERE a1.agent_type = 'architect'
RETURN m.content, m.category, m.quality_score
ORDER BY m.quality_score DESC
LIMIT 5
Troubleshooting¶
No Memories Injected¶
Problem: Agent runs but no memory context appears
Solutions:
- Check Neo4j is running:
docker ps | grep amplihack-neo4j - Check logs:
tail -f .claude/runtime/logs/session_start.log - Verify memories exist: Query Neo4j browser
- Check quality threshold: Lower
min_quality_thresholdin config
Learnings Not Extracted¶
Problem: Agent completes but no learnings stored
Solutions:
- Check agent output format: Learnings need specific patterns
- Review extraction patterns in
extraction_patterns.py - Check logs for extraction errors
- Verify Neo4j connection
Low Quality Memories¶
Problem: Extracted memories have low quality scores
Solutions:
- Ensure agent outputs include reasoning and outcomes
- Use structured formats (Decision, Recommendation, etc.)
- Include concrete examples
- Mark important learnings explicitly
Memory Overload¶
Problem: Too much memory context slows down agents
Solutions:
- Reduce
max_context_memoriesin config - Increase
min_quality_thresholdto be more selective - Use more specific task categories
- Archive old low-quality memories
Future Enhancements¶
Planned Features¶
- Semantic Search: Use embeddings for better relevance matching
- Memory Consolidation: Periodic cleanup and merging of similar memories
- Feedback Loop: Agents rate memory usefulness
- Memory Visualization: Web UI for exploring memory graph
- Cross-Project Learning: Share high-quality memories across projects
Experimental Features¶
- Agent-specific extraction patterns: Custom patterns per agent type
- Automatic quality assessment: LLM-based quality scoring
- Memory decay: Reduce quality scores over time for stale memories
- Context-aware injection: Adjust memory selection based on conversation
API Reference¶
inject_memory_context()¶
def inject_memory_context(
agent_type: str,
task: str,
task_category: Optional[str] = None,
min_quality: float = 0.6,
max_memories: int = 5,
) -> str
Load and format relevant memories for agent prompt.
Args:
agent_type: Type of agent (e.g., "architect")task: Task descriptiontask_category: Optional category (auto-detected if None)min_quality: Minimum quality score (0-1)max_memories: Maximum memories to include
Returns:
- Formatted memory context string (empty if no memories)
extract_and_store_learnings()¶
def extract_and_store_learnings(
agent_type: str,
output: str,
task: str,
task_category: Optional[str] = None,
success: bool = True,
duration_seconds: float = 0.0,
) -> List[str]
Extract learnings from agent output and store in Neo4j.
Args:
agent_type: Type of agentoutput: Full agent outputtask: Task that was performedtask_category: Optional category (auto-detected if None)success: Whether task succeededduration_seconds: Task duration
Returns:
- List of memory IDs that were stored
Contributing¶
To add new extraction patterns:
- Add pattern matcher to
extraction_patterns.py - Add tests to
test_agent_memory_integration.py - Document pattern in this file
- Update agent documentation with usage examples