Memory trait defines the interface for all persistence backends in ZeroClaw. Implement this trait to integrate new storage systems like databases, vector stores, or custom memory solutions.
Trait Definition
Required Methods
name
Return the backend name.
&str
Stable lowercase identifier (e.g., “sqlite”, “markdown”, “qdrant”)
store
Store a memory entry.
&str
required
Unique identifier for this memory entry
&str
required
Memory content (text, JSON, or any string data)
MemoryCategory
required
Memory category for organization:
MemoryCategory::Core- Long-term facts, preferences, decisionsMemoryCategory::Daily- Daily session logsMemoryCategory::Conversation- Conversation contextMemoryCategory::Custom(String)- User-defined category
Option<&str>
Optional session identifier to scope the memory
anyhow::Result<()>
Success or error
recall
Recall memories matching a query (keyword or semantic search).
&str
required
Search query string
usize
required
Maximum number of results to return
Option<&str>
Optional session filter
Vec<MemoryEntry>
Array of matching memory entries, ranked by relevance
get
Get a specific memory by key.
&str
required
Memory key to retrieve
anyhow::Result<Option<MemoryEntry>>
Memory entry if found, or
Nonelist
List all memory entries with optional filters.
Option<&MemoryCategory>
Filter by category (all categories if
None)Option<&str>
Filter by session (all sessions if
None)anyhow::Result<Vec<MemoryEntry>>
Array of matching memory entries
forget
Remove a memory by key.
&str
required
Memory key to remove
anyhow::Result<bool>
true if memory was deleted, false if not foundcount
Count total memories.
anyhow::Result<usize>
Total number of memory entries
health_check
Check if the memory backend is healthy.
bool
true if backend is operationalOptional Methods
reindex
Rebuild embeddings for all memories using the current embedding provider.
Option<Box<dyn Fn(usize, usize) + Send + Sync>>
Optional callback for progress updates:
(current, total)anyhow::Result<usize>
Number of memories reindexed
Types
MemoryEntry
MemoryCategory
Core→"core"Daily→"daily"Conversation→"conversation"Custom("notes")→"notes"
"core", "daily", "conversation").
Implementation Example
Here’s a simplified in-memory backend:Vector Search Example
For vector-based semantic search (SQLite with embeddings, Qdrant, etc.):Factory Registration
Register your memory backend in the factory:Best Practices
Concurrent Access: Use proper locking (
RwLock, Mutex) for in-memory backends or rely on database transaction isolation for persistence.Vector Reindexing: Implement
reindex() for vector-based backends to support model migration.Testing
Related
- Tool Trait - Tools can store results in memory
- Provider Trait - Providers for embeddings
- Configuration Reference - Configure memory backends in config.toml