Chuyển đến nội dung chính

Lesson 8: Agent Memory — Short-term, Long-term & Episodic

Types of memory: conversation buffer, summary memory, entity memory. Implement long-term memory with DB vector. Episodic memory allows the agent to "learn" from experience.

🧠 AI & ML — Lesson 7 Lesson 8: Agent Memory — Short-term, Long-term & Episodic

Build AI Agents: From Zero to Production

Part 3: RAG & Memory — Give Agent memory

xdev.asia

Introduction

Memory turns agents from "goldfish" (forgetting after each conversation) into true assistants — remembering preferences, learning from mistakes, and accumulating knowledge over time.


1. Types of Memory

1.1 Short-term Memory (Working Memory)

  • Conversation history in the current session
  • Limited by context window

1.2 Long-term Memory

  • Persistent across sessions
  • Save in vector DB or database
  • For example: user preferences, important facts

1.3 Episodic Memory

  • Remember "episodes" — the times the agent completes the task
  • Help agents learn from experience
  • "Last time a user asked the same thing, I did this and it worked"

2. Implementation

class AgentMemory:
    def __init__(self):
        self.short_term = []
        self.long_term = chromadb.Collection("long_term")
        self.episodes = chromadb.Collection("episodes")
    
    def remember(self, content, memory_type="long_term"):
        if memory_type == "long_term":
            self.long_term.add(documents=[content], ...)
    
    def recall(self, query, n=5):
        return self.long_term.query(query_texts=[query], n_results=n)
    
    def save_episode(self, task, steps, outcome):
        episode = f"Task: {task}\nSteps: {steps}\nOutcome: {outcome}"
        self.episodes.add(documents=[episode], ...)

Summary

  • 3 types of memory: short-term (conversation), long-term (facts), episodic (experiences)
  • Vector DB is the backbone for long-term and episodic memory
  • Summary memory helps fit long conversations into the context window
  • Episodic memory helps agents improve over time

Exercises

  1. Implement the full AgentMemory class
  2. Build an agent that "remembers" user preferences across sessions
  3. Implement episodic memory and will the test agent improve?
  4. Comparison: buffer memory vs summary memory for long conversations