org.llm4s.agent.memory
Memory system for agent context and knowledge retention.
The memory package provides a comprehensive system for agents to remember information across conversations, learn about entities, and retrieve relevant context for responses.
== Core Concepts ==
'''Memory Types:'''
Conversation- Messages from conversation historyEntity- Facts about people, organizations, conceptsKnowledge- Information from external documentsUserFact- Learned user preferences and backgroundTask- Records of completed tasks and outcomes
'''Components:'''
- memory.Memory - Individual memory entry
- memory.MemoryStore - Storage backend trait
- memory.MemoryManager - High-level memory operations
- memory.MemoryFilter - Composable filtering predicates
== Quick Start ==
import org.llm4s.agent.memory._
// Create a memory manager
val manager = SimpleMemoryManager.empty
// Record a user fact
val updated = manager.recordUserFact(
"User prefers Scala over Java",
userId = Some("user123")
)
// Record an entity fact
val entityId = EntityId.fromName("Scala")
val withEntity = updated.flatMap(_.recordEntityFact(
entityId = entityId,
entityName = "Scala",
fact = "Scala is a programming language",
entityType = "technology"
))
// Retrieve relevant context for a query
val context = withEntity.flatMap(_.getRelevantContext("Tell me about Scala"))
== Memory Filtering ==
Filters can be composed for complex queries:
import org.llm4s.agent.memory.MemoryFilter._
// Simple filters
val conversationsOnly = conversations
val importantOnly = important(0.7)
// Combined filters
val recentImportant = after(weekAgo) && important(0.5)
val entityOrKnowledge = entities || knowledge
// Recall with filter
store.recall(recentImportant, limit = 10)
== Storage Backends ==
Multiple backends are available:
- memory.InMemoryStore - Fast, ephemeral storage for testing
- (Future) SQLiteStore - Persistent local storage
- (Future) VectorStore - Semantic search with embeddings
Attributes
- See also
-
memory.SimpleMemoryManager for basic memory management
memory.MemoryFilter for filtering options
Members list
Type members
Classlikes
Service for generating embeddings for memory content.
Service for generating embeddings for memory content.
This trait abstracts over the embedding generation process, allowing different implementations (LLM providers, local models, etc.).
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class LLMEmbeddingServiceclass MockEmbeddingService
Type-safe wrapper for entity identifiers.
Type-safe wrapper for entity identifiers.
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass AnyValtrait Matchableclass AnyShow all
In-memory implementation of MemoryStore.
In-memory implementation of MemoryStore.
This implementation stores all memories in memory, making it suitable for testing, short-lived agents, and scenarios where persistence isn't required.
Features:
- Fast lookups using indexed data structures
- Basic keyword search (semantic search requires embeddings)
- Thread-safe for concurrent access
- No external dependencies
Limitations:
- Data is lost when the application terminates
- Memory usage grows with stored memories
- Keyword search is less sophisticated than vector search
Value parameters
- config
-
Configuration options
- memories
-
All stored memories indexed by ID
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait MemoryStoreclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
InMemoryStore.type
Embedding service using an LLM provider (OpenAI, VoyageAI, etc.).
Embedding service using an LLM provider (OpenAI, VoyageAI, etc.).
Value parameters
- client
-
The embedding client
- modelConfig
-
The model configuration (dimensions, model name)
Attributes
- Companion
- object
- Supertypes
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
LLMEmbeddingService.type
A single memory entry in the memory system.
A single memory entry in the memory system.
Memories are immutable records that capture information for later retrieval. They include content, metadata for filtering, and timestamps for recency-based retrieval.
Value parameters
- content
-
The actual content/text of the memory
- embedding
-
Optional pre-computed embedding vector for semantic search
- id
-
Unique identifier for this memory
- importance
-
Optional importance score (0.0 to 1.0) for prioritization
- memoryType
-
Classification of the memory
- metadata
-
Key-value pairs for filtering and context
- timestamp
-
When this memory was created
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Filter criteria for memory recall.
Filter criteria for memory recall.
MemoryFilters are composable predicates that can be combined to create complex filtering logic for memory retrieval.
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
object Allclass Andclass ByConversationclass ByEntityclass ByMetadataclass ByTimeRangeclass ByTypeclass ByTypesclass ContentContainsclass Customclass HasMetadataclass MetadataContainsclass MinImportanceobject Noneclass Notclass OrShow all
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MemoryFilter.type
Type-safe wrapper for memory identifiers.
Type-safe wrapper for memory identifiers.
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass AnyValtrait Matchableclass AnyShow all
High-level memory management for agents.
High-level memory management for agents.
MemoryManager provides semantic operations on top of the raw MemoryStore, including entity extraction, memory consolidation, and intelligent retrieval strategies.
This is the primary interface for agent memory integration.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class SimpleMemoryManager
Configuration for memory manager behavior.
Configuration for memory manager behavior.
Value parameters
- autoExtractEntities
-
Whether to automatically extract entities from messages
- autoRecordMessages
-
Whether to automatically record conversation messages
- consolidationEnabled
-
Whether to enable automatic memory consolidation
- contextTokenBudget
-
Default token budget for context retrieval
- defaultImportance
-
Default importance score for unscored memories
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MemoryManagerConfig.type
Statistics about memory usage.
Statistics about memory usage.
Value parameters
- byType
-
Count of memories by type
- conversationCount
-
Number of distinct conversations
- embeddedCount
-
Number of memories with embeddings
- entityCount
-
Number of distinct entities
- newestMemory
-
Timestamp of newest memory
- oldestMemory
-
Timestamp of oldest memory
- totalMemories
-
Total number of memories stored
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MemoryStats.type
Storage backend for agent memories.
Storage backend for agent memories.
MemoryStore provides the interface for persisting and retrieving memories. Implementations can be in-memory (for testing), file-based, database-backed, or use vector databases for semantic search.
The trait follows functional principles - operations return new instances rather than mutating state.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
Configuration for memory store behavior.
Configuration for memory store behavior.
Value parameters
- cleanupThreshold
-
Memory count threshold for triggering cleanup
- defaultEmbeddingDimensions
-
Dimensions for embedding vectors
- enableAutoCleanup
-
Whether to automatically clean old memories
- maxMemories
-
Maximum number of memories to retain (None for unlimited)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MemoryStoreConfig.type
Classification of memory types.
Classification of memory types.
Different memory types enable different retrieval strategies and allow for targeted filtering during recall.
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MemoryType.type
A mock embedding service for testing.
A mock embedding service for testing.
Generates deterministic embeddings based on text content hash. Not suitable for production use.
Attributes
- Companion
- object
- Supertypes
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
MockEmbeddingService.type
SQLite-backed implementation of MemoryStore.
SQLite-backed implementation of MemoryStore.
Provides persistent storage for memories using SQLite database. Supports full-text search via SQLite FTS5 extension.
Thread Safety: This implementation is NOT thread-safe. For concurrent access, use connection pooling or synchronization.
Value parameters
- config
-
Store configuration
- dbPath
-
Path to SQLite database file (use ":memory:" for in-memory)
Attributes
- Companion
- object
- Supertypes
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
SQLiteMemoryStore.type
A memory with an associated relevance score.
A memory with an associated relevance score.
Used in search results to indicate how well a memory matches the search query.
Value parameters
- memory
-
The memory
- score
-
Relevance score (0.0 to 1.0, higher is more relevant)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
ScoredMemory.type
Simple implementation of MemoryManager.
Simple implementation of MemoryManager.
This implementation provides basic memory management without requiring an LLM for entity extraction or consolidation. It's suitable for most use cases where you don't need automatic entity recognition.
For advanced features like LLM-powered entity extraction, use LLMMemoryManager instead.
Value parameters
- config
-
Configuration options
- store
-
The underlying memory store
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait MemoryManagerclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
SimpleMemoryManager.type
Memory store with vector embedding support for semantic search.
Memory store with vector embedding support for semantic search.
This store extends SQLite-based storage with embedding vectors, enabling semantic similarity search in addition to keyword search.
Embeddings are generated on-demand using the provided EmbeddingService and stored alongside memories for efficient retrieval.
Value parameters
- config
-
Store configuration
- dbPath
-
Path to SQLite database file
- embeddingService
-
Service for generating embeddings
Attributes
- Companion
- object
- Supertypes
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
VectorMemoryStore.type
Utilities for working with embedding vectors.
Utilities for working with embedding vectors.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
VectorOps.type
Statistics for a vector memory store.
Statistics for a vector memory store.
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Types
Type alias for memory retrieval results with scores.
Type alias for memory retrieval results with scores.
Attributes
Value members
Concrete fields
Implicit ordering for memories by importance (highest first).
Implicit ordering for memories by importance (highest first).
Attributes
Implicits
Implicits
Implicit ordering for memories by timestamp (most recent first).
Implicit ordering for memories by timestamp (most recent first).