AI Agents and Agentic Systems
Intermediate 15 min Module 3 of 6
Module 3 of 6

Memory and Context Management

An agent working on a long task faces the same problem a person would if they could only hold a certain number of sticky notes at once. Once the notes run out, old information falls away. This module explains the four ways agents manage memory, and why getting memory right is one of the hardest practical challenges in building agentic systems.

By the end of this module you will be able to

Why Memory Matters for Agents

A simple chatbot has no memory between conversations. Every session starts fresh. For answering questions, that is often fine. But for agents working on multi-step tasks that take minutes or hours, memory is everything. Without memory, an agent cannot build on what it has already discovered, avoid repeating steps it already completed, or maintain a coherent plan across a long chain of actions.

Think about what a researcher does: they take notes, organize them, refer back to earlier findings, and update their understanding as they learn more. An agent needs to do the same thing, but the mechanisms for doing it are built into software rather than a notebook.

The Four Types of Agent Memory

Quick reference In-context: what is in the prompt right now. External: stored in a database and retrieved. Episodic: records of what happened in past sessions. Procedural: learned skills and workflows encoded in the model or code.
Fig 3 · Agent Memory Hierarchy
Agent Memory Hierarchy Context Window (In-Context Memory) current prompt, tool outputs, active plan · fastest access, limited size less available, more durable Episodic Memory logs of past sessions and outcomes · persistent, queryable less available, more durable Semantic Memory (External Retrieval / RAG) documents, databases, vector stores · fetched on demand less available, more durable Procedural Memory skills in model weights · permanent until retrained most available most durable
Memory type Where it lives Duration Example
In-context The current prompt window Current task only The conversation so far, tool outputs, current plan
External retrieval A database, vector store, or file system Persistent across sessions Customer records, product docs, past reports
Episodic A log of past tasks and their outcomes Persistent, queryable "Last time this customer called, we resolved it by issuing a credit"
Procedural Model weights or coded workflows Permanent (until retrained) The agent's general ability to search the web, format reports, handle exceptions

In-context memory is the most immediate. Everything in the current prompt window counts as in-context memory: the original task, tool results, reasoning steps so far, and any information the agent has retrieved. It is the fastest to use and the first to run out.

External retrieval memory is stored outside the model, in a database or document store. The agent uses a tool to search it when it needs something. This is the basis of Retrieval-Augmented Generation (RAG): instead of stuffing all relevant documents into the prompt, the agent fetches only what it needs for the current step.

Episodic memory is a record of what the agent has done in the past. A customer support agent with episodic memory can look up what happened in a previous ticket from the same customer. This type of memory makes agents feel more like experienced professionals and less like people with no long-term recall.

Procedural memory is encoded in the model itself through training and fine-tuning. When an agent "knows" how to write a structured report or how to handle an edge case in a refund policy, that knowledge lives in the model's weights, not in the prompt. Procedural memory is the most durable but the hardest to update.

The Context Window Problem

The context window is the total amount of text a model can see at one time. Once a conversation or task exceeds the context window, older information either gets cut off or the whole thing fails. For short tasks, this is rarely a problem. For long agentic tasks, it is a constant design constraint.

Imagine an agent processing a 500-page legal contract. Even models with very large context windows have limits, and stuffing an entire document into a prompt is often inefficient. The agent needs a strategy for what to keep in context and what to retrieve on demand.

What happens if an agent runs out of context window mid-task?
Several things can go wrong. The model may start forgetting earlier instructions, leading it to contradict itself. It may lose track of tools it already called, and call them again unnecessarily. It may drop the original goal and start responding to only the most recent context. In practice, most frameworks deal with this by summarizing older parts of the conversation and keeping only the summary in context, or by storing detailed notes in an external database and loading only what is currently relevant.

Two Strategies for Managing Long Tasks

Summarization. As the context fills up, the agent periodically summarizes what it has learned and replaces the raw history with the summary. This compresses information but loses detail. The key design question is: what can be compressed, and what must be kept verbatim?

External note-taking. The agent uses a write-to-database tool to store key findings, decisions, and intermediate results as it goes. When it needs to recall something, it queries the database rather than relying on in-context memory. This is more reliable for long tasks but requires the agent to explicitly decide what to save, which is itself a reasoning challenge.

Design rule of thumb For tasks longer than a few dozen steps, do not rely on in-context memory alone. Give the agent a note-taking tool and build a habit of saving key findings before they scroll out of the context window.

A Second Analogy: The Detective's Casebook

A detective investigating a complex case does not try to hold every clue in working memory. They maintain a casebook: a written record of every lead, every interview, every piece of evidence. When they need to connect two facts from a week apart, they consult the book rather than relying on recall. The casebook is their external retrieval memory.

But the detective also carries procedural knowledge from years of experience: how to interview a witness, how to read a crime scene, how to spot when a alibi has a hole in it. That knowledge is not in the casebook. It is in their training, applied automatically without requiring lookup.

And the detective remembers what they did yesterday on this case (episodic memory) without needing to reread their entire casebook every morning. They pick up where they left off. Good agent memory architecture works the same way: separate stores for different types of information, each optimized for how that information is used.

Common misconception "Bigger context window = better agent memory." A larger context window is useful, but it is not a substitute for external memory architecture. Stuffing an entire document corpus into a prompt is expensive and slow. More importantly, the model's ability to attend equally to all parts of a very long context degrades: information in the middle of a very long context is consistently less well-attended than information at the start or end. External retrieval solves this by fetching only the relevant pieces for the current step.

A Healthcare Example: Patient History Retrieval

A clinical decision-support agent working with a patient's record faces a real memory challenge. A patient who has been in the healthcare system for ten years has a record that may span thousands of entries: diagnoses, lab results, medications, procedures, referral letters, and clinical notes. No context window can hold all of that. But the agent does not need all of it for every question. When the physician asks about the patient's cardiac history, the agent retrieves only the relevant cardiac entries from the external record store. When the question shifts to current medications and potential interactions, the agent retrieves the medication list. The retrieval is targeted, not total.

This is why RAG (Retrieval-Augmented Generation) is the standard architecture for clinical AI: it lets the agent work with effectively unlimited external memory while keeping the in-context prompt focused on the specific information needed for the current reasoning step. The agent's procedural knowledge (clinical reasoning patterns, drug interaction logic) lives in the model weights. Patient-specific facts live in the external store. The two work together, but they are stored separately.

Python · Simple Two-Tier Agent Memory
from datetime import datetime

class AgentMemory:
    """Two-tier memory: in-context list + external key-value store."""

    def __init__(self, context_limit: int = 20):
        self.context = []          # in-context (fast, limited)
        self.store = {}            # external store (persistent)
        self.context_limit = context_limit

    def add(self, key: str, value: str, persist: bool = False):
        """Add to in-context memory; optionally persist externally."""
        entry = {"key": key, "value": value, "ts": datetime.utcnow()}
        self.context.append(entry)

        if persist:
            self.store[key] = value  # write to external store

        # Trim context if over limit
        if len(self.context) > self.context_limit:
            self.context = self.context[-self.context_limit:]

    def retrieve(self, key: str) -> str | None:
        """Check context window first, fall back to external store."""
        for entry in reversed(self.context):
            if entry["key"] == key:
                return entry["value"]   # found in context
        return self.store.get(key)      # fall back to external store

    def get_context_window(self) -> list[dict]:
        """Return current context formatted as system messages."""
        return [{"role": "system",
                 "content": f"{e['key']}: {e['value']}"}
                for e in self.context]

# Usage
memory = AgentMemory(context_limit=20)
memory.add("patient_id", "PT-88421", persist=True)
memory.add("current_issue", "chest pain onset 2 days ago")
print(memory.retrieve("patient_id"))  # "PT-88421"
Try this

Think of a 10-step task you know well, for example, onboarding a new client or processing an expense report. Map each step: which information from step 1 does step 10 need? Which information only matters for step 2 and can be forgotten after? Which facts need to persist across sessions? You have just mapped the memory requirements of an agent for that task, and you can see exactly which type of memory (in-context, external retrieval, episodic) belongs at each point.

Knowledge check
Which type of memory allows an agent to recall what happened in a previous session with the same user?
Correct. Episodic memory is a log of past tasks and interactions. An agent with episodic memory can look up what happened with a user in a previous session.
Episodic memory stores records of past tasks and interactions. In-context memory only covers the current session, and procedural memory stores skills rather than history.
What is the main limitation of in-context memory for long tasks?
Exactly. The context window is a hard limit. Once a task grows beyond it, the agent cannot see earlier parts of the conversation without a strategy to manage that.
The context window is the key constraint. It is fast, but it is finite, and when a task exceeds it, older content scrolls out of the model's view.
Interactive 3: Memory Layer Visualizer Try it

Drag the slider to simulate conversation turns passing. Watch the context window fill up (turning red as it nears capacity) while episodic memory grows. At turn 8 the agent switches to external retrieval.

1
Context remaining: 4000 tokens    Episodic memories: 0    External retrieval: No
Before you go
Reflection: Think of a task that requires remembering many things from earlier steps to complete correctly. Which type of memory is most important for each piece of information the agent would need to track?
You might also like
Was this module helpful?
← Module 2: Tool Use Module 4: Multi-Agent Systems →