🧠 Lesson: Adding Memory to Your AI Agent
🎯 Lesson Objective
By the end of this lesson, you’ll be able to:
-
Understand what memory is in LLMs
-
Add short-term and long-term memory to your local agent
-
Use LangChain memory tools like
ConversationBufferMemoryand vector store-backed memory -
Enable your agent to remember context across turns or even across sessions
🧠 What Is Memory in an LLM Agent?
Without memory, your agent forgets everything after each message.
With memory, your agent can:
-
Keep track of previous user questions
-
Personalize responses (“as you said earlier…”)
-
Build long-term context over multiple chats
🧩 Types of Memory
| Type | Tool | Stores | Use Case |
|---|---|---|---|
| 🧠 Short-Term Memory | ConversationBufferMemory |
Chat history | Keeps past turns in one session |
| 📁 Long-Term Memory | Vector DB (e.g., Chroma) | Embeddings | Store facts, files, profiles, logs |
✅ STEP 1: Add Short-Term Memory (One Session)
This is perfect for live chat in main.py.
🧪 Code Example
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
# Define memory buffer
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Wrap your QA chain in Conversational Retrieval Chain
qa_chain = ConversationalRetrievalChain.from_llm(
llm=Ollama(model="mistral"),
retriever=vectordb.as_retriever(),
memory=memory
)
# Chat loop
chat_history = []
while True:
query = input("Ask something: ")
if query.lower() in ['exit', 'quit']:
break
response = qa_chain.run({"question": query})
print("🤖", response)
Now it remembers context:
User: What is LangChain?
User: How does it handle memory? ← This question now depends on the previous one!
✅ STEP 2: Add Long-Term Memory (Persistent Storage)
Use ChromaDB or FAISS as a memory system.
When you upload new info (e.g., your bio, your company’s policies), it gets embedded and stored for recall across sessions.
Code Snippet: Persistent Vector Store
vectordb = Chroma(
persist_directory="./db_memory",
embedding_function=embedding
)
# Save memory to disk
vectordb.persist()
Next time you run the script, just load the existing DB instead of reprocessing:
vectordb = Chroma(
persist_directory="./db_memory",
embedding_function=embedding
)
You can then query:
“What did I tell you last week about my product roadmap?”
🧠 Hybrid Approach: Chat + File Memory
You can combine:
-
Chat memory for conversation
-
Document/file memory for long-term reference
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa_chain = ConversationalRetrievalChain.from_llm(
llm=Ollama(model="mistral"),
retriever=vectordb.as_retriever(),
memory=memory
)
📁 Optional: Saving and Loading Chat History (JSON or TXT)
You can serialize chat history using LangChain or manually:
import json
with open("chat_memory.json", "w") as f:
json.dump(memory.chat_memory.messages, f)
And load it later.
🧪 Test It Now
-
Run your script
-
Ask:
“My name is Ronny. I build AI agents.”
-
Then ask:
“What’s my name?”
“What do I build?”
🎯 If memory works, it will remember your previous input.
✅ Summary
| Feature | Tool |
|---|---|
| Session memory | ConversationBufferMemory |
| Long-term facts | Chroma / FAISS |
| Memory + LLM + Retrieval | ConversationalRetrievalChain |
You’ve now added a brain to your agent! 🧠
70
