Sub topic 1
Why Agents Need Memory: LLM Calls Are Stateless
Prerequisites
This chapter assumes you know basic Python and have called a chat-style LLM API before. You should also know that an agent is an LLM running in a loop with tools. You do not need to know about vectors yet.
A model call remembers nothing
A language model call is a function. You send input, and you get output. The model keeps no record of earlier calls. Its weights do not change when you talk to it, and the server does not hold your conversation between requests.
This is what "stateless" means. If you send "My name is Priya" in one call and "What is my name?" in the next, the second call has no way to know the answer.
def call_llm(messages):
"""Stand-in for a real chat API. It only sees what is in `messages`."""
text = " ".join(m["content"] for m in messages)
if "What is my name?" in text and "Priya" in text:
return "Your name is Priya."
if "What is my name?" in text:
return "I don't know your name."
return "OK."
# Call 1: the user introduces themselves.
print(call_llm([{"role": "user", "content": "My name is Priya."}]))
# Call 2: sent alone, the model has no memory of call 1.
print(call_llm([{"role": "user", "content": "What is my name?"}]))
# Call 3: the code resends the earlier turn, so the answer is possible.
print(call_llm([
{"role": "user", "content": "My name is Priya."},
{"role": "assistant", "content": "OK."},
{"role": "user", "content": "What is my name?"},
]))The first call prints OK.. The second prints I don't know your name.. The third prints Your name is Priya.. The only difference is what the code put in the prompt.
Chat apps fake continuity
Chat products feel continuous because the application stores the conversation and resends it on every turn. The model is not remembering. It is rereading.
For an agent, this matters more. An agent runs many steps per task. Each step produces tool calls and tool results that later steps need. If the code drops them, the agent repeats work or loses its goal.
What memory means for an agent
Agent memory is whatever the surrounding code chooses to put into the prompt. The model's own memory is limited to what it learned in training, and that is fixed. So memory design is a set of engineering decisions:
- What to keep from the current task.
- What to save for later sessions.
- What to bring back, and when.
This tutorial splits the answer into two kinds.
| Kind | Where it lives | Lifetime | Example |
|---|---|---|---|
| Short-term memory | The prompt, as the conversation buffer | One session or task | The last ten messages |
| Long-term memory | Storage outside the model | Across sessions | "Priya prefers metric units" |
Why not just resend everything?
Resending the full history works until it does not. Three limits push you toward real memory design:
- Context window size. The model accepts only a fixed number of tokens per call. Long histories stop fitting.
- Cost and latency. You pay for every input token on every call, so a growing history makes each turn slower and more expensive.
- Noise. Old, irrelevant text can distract the model from the current task.
Also, a resent history disappears when the session ends. A new session starts blank unless you stored something durable.
The next chapters cover the context window first, then each memory kind in turn.
Exercise
A user tells your agent their favorite language in session 1. In session 2, a day later, the agent asks again. The code only keeps a list of messages in a Python variable that is reset at startup. Which kind of memory is missing, and why did the model not know the answer?
Show answer
Long-term memory is missing. The model is stateless, and the message list that served as short-term memory was discarded when the session ended. Nothing durable was stored or put back into the session 2 prompt, so the model had no way to see the fact.
Exercise
In the code above, what single change to the second call makes the model answer correctly, and what does that show about where memory lives?
Show answer
Include the earlier user turn (and the assistant reply) in the messages list for the second call. This shows that memory lives in the application's prompt construction, not in the model.