AI Agent Memory Systems: Short-Term and Long-Term Memory
Build AI agents that remember and learn from interactions
AI Agent Memory Systems
Why Memory Matters for AI Agents
Without memory, AI agents are stateless - every conversation starts fresh. Memory enables:Types of Memory
1. Working Memory (In-Context)
Short-term memory stored directly in the conversation context:python
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "My name is Alice"},
{"role": "assistant", "content": "Nice to meet you, Alice!"},
{"role": "user", "content": "What's my name?"}, # Agent can answer from context
]
2. Episodic Memory
Records of past interactions stored in a database:python
class EpisodicMemory:
def __init__(self, db):
self.db = db
def store(self, user_id, interaction):
self.db.insert({
"user_id": user_id,
"timestamp": datetime.now(),
"summary": interaction.summary,
"key_facts": interaction.extract_facts()
})
def retrieve(self, user_id, query, k=5):
return self.db.semantic_search(
query=query,
filter={"user_id": user_id},
limit=k
)
3. Semantic Memory (Knowledge Base)
Factual knowledge stored as embeddings:python
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddingssemantic_memory = Chroma(
embedding_function=OpenAIEmbeddings()
)
Store facts
semantic_memory.add_texts([
"Alice prefers Python over JavaScript",
"Alice works at TechCorp as a data scientist",
"Alice is interested in machine learning"
])Retrieve relevant context
results = semantic_memory.similarity_search(
"What programming language does the user prefer?"
)
Memory Management
As memories accumulate, you need strategies to:Privacy Considerations
Always implement memory with user consent and provide options to view/delete stored memories.Also available in 中文.