Three kinds of memory
The model remembers nothing between requests. Everything the agent “remembers” is stored separately by the software and added to the model’s context, that is, the text the model sees when it answers.
Short-term memory: the history of the current conversation. The agent remembers what the customer wrote three messages ago and does not ask again. In the LangChain documentation, this is memory within one conversation.
Long-term memory: facts about a customer across conversations. The name, past orders, preferences, agreements. It is stored in a database linked to the customer and retrieved when the customer writes again.
Shared memory: rules and experience that are the same for all customers. LangChain splits long-term memory into facts, experience from the agent’s past actions and instructions, and the agent’s instructions are its system prompt.
Memory is one of the 7 parts of an agent, along with the model, the instructions, the tools and the knowledge. How the parts fit together is shown in the agent structure diagram.
How to set it up
In n8n, short-term memory is connected to the “AI Agent” node as a separate node. “Simple Memory” stores the history inside n8n, and the “Context Window Length” parameter sets how many past messages the agent takes into account.
“Simple Memory” has a limit stated in the n8n documentation: in queue mode, when n8n runs on several workers, the node is not suitable for production.
For production, keep the history in your own database, for example PostgreSQL on a server in Russia. The steps in n8n:
1. Connect the “Postgres Chat Memory” node to the “AI Agent” node and choose the credentials of your PostgreSQL database.
2. In the “Table Name” field, enter the table for the history. If the table does not exist, n8n creates it.
3. In the “Session Key” field, enter the conversation key, for example the customer’s chat number, so different customers’ histories do not mix.
4. In the “Context Window Length” field, set how many past messages the agent takes into account in its answer.
Do not pass a long history in full. LangChain warns that a full history may not fit into the model’s context, and with a long context models answer worse, slower and at a higher cost. So old messages are removed from the context.
Long-term memory is kept in a database with a customer card or directly in the CRM. The agent reads it with a “find customer” tool and writes new facts with an “update card” tool. This way staff can see the memory; it is not hidden inside the agent’s software.
Anthropic built its memory tool the same way: the model only asks to read or write a file, and your software stores the data in your storage. For 152-FZ this is convenient: you choose where the data is stored.
The Claude Agent SDK describes the agent loop as “gather context, take action, verify work, repeat”. Memory is part of the context the agent gathers before an action.
Personal data in memory
A customer’s name, phone number, address and order history are personal data. Your company is the data operator, and 152-FZ requires collecting and storing the data of Russian citizens in databases in Russia.
If the memory or the model is abroad, the operator must notify Roskomnadzor, Russia’s data protection regulator, before transferring data. This rule has applied since 1 March 2023.
The rule for memory: store only what the process needs. A booking agent needs the name, phone number and visit time; it does not need the passport or the full purchase history.
Set the storage period in advance. When the purpose of processing is reached, for example the order is complete and the warranty period has passed, Part 4 of Article 21 requires stopping the processing and destroying the data within 30 days.
Do not write into long-term memory what a customer mentioned in passing: health information, family details, other people’s data. The agent does not need them for its work, and a leak of them does more harm.
How we store customer data in our projects is described on the page about data security. The agent’s rights and action log are covered in the agent protection checklist.
Deletion on request
A customer has the right to know what is stored about them and to withdraw consent. Under Article 20 of 152-FZ, the operator answers a request for information within 10 working days and can extend this by another 5 working days with a stated reason.
If a customer shows that data is inaccurate or was collected without a legal basis, the operator corrects or destroys it within 7 working days.
When consent is withdrawn, Part 5 of Article 21 requires stopping the processing and destroying the data within 30 days, unless a contract or law allows keeping it longer.
For the agent, this means one requirement: memory must be deletable per customer, not only as a whole. Store the conversation history and long-term memory with a customer key, so you can find and erase all of a customer’s records in one operation.
Check the copies. The action log, exports for quality checks and backups also contain conversations, and deletion must cover every place where the data lives.
This is not legal advice: a lawyer checks the deletion procedure and the consent texts.