How to build an AI agent memory system you control
Decide what gets remembered, where it lives and who may change it, instead of hoping a summary survives.
An AI agent memory system is a storage and retrieval layer that keeps decisions, constraints and working history outside the conversation, so the next session or another agent can start from them. You control it when you decide what gets written, where it is stored, who may change it and how it is read back.
This guide covers the kinds of memory, a five-step build with files you own, an n8n setup, and when a repository file is enough.
How does an AI agent memory system work?
Most systems run the same loop. Retrieve: before the agent answers, the relevant entries are read from a store. Prompt: they are added to the context window with your request. Persist: new decisions and results are written back. The store lives outside the agent, which is why it can serve Claude Code, Codex and Cursor alike.
What kinds of memory does an agent need?
| Kind | What it holds | Lifetime |
|---|---|---|
| Working context | Current files, recent tool calls, the system prompt | One session |
| Episodic | What happened: commands run, bugs fixed, decisions and their reasons | Months |
| Semantic | Distilled facts: the package manager, the test runner, the forbidden folder | As long as true |
| Retrieval index | Embeddings or keyword indexes over the stored entries | Rebuilt as entries change |
Working context disappears with the session or after compaction. Episodic and semantic memory are what stop you re-explaining the project. A retrieval index only helps once the other two are written well.
How do you build memory for an agent step by step?
1. Pick the storage
Markdown in the repository is a strong default: it is versioned, reviewed in diffs and readable by every tool. A database fits when entries need tags, dates or many projects. A hosted store fits when the memory must be reachable from several machines and agents.
2. Define what a memory is
Write the format before the code. A useful entry has four fields: the decision or fact, why, the date, and the project or area it belongs to. Vaguer entries become noise the agent rereads every session.
3. Write when work ends
Capture is freshest at the end of a task. In Claude Code, a SessionEnd hook can run a script when a session ends (hooks reference); a short final prompt such as "summarize the decisions, files touched and dead ends, then append them to the decisions log" works in any tool.
4. Retrieve at the start of work
Load the few entries that matter: filter by project, then by date. Serve them from a file the agent reads or from an MCP tool it calls. Add semantic search only when the store grows past what an agent can reasonably read.
5. Prune
Entries go stale. Remove the ones a newer decision contradicts and merge duplicates. Without this step the memory becomes a drawer the agent searches in vain.
How do you add memory to an n8n AI agent?
n8n builds this with three pieces: a Chat Trigger node to start the workflow, a memory sub-node attached to the AI Agent node, and a vector store for facts that must outlive the conversation (n8n Simple Memory documentation).
- Simple Memory keeps the last interactions, set by Context Window Length, under a Session Key. Use a key that is stable per project or conversation, not per browser tab, or every run starts a new thread. n8n's documentation advises against this node in queue mode.
- For history that must survive restarts, n8n also offers chat memory nodes backed by a database, such as Postgres Chat Memory.
- A vector store node in Retrieve Documents (As Tool for AI Agent) mode lets the agent look up stored facts when it needs them. A separate step in the workflow writes the facts worth keeping into that store.
Recent messages and durable facts are different jobs: keep the first in the memory node and promote the second to the store.
How do you choose a memory layer?
Ask five questions of any option, including your own files:
- Can Claude Code, Codex and Cursor all read it?
- Does it survive a new session without you restating anything?
- Does it hold decisions and dead ends, not only files?
- Is it scoped per project, so one project never leaks into another?
- Can you read and export everything it stored?
An AGENTS.md in the repository passes the fourth and fifth questions. It fails when it goes stale, when it has to carry working history, or when two agents edit it and the conflict appears at the merge.
Where a hosted project workspace fits
Fileaify is one option for the shared layer. It keeps a project's decisions, current state and known issues in documents with fixed sections; agents read them over REST or the MCP connector, search them with combined keyword and semantic search, and propose changes that your publication policy reviews. It does not record your conversations on its own: what is kept is what an agent or a person writes and publishes. Keys stay in a separate vault and are used without being shown to the agent. The trade-off is depending on a hosted service for memory you could keep in the repository; the free plan is the way to test whether that trade is worth it.
When is AGENTS.md enough?
For one agent on one repository, a committed AGENTS.md or CLAUDE.md plus a decisions log covers most needs, and it is the right place to start. It reaches its limits in three cases: several agents changing the same file, credentials that must never sit in a file the agent reads, and work that spans several repositories, servers or months. The comparison of AGENTS.md, CLAUDE.md and Fileaify shows what belongs where.
Sources
- Claude Code hooks reference: the
SessionEndandStopevents. - n8n Simple Memory node: session key and context window length.
- n8n Simple Vector Store: the retrieve-as-tool mode.
Try it
The demo runs on a prepared project, with no account. To give your own agents a shared project memory, create a free workspace and give your agent https://fileaify.com/agent.
Common questions
Is an AI agent memory system the same as RAG?
No. Retrieval-augmented generation is one way to read memory back. A memory system also decides what gets written, when, at which scope, and when old entries are removed.
Can I add memory to an n8n AI agent without a vector database?
Yes. Attach a Simple Memory node to the AI Agent node and give it a stable session key. It keeps recent messages; facts that must last longer belong in a persistent store.
Do coding agents need a memory system if I already use CLAUDE.md or AGENTS.md?
Not at first. One agent on one repository works well with an instruction file and a decisions log. Add a shared layer when several agents or tools must start from the same decisions.
How do I stop two agents from writing conflicting memories?
Give shared memory one path for changes: append-only entries, or proposals that a person or one agent reviews, instead of several agents editing the same file freely.
Written with AI assistance, reviewed by Stéphane Mermet and checked against the product. Fileaify publishes this guide and sells the service it describes.