API Reference
Protocols
Section titled “Protocols”ConsolidationStrategyProtocol
Section titled “ConsolidationStrategyProtocol”Strategy for consolidating episodic memories into semantic memories.
MemoryIndexProtocol
Section titled “MemoryIndexProtocol”Indexes and retrieves memory entries by semantic similarity.
Index a memory entry and return its ID.
RelevanceScorerProtocol
Section titled “RelevanceScorerProtocol”Scores a MemoryEntry for pruning priority (higher = keep).
Score a memory entry for retention.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | The memory entry to score. |
| `query` | str | None | Optional query context for relevance scoring. |
| Type | Description |
|---|---|
| float | A score in range [0, 1] or higher, where higher means more important to keep. |
Classes
Section titled “Classes”AccessFrequencyStrategy
Section titled “AccessFrequencyStrategy”Preserves high-importance entries; prunes low-importance stale ones.
Initialise the importance threshold strategy.
| Parameter | Type | Description |
|---|---|---|
| `importance_threshold` | float | Entries below this value are candidates for pruning. |
Return True if entry importance is below threshold.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Entry to evaluate. |
| Type | Description |
|---|---|
| bool | True if importance is below threshold. |
Split entries into (kept, pruned).
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | Entries to evaluate. |
| Type | Description |
|---|---|
| tuple[list[MemoryEntry], list[MemoryEntry]] | Tuple of (kept, pruned) entry lists. |
BufferMemoryStore
Section titled “BufferMemoryStore”Fixed-capacity FIFO buffer of raw conversation turns.
The oldest entries are evicted when the buffer reaches max_entries.
Initialise the buffer.
| Parameter | Type | Description |
|---|---|---|
| `max_entries` | int | Maximum number of entries to retain. |
Append entry to the buffer, evicting the oldest if full.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry to store. |
Return the top_k most recent entries as search results.
| Parameter | Type | Description |
|---|---|---|
| `query` | MemoryQuery | Search parameters (only top_k is used). |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Most recent entries wrapped in search results with score 1.0. |
Return the n most recent entries.
| Parameter | Type | Description |
|---|---|---|
| `n` | int | Maximum entries to return. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Entries ordered newest-first. |
Remove entry with entry_id from the buffer.
| Parameter | Type | Description |
|---|---|---|
| `entry_id` | str | ID of the entry to remove. |
Clear all entries from the buffer.
CacheMemoryBackend
Section titled “CacheMemoryBackend”MemoryStoreProtocol backed by a CacheBackendProtocol (e.g. Redis).
ConsolidationConfig
Section titled “ConsolidationConfig”Configuration for the background consolidation pipeline.
Attributes: enabled: Whether automatic consolidation is active. interval_seconds: How often to run a consolidation pass. age_threshold_hours: Minimum entry age before it can be consolidated. importance_prune_threshold: Entries below this importance are eligible for pruning. batch_size: Maximum entries processed per consolidation pass.
ConsolidationScheduler
Section titled “ConsolidationScheduler”Runs MemoryConsolidator on a configurable interval.
Designed to be started once and cancelled on shutdown. Consolidation is only triggered when the interval elapses and entries are available.
def __init__( store: MemoryStoreProtocol, consolidator: MemoryConsolidatorProtocol, config: ConsolidationConfig | None = None ) -> None
Initialise the scheduler.
| Parameter | Type | Description |
|---|---|---|
| `store` | MemoryStoreProtocol | Memory store to read entries from. |
| `consolidator` | MemoryConsolidatorProtocol | Consolidator run on each cycle. |
| `config` | ConsolidationConfig | None | Scheduling configuration. |
Start the background consolidation loop.
Cancel the background consolidation loop.
Execute a single consolidation pass immediately.
| Type | Description |
|---|---|
| ConsolidationResult | Result of the consolidation pass. |
ConversationBuffer
Section titled “ConversationBuffer”FIFO buffer that keeps the most recent conversation turns.
Provides a simple, bounded working-memory strategy that auto-evicts
the oldest entries when max_turns or max_tokens limits are hit.
Example
buffer = ConversationBuffer(max_turns=20, max_tokens=4096)await buffer.add(entry)context = buffer.get_context()| Parameter | Type | Description |
|---|---|---|
| `max_turns` | Maximum number of turns to retain. | |
| `max_tokens` | Soft token cap — oldest entries are evicted until the total estimated token count is at or below this limit. Set to 0 to disable token-based eviction. |
Initialise the conversation buffer.
| Parameter | Type | Description |
|---|---|---|
| `max_turns` | int | Maximum number of turns to retain. |
| `max_tokens` | int | Soft token cap (0 = no token limit). |
Add a memory entry to the buffer.
If adding the entry would exceed max_turns, the oldest entry
is automatically evicted. After insertion, token-based eviction
is applied if max_tokens > 0.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | The memory entry to add. |
Return all entries currently in the buffer, oldest-first.
| Type | Description |
|---|---|
| list[MemoryEntry] | Ordered list of memory entries. |
Remove all entries from the buffer.
Number of entries currently in the buffer.
Estimated total token count across all buffered entries.
ConversationMemoryStore
Section titled “ConversationMemoryStore”Stores complete conversation history partitioned by session_id.
Each session’s turns are appended in order. Retrieval returns the most recent turns for the requested session (filter via metadata).
Initialise the conversation store.
| Parameter | Type | Description |
|---|---|---|
| `max_turns_per_session` | int | Maximum turns kept per session. |
Persist entry under its session (from metadata).
The session is derived from entry.metadata["session_id"] when
present, otherwise filed under a "_default" session.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry to store. |
Return entries for the session specified in query.filters.
Falls back to all entries if no session_id filter is given.
| Parameter | Type | Description |
|---|---|---|
| `query` | MemoryQuery | Search parameters including optional ``session_id`` filter. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Most recent entries for the matching session. |
Return the n globally most recent entries.
| Parameter | Type | Description |
|---|---|---|
| `n` | int | Maximum entries to return. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Entries ordered newest-first. |
Remove entry from all sessions.
| Parameter | Type | Description |
|---|---|---|
| `entry_id` | str | ID of the entry to remove. |
Clear all sessions and entries.
Return all entries for a given session in chronological order.
| Parameter | Type | Description |
|---|---|---|
| `session_id` | str | Session identifier. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Entries in insertion order. |
DatabaseMemoryBackend
Section titled “DatabaseMemoryBackend”MemoryStoreProtocol backed by an SQL database.
DeduplicationStrategy
Section titled “DeduplicationStrategy”Removes near-duplicate entries based on content similarity.
Two entries are duplicates if their lowercased content shares more than similarity_threshold characters of the shorter one (Jaccard-like).
Initialise the deduplication strategy.
| Parameter | Type | Description |
|---|---|---|
| `similarity_threshold` | float | Jaccard-like overlap above which entries are considered duplicates. |
Return (unique, duplicates).
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | Entries to deduplicate. |
| Type | Description |
|---|---|
| tuple[list[MemoryEntry], list[MemoryEntry]] | Tuple of (unique, duplicate) entry lists. |
DynamicContextPruner
Section titled “DynamicContextPruner”Prunes MemoryEntry lists to fit within a token budget.
Uses pluggable scoring strategies to rank entries by importance, then greedily selects entries until the token budget is exhausted.
Attributes: token_counter: Protocol for counting tokens in text. default_strategy: Default pruning strategy when none is specified.
def __init__( token_counter: TokenCounterProtocol, default_strategy: PruningStrategy = PruningStrategy.HYBRID ) -> None
Initialize the pruner.
| Parameter | Type | Description |
|---|---|---|
| `token_counter` | TokenCounterProtocol | Implementation of TokenCounterProtocol for token counting. |
| `default_strategy` | PruningStrategy | Default strategy to use if not overridden. Default is HYBRID. |
async def prune( entries: list[MemoryEntry], token_budget: int, query: str | None = None, strategy: PruningStrategy | None = None, **kwargs ) -> PruningResult
Prune a list of memory entries to fit within a token budget.
Scores all entries using the specified strategy, sorts them by score (descending), then greedily keeps entries until adding the next entry would exceed the remaining budget.
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | List of MemoryEntry objects to prune. |
| `token_budget` | int | Maximum number of tokens to keep. |
| `query` | str | None | Optional query context for relevance-based scoring. |
| `strategy` | PruningStrategy | None | Override the default pruning strategy. If None, uses default_strategy. **kwargs: Additional keyword arguments (reserved for future use). |
| Type | Description |
|---|---|
| PruningResult | PruningResult containing kept entries (score-ordered), counts, and metadata about the pruning operation. |
EntityExtractor
Section titled “EntityExtractor”Extracts structured subject/predicate/object triples from memory entries.
When an LLM extract callable is injected, delegation occurs there. Otherwise a lightweight heuristic fallback is used, suitable for testing and environments without LLM access.
Initialise the extractor.
| Parameter | Type | Description |
|---|---|---|
| `extract_fn` | Callable[[str], Awaitable[list[Triple]]] | None | Async callable that returns triples from raw text. When *None*, a heuristic fallback is used. |
Extract triples from entry.content.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry whose content to parse. |
| Type | Description |
|---|---|
| list[Triple] | List of (subject, predicate, object_) tuples. |
| Exception | Description |
|---|---|
| FactExtractionError | If the extraction callable raises. |
EntityMemoryStore
Section titled “EntityMemoryStore”Indexes memory entries by the entities they mention.
Entries are stored normally but also indexed by lowercase entity token so that entity-scoped retrieval is O(1) per entity.
Initialise the entity store.
Store entry and index it under each entity in entities.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry to persist. |
| `entities` | list[str] | None | Entity names to associate with this entry. |
Return entries scored by importance.
| Parameter | Type | Description |
|---|---|---|
| `query` | MemoryQuery | Search parameters. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Top-k entries ranked by importance. |
Return the n most recent entries.
| Parameter | Type | Description |
|---|---|---|
| `n` | int | Maximum entries to return. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Entries ordered newest-first. |
Remove entry and its entity index entries.
| Parameter | Type | Description |
|---|---|---|
| `entry_id` | str | ID of the entry to remove. |
Remove all entries and entity index data.
Return all entries mentioning entity.
| Parameter | Type | Description |
|---|---|---|
| `entity` | str | Entity name to search. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Entries referencing the entity, newest-first. |
EpisodicCompressor
Section titled “EpisodicCompressor”Compresses episodic memory entries by LLM-assisted summarisation.
When the episodic store grows beyond a threshold, older entries are grouped by session and collapsed into a single summary entry.
def __init__(summarise_fn: Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None = None) -> None
Initialise the compressor.
| Parameter | Type | Description |
|---|---|---|
| `summarise_fn` | Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None | Async callable that accepts a list of entries and returns a single summary entry. When *None*, a simple concatenation fallback is used. |
Compress entries into a single condensed memory entry.
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | Chronologically ordered entries to compress. |
| `max_tokens` | int | Hint to the summariser for output length. |
| Type | Description |
|---|---|
| MemoryEntry | A new MemoryEntry representing the compressed form. |
| Exception | Description |
|---|---|
| ConsolidationError | If the summarisation callable raises. |
EpisodicMemoryConfig
Section titled “EpisodicMemoryConfig”Configuration for episodic memory tier.
Attributes: default_top_k: Default number of episodes to retrieve. recency_weight: Weight applied to temporal recency during scoring. importance_weight: Weight applied to entry importance during scoring. relevance_weight: Weight applied to semantic similarity during scoring. ttl_seconds: Time-to-live for entries (0 = never expire).
EpisodicMemoryStore
Section titled “EpisodicMemoryStore”Episodic memory layer backed by a pluggable MemoryStoreProtocol.
def __init__(backend: MemoryStoreProtocol) -> None
FactStore
Section titled “FactStore”In-memory graph of subject/predicate/object facts.
Used by SemanticMemoryStore to persist extracted knowledge.
Initialise an empty fact store.
def add( subject: str, predicate: str, object_: str, confidence: float = 1.0, metadata: dict | None = None ) -> str
Add a new fact triple.
| Parameter | Type | Description |
|---|---|---|
| `subject` | str | Subject entity. |
| `predicate` | str | Relationship type. |
| `object_` | str | Object value. |
| `confidence` | float | Confidence score in [0.0, 1.0]. |
| `metadata` | dict | None | Optional additional metadata. |
| Type | Description |
|---|---|
| str | Unique ID assigned to the stored fact. |
Return all facts where subject matches (case-insensitive prefix).
| Parameter | Type | Description |
|---|---|---|
| `subject` | str | Subject to filter by. |
| Type | Description |
|---|---|
| list[dict] | List of fact dicts (id, subject, predicate, object_, confidence). |
Return all facts mentioning entity as subject or object.
| Parameter | Type | Description |
|---|---|---|
| `entity` | str | Entity name to search. |
| Type | Description |
|---|---|
| list[dict] | List of matching fact dicts. |
Update the confidence of an existing fact.
| Parameter | Type | Description |
|---|---|---|
| `fact_id` | str | ID of the fact to update. |
| `confidence` | float | New confidence value in [0.0, 1.0]. |
Remove a fact.
| Parameter | Type | Description |
|---|---|---|
| `fact_id` | str | ID of the fact to remove. |
Remove all facts.
HybridScorerImpl
Section titled “HybridScorerImpl”Weighted blend of recency and content length as a proxy for relevance.
Content length serves as a simple heuristic for information density: longer entries are assumed to contain more contextual information.
Attributes: recency_weight: Weight for the recency component (default 0.6). relevance_weight: Weight for the content length component (default 0.4).
Initialize the hybrid scorer.
| Parameter | Type | Description |
|---|---|---|
| `recency_weight` | float | Weight for recency in blended score. Default 0.6. |
| `relevance_weight` | float | Weight for content length (relevance proxy). Default 0.4. |
Score all entries together, normalizing recency to [0, 1].
| Parameter | Type | Description |
|---|---|---|
| `entries` | list | List of memory entries to score. |
| `query` | str | None | Optional query context (unused by HybridScorerImpl). |
| Type | Description |
|---|---|
| list[float] | List of scores parallel to the input entries list. |
Score a single entry (recency not normalized — use score_batch for batches).
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | The memory entry to score. |
| `query` | str | None | Optional query (unused by HybridScorerImpl). |
| Type | Description |
|---|---|
| float | Weighted score of content length (recency omitted without batch context). |
InMemoryMemoryBackend
Section titled “InMemoryMemoryBackend”MemoryStoreProtocol backed by an in-process dictionary.
MemoryConfig
Section titled “MemoryConfig”Top-level configuration for lexigram-ai-memory.
Attributes: working: Working memory token budget configuration. episodic: Episodic memory retrieval configuration. semantic: Semantic memory fact storage configuration. consolidation: Background consolidation pipeline configuration. default_backend: Backend type to use (‘in_memory’, ‘cache’, ‘database’, ‘vector’). ttl_seconds: Default entry TTL in seconds (0 = never expire).
MemoryConsolidatedHook
Section titled “MemoryConsolidatedHook”Payload fired after a memory consolidation pass completes.
Attributes:
strategy: Name of the consolidation strategy that ran (e.g.
"recency_decay" or "deduplication").
MemoryConsolidator
Section titled “MemoryConsolidator”Orchestrates consolidation of a batch of MemoryEntry objects.
Applies deduplication, recency decay pruning, and importance-floor pruning in sequence. Optionally runs a summarisation pass on the remaining aged entries.
def __init__( config: ConsolidationConfig | None = None, summarise_fn: Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None = None ) -> None
Initialise the consolidator.
| Parameter | Type | Description |
|---|---|---|
| `config` | ConsolidationConfig | None | Consolidation thresholds. Defaults to ``ConsolidationConfig()``. |
| `summarise_fn` | Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None | Optional async callable for summarising aged entry groups. |
Consolidate entries via deduplication, decay, and importance pruning.
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | Entries to process. |
| Type | Description |
|---|---|
| ConsolidationResult | ConsolidationResult with counts of processed, consolidated, pruned, and extracted entities. |
Report consolidator health.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Maximum seconds for the health check. |
| Type | Description |
|---|---|
| HealthCheckResult | HealthCheckResult indicating HEALTHY status. |
MemoryModule
Section titled “MemoryModule”Three-tier AI memory: working, episodic, and semantic.
Call configure to register MemoryProvider and expose all memory protocol contracts for injection.
Usage
from lexigram.ai.memory.config import MemoryConfigfrom lexigram.ai.memory import MemoryModule
@module( imports=[MemoryModule.configure(MemoryConfig(...))])class AppModule(Module): passdef configure( cls, config: MemoryConfig | None = None, enable_consolidation: bool = True ) -> DynamicModule
Create a MemoryModule with explicit configuration.
| Parameter | Type | Description |
|---|---|---|
| `config` | MemoryConfig | None | MemoryConfig or ``None`` for defaults (in-memory backend, standard budget fractions). |
| `enable_consolidation` | bool | Register the ConsolidationScheduler which periodically promotes episodic memories into semantic storage. Defaults to ``True``. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
def stub( cls, config: MemoryConfig | None = None ) -> DynamicModule
Create a MemoryModule suitable for unit and integration testing.
Uses in-memory backends with minimal side effects. Consolidation scheduling is disabled by default to avoid background timer tasks during tests.
| Parameter | Type | Description |
|---|---|---|
| `config` | MemoryConfig | None | Optional MemoryConfig override. Uses safe test defaults when ``None``. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
MemoryProvider
Section titled “MemoryProvider”Registers all memory services in the DI container.
Wires up the MemoryStoreProtocol, EpisodicMemoryProtocol,
SemanticMemoryProtocol, WorkingMemoryProtocol, and consolidation
services. The default backend is InMemoryMemoryBackend; callers
with persistent needs should register a MemoryStoreProtocol
override after this provider runs.
Initialise the provider.
| Parameter | Type | Description |
|---|---|---|
| `config` | MemoryConfig | None | Memory configuration. Defaults to ``MemoryConfig()``. |
| `enable_consolidation` | bool | Start the consolidation scheduler during boot. When ``False``, the scheduler is never started regardless of ``config.consolidation.enabled``. |
async def register(container: ContainerRegistrarProtocol) -> None
Register memory services.
| Parameter | Type | Description |
|---|---|---|
| `container` | ContainerRegistrarProtocol | DI container registrar. |
async def boot(container: ContainerResolverProtocol) -> None
Start the consolidation scheduler if enabled.
| Parameter | Type | Description |
|---|---|---|
| `container` | ContainerResolverProtocol | Resolved DI container. |
Stop the consolidation scheduler.
Health check — always healthy (in-process domain provider).
No external backend to ping.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Ignored for in-process providers. |
| Type | Description |
|---|---|
| HealthCheckResult | Always HEALTHY — no external backend to ping. |
MemoryRetrievedEvent
Section titled “MemoryRetrievedEvent”Emitted when a memory query returns results.
Consumed by: AI audit trails, context relevance analytics.
MemoryRetrievedHook
Section titled “MemoryRetrievedHook”Payload fired when entries are retrieved from a memory store.
Attributes: tier: Memory tier that was queried. result_count: Number of entries returned by the retrieval.
MemoryRetriever
Section titled “MemoryRetriever”Queries one or more MemoryStoreProtocol backends and merges results.
Results from each backend are pooled, deduplicated by entry ID, and re-ranked by the configured RelevanceRanker. Retrieval counts are tracked per entry to enable downstream analytics and relevance decay.
def __init__( sources: list[MemoryStoreProtocol], ranker: RelevanceRanker | None = None ) -> None
Initialise the retriever.
| Parameter | Type | Description |
|---|---|---|
| `sources` | list[MemoryStoreProtocol] | Memory store backends to query in parallel. |
| `ranker` | RelevanceRanker | None | Optional ranker. Defaults to a new ``RelevanceRanker``. |
Retrieve and merge results from all configured sources.
| Parameter | Type | Description |
|---|---|---|
| `query` | MemoryQuery | Search query with weights and filters. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | De-duplicated, re-ranked results capped at ``query.top_k``. |
def add_source(source: MemoryStoreProtocol) -> None
Register a new backend source.
| Parameter | Type | Description |
|---|---|---|
| `source` | MemoryStoreProtocol | Additional MemoryStoreProtocol to query. |
Return how many times a specific entry has been retrieved.
| Parameter | Type | Description |
|---|---|---|
| `entry_id` | str | The memory entry ID. |
| Type | Description |
|---|---|
| int | Number of times the entry appeared in retrieval results. |
Return aggregate retrieval statistics.
| Type | Description |
|---|---|
| dict[str, Any] | Dictionary with total retrievals, unique entries, and top-10 most-retrieved entries. |
Clear all retrieval tracking data.
MemoryStoredEvent
Section titled “MemoryStoredEvent”Emitted when a memory entry is persisted to the memory store.
Consumed by: AI audit trails, context management, safety review.
MemoryWrittenHook
Section titled “MemoryWrittenHook”Payload fired when an entry is written to a memory store.
Attributes:
tier: Memory tier that received the write (e.g. "working",
"episodic", or "semantic").
backend: Backend identifier that persisted the entry (e.g.
"in_memory" or "vector").
PruningResult
Section titled “PruningResult”Result of a context pruning operation.
Attributes: kept: List of MemoryEntry items kept (score-ordered, highest first). pruned_count: Number of entries that were removed. original_count: Number of entries that came in. token_budget: The token budget that was applied. strategy: The pruning strategy used. metadata: Optional metadata dictionary with additional pruning details.
PruningStrategy
Section titled “PruningStrategy”Enum of pruning scoring strategies.
Attributes: RECENCY: Keep most recent entries by timestamp. RELEVANCE: Keep highest-relevance entries (placeholder for future embedding-based scoring). HYBRID: Weighted blend of recency and relevance (content length as proxy).
RecencyDecayStrategy
Section titled “RecencyDecayStrategy”Prunes entries whose recency score falls below a threshold.
Uses an exponential decay model with a configurable half-life.
Initialise the recency decay strategy.
| Parameter | Type | Description |
|---|---|---|
| `half_life_hours` | float | Time (h) for importance to halve. |
| `threshold` | float | Entries with recency below this are pruned. |
Return True if entry should be pruned.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Entry to evaluate. |
| Type | Description |
|---|---|
| bool | True if the entry's recency score is below threshold. |
Split entries into (kept, pruned).
| Parameter | Type | Description |
|---|---|---|
| `entries` | list[MemoryEntry] | Entries to evaluate. |
| Type | Description |
|---|---|
| tuple[list[MemoryEntry], list[MemoryEntry]] | Tuple of (kept, pruned) entry lists. |
RecencyScorerImpl
Section titled “RecencyScorerImpl”Scores memory entries by recency — more recent entries get higher scores.
Uses the entry’s timestamp to produce a normalized score relative to the entire batch of entries being pruned.
Score entry by recency.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | The memory entry to score. |
| `query` | str | None | Optional query (unused by RecencyScorerImpl). |
| Type | Description |
|---|---|
| float | Score based on entry's timestamp (normalized in batch context). |
RelevanceRanker
Section titled “RelevanceRanker”Re-ranks a list of MemorySearchResult by a multi-factor score.
Combines the raw retrieval score with recency and importance using the weights from the originating MemoryQuery.
Re-rank results and return a new sorted list.
| Parameter | Type | Description |
|---|---|---|
| `results` | list[MemorySearchResult] | Raw search results to re-rank. |
| `query` | MemoryQuery | Original query with weighting parameters. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Results sorted by descending combined score. |
def top_k( results: list[MemorySearchResult], query: MemoryQuery, k: int | None = None ) -> list[MemorySearchResult]
Re-rank and return the top k results.
| Parameter | Type | Description |
|---|---|---|
| `results` | list[MemorySearchResult] | Raw results to rank. |
| `query` | MemoryQuery | Query parameters. |
| `k` | int | None | Maximum results to return. Defaults to ``query.top_k``. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Top-k results after re-ranking. |
SemanticMemoryConfig
Section titled “SemanticMemoryConfig”Configuration for semantic memory tier.
Attributes: min_confidence: Minimum confidence to store a fact. max_facts_per_entity: Hard cap on stored facts per entity.
SemanticMemoryStore
Section titled “SemanticMemoryStore”Semantic memory backed by an in-process FactStore.
def __init__( fact_store: FactStore | None = None, extractor: EntityExtractor | None = None, min_confidence: float = 0.5, max_facts_per_entity: int = 100 ) -> None
SummaryMemoryStore
Section titled “SummaryMemoryStore”Memory store that periodically compresses older turns into summaries.
Maintains a hot buffer of recent turns and a list of compressed summary entries. When the hot buffer exceeds compress_threshold, the oldest compress_batch entries are collapsed via summarise_fn.
def __init__( compress_threshold: int = 20, compress_batch: int = 10, summarise_fn: Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None = None ) -> None
Initialise the summary store.
| Parameter | Type | Description |
|---|---|---|
| `compress_threshold` | int | Number of hot entries that triggers compression. |
| `compress_batch` | int | Number of oldest entries to compress per run. |
| `summarise_fn` | Callable[[list[MemoryEntry]], Awaitable[MemoryEntry]] | None | Async callable that reduces a batch to one entry. |
Add entry to the hot buffer, compressing if necessary.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry to add. |
Return summaries plus most recent hot entries.
| Parameter | Type | Description |
|---|---|---|
| `query` | MemoryQuery | Search parameters. |
| Type | Description |
|---|---|
| list[MemorySearchResult] | Combined results from summaries and hot buffer. |
Return the n most recent entries (hot buffer only).
| Parameter | Type | Description |
|---|---|---|
| `n` | int | Maximum entries to return. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Most recent entries, newest-first. |
Remove entry with entry_id from hot buffer or summaries.
| Parameter | Type | Description |
|---|---|---|
| `entry_id` | str | ID of the entry to remove. |
Clear all hot entries and summaries.
TokenBudgetAllocator
Section titled “TokenBudgetAllocator”Distributes a total token budget across working memory sections.
The budget is split in this order:
- System prompt receives a fixed allocation.
- The remaining budget is divided among recent turns, episodic recall, semantic facts, and tool descriptions using the configured fractions.
def __init__(config: WorkingMemoryConfig | None = None) -> None
Initialise with optional config.
| Parameter | Type | Description |
|---|---|---|
| `config` | WorkingMemoryConfig | None | Working memory configuration; uses defaults if None. |
Compute token allocations for each memory section.
| Parameter | Type | Description |
|---|---|---|
| `total_tokens` | int | Total token budget available. |
| Type | Description |
|---|---|
| dict[str, int] | Mapping of section name to token allocation. |
Return the token budget for a single named section.
| Parameter | Type | Description |
|---|---|---|
| `section` | str | Section name (e.g. 'episodic', 'semantic'). |
| `total_tokens` | int | Total token budget. |
| Type | Description |
|---|---|
| int | Token count allocated to the requested section. |
VectorMemoryBackend
Section titled “VectorMemoryBackend”MemoryStoreProtocol that persists entries as vector-searchable documents.
def __init__( vector_store: DocumentVectorStoreProtocol, embed_fn: Callable[[str], Awaitable[list[float]]] | None = None, collection: str = 'memory', fallback: MemoryStoreProtocol | None = None ) -> None
WorkingMemoryConfig
Section titled “WorkingMemoryConfig”Token-budget allocation for working memory assembly.
Attributes: system_prompt_tokens: Fixed token allocation for system prompt. recent_turns_fraction: Fraction of remaining budget for recent turns. episodic_fraction: Fraction of remaining budget for episodic recall. semantic_fraction: Fraction of remaining budget for semantic facts. tool_descriptions_fraction: Fraction of remaining budget for tool descriptions. max_recent_turns: Hard cap on recent turns regardless of budget.
WorkingMemoryManager
Section titled “WorkingMemoryManager”Assembles the optimal context window from all memory tiers.
Pulls recent turns from episodic memory and relevant facts from semantic memory, fitting everything within a configurable token budget.
def __init__( episodic: EpisodicMemoryProtocol | None = None, semantic: SemanticMemoryProtocol | None = None, config: WorkingMemoryConfig | None = None ) -> None
Initialise the working memory manager.
| Parameter | Type | Description |
|---|---|---|
| `episodic` | EpisodicMemoryProtocol | None | Episodic memory source for past interactions. |
| `semantic` | SemanticMemoryProtocol | None | Semantic memory source for structured facts. |
| `config` | WorkingMemoryConfig | None | Token budget configuration. |
async def assemble( query: str, token_budget: int, session_id: str | None = None ) -> list[MemoryEntry]
Assemble context window from all available memory tiers.
| Parameter | Type | Description |
|---|---|---|
| `query` | str | Current user query used to retrieve relevant memories. |
| `token_budget` | int | Total token budget for the assembled context. |
| `session_id` | str | None | Optional session scope for retrieval filtering. |
| Type | Description |
|---|---|
| list[MemoryEntry] | Ordered list of memory entries ready for LLM context injection. |
Add an entry to the current working memory stream.
| Parameter | Type | Description |
|---|---|---|
| `entry` | MemoryEntry | Memory entry to add. |
Return the entries currently assembled in working context.
| Type | Description |
|---|---|
| list[MemoryEntry] | Current context entry list. |
Clear the current context assembly.
Check health of underlying memory tiers.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Maximum seconds for the health check. |
| Type | Description |
|---|---|
| HealthCheckResult | HealthCheckResult aggregating episodic and semantic tier health. |
Exceptions
Section titled “Exceptions”ConsolidationError
Section titled “ConsolidationError”Error raised during memory consolidation.
EmbeddingError
Section titled “EmbeddingError”Raised when generating or storing an embedding fails.
FactExtractionError
Section titled “FactExtractionError”Raised when entity/fact extraction from text fails.
MemoryCapacityError
Section titled “MemoryCapacityError”Raised when a memory store is at capacity and cannot accept new entries.
Initialise with optional capacity context.
| Parameter | Type | Description |
|---|---|---|
| `message` | str | Human-readable description. |
| `capacity` | int | None | Maximum capacity that was exceeded. |
MemoryStoreError
Section titled “MemoryStoreError”Raised when a backend store operation fails.
Initialise with optional store name context.
| Parameter | Type | Description |
|---|---|---|
| `message` | str | Human-readable description. |
| `store` | str | None | Name of the store that raised the error. |
MemorySystemError
Section titled “MemorySystemError”Base exception for memory operations.