Memory
Agno provides 3 types of memory for Agents:
- Chat History: The message history of the session. Agno will store the sessions in a database for you, and retrieve them when you resume a session.
- User Memories: Notes and insights about the user, this helps the model personalize the response to the user.
- Summaries: A summary of the conversation, which is added to the prompt when chat history gets too long.
Before we dive in, let’s understand the terminology:
- Session: Each conversation with an Agent is called a session. Sessions are identified by a
session_id
. - Run: Every interaction (i.e. chat) within a session is called a run. Runs are identified by a
run_id
. - Messages: are the individual messages sent to and received from the model. They have a
role
(system
,user
orassistant
) andcontent
.
Sessions are equivalent to threads in the OpenAI Assistant API.
Built-in Memory
Every Agent comes with built-in memory that can be used to access the historical runs and messages. Access it using agent.memory
You can give your agent access to memory 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.
Example
Persistent Memory
The built-in memory only lasts while the session is active. To persist memory 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.
Let’s test this out, create a file persistent_memory.py
with the following code:
Run the agent
Install dependencies and run the agent:
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.
User preferences and conversation summaries
Along with storing chat history and run messages, AgentMemory
can be extended to automatically classify and store user preferences and conversation summaries.
To do this, add a db
to AgentMemory
and set create_user_memories=True
and create_session_summary=True
User memories are stored in the AgentMemory
whereas session summaries are stored in the AgentStorage
table with the rest of the session information.
User preferences and conversation summaries are currently only compatible with
OpenAI
and OpenAILike
models. While Persistent Memory is compatible with
all model providers.
Example
Attributes
Parameter | Type | Default | Description |
---|---|---|---|
memory | AgentMemory | AgentMemory() | 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. |
create_user_memories | bool | False | If true, create and store personalized memories for the user. |
update_user_memories_after_run | bool | True | If true, update memories for the user after each run. |
create_session_summary | bool | False | If true, create and store session summaries. |
update_session_summary_after_run | bool | True | If true, update session summaries after each run. |
Developer Resources
- View Cookbook