Memory
Multi-tiered memory system with short-term, long-term, entity, and user-specific memory.Quick Start
Usage Forms Table
| Form | Example | When to Use |
|---|---|---|
| Bool | memory=True | Enable with defaults (SQLite) |
| String preset | memory="redis" | Use predefined backend |
| URL | memory="redis://localhost:6379" | Backend-specific connection |
| Dict | memory={"backend": "postgres", "user_id": "u1"} | Custom config |
| Array + overrides | memory=["redis", {"user_id": "u1"}] | Preset + customization |
| Config instance | memory=MemoryConfig(backend="redis") | Full control |
Presets & Options
| Preset | Backend | Description |
|---|---|---|
"file" | Local file | File-based storage |
"sqlite" | SQLite | Default, local database |
"redis" | Redis | Fast, distributed |
"postgres" | PostgreSQL | Scalable, persistent |
"postgresql" | PostgreSQL | Alias for postgres |
"mongodb" | MongoDB | Document-based |
"mem0" | Mem0 | External memory service |
URL Schemes
| Scheme | Backend |
|---|---|
postgresql:// | PostgreSQL |
postgres:// | PostgreSQL |
redis:// | Redis |
rediss:// | Redis with SSL |
sqlite:// | SQLite |
mongodb:// | MongoDB |
mongodb+srv:// | MongoDB Atlas |
Precedence Ladder
Resolution Order: Instance > Config > Array > Dict > String > Bool > DefaultWhen you pass
memory=, the resolver checks in this order:- Instance - MemoryManager instance? Use as-is
- Config - MemoryConfig instance? Use as-is
- Array -
["preset", {"override": value}]? Apply overrides - Dict -
{"key": value}? Convert to config - String - URL or preset? Parse URL or look up preset
- Bool -
True? Use defaults.False? Disable
Memory Tiers
| Tier | Purpose | Storage | Retention |
|---|---|---|---|
| Short-Term (STM) | Active conversation | SQLite | Ephemeral |
| Long-Term (LTM) | Persistent knowledge | SQLite + Vector | Permanent |
| Entity Memory | Structured entities | LTM subset | Permanent |
| User Memory | User preferences | LTM with user_id | Permanent |
Classes
MemoryClient
| Method | Description |
|---|---|
store_in_short_memory(memory, ...) | Store in STM |
store_in_long_memory(memory, ...) | Store in LTM |
search_memories(query, ...) | Search across tiers |
get_short_memories(...) | Retrieve STM |
get_long_memories(...) | Retrieve LTM |
clear_short_memory(...) | Clear STM |
clear_long_memory(...) | Clear LTM |
Quality Scoring Functions
compute_quality_score
Calculate overall quality score from individual metrics.calculate_quality_metrics
Use LLM to evaluate output quality against expectations.Memory Tiers
1. Short-Term Memory (STM)
- Purpose: Immediate context and active conversation
- Storage: SQLite database (
.praison/short_term.db) - Retention: Ephemeral, cleared between sessions
- Use Cases: Current task context, recent interactions
2. Long-Term Memory (LTM)
- Purpose: Persistent knowledge across sessions
- Storage: SQLite + optional vector store
- Retention: Permanent with quality filtering
- Use Cases: Learned facts, important outcomes
3. Entity Memory
- Purpose: Structured information about entities
- Storage: Subset of LTM with special formatting
- Format:
Entity {name}({type}): {desc} | relationships: {relations} - Use Cases: People, organizations, locations
4. User Memory
- Purpose: User-specific preferences and history
- Storage: LTM with user_id filtering
- Isolation: Strict user separation
- Use Cases: Personalization, preferences
Configuration
Basic Configuration
Advanced Configuration with Graph Support
Graph Database Options
Neo4j Configuration
Memgraph Configuration
Quick Start
Configuration
Basic Configuration
Advanced Configuration
Memory Types
API Reference
Constructor
Core Methods
add()
Store information in memory with optional quality scoring.text- Content to storememory_type- Type: “short”, “long”, “entity”, or “user”quality_score- Quality rating (0.0-1.0, auto-calculated if None)metadata- Additional metadata
search()
Search across all memory types for relevant information.update()
Update an existing memory entry.delete()
Delete a specific memory entry.Context Building
build_context_for_task()
Build formatted context for a specific task.get_context()
Get all memories formatted as context.Quality Management
calculate_quality_score()
Calculate quality score for a memory entry.- Information density
- Specificity
- Relevance indicators
- Entity mentions
- Temporal relevance
get_quality_memories()
Retrieve only high-quality memories.Utility Methods
get_memories()
Retrieve raw memories from storage.clear()
Clear all memories of a specific type.get_stats()
Get memory system statistics.Usage Examples
Basic Memory Operations
Quality-Based Storage
Entity Memory Management
Agent Integration
Graph-Enhanced Memory
Quality Metrics
Completeness (0-1)
How thoroughly the content addresses the requirements.Relevance (0-1)
How well the content matches the expected output.Clarity (0-1)
How clear and well-structured the content is.Accuracy (0-1)
Factual correctness of the information.Custom Weights
Best Practices
- Use Quality Filtering - Set appropriate
min_qualitythresholds - Scope Memories - Use user_id and agent_id for proper isolation
- Regular Cleanup - Clear short-term memory between sessions
- Graph for Relationships - Use graph stores for complex entity relationships
- Monitor Storage - Check database sizes periodically
- Test Retrieval - Verify context building produces relevant results =======
Basic Memory Usage
Quality-Based Storage
Graph Memory Example
Multi-Agent Memory Sharing
Best Practices
Memory Type Selection
Short-term: Conversation context, temporary state
Long-term: Facts, preferences, important information
Entity: People, places, organizations, relationships
User: Personal data, settings, history
Quality Management
- Set appropriate quality thresholds (0.7-0.8 recommended)
- Manually score critical information higher
- Periodically review and clean low-quality memories
- Use quality scores for retrieval filtering
Performance Optimization
- Limit memory searches to necessary types
- Use appropriate search limits
- Clear short-term memory periodically
- Index frequently accessed memories
Privacy & Security
- Separate user memories by user_id
- Avoid storing sensitive data in plain text
- Implement access controls for shared memory
- Regular cleanup of old user data
Provider Comparison
Troubleshooting
Common Issues:Advanced Configuration
Custom Quality Scoring
Memory Middleware
Summary
The Memory module provides a comprehensive solution for agent memory management: ✅ Multi-tiered Architecture - Different memory types for different needs✅ Quality Management - Automatic scoring and filtering
✅ Graph Support - Complex relationship mapping with Neo4j/Memgraph
✅ Flexible Storage - Multiple backend options
✅ Context Building - Automatic context generation for tasks Perfect for building agents that:
- Maintain conversation context
- Remember user preferences
- Track entity relationships
- Build knowledge over time

