AI Agents

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.

Storage Context window Rules History Retrieved Dropped Model

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.

System prompt Conversation so far New message Tool descriptions Tool results Free one call: the model reads the whole bar (example proportions)
Five kinds of text share one window, and the conversation and tool results are the two that grow during a run.

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.

Short-term lives for one conversation trimmed trimmed A buffer keeps the last few turns and resends them on each call Context window what the model reads Recent turns Retrieved Model Long-term outlasts the conversation Retrieval fetches the few items that match the request
Both kinds of memory end in the same place: text placed in the context window before the model reads it.

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.

PlaceHoldsChangesExample
System promptRole, rules, tone, limitsOn the day you edit it"Pause for approval before sending an email"
Short-term memoryThis conversation's messages and tool resultsEach turnThe applicant's answer two messages ago
Long-term store or databaseRecords, documents, past outcomesAs the business updates themA 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.

System prompt role, rules, limits Short-term memory this conversation Database records that change Standing rules Price list Last few turns Rule typed in chat Order history Price list dashed: stored in the wrong place; arrow: where it belongs
The price list pasted into the prompt belongs in the database, and the rule typed in chat belongs 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

  1. Anthropic, Context windows
  2. n8n, Advanced AI documentation
  3. Anthropic, Building effective agents

Concepts covered: agent memory, context window, short-term memory, long-term memory, retrieval, vector store, system prompt, session key.

Weekly newsletter

The AI Brief by Clarence Cheang

A weekly exploration of the developments, technologies and ideas shaping artificial intelligence.

One email a week. Unsubscribe in one click. What is in each issue