How to Use Memory in AI Agent: Simple Guide
To use
memory in an AI agent, you store important information from past interactions so the agent can remember context and make better decisions. This is done by saving data in a memory module that the agent accesses during conversations or tasks.Syntax
An AI agent uses memory by creating a memory object that stores information. The agent updates this memory as it interacts and retrieves it to keep context.
Key parts:
Memory(): Initializes memory storage.agent = Agent(memory=memory): Connects memory to the agent.memory.load(): Retrieves stored data.memory.save(): Updates stored data.
python
class Memory: def __init__(self): self.data = {} def load(self, key): return self.data.get(key, None) def save(self, key, value): self.data[key] = value class Agent: def __init__(self, memory): self.memory = memory def remember(self, key, value): self.memory.save(key, value) def recall(self, key): return self.memory.load(key) memory = Memory() agent = Agent(memory=memory)
Example
This example shows an AI agent remembering a user's name and greeting them using stored memory.
python
class Memory: def __init__(self): self.data = {} def load(self, key): return self.data.get(key, None) def save(self, key, value): self.data[key] = value class Agent: def __init__(self, memory): self.memory = memory def remember(self, key, value): self.memory.save(key, value) def recall(self, key): return self.memory.load(key) def greet_user(self): name = self.recall('user_name') if name: return f"Hello again, {name}!" else: return "Hello! What's your name?" memory = Memory() agent = Agent(memory=memory) # First interaction print(agent.greet_user()) # User tells name agent.remember('user_name', 'Alice') # Next interaction print(agent.greet_user())
Output
Hello! What's your name?
Hello again, Alice!
Common Pitfalls
Common mistakes when using memory in AI agents include:
- Not updating memory after new information, causing stale context.
- Storing too much data without cleanup, leading to slow performance.
- Forgetting to retrieve memory before responding, losing context.
- Using memory that is not persistent across sessions if needed.
Always ensure memory is updated and accessed properly to keep conversations relevant.
python
class Memory: def __init__(self): self.data = {} def load(self, key): # Wrong: forgetting to check if key exists return self.data[key] # May cause error if key missing def save(self, key, value): self.data[key] = value # Correct way: # Use get() to avoid errors if key is missing class MemoryFixed: def __init__(self): self.data = {} def load(self, key): return self.data.get(key, None) def save(self, key, value): self.data[key] = value
Quick Reference
- Initialize memory: Create a memory object to store data.
- Connect memory to agent: Pass memory to the agent on creation.
- Save info: Use
memory.save(key, value)to store data. - Load info: Use
memory.load(key)to retrieve data. - Update often: Keep memory current to maintain context.
- Handle missing data: Always check if data exists before using it.
Key Takeaways
Memory lets AI agents keep context by storing and retrieving past information.
Always update memory after new inputs to keep conversations relevant.
Check for missing data in memory to avoid errors.
Use simple key-value storage for easy memory management.
Persistent memory helps maintain context across sessions.