Memory
Memory gives an Agent the ability to recall relavant information. Memory is a part of the Agent’s context that helps it provide the best, most personalized response.
If the user tells the Agent they like to ski, then future responses can reference this information to provide a more personalized experience.
In Agno, Memory covers chat history, user preferences and any supplemental information about the task at hand. Agno supports 3 types of memory out of the box:
-
Session Storage (chat history and session state): Session storage saves an Agent’s sessions in a database and enables Agents to have multi-turn conversations. Session storage also holds the session state, which is persisted across runs because it is saved to the database after each run. Session storage is a form of short-term memory called “Storage” in Agno.
-
User Memories (user preferences): The Agent can store insights and facts about the user that it learns through conversation. This helps the agents personalize its response to the user it is interacting with. Think of this as adding “ChatGPT like memory” to your agent. This is called “Memory” in Agno.
-
Session Summaries (chat summary): The Agent can store a condensed representations of the session, useful when chat histories gets too long. This is called “Summary” in Agno.
It is relatively easy to use your own memory implementation using Agent.context
.
To become an expert in Agentic Memory, you need ot learn about:
Show me the code: Memory & Storage in Action
Here’s a simple but complete example of using Memory and Storage in an Agent.
Notes
enable_agentic_memory=True
gives the Agent a tool to manage memories of the user, this tool passes the task to theMemoryManager
class. You may also setenable_user_memories=True
which always runs theMemoryManager
after each user message.add_history_to_messages=True
adds the chat history to the messages sent to the Model, thenum_history_runs
determines how many runs to add.read_chat_history=True
adds a tool to the Agent that allows it to read chat history, as it may be larger than what’s included in thenum_history_runs
.
Default Memory
Every Agent comes with built-in memory which keeps track of the messages in the session i.e. the chat history.
You can access these messages using agent.get_messages_for_session()
.
We can give the Agent access to the chat history in the following ways:
- We can set
add_history_to_messages=True
andnum_history_runs=5
to add the messages from the last 5 runs automatically to every message sent to the agent. - We 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. - We recommend setting all 3:
add_history_to_messages=True
,num_history_runs=3
andread_chat_history=True
for the best experience. - We can also 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.
The default memory is not persisted across execution cycles. So after the script finishes running, or the request is over, the built-in default memory is lost.
You can persist this memory in a database by adding a storage
driver to the Agent.
Built-in memory example
Run the example
Install libraries
Export your key
Run the example
Session Storage
The built-in memory is only available during the current execution cycle. Once the script ends, or the request is over, the built-in memory is lost.
Storage help us save Agent sessions and state to a database or file.
Adding storage to an Agent is as simple as providing a storage
driver and Agno handles the rest. You can use Sqlite, Postgres, Mongo or any other database you want.
Here’s a simple example that demostrates persistence across execution cycles:
The first time you run this, the answer to “What was my last question?” will not be available. But run it again and the Agent will able to answer properly. Because we have fixed the session id, the Agent will continue from the same session every time you run the script.
Read more in the storage section.
User Memories
Along with storing session history and state, Agents can also create user memories based on the conversation history.
To enable user memories, give your Agent a Memory
object and set enable_agentic_memory=True
.
Enabling agentic 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.
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
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. |