Agent memory: three patterns that work, and one that does not

AI Tools & ModelsWister Xue3 repliesThis topic is about crewAIInc/crewAI

Wister XueMember#1

"Give the agent memory" is a request every framework answers, and the answer is usually a vector store plus a summary. After watching a few of these work and a few of them not, here are the three patterns I come back to, and the one that reliably wastes money.

The three that work

A file the agent writes. Not a database, not embeddings: a plain Markdown file in the project directory, with the facts the agent has established. "The auth middleware lives in src/middleware.ts and it reads the session cookie, not a header." That is the whole memory.

It works because it is cheap, it is diffable, it survives the session, and the agent can read it with the same tool it reads any other file. A grep over a Markdown file is exact; a search over embeddings is approximate. For facts of the form "where is X", a file wins.

A structured summary the agent maintains. The agent keeps a short document of the current state of the task: what has been tried, what the plan is, what is known. It is rewritten rather than appended, so it does not grow without bound.

This is what the compaction command in the coding agents does, and it is the single most effective memory feature, because it changes the size of every subsequent request rather than one of them.

Retrieval over a corpus you control. For facts that are genuinely many and genuinely fuzzy — "how did we handle rate limiting in the payments service" — embeddings are the right tool. The condition is that the corpus is worth indexing and that you can measure recall on it.

The one that does not

Appending everything to a conversation log and re-sending it. The transcript grows, the cost per request grows with it, and the signal-to-noise ratio falls, so the model is reading more noise on every turn.

The failure is not the cost. It is that the model's attention over a long conversation is not uniform, and the early turns of a long session are the ones it follows least. A memory that appends is a memory that stops working exactly when the session is long enough to need it.

The rule

Match the memory to the question. "Where is X" gets a file. "What is the state of this task" gets a maintained summary. "What did we do about Y" gets retrieval. The thing that does not work is one mechanism for all three.

3 replies

NicoMember#2

The file memory point matches what I do by hand. I keep a NOTES.md in the repo with the non-obvious things — which test fixture is stale, which flag has a confusing name — and the agent reads it when it opens the project.

It is also the only memory that a human reviewing the work can check, which turns out to matter more than I expected.

EdenMember#3

In reply to Nico (#2)

Worth adding a fourth pattern for the case where the memory is about a person rather than a project: a short profile document with preferences and constraints, loaded into the system prompt.

It is small, it is fixed, and it is the case where you want the model to attend to it, so it belongs at the front, not in a store.

Wister XueAuthorMember#4

In reply to Eden (#3)

That is a good fourth, and it makes the pattern clearer: the mechanism follows how often the memory changes and how hard it is to say.

Fixed and small goes in the prompt. Changes per task goes in a maintained summary. Facts about the code go in a file. Facts about a large corpus go in retrieval. The one that does not work is the mechanism that has no rule about which of those it is.