Memory
If you’re building intelligent agents, you need to give them memory which is the ability to remember, reason and personalize responses to users. Memory comes in 3 shapes:
- Chat History: This is the current conversation between users and the agent, stored as sessions in chronological order. This is the most basic form of memory and called “Storage” in Agno.
- User Memories: Insights and facts about users extracted from conversations, helping agents personalize their responses to users. Think of this as adding a “ChatGPT like memory” to your agent. This is called “Memory” in Agno.
- Session Summaries: Condensed representations of conversations, useful when chat histories grow too long. This is called “Summary” in Agno.
Memory helps Agents:
- Manage conversation state (chat history)
- Personalize responses to users (user memories)
- Maintain long-session context (session summaries)
Memory is critical for personal assistants, it allows Agents to “Remember” and “Personalize” their responses to users.
Built-in Memory
Every Agent comes with built-in memory that can be used to access the historical runs per session.
You can give your agent access to chat history in the following ways:
- You can set
add_history_to_messages=True
andnum_history_responses=5
to add the previous 5 messages automatically to every message sent to the agent. - You can set
read_chat_history=True
to provide aget_chat_history()
tool to your agent allowing it to read any message in the entire chat history. - You can set
read_tool_call_history=True
to provide aget_tool_call_history()
tool to your agent allowing it to read tool calls in reverse chronological order.
Built-in memory example
Run the example
Install libraries
Export your key
Run the example
Persistent Memory
The built-in memory only lasts while the session is active. To persist chat history across sessions, we can store agent sessions in a database using AgentStorage
.
Storage is a necessary component when building user facing AI products as any production application will require users to be able to “continue” their conversation with the Agent.
Persistent memory example
Let’s test this out, create a file persistent_memory.py
with the following code:
Run the example
Install libraries
Export your key
Run the example
You can view the agent sessions in the sqlite database and continue any conversation by providing the same session_id
.
Read more in the storage section.
Creating User Memories
Along with storing chat history and run messages, Memory
can be used by the agent to create user memories based on the conversation history.
To enable user memory creation, create an Agent
with Memory
and a memory database, and set enable_user_memories=True
.
Enabling user memory will also add all existing user memories to the agent’s system prompt.
User memory example
Run the example
Install libraries
Export your key
Run the example
User memories are stored in the Memory
object and persisted in the SqliteMemoryDb
to be used across multiple users and multiple sessions.
Creating Session Summaries
To enable session summaries, set enable_session_summaries=True
on the Agent
.
Session summary example
Run the example
Install libraries
Export your key
Run the example
Agentic Memory Management
You can also enable an agent to manage the user memories for you. Enable agentic memory management by setting enable_agentic_memory=True
on the Agent
.
Enabling agentic memory will also add all existing user memories to the agent’s system prompt.
Agentic memory management example
Run the example
Install libraries
Export your key
Run the example
Attributes
Parameter | Type | Default | Description |
---|---|---|---|
memory | Memory | Memory() | Agent’s memory object used for storing and retrieving information. |
add_history_to_messages | bool | False | If true, adds the chat history to the messages sent to the Model. Also known as add_chat_history_to_messages . |
num_history_responses | int | 3 | Number of historical responses to add to the messages. |
enable_user_memories | bool | False | If true, create and store personalized memories for the user. |
enable_session_summaries | bool | False | If true, create and store session summaries. |
enable_agentic_memory | bool | False | If true, enables the agent to manage the user’s memory. |