~/TechPurAI
~/tutorials/building-ai-agents/ai-agent-memory-explained
intermediate·part 7 of 22·3 min read

AI Agent Memory Explained

Updated Aug 16, 2026AI

Part 6's agent already has a form of memory — the messages list growing with each real loop iteration. This part covers the complete, real picture: three genuinely distinct kinds of memory an agent can have, each solving a different, real problem.

Working memory: within one, real task's loop

python
messages = [{"role": "user", "content": request}]
# ...grows with every real loop iteration — this IS working memory

This is exactly part 6's messages list — the real, running record of everything that's happened within the current task's loop. It's what lets the model correctly reference "the stock level I just checked" two iterations later; it's genuinely temporary, existing only for the duration of one real task run.

python
class AgentSession:
    def __init__(self):
        self.conversation_history = []

    def run(self, new_request: str) -> str:
        self.conversation_history.append({"role": "user", "content": new_request})
        # ...run the real agent loop using self.conversation_history,
        # exactly like the AI Fundamentals series' own chatbot memory
        return final_answer

This directly extends the AI Fundamentals series' own conversation memory coverage — a genuine, real session lets a person ask a follow-up ("now check the Gift Subscription bags too") that references earlier, real context, without repeating everything explicitly.

Why it matters

Working memory and session memory solve genuinely different, real problems, easy to conflate. Working memory is the agent's own internal reasoning trail for ONE task; session memory is what lets a HUMAN have a real, ongoing conversation with the agent across multiple, related tasks. Bright Leaf Coffee's inventory agent (part 6) needs working memory to function at all; it only needs session memory if a real person is meant to interact with it conversationally, task after task.

Long-term memory: persisting across genuinely separate sessions

python
# a real, persistent store — a database, not an in-memory list
class LongTermMemory:
    def remember(self, fact: str, category: str) -> None:
        AgentMemory.objects.create(fact=fact, category=category)

    def recall(self, category: str) -> list[str]:
        return [m.fact for m in AgentMemory.objects.filter(category=category)]
text
Real, concrete example: the inventory agent learns, over several
  real, separate runs, that the Ethiopian Light Roast tends to sell
  out faster around real holiday periods — a real, useful pattern
  worth remembering ACROSS sessions, not just within one

This is genuinely the most structurally different kind of memory — it survives even after a real Python process restarts, requiring actual, persistent storage (a real database, exactly the Django series' own models coverage), not just an in-memory Python list or object.

Why long-term memory is genuinely harder than it sounds

text
The real, honest challenge isn't STORING facts — it's deciding
  WHICH facts are worth remembering, and correctly RETRIEVING the
  relevant ones at the right, real moment

This is directly the same real problem the LLM & Advanced AI series solved for documents — a real, growing set of remembered facts needs the same embedding-based semantic retrieval to surface the genuinely relevant ones for a new, current situation, rather than dumping every remembered fact into every prompt regardless of relevance.

A real, practical decision framework for which memory type a given agent needs

text
Every agent needs working memory — it's structurally required for
  part 1's loop to function at all
Add session memory if: a real human interacts with the agent across
  multiple, related requests in one sitting
Add long-term memory if: the agent genuinely benefits from patterns
  or facts learned across SEPARATE, real sessions over time — a
  genuinely rarer, more advanced real need

Bright Leaf Coffee's part 6 inventory agent needs only working memory to function correctly for its actual, current scope — a genuine, honest example of not reaching for more sophistication than a real task requires.

Next: giving your agent persistent memory — a real, working implementation of long-term memory using an actual database.

VK

Vijay Kumar

Founder of TechPurAI — writing hands-on tutorials and honest tool breakdowns.

LinkedIn ↗
← previous6. Build Your First AI Agent with Pythonnext →8. Giving Your Agent Persistent Memory