Overview
CompositeMemory chains multiple MemoryProvider implementations together. This is useful for combining fast local memory with slow persistent memory.
Best for: multi-tier memory architectures (L1 cache + L2 persistent storage).
Constructor
Configuration
Providers chained in order.
read()queries them in order and returns the first non-empty result (or merges all)write()fans out to ALL providers
Read strategy:
'first-hit': Return the first provider that has data (default)'merge': Merge results from all providers (deduplicates by content)
Methods
read()
Retrieve conversation history using the configured strategy.options.sessionId- Session identifier- Other
MemoryReadOptionsfields passed to underlying providers
readStrategy:
first-hit strategy:
- Queries providers in order
- Returns the first non-empty result
- Returns empty array if all providers are empty
- Queries all providers in parallel
- Combines results and deduplicates by content fingerprint
- Returns merged array
write()
Persist messages to ALL providers.messages- Array of messages to storeoptions- Write options passed to all providers
- Fans out write to all providers in parallel
- All providers receive the same messages and options
clear()
Remove stored memory from all providers.sessionId- If provided, clears only that session. Otherwise clears all sessions.
- Calls
clear()on all providers in parallel - Providers without
clear()are skipped
entries()
Retrieve raw memory entries from all providers.sessionId- If provided, returns only that session’s entries
Usage Examples
Two-Tier Memory Architecture
- Reads check BufferMemory first (fast), fall back to Redis (slow)
- Writes go to both providers
- In-memory cache warms up over time
Merge Strategy
- Reads from both providers and merges results
- Deduplicates by message content
- Useful for combining different memory types
Multiple Storage Backends
- Reads try BufferMemory → Redis → Postgres
- Writes replicate to all three
- Different tiers can have different retention policies
Inspecting All Providers
Read Strategies
first-hit (default)
- Queries providers sequentially in order
- Returns immediately when a provider has data
- Faster for typical cache-hit scenarios
- Best for L1/L2 cache patterns
merge
- Queries all providers in parallel
- Combines and deduplicates results
- Useful for heterogeneous memory types
- Best for semantic + chronological memory
Deduplication
When usingreadStrategy: 'merge', messages are deduplicated by fingerprint:
Error Handling
Type Reference
Source:/packages/memory/src/composite.ts:9-23