AI Agent Memory: How Agents Remember
Decide what your agent keeps in its system prompt, in conversation memory and in a database, and avoid the three common memory failures.
The short answer
A language model keeps nothing between calls. An agent remembers because the software around the model stores earlier text and sends it back inside the context window, the limited amount of text the model reads per call. Short-term memory holds the current conversation. Long-term memory sits in a store the agent searches.
How does an agent remember anything?
A language model keeps nothing between calls. An agent appears to remember because the software around the model stores earlier text and sends it back with each new request. Agent memory is that storing and resending.
The context window is the model's working memory: the maximum amount of text it can read in one call. Vendors measure it in tokens, chunks of text about the size of a short word or part of a longer one. The system prompt, the tool descriptions, the conversation so far, each tool result and the new message share that one window. Text outside the window does not exist for the model on that call.
Each memory design choice below comes down to which text you place in the window for a given call, and which text you leave in storage.
What is short-term memory?
Short-term memory is the record of the current conversation or run: your messages, the agent's replies, its tool calls and their results. The software appends each new item and resends the record on the next call.
The record grows with each turn, so builders trim it. A window buffer keeps the last few exchanges and drops older ones. A summary buffer asks the model to compress older turns into a short paragraph and keeps the paragraph. The first is cheap and forgets early details. The second keeps the gist and can lose an exact figure.
In n8n, a no-code automation platform, a memory sub-node attached to the agent node does this job, with a setting for how many past exchanges to keep. My intake agent has one attached. How AI agents work shows the loop that fills it.
What is long-term memory?
Long-term memory is information kept outside the conversation in a store, such as a database, a spreadsheet or a document index, which the agent reads through a tool or a retrieval step. It outlasts the conversation.
Retrieval means fetching the few stored items relevant to the current request and placing them in the context window. Two methods cover most builds. An exact lookup finds a record by a key, such as a customer's email address. A semantic search finds passages close in meaning to the question. It relies on embeddings, lists of numbers that represent the meaning of a passage, which a vector store holds and compares.
The model then reads the retrieved text like any other text in its window. The grounded chatbot I built in n8n works this way: a webhook receives the question, the agent answers from an approved source and nothing else, and the reply goes back out.
Writing to long-term memory needs more care than reading from it. An agent that saves its own notes can save a mistake, and the mistake returns in later conversations with the standing of a fact.
What belongs in the system prompt, in memory and in a database?
Put rules that apply to each run in the system prompt, the events of the current conversation in short-term memory, and facts about customers, orders or products in a database the agent queries.
| Place | Holds | Changes | Example |
|---|---|---|---|
| System prompt | Role, rules, tone, limits | On the day you edit it | "Pause for approval before sending an email" |
| Short-term memory | This conversation's messages and tool results | Each turn | The applicant's answer two messages ago |
| Long-term store or database | Records, documents, past outcomes | As the business updates them | A customer's order history |
One frequent mistake is pasting a price list or a staff directory into the system prompt. The text goes stale, the model reads it on each call whether or not the request needs it, and the provider bills you for those tokens each time. Keep changing facts in the system that owns them and give the agent a lookup tool.
The reverse mistake is leaving a standing rule in conversation memory. A rule you typed in chat last week drops out of the window once the buffer trims it. Standing rules go in the prompt.
How does agent memory fail?
Memory fails in three ways: the stored text goes stale, the context window overflows, or one user's information reaches another user.
Stale memory
The agent retrieves a fact that was true last quarter and states it as current. Store a date with each item, prefer the live system over a saved copy, and name the person who deletes old entries.
Context overflow
A long conversation or a large tool result fills the window. The software then drops the oldest text, or the call fails. Early instructions and early facts go first. Trim each tool result to the fields the agent needs, and summarise a long history before it reaches the limit.
Leaking between users
Memory keyed to the wrong identifier mixes people up. If two customers share one session key, the label the software uses to find a conversation's memory, the second customer can receive details the first one typed. Give each conversation a unique session key. For long-term records, have the query filter by the user's own ID in fixed logic, so the model cannot ask for someone else's rows.
What should a small business build first?
Start with a short conversation buffer and one lookup tool against the system that holds your records. That pair is enough for an intake, enquiry or booking agent, and you can inspect both parts by eye.
- Keep the system prompt to role, rules and limits. Move reference material out to a store.
- Set the buffer to the last several exchanges, then test a long conversation and note what the agent forgets.
- Use the spreadsheet or customer database you have as long-term memory before you add a vector store. Add semantic search once people ask questions an exact lookup cannot answer, such as questions over policy documents.
- Give each user or conversation its own session key.
- Let the agent read long-term records, and put a person or a fixed rule in front of each write.
- Decide what personal data the agent stores and for how long, in line with the data protection law that applies to you.
More memory raises cost, because each stored item you resend adds tokens to the call. What AI agents cost to run shows that arithmetic, and AI agent architecture places memory among the other parts of the system.
Common questions
Does an AI model learn from my conversations with an agent?
Within an agent, no. The model does not change between calls. The agent recalls earlier text because the software stores it and sends it back. Whether a provider uses your data to train future models is a separate matter set by its terms.
Do I need a vector database for agent memory?
A first build can do without one. A conversation buffer plus an exact lookup against a spreadsheet or database covers many small builds. A vector store helps once the agent must search long documents by meaning.
What happens when the context window is full?
The software has to drop or summarise older text, or the call fails. Early instructions and facts are the first to go, so trim tool results and summarise long histories before the window fills.
Sources and references
- Anthropic, Context windows
- n8n, Advanced AI documentation
- Anthropic, Building effective agents
Concepts covered: agent memory, context window, short-term memory, long-term memory, retrieval, vector store, system prompt, session key.