Module 2 · Lesson 1 · 13 min read
Why does an agent need memory?
Why Agents Need Memory
Introduction
A language model remembers nothing between one call and the next, and all it knows at any moment is what we put in the context window. In this lesson we understand why memory is an engineering decision, what the limits of the context window are, and what the agent needs to remember.
Learning objectives
- Explain what it means for a language model to be stateless
- Describe the context window and why it fills up quickly in agents
- Classify what an agent needs to remember into four categories
- Build a simple long-term memory with a context budget in Python
The model remembers nothing
A fact that surprises many beginners: a large language model is stateless. Each call to it is completely independent of the one before. When a conversation seems continuous in chat applications, it is because the application resends the earlier messages with every new message. What is not sent in the current call, the model does not know.
So an agent's "memory" is not something inside the model; it is an engineering decision you make: what do you save? Where? And what do you put into the context in each cycle?
The context window: the small desk
The context window is the maximum number of tokens the model can read in a single call, including the instructions, the messages, the tool results and the reply itself. Imagine it as a small desk: everything you want the model to see must be placed on it, and the space is limited.
In agents this window fills up quickly because:
- Every cycle of the loop adds the model's decision and the tool's result.
- The results of some tools are very long (a web page, a data file).
- The instructions and the tool descriptions are sent with every call.
Even when the window is large, filling it with everything is not free: cost rises with the number of tokens, latency grows, and the important information can get lost among a lot of unimportant information.
What does the agent need to remember?
What an agent needs can be divided into four categories, which we cover in detail in the following lessons:
- Current task context: what the user asked for and what the agent has done so far.
- Information about the user: their preferences, their language and what you agreed on before.
- Domain knowledge: facts and documents it needs to answer, such as the university regulations.
- Past experience: what worked and what failed in similar tasks.
A practical example: the academic advisor who forgets
A student talks to an "academic advisor" agent:
- The first session (Sunday): "I'm in computer science, and I prefer evening lectures because I work in the mornings."
- The second session (Tuesday): "Suggest courses for next semester."
Without long-term memory, the agent will suggest morning courses from a random major, or ask the same questions again. With a simple memory, it retrieves the major and the preference and puts them in the context before deciding.
The following code simulates this difference, and estimates the number of tokens roughly (the number of words times a factor), because an exact count depends on the tokenizer of the model used:
def estimate_tokens(text, factor=1.5):
# A rough estimate only; the real count depends on the model's tokenizer
return int(len(text.split()) * factor)
long_term = {} # a simple long-term memory: one dictionary per user
def remember(user_id, key, value):
long_term.setdefault(user_id, {})[key] = value
def build_context(user_id, message, budget=60):
facts = long_term.get(user_id, {})
lines = [f"- {k}: {v}" for k, v in facts.items()]
context = "Known information about the user:\n" + "\n".join(lines) + "\nRequest: " + message
used = estimate_tokens(context)
if used > budget:
raise ValueError(f"The context ({used}) exceeds the budget ({budget})")
return context, used
# The first session: we save what the student said
remember("stu-9", "Major", "Computer science")
remember("stu-9", "Preference", "Evening lectures")
# The second session: a completely new session, but the memory remains
ctx, used = build_context("stu-9", "Suggest courses for next semester")
print(ctx)
print("Estimated tokens:", used)Notice two points: long-term memory lives outside the model (here in a dictionary, in practice in a database), and the agent decides what goes into the context within a set budget.
Memory is a double-edged sword
Memory makes the agent smarter, but it brings new questions: what if wrong information is saved? What if the user's preference changes? Does the user have a right to be forgotten? These questions are the subject of the memory management lesson.
Summary
A language model is stateless, and what looks like memory in conversations is the resending of earlier messages. The context window is limited and fills up quickly in agents because of tool results and repeated cycles, and filling it has a cost in money, time and quality. An agent needs to remember the task context, user information, domain knowledge and past experience. Long-term memory lives outside the model, and the agent chooses what goes into the context within a budget.
Terms
| Term | English | Definition |
|---|---|---|
| Stateless | Stateless | Keeps no information between one call and the next |
| Context window | Context Window | The maximum number of tokens the model reads in a single call |
| Token | Token | The unit of text the model processes and by which cost is calculated |
| Tokenizer | Tokenizer | A component that turns text into tokens for a particular model |
| Context budget | Context Budget | The limit the developer sets on what enters the context in each cycle |
Review questions
- Why do chat applications seem to remember even though the model is stateless?
- Name three reasons why the context window fills up quickly in agents.
- What are the four categories of what an agent needs to remember? Give an example of each.
- Why is it not advisable to fill the context window with everything even when it is large?
References
- Cognitive Architectures for Language Agents (CoALA) arxiv.org Reviewed on 2026-11-18
- MemGPT: Towards LLMs as Operating Systems arxiv.org Reviewed on 2026-11-18
