Storing, Retrieving, Reflecting: Essential Memory Management for LLM Agents with Memori
As a software engineer, you can see Memori as a crucial component for building more sophisticated, stateful, and context-aware AI applications.
Memori is an Open-Source Memory Engine for LLMs, AI Agents, and Multi-Agent Systems. In essence, it provides a dedicated system for an agent to store, retrieve, and reflect on past experiences, turning a stateless LLM into a stateful, learning entity.
From a software engineering perspective, Memori solves several critical problems when developing agent-based applications
The Problem
Standard LLMs have a fixed context window (the maximum amount of text they can process at one time). Past conversation or interaction history quickly gets truncated.
Memori's Solution
It stores the vast majority of the history outside the context window. When an agent needs information, Memori uses sophisticated retrieval methods (like vector search) to pull only the most relevant memories and insert them into the LLM's prompt. This allows your agent to remember things from days or weeks ago.
The Problem
Without a memory system, an agent starts every new interaction fresh, leading to repetitive or inconsistent behavior.
Memori's Solution
It gives the agent a long-term state. The agent can remember a user's name, preferences, or the progress of a complex, multi-step task (e.g., "I am currently researching a vacation to Hawaii").
The Problem
Pure LLMs often lack the ability to learn or reflect on their past actions to improve future behavior.
Memori's Solution
It provides mechanisms for summary and reflection. Periodically, the agent can use Memori to review many small memories and condense them into higher-level, more abstract insights (e.g., "User A prefers short, direct answers"). This makes the agent's behavior evolve over time.
The Problem
Building systems where multiple agents work together requires a shared, consistent understanding of the task and history.
Memori's Solution
It can serve as a Shared Memory Bus where different agents can contribute and access the same pool of knowledge, ensuring they are always working with the latest context and avoiding duplication of effort.
The integration process is typically straightforward, following standard Python package management.
You can install Memori using pip
pip install MemoriLabs-Memori
You first need to initialize the core memory structure, often linked to a specific storage backend (like a vector database or even a simple file for development).
from memori.memori import Memori
from memori.config import MemoryConfig
# 1. Define configuration (optional, uses defaults if omitted)
config = MemoryConfig(
embedding_model="openai", # Specifies the model for creating memory embeddings
# other configurations for retrieval, summarization, etc.
)
# 2. Initialize the Memory Engine
# 'my_agent_id' is a unique identifier for your agent or user
memori_engine = Memori(agent_id="my_agent_id", config=config)
print("Memori engine initialized and ready!")
Here is a simplified Python example showing the core loop of an agent using Memori
Storing a conversation and then retrieving a relevant fact later.
# Assuming the Memori object 'memori_engine' is already initialized
## Section 1: Store Initial Memories (Agent Interaction)
# A user tells the agent a personal fact
interaction_1 = "User: I am planning a hiking trip to Patagonia next year."
memori_engine.store_memory(
content=interaction_1,
source="User_Chat",
type="fact"
)
# The agent performs an action (e.g., searches for info)
interaction_2 = "Agent: I looked up the best months for hiking in Patagonia: December to February."
memori_engine.store_memory(
content=interaction_2,
source="Agent_Action",
type="action"
)
print("--- Memories Stored ---")
## Section 2: Retrieve Relevant Memories for a New Query
# A new query comes in, days later
new_query = "What was the name of the place I was thinking of traveling to?"
# The agent uses Memori to retrieve relevant past memories
# The 'query' is embedded, and the system finds the closest memory vectors
retrieved_memories = memori_engine.retrieve_memories(
query=new_query,
limit=3 # Retrieve the top 3 most relevant memories
)
print(f"\n--- Retrieved Memories for Query: '{new_query}' ---")
for i, memory in enumerate(retrieved_memories):
print(f"[{i+1}] Relevance: {memory.relevance_score:.2f}")
print(f" Content: '{memory.content}'")
# The agent would then combine the 'new_query' and the 'retrieved_memories'
# into a single, comprehensive prompt for the LLM to generate the final answer:
# "You were planning a hiking trip to Patagonia."
The output would show the memory about Patagonia being retrieved because its embedding vector is semantically close to the vector of the query "traveling to." This process demonstrates how the agent can recall specific context without having to scan the entire, potentially massive, memory archive.