Skip to content

Commit 1c96065

Browse files
committed
update docs
1 parent 14b7601 commit 1c96065

File tree

5 files changed

+357
-351
lines changed

5 files changed

+357
-351
lines changed

agents/memory.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
title: Memory
33
---
44

5-
Memory is an Agent's ability to recall relavant information to 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.
5+
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.
6+
7+
<Check>
8+
If the user tells the Agent they like to ski, then future responses can reference this information to provide a more personalized experience.
9+
</Check>
610

711
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:**
812

9-
1. **Session Storage:** 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**.
13+
1. **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**.
1014

11-
2. **User Memories:** 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**.
15+
2. **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**.
1216

13-
3. **Session Summaries:** The Agent can store a condensed representations of the session, useful when chat histories gets too long. **This is called "Summary" in Agno**.
17+
3. **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**.
1418

1519
<Note>
16-
It is also relatively easy to use your own memory implementation using `Agent.context`.
20+
It is relatively easy to use your own memory implementation using `Agent.context`.
1721
</Note>
1822

1923
To become an expert in Agentic Memory, you need ot learn about:
@@ -22,9 +26,7 @@ To become an expert in Agentic Memory, you need ot learn about:
2226
3. [User Memories](/agents/memory#user-memories)
2327
4. [Session Summaries](/agents/memory#session-summaries)
2428

25-
> __You can also read about Memory in the [Memory](/memory) section.__
26-
27-
## Example: Memory & Storage in Action
29+
## Show me the code: Memory & Storage in Action
2830

2931
Here's a simple but complete example of using Memory and Storage in an Agent.
3032

memory/introduction.mdx

Lines changed: 8 additions & 340 deletions
Original file line numberDiff line numberDiff line change
@@ -4,350 +4,18 @@ title: Introduction
44

55
# Memory for Agents
66

7-
Memory is an Agent's ability to recall relavant information to 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.
7+
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.
88

9-
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:**
9+
<Check>
10+
If the user tells the Agent they like to ski, then future responses can reference this information to provide a more personalized experience.
11+
</Check>
1012

11-
1. **Session Storage:** 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**.
13+
1. **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**.
1214

13-
2. **User Memories:** 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**.
15+
2. **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**.
1416

15-
3. **Session Summaries:** The Agent can store a condensed representations of the session, useful when chat histories gets too long. **This is called "Summary" in Agno**.
17+
3. **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**.
1618

1719
<Note>
18-
If you haven't, we recommend reading the Memory section of the [Agents](/agents/memory) to get familiar with the basics.
20+
If you haven't, we also recommend reading the Memory section of the [Agents](/agents/memory) to get familiar with the basics.
1921
</Note>
20-
21-
## Managing User Memory
22-
23-
When we speak about Memory, the commonly agreed upon understanding of Memory is the ability to store insights and facts about the user the Agent is interacting with. In short, build a persona of the user, learn about their preferences and use that to personalize the Agent's response.
24-
25-
### Agentic Memory
26-
27-
Agno Agents natively support Agentic Memory Management and recommends it as the starting point for your memory journey.
28-
29-
With Agentic Memory, The Agent itself creates, updates and deletes memories from user conversations.
30-
31-
Set `enable_agentic_memory=True` to give the Agent a tool to manage memories of the user, this tool passes the task to the `MemoryManager` class.
32-
33-
> You may also set `enable_user_memories=True` which always runs the `MemoryManager` after each user message. [See below for an example.](#create-memories-after-each-run)
34-
35-
```python agentic_memory.py
36-
from agno.agent import Agent
37-
from agno.memory.v2.db.sqlite import SqliteMemoryDb
38-
from agno.memory.v2.memory import Memory
39-
from agno.models.openai import OpenAIChat
40-
from agno.storage.sqlite import SqliteStorage
41-
from rich.pretty import pprint
42-
43-
user_id = "ava"
44-
db_file = "tmp/memory.db"
45-
memory = Memory(
46-
db=SqliteMemoryDb(table_name="user_memories", db_file=db_file),
47-
delete_memories=True,
48-
clear_memories=True,
49-
)
50-
memory.clear()
51-
storage = SqliteStorage(table_name="agent_sessions", db_file=db_file)
52-
53-
memory_agent = Agent(
54-
model=OpenAIChat(id="gpt-4.1"),
55-
memory=memory,
56-
enable_agentic_memory=True,
57-
storage=storage,
58-
add_history_to_messages=True,
59-
num_history_runs=3,
60-
read_chat_history=True,
61-
)
62-
63-
memory_agent.print_response(
64-
"My name is Ava and I like to ski.",
65-
user_id=user_id,
66-
stream=True,
67-
stream_intermediate_steps=True,
68-
)
69-
print("Memories about Ava:")
70-
pprint(memory.get_user_memories(user_id=user_id))
71-
72-
memory_agent.print_response(
73-
"I live in san francisco",
74-
user_id=user_id,
75-
stream=True,
76-
stream_intermediate_steps=True,
77-
)
78-
print("Memories about Ava:")
79-
pprint(memory.get_user_memories(user_id=user_id))
80-
```
81-
82-
- `add_history_to_messages=True` adds the chat history to the messages sent to the Model, the `num_history_runs` determines how many runs to add.
83-
- `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 the `num_history_runs`.
84-
85-
### Create Memories after each run
86-
87-
While `enable_agentic_memory=True` gives the Agent a tool to manage memories of the user, we can also always "trigger" the `MemoryManagement` after each user message.
88-
89-
Set `enable_user_memories=True` which always process memories after each user message.
90-
91-
```python create_memories_after_each_run.py
92-
from agno.agent.agent import Agent
93-
from agno.memory.v2.db.sqlite import SqliteMemoryDb
94-
from agno.memory.v2.memory import Memory
95-
from agno.models.openai import OpenAIChat
96-
from rich.pretty import pprint
97-
98-
memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
99-
# No need to set the model, it gets set to the model of the agent
100-
memory = Memory(db=memory_db, delete_memories=True, clear_memories=True)
101-
102-
# Reset the memory for this example
103-
memory.clear()
104-
105-
# User ID for the memory
106-
john_doe_id = "[email protected]"
107-
agent = Agent(
108-
model=OpenAIChat(id="gpt-4o-mini"),
109-
memory=memory,
110-
enable_user_memories=True,
111-
)
112-
113-
# Send a message to the agent that would require the memory to be used
114-
agent.print_response(
115-
"My name is John Doe and I like to hike in the mountains on weekends.",
116-
stream=True,
117-
user_id=john_doe_id,
118-
)
119-
120-
# Send a message to the agent that checks the memory is working
121-
agent.print_response("What are my hobbies?", stream=True, user_id=john_doe_id)
122-
123-
# Print the memories for the user
124-
memories = memory.get_user_memories(user_id=john_doe_id)
125-
print("Memories about John Doe:")
126-
pprint(memories)
127-
128-
# Send a message to the agent that removes all memories for the user
129-
agent.print_response(
130-
"Remove all existing memories of me.",
131-
stream=True,
132-
user_id=john_doe_id,
133-
)
134-
memories = memory.get_user_memories(user_id=john_doe_id)
135-
print("Memories about John Doe:")
136-
pprint(memories)
137-
```
138-
139-
## Memory Architecture
140-
141-
The `Memory` class in Agno lets you manage all aspects of user memory. Let's start with some examples of using `Memory` outside of Agents. We will:
142-
143-
- Add, update and delete memories
144-
- Store memories in a database
145-
- Create memories from conversations
146-
- Search over memories
147-
148-
```python
149-
from agno.memory.v2.memory import Memory
150-
from agno.memory.v2.db.sqlite import SqliteMemoryDb
151-
152-
# Create a memory instance with persistent storage
153-
memory_db = SqliteMemoryDb(table_name="memory", db_file="memory.db")
154-
memory = Memory(db=memory_db)
155-
```
156-
157-
### Adding a new memory
158-
159-
```python
160-
from agno.memory.v2.memory import Memory
161-
from agno.memory.v2.schema import UserMemory
162-
163-
memory = Memory()
164-
165-
# Create a user memory manually
166-
memory_id = memory.add_user_memory(
167-
memory=UserMemory(
168-
memory="The user's name is Jane Doe",
169-
topics=["personal", "name"]
170-
),
171-
172-
)
173-
```
174-
175-
### Updating a memory
176-
177-
```python
178-
from agno.memory.v2.memory import Memory
179-
from agno.memory.v2.schema import UserMemory
180-
181-
memory = Memory()
182-
183-
# Replace a user memory
184-
memory_id = memory.replace_user_memory(
185-
# The id of the memory to replace
186-
memory_id=previous_memory_id,
187-
# The new memory to replace it with
188-
memory=UserMemory(
189-
memory="The user's name is Verna Doe",
190-
topics=["personal", "name"]
191-
),
192-
193-
)
194-
```
195-
196-
### Deleting a memory
197-
198-
```python
199-
from agno.memory.v2.memory import Memory
200-
201-
memory = Memory()
202-
203-
# Delete a user memory
204-
memory.delete_user_memory(user_id="[email protected]", memory_id=memory_id)
205-
```
206-
207-
### Creating memories from user information
208-
209-
```python
210-
from agno.memory.v2 import Memory
211-
from agno.memory.v2.db.sqlite import SqliteMemoryDb
212-
from agno.models.google import Gemini
213-
214-
memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
215-
memory = Memory(model=Gemini(id="gemini-2.0-flash-exp"), db=memory_db)
216-
217-
john_doe_id = "[email protected]"
218-
219-
memory.create_user_memories(
220-
message="""
221-
I enjoy hiking in the mountains on weekends,
222-
reading science fiction novels before bed,
223-
cooking new recipes from different cultures,
224-
playing chess with friends,
225-
and attending live music concerts whenever possible.
226-
Photography has become a recent passion of mine, especially capturing landscapes and street scenes.
227-
I also like to meditate in the mornings and practice yoga to stay centered.
228-
""",
229-
user_id=john_doe_id,
230-
)
231-
232-
233-
memories = memory.get_user_memories(user_id=john_doe_id)
234-
print("John Doe's memories:")
235-
for i, m in enumerate(memories):
236-
print(f"{i}: {m.memory} - {m.topics}")
237-
```
238-
239-
### Creating memories from a conversation
240-
241-
```python
242-
from agno.memory.v2 import Memory
243-
from agno.memory.v2.db.sqlite import SqliteMemoryDb
244-
from agno.models.google import Gemini
245-
from agno.models.message import Message
246-
247-
memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
248-
memory = Memory(model=Gemini(id="gemini-2.0-flash-exp"), db=memory_db)
249-
250-
251-
jane_doe_id = "[email protected]"
252-
# Send a history of messages and add memories
253-
memory.create_user_memories(
254-
messages=[
255-
Message(role="user", content="My name is Jane Doe"),
256-
Message(role="assistant", content="That is great!"),
257-
Message(role="user", content="I like to play chess"),
258-
Message(role="assistant", content="That is great!"),
259-
],
260-
user_id=jane_doe_id,
261-
)
262-
263-
memories = memory.get_user_memories(user_id=jane_doe_id)
264-
print("Jane Doe's memories:")
265-
for i, m in enumerate(memories):
266-
print(f"{i}: {m.memory} - {m.topics}")
267-
```
268-
269-
## Memory Search
270-
271-
Agno provides several retrieval methods to search and retrieve user memories:
272-
273-
### Basic Retrieval Methods
274-
275-
You can retrieve memories using chronological methods such as `last_n` (most recent) or `first_n` (oldest first):
276-
277-
```python
278-
from agno.memory.v2 import Memory, UserMemory
279-
280-
memory = Memory()
281-
282-
john_doe_id = "[email protected]"
283-
284-
memory.add_user_memory(
285-
memory=UserMemory(memory="The user enjoys hiking in the mountains on weekends"),
286-
user_id=john_doe_id,
287-
)
288-
memory.add_user_memory(
289-
memory=UserMemory(
290-
memory="The user enjoys reading science fiction novels before bed"
291-
),
292-
user_id=john_doe_id,
293-
)
294-
295-
# Get the most recent memory
296-
memories = memory.search_user_memories(
297-
user_id=john_doe_id, limit=1, retrieval_method="last_n"
298-
)
299-
print("John Doe's last_n memories:")
300-
for i, m in enumerate(memories):
301-
print(f"{i}: {m.memory}")
302-
303-
# Get the oldest memory
304-
memories = memory.search_user_memories(
305-
user_id=john_doe_id, limit=1, retrieval_method="first_n"
306-
)
307-
print("John Doe's first_n memories:")
308-
for i, m in enumerate(memories):
309-
print(f"{i}: {m.memory}")
310-
```
311-
312-
### Agentic Memory Search
313-
314-
Agentic search allows you to find memories based on meaning rather than exact keyword matches. This is particularly useful for retrieving contextually relevant information:
315-
316-
```python
317-
from agno.memory.v2.memory import Memory, UserMemory
318-
from agno.models.google.gemini import Gemini
319-
320-
# Initialize memory with a model for agentic search
321-
memory = Memory(model=Gemini(id="gemini-2.0-flash-exp"))
322-
323-
john_doe_id = "[email protected]"
324-
325-
memory.add_user_memory(
326-
memory=UserMemory(memory="The user enjoys hiking in the mountains on weekends"),
327-
user_id=john_doe_id,
328-
)
329-
memory.add_user_memory(
330-
memory=UserMemory(
331-
memory="The user enjoys reading science fiction novels before bed"
332-
),
333-
user_id=john_doe_id,
334-
)
335-
336-
# Search for memories related to the query
337-
memories = memory.search_user_memories(
338-
user_id=john_doe_id,
339-
query="What does the user like to do on weekends?",
340-
retrieval_method="agentic",
341-
)
342-
print("John Doe's found memories:")
343-
for i, m in enumerate(memories):
344-
print(f"{i}: {m.memory}")
345-
```
346-
347-
With agentic search, the model understands the intent behind your query and returns the most relevant memories, even if they don't contain the exact keywords from your search.
348-
349-
350-
## Developer Resources
351-
352-
- Find full examples in the [Cookbook](https://github.com/agno-agi/agno/tree/main/cookbook/agent_concepts/memory)
353-
- View the class reference for the `Memory` class [here](/reference/memory/memory)

0 commit comments

Comments
 (0)