File size: 6,448 Bytes
e706de2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# Concept: Persistent Memory & State Management
## Overview
Adding persistent memory transforms agents from stateless responders into systems that can maintain context and relationships across sessions.
## The Memory Problem
```
Without Memory With Memory
ββββββββββββββ βββββββββββββ
Session 1: Session 1:
"I'm Alex" "I'm Alex" β Saved
"I love pizza" "I love pizza" β Saved
Session 2: Session 2:
"What's my name?" "What's my name?"
"I don't know" "Alex!" β
```
## Architecture
```
βββββββββββββββββββββββββββββββββββ
β Agent Session β
βββββββββββββββββββββββββββββββββββ€
β System Prompt β
β + Loaded Memories β
β + saveMemory Tool β
ββββββββββ¬βββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββ
β Memory Manager β
βββββββββββββββββββββββββββββββββββ€
β β’ Load from storage β
β β’ Save to storage β
β β’ Format for prompt β
ββββββββββ¬βββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββ
β Persistent Storage β
β (agent-memory.json) β
βββββββββββββββββββββββββββββββββββ
```
## How It Works
### 1. Startup
```
1. Load agent-memory.json
2. Extract facts and preferences
3. Add to system prompt
4. Agent "remembers" past information
```
### 2. During Conversation
```
User shares information
β
Agent recognizes important fact
β
Agent calls saveMemory()
β
Saved to JSON file
β
Available in future sessions
```
### 3. Memory Types
**Facts**: General information
```json
{
"memories": [
{
"type": "fact",
"key": "user_name",
"value": "Alex",
"source": "user",
"timestamp": "2025-10-29T11:22:57.372Z"
}
]
}
```
**Preferences**:
```json
{
"memories": [
{
"type": "preference",
"key": "favorite_food",
"value": "pizza",
"source": "user",
"timestamp": "2025-10-29T11:22:58.022Z"
}
]
}
```
## Memory Integration Pattern
### System Prompt Enhancement
```
Base Prompt:
"You are a helpful assistant."
Enhanced with Memory:
"You are a helpful assistant with long-term memory.
=== LONG-TERM MEMORY ===
Known Facts:
- User's name is Alex
- User loves pizza"
```
### Tool-Assisted Saving
```
Agent decides when to save:
User: "My favorite color is blue"
β
Agent: "I should remember this"
β
Calls: saveMemory(type="preference", key="color", content="blue")
```
## Real-World Applications
**Personal Assistant**
- Remember appointments, preferences, contacts
- Personalized responses based on history
**Customer Service**
- Past interactions and issues
- Customer preferences and context
**Learning Tutor**
- Student progress and weak areas
- Adapted teaching based on history
**Healthcare Assistant**
- Medical history
- Medication reminders
- Health tracking
## Memory Strategies
### 1. Episodic Memory
Store specific events and conversations:
```
- "On 2025-01-15, user asked about Python"
- "User struggled with async concepts"
```
### 2. Semantic Memory
Store facts and knowledge:
```
- "User is a software engineer"
- "User prefers TypeScript over JavaScript"
```
### 3. Procedural Memory
Store how-to information:
```
- "User's workflow: design β code β test"
- "User's preferred tools: VS Code, Git"
```
## Challenges & Solutions
### Challenge 1: Memory Bloat
**Problem**: Too many memories slow down agent
**Solution**:
- Importance scoring
- Periodic cleanup
- Summary compression
### Challenge 2: Conflicting Information
**Problem**: "User likes pizza" vs "User is vegan"
**Solution**:
- Timestamps for recency
- Explicit updates
- Conflict resolution logic
### Challenge 3: Privacy
**Problem**: Sensitive information in memory
**Solution**:
- Encryption at rest
- Access controls
- Expiration policies
## Key Concepts
### 1. Persistence
Memory survives:
- Application restarts
- System reboots
- Time gaps
### 2. Context Augmentation
Memories enhance system prompt:
```
Prompt = Base + Memories + User Input
```
### 3. Agent-Driven Storage
Agent decides what to remember:
```
Important? β Save
Trivial? β Ignore
```
## Evolution Path
```
1. Stateless β Each interaction independent
2. Session memory β Remember during conversation
3. Persistent memory β Remember across sessions
4. Distributed memory β Share across instances
5. Semantic search β Find relevant memories
```
## Best Practices
1. **Structure memory**: Use types (facts, preferences, events)
2. **Add timestamps**: Know when information was saved
3. **Enable updates**: Allow overwriting old information
4. **Implement search**: Find relevant memories efficiently
5. **Monitor size**: Prevent unbounded growth
## Comparison
```
Feature Simple Agent Memory Agent
βββββββββββββββββββ βββββββββββββ ββββββββββββββ
Remembers names β β
Recalls preferences β β
Personalization β β
Context continuity β β
Cross-session state β β
```
## Key Takeaway
Memory transforms agents from tools into assistants. They can build relationships, provide personalized experiences, and maintain context over time.
This is essential for production AI agent systems.
|