Memory
What is Memory?
Memory enables your agent to recall past interactions and use them to inform future responses. In Galadriel, memory is implemented through the MemoryStore
class, which provides both short-term and long-term memory capabilities.
- Short-term memory: Stores recent conversations in a simple list structure (enabled by default).
- Long-term memory: Uses vector embeddings to store and retrieve relevant past interactions based on semantic similarity (optional).
How Memory Works
When a user interacts with an agent:
- The conversation is stored in short-term memory.
- If short-term memory exceeds its limit (default: 20 interactions), the oldest memory is moved to long-term storage (if enabled).
- When responding to a new query, the agent can access:
- All recent conversations from short-term memory
- Semantically relevant past interactions from long-term memory
This approach ensures your agent maintains context in ongoing conversations while also being able to recall relevant information from past interactions.
Adding Memory to Your Agent
Prerequisites
Make sure you’ve gone through the quick start guide and have the development environment set up.
Basic Usage (Short-Term Memory Only)
Short-term memory is enabled by default when using the AgentRuntime
:
Enabling Long-Term Memory
To enable long-term memory, explicitly create a MemoryStore
with OpenAI API credentials and an embedding model:
How Memory Appears in Prompts
When your agent receives a query, memory is automatically integrated into its prompt. Here’s an example:
If long-term memory is enabled, semantically relevant past interactions will also be included under a “long term memories that might be relevant” section.
Advanced Memory Features
Saving and Loading Memory
For persistent agents, you can save memory to disk and load it later:
Customizing Memory Retrieval (Long-Term Memory Only)
When retrieving memories for a specific query, you can customize:
- The number of relevant memories to retrieve (
top_k
) - Filter criteria for memory retrieval
Memory Configuration Options
The MemoryStore
class accepts several parameters:
Parameter | Description | Default |
---|---|---|
short_term_memory_limit | Maximum number of interactions in short-term memory | 20 |
api_key | OpenAI API key for embeddings (required for long-term memory) | None |
embedding_model | OpenAI embedding model to use | None |
agent_name | Identifier for the agent using this memory store | ”agent” |
Adjusting Short-Term Memory Capacity
The short_term_memory_limit
parameter controls how many recent interactions your agent remembers before moving older memories to long-term storage. You can adjust this based on your use case:
Increasing this limit allows your agent to maintain more context in active memory, which can be beneficial for complex, multi-turn conversations. However, it may also increase token usage in your LLM prompts.
When short-term memory reaches its limit, the oldest memory is automatically moved to long-term storage (if long-term memory is enabled). If long-term memory is not enabled, the oldest memory is simply discarded.
Conclusion
Memory is a crucial component for building agents that maintain context and learn from past interactions. Galadriel’s memory system provides a flexible approach that balances recent context with long-term recall, enabling more natural and helpful agent responses.