EpisodicMemory class acts as the primary orchestrator for this session. It cleverly integrates short-term memory (recent chat history) and long-term memory (declarative facts and older information) to give you a unified view.
Key Responsibilities:
- Unified Storage: Adds new conversational Episode objects to both short-term (Session) and long-term (Declarative) memory simultaneously.
- Smart Retrieval: Finds the most relevant context for any query by searching both memory types and handling duplicates.
- Context Generation: Formats retrieved memories into a structured (XML-like) query for seamless integration with your Language Models.
- Lifecycle Management: Uses reference counting to ensure the memory instance is only active when needed, saving resources.
The EpisodicMemory Class
MemoryContext (defined by group, agent, user, and session IDs).
init
EpisodicMemory instance. This method handles all the setup, including configuring the internal short-term and long-term memory components based on your provided configuration dictionary.
Arguments:
manager: TheEpisodicMemoryManagerthat created this instance.config: A dictionary containing all necessary configurations (model, prompts, and memory settings) for this memory instance.memory_context: The unique context that defines this memory session (who is talking to whom).
reference
True if the reference was successfully added, or False if the instance is already marked for closure.
add_memory_episode
producer: The ID of the user or agent that created the episode (e.g., sent a message).produced_for: The ID of the intended recipient.episode_content: The content of the episode, which can be a raw text string or a pre-computed embedding vector (list[float]).episode_type: The category of the episode (e.g.,'message','thought','summary').content_type: Specifies the format of theepisode_content(e.g.,ContentType.STRINGorContentType.VECTOR).timestamp: Optional. The timestamp of the episode. Defaults to the current time if not provided.metadata: Optional. A dictionary for any custom, user-defined metadata you want to attach to the episode.
True if the episode was added successfully. (Will raise a ValueError if the producer or produced_for IDs are not part of the defined MemoryContext).
query_memory
query: The question or query string used to search for context.limit: Optional. The maximum number of episodes to return from each memory type. Defaults to 20.property_filter: Optional. A dictionary of properties (key-value pairs) to filter the search in declarative memory.
- A list of
Episodeobjects found in short-term memory. - A list of
Episodeobjects found in long-term memory (already deduplicated). - A list of summary strings generated from the short-term context.
formalize_query_with_context
query_memory to get the context, then formats the summary and episodes into a structured string (using <Summary>, <Episodes>, and <Query> tags) that is easy for the LLM to parse and reason over.
Arguments:
query: The original query string.limit: The maximum number of episodes to include in the context.property_filter: Filters for the search.
delete_data
MemoryContext.
close
Async Context Manager
EpisodicMemory lifecycle using Python’s asynchronous context manager (async with). This is the safest and cleanest way to ensure you call reference() and close() correctly.

