Resource Orchestration
In a high-concurrency server environment, creating a new memory instance for every single API request is expensive. The Manager solves this by maintaining an active cache of instances.The Lifecycle Flow
- Request: An API call arrives with a
session_key(derived fromorg_idandproject_id). - Lookup: The Manager checks the
instance_cacheto see if a memory object for that session already exists. - Instantiation: If not found, it uses
EpisodicMemoryParamsto build a new instance. - Reference Increment: The manager increments the “Active User” count for that instance.
- Cleanup: When the request finishes, the count decrements. If the count hits zero and the
max_life_timeexpires, the instance is closed and purged from memory.
Class EpisodicMemoryManagerParams
These parameters define the “Rules of Engagement” for the server’s memory usage.
| Attribute | Type | Description |
|---|---|---|
instance_cache_size | int | The maximum number of unique sessions to keep in RAM simultaneously. |
max_life_time | int | How long (in seconds) an idle session stays in the cache before being evicted. |
resource_manager | ResourceManager | Provides shared access to the LLM and Embedding providers. |
session_data_manager | SessionDataManager | Handles the low-level database persistence for session metadata. |
Class EpisodicMemoryManager
The manager class is responsible for the thread-safe creation and retrieval of memory pillars.
Key Responsibilities
1. Instance Retrieval (get_instance)
The manager ensures that two concurrent requests for the same project_id share the same memory instance, preventing data race conditions and redundant database connections.
2. Cross-Namespace Support
While primarily focused on Episodic data, the manager also facilitates the retrieval of Semantic Memory instances when a hybrid search is required, ensuring that the Knowledge Graph and the Chat History are synchronized.3. Graceful Shutdown
When the server receives a shutdown signal, the Manager is responsible for:- Flushing any pending “summarization” loops to the database.
- Closing all active vector database connections.
- Clearing the instance cache.
Implementation Example (Internal)
Developer Note: Because the Manager uses reference counting, it is vital to use the memory instance within an async with block or manually call close() to prevent memory leaks in the server process.

