stoneskin (Mr. Sun)

Personal homepage of Mr Sun - AI Solutions Architect, Tech Lead & AWS Cloud Specialist: resume, selected projects, and teaching archive

Don’t Let Your AI Remember in the Wrong Place: How Memory, Instructions, and Docs Divide the Work

← Back to home

2026-12-02 · 中文版

OpenCode did something annoying to me the other day.

I had told it to always reply in Chinese, even when I write in English — because sometimes I can’t type Chinese. That rule lived in its memory. Then, in the middle of a long session, it auto-compacted, and when it came back everything had flipped: I wrote English, it replied in English; I wrote Chinese, it replied in Chinese. I asked if it had forgotten the language setting. It answered that it got it wrong during compression.

That isn’t the model being dumb. That’s how compaction works: older conversation is replaced by a summary, and the summary is lossy. Instruction files (AGENTS.md) are different — after compaction, OpenCode re-reads the latest version from disk and makes it the session baseline, verbatim. The docs say it plainly [3]: “Compaction is lossy.”

So the question isn’t “how did it forget.” It’s “why did I put that rule in the wrong place.”

Where things go: three questions to ask

That incident made me think seriously about a question: what actually belongs in memory? Rules, preferences, facts, decisions, task status — where does each live? In an instruction file, long-term memory, documentation, a spec, a TODO list, a database, or RAG?

After looking around, the answer comes down to three questions:

  1. Does it need to show up verbatim every time — does it survive compaction? If it does, it has to live where compaction can’t reach it.
  2. Who reads it — a human, the agent, or both? Things humans read need to be written for humans.
  3. Does it change — and must the new version beat the old one? Things that change need a way to expire. Otherwise the old version haunts you forever.

By these three criteria, the things to store can have seven homes.

The seven homes

Instruction files (AGENTS.md / CLAUDE.md): standing rules. This is the only home that comes back verbatim after compaction. Keep only short, durable rules and facts that every task needs — “always reply in Chinese”, “run the tests before committing”, “don’t touch /etc”. More is worse: the longer the file, the less reliably the agent follows it. Claude Code’s own guidance [1] is under 200 lines per file. What doesn’t fit becomes a skill, loaded on demand.

Long-term memory: facts and preferences about a person. Who you are, what you do, what you like and dislike — across sessions, kept, and changeable. “Changeable” is the key part: when a preference changes, the new one must win. Zep has the classic example [5]: a user loves Adidas, the shoes break, they switch to Puma, and ask “what sneakers should I buy?” Pure RAG returns the old “loves Adidas” fact, because it is the closest semantic match, completely ignoring that the preference changed. So memory needs an invalidation mechanism: when a fact is corrected, the old one must be marked invalid, not just appended to.

Documentation: decisions and reasons. “We chose SQLite because…” — a decision plus its reasoning. Humans and the agent both read it, but not on every turn. Put it in a design doc and leave a one-line pointer in the instruction file. Rationale stuffed into the instruction file is the most common form of bloat, and it makes the agent follow the file less reliably.

Spec / plan / tasks: the what, how, and status of one feature. The spec says what to build and why; the plan says how; the tasks break it into small checkable steps and track progress. The state lives in files on disk, not in the conversation — the next session re-reads the files and picks up where the last one stopped.

TODO: what is being done right now. A TODO list is working memory externalized to disk: current state, not durable knowledge. Decisions, reasons, and preferences stuffed into a TODO get lost the moment they are checked off. And task status stuffed into the instruction file or memory goes stale the next day while still occupying a slot that is loaded every time.

A structured database / system of record: live, single-truth state. Order status, balances, permissions, slots — queried live, never stored in memory, never in RAG. Memory and RAG can’t answer “what is true right now”; they answer “what was recorded before.”

RAG: shared, slowly changing knowledge. Policies, manuals, docs, code — the same copy for everyone, re-indexed when it changes. RAG is not memory: it doesn’t know who you are, and it doesn’t know which version is current.

Two numbers worth noting

Two sets of numbers from 2026 arXiv papers — papers posted publicly before peer review — are worth noting.

The first is from Salle et al. [8], who measured OpenCode’s compaction: the first compaction already drops about a third of a task’s anti-cheating rules, and how-to instructions are dropped about six times more often than specifications of what to build. A dropped rule never comes back — each summary is written from the previous summary.

The second is from Liu et al. [9]: lifting standing rules out of retrieved notes and into a labelled block at the top of context raised rule application from 52.6% to 94.7%. That is where my language rule belongs — not as one entry in memory, but as a block at the top of a file.

How to tell memory from RAG

There is a simple five-question test [6]:

  1. Is the answer the same for every user? Yes → RAG.
  2. Does it depend on what this user said or did before? Yes → memory.
  3. Can it change, with the newer fact needing to win? Yes → memory with an update mechanism.
  4. Is it live business state? Yes → call the system of record, not memory, not RAG.
  5. Does it need to survive the session? Yes → memory; no → the conversation history is enough.

And four tests for whether your RAG setup already covers memory: change a preference — does the new one win? Start a fresh session — is the earlier fact still there? Ask as a different user — does the fact leak across? Ask it to forget — does it stay forgotten? If it fails correction or isolation, that is what a memory layer is for.

Ways to put things in the wrong home

What this means for open-memex

open-memex is my local-first memory layer. Applied to it, the divide-the-work rules come down to three things.

First, memory is the capture layer, not the destination. Memory → document → knowledge base is one road: what memory catches gets distilled into a document, shared, and becomes a knowledge base. open-memex is both a place to store things and a transition point toward documents and RAG. But promotion has a bar: only memories that generalize, are verified, and are meant to be shared move up; the document links back to the memories it came from; and promotion is a human decision, not an automatic one.

Second, memory needs an admission checklist. Mem0 lists six criteria [4], and they work as-is: will this be useful in a future request? How fast does it go stale? Did the user state it, or did the model infer it? Should it be stored at all? How long should it live? Does it need to survive the session? If you can’t name a future situation where it helps, don’t store it.

Third, imports should aggregate, not store one entry per note. When importing notes from Obsidian, don’t store one tiny memory entry per note — each memory is very short, and a pile of near-identical fragments crowds out the one that matters at retrieval time. Aggregate by topic into a structured file, and at import time deduplicate, resolve contradictions, and filter. Importing is itself one “capture → curate” step.

A one-line test

When you write something down, ask yourself one question: if this were deleted, would the agent get something wrong in a way that cannot be recovered by re-reading a file?

Put things where they belong, and the agent remembers when it should remember, forgets when it should forget, and looks things up when it should look them up. Put them in the wrong home, and the better it remembers, the more confidently it gets things wrong.


Sources

  1. Anthropic, “Effective context engineering for AI agents”
  2. Anthropic, “Effective harnesses for long-running agents”
  3. OpenCode, “Compaction”
  4. Mem0, “Types of AI Agent Memory”
  5. Zep, “Stop Using RAG for Agent Memory”
  6. Supermemory, “RAG vs agent memory”
  7. Maximem, “Agent Memory vs RAG”
  8. Salle et al., “Benchmarks Are Surprisingly Compaction-Friendly” (arXiv:2610.31778)
  9. Liu et al., “RAG in a Trenchcoat” (arXiv:2611.22179)
  10. Palatekit, “Skills or instruction files”

The combined “seven homes” table is synthesized from multiple sources, not quoted from any single one.