Agent Memory Patterns: What Coding Agents Should Persist, Where, and What to Forget
19 November 2026 · Updated 28 November 2026 · 12 min read · by Yang Kai
Memory patterns for coding agents that actually work: the file-based patterns for facts, decisions and handoffs, what to persist, and why forgetting is a feature.
Template inside: Memory-file template set
Contents
- The layers at a glance
- Pattern 1: the standing file
- Pattern 2: the decision log
- Pattern 3: the rolling handoff
- Pattern 4: scoped memory, or, memory with an address
- Pattern 5: auto memory, the layer the agent writes itself
- What to forget, on purpose
- A worked example: one event, four layers
- Memory-file template set
- How we work here
- 2026-11-10 — [Decision in one sentence]
- 2026-11-18 — [Decision]
- Putting the patterns together
Give a coding agent a place to write notes and, left alone, it will remember everything and retrieve nothing. The transcripts pile up, last month’s workaround reads like current instructions, and the one fact that mattered sits buried under forty that did not.
This guide takes the opposite approach: memory as a small set of named patterns, each with one job, one home, and a deletion habit. It covers the standing file, the decision log, the rolling handoff, scoped topic files, and the newer agent-written layer — plus what to forget on purpose.
The mechanics below are checked against Anthropic’s Claude Code memory documentation and the agents.md format as of November 2026, because loading behaviour is exactly where memory advice goes stale.
The layers at a glance
| Layer | Holds | Who writes it | When it loads |
|---|---|---|---|
| Standing file | How work is done here | You | Every session |
| Decision log | What was settled, and why | You, append-only | When a task touches the area |
| Rolling handoff | What is true right now | You or the agent, rewritten | Start of the next session |
| Scoped topic files | One subsystem’s detail | You | Only when the task touches it |
| Auto memory | The agent’s own notes | The agent | Every session, capped |
Five layers sounds like a lot. In practice most projects need the first three, add the fourth when the standing file bloats, and get the fifth whether they asked for it or not — which is why it needs rules too.
Pattern 1: the standing file
Every agent setup already has one, whether it is called CLAUDE.md, AGENTS.md, or GEMINI.md: the file loaded at session start carrying identity, commands, conventions, and boundaries. Its memory job is procedural — how work is done here — and its cardinal rule is that everything in it must be checkable against the repo today.
The moment it records history (“we migrated to X in March”) it starts lying by omission, because the agent reads history as instruction. Keep the standing file present-tense. The format that works is in the CLAUDE.md structure that actually works, and the cross-tool version of the same pattern is mapped in the AGENTS.md field guide.
What the loading mechanics mean for you
Claude Code’s documented behaviour, checked November 2026, shapes how this file should be written:
- Four scopes stack. A managed policy file (set by an organisation), your user file at
~/.claude/CLAUDE.md, the project file at./CLAUDE.mdor./.claude/CLAUDE.md, and a personal./CLAUDE.local.md. All of them are concatenated into context — none overrides another, so a contradiction between scopes is a contradiction the agent has to guess at. - Closer files load later. Files are read from the filesystem root down to your working directory, so the file nearest the work is read last and carries the most weight in practice.
- Subdirectory files load on demand. A CLAUDE.md in a subfolder is not loaded at launch; it loads when the agent reads files in that folder. That is free scoped memory, built in.
- Size is a quality rule, not a style one. Anthropic’s guidance is to target under 200 lines per file, because longer files consume context and are followed less consistently.
One more fact worth internalising: Anthropic’s docs state plainly that these files are context, not enforced configuration. A standing file guides the agent; it does not guarantee compliance. Anything that must never happen belongs in a hook or a permission rule, not in prose.
Note: Block-level HTML comments in a CLAUDE.md are stripped before the file loads, so notes to human maintainers cost no context. Use them for the “why” you do not want to re-explain in reviews.
Pattern 2: the decision log
Decisions are the highest-value memory an agent can have, because re-deriving them is expensive and re-litigating them is worse. The pattern is an append-only file of dated entries — decision, one-line reason, and what would change it — that the agent reads when a task touches the area.
Append-only matters. Editing a decision’s history destroys the very thing the log exists to preserve, which is why the current state is what it is. When a decision is reversed, add a new entry that names the reversal; never quietly rewrite the old one.
Keep entries brutally short. A decision log that reads like meeting minutes will not be read; one that reads like a changelog will be. Three lines per decision is a good ceiling, enforced by the template below.
Pattern 3: the rolling handoff
Working state — what is done, what is unverified, what is next, what already failed — belongs in a handoff file that is rewritten at the end of each session and read at the start of the next.
The rewriting is the pattern. It forces a fresh judgement about what still matters, and it keeps the file at one screenful forever. A handoff that grows by appending is a transcript, and transcripts are where facts go to hide.
This is also the pattern that makes delegation safe. A handoff written for a memory-less reader is exactly the brief a subagent needs — the connection to briefing subagents so the briefs get used is direct: same document, two uses.
Pattern 4: scoped memory, or, memory with an address
Not all memory belongs to the project. Some belongs to a subsystem, a client, or a workflow, and loading it globally taxes every session for the benefit of a few.
The fix is progressive disclosure: memory files that the standing file points to by topic, loaded when the task touches that topic. Two mechanisms now do this natively in Claude Code:
.claude/rules/files. Split project instructions into topic files in this directory. A rule file withpathsfrontmatter loads only when the agent works on files that match — testing rules appear for test files, migration rules for migrations, and neither taxes unrelated work.@pathimports. A CLAUDE.md can import other files with@path/to/filesyntax, nested up to four levels deep. Use imports to organise, not to save space: Anthropic’s docs are explicit that an imported file loads in full alongside the file that imports it, so imports cost the same context as pasting the text in.
If you have built skills before, you already know this shape. Advanced skill authoring applies the identical layering to instructions, and sharing skills across projects covers the same scoping problem one level up. Memory deserves the same discipline.
Pattern 5: auto memory, the layer the agent writes itself
Newer, and easy to miss: Claude Code now keeps a second memory system that the agent writes on its own. It lives outside your repo, at ~/.claude/projects/<project>/memory/, with a MEMORY.md index and one topic file per subject. At the start of every session, the first 200 lines (or 25KB, whichever comes first) of that index load into context.
Auto memory is per project and per machine. It is shared across worktrees of the same repo, but it is not committed to git and never reaches your teammates. What the agent learns on your laptop stays on your laptop.
Treat it as a draft layer, not a record:
- Let it accumulate working notes, corrections, and preferences — that is what it is for.
- Promote deliberately. Anything the whole team needs, or that must survive a machine change, gets copied into the repo’s standing file or decision log by a human. Auto memory is the inbox; the patterns above are the archive.
- Audit it like agent output, because it is. An agent summarising its own session can promote a guess into a “fact” the next session will trust. The verified/unverified split from the handoff pattern applies here with full force.
What to forget, on purpose
Forgetting is the pattern nobody implements, and its absence is why agent memory degrades.
- Forget exploration. The twelve files the agent read to find the one that mattered — keep the conclusion, drop the tour.
- Forget failed attempts in narrative form. Keep them as one-line traps (“approach X fails because Y”) or they will be re-read as suggestions.
- Forget anything the code now says better than the note did. Once a convention is visible in every file, the note is redundancy that can only drift.
- Forget on a schedule. A monthly pass that deletes or demotes anything no session has needed is maintenance, not loss.
Stale memory is not neutral. An agent that trusts an outdated note is worse off than one with no note at all — the failure mode behind sessions that loop on false premises is usually a memory problem wearing a reasoning costume.
A worked example: one event, four layers
Say your project moves its staging deploy from a manual script to CI. That single event produces four different memory writes — and one deliberate non-write.
Standing file. The deploy command changes, present tense: “Deploy staging: push to the staging branch; CI runs the deploy.” The old script’s name disappears entirely. The standing file describes how work is done now; it keeps no trace that anything was ever different.
Decision log. A new dated entry: “Deploy moved to CI. Reason: two manual deploys shipped stale builds in October. Revisit if: CI queue times pass ten minutes.” Six months later, when someone asks why the script is gone, this entry is the answer — and its revisit condition is the only sanctioned way back.
Handoff. Until the first CI deploy is watched end to end, the handoff carries it as unverified: “CI deploy configured; not yet seen green. Next: trigger a staging push and watch the run.” The moment it goes green, the line moves to verified — or the whole entry vanishes at the next rewrite, because a finished migration is no longer working state.
Auto memory. The agent may note “deploys go through CI now” in its own MEMORY.md. Fine as a draft. It becomes team memory only when a human promotes it — and the promotion already happened, in the standing file, so the auto-memory note is harmless duplication rather than the record.
Nowhere. The afternoon spent debugging the old script’s path bug gets no entry anywhere. The script is gone; the trap died with it. If the bug’s lesson generalises — say, “relative paths in deploy scripts break under cron” — it earns one line in the handoff’s traps while the script still exists, and is deleted with the script.
That sorting instinct — present-tense fact, dated decision, current state, or nothing — is the entire skill. The templates below just give each sort a fixed address.
Memory-file template set
# 1) STANDING FILE — excerpt (present tense, checkable)
## How we work here
- [procedural rules only; no history, no narrative]
# 2) DECISIONS.md — append-only, 3 lines per entry
## 2026-11-10 — [Decision in one sentence]
Reason: [one line]. Revisit if: [condition that would change it].
## 2026-11-18 — [Decision]
Reason: [one line]. Revisit if: [condition].
(Supersedes 2026-11-10 entry on [topic], if applicable.)
# 3) HANDOFF.md — rewritten each session, one screen maximum
State: [one line — where the work stands]
Verified: [what is proven, and how]
Unverified: [what is claimed but not yet checked]
Next: [ordered, with starting files/commands]
Traps: [one line per dead end — approach + reason]
# 4) MEMORY HYGIENE — monthly, 15 minutes
[ ] Standing file: every line still true of the repo today?
[ ] Decisions: any reversed without a superseding entry?
[ ] Handoff: still one screen? Still rewritten, not appended?
[ ] Topic files: any unreferenced for a month? Demote or delete.
[ ] Auto memory: anything in MEMORY.md the team needs? Promote it.
[ ] Anything here that the code now states better? Delete it.
Putting the patterns together
Start with patterns one and three — a standing file and a handoff — because they cover the two questions every new session asks: how do we work here, and where did we leave off. If the standing file does not exist yet, your first CLAUDE.md is the fifteen-minute version of pattern one. Add the decision log the first time you catch an agent re-arguing a settled question. Add scoped memory the first time your standing file grows a section only one task in five needs. And put auto memory under the same hygiene pass as everything else, because it writes itself whether you supervise it or not.
The week-to-week budgeting system that runs on top of these patterns — how much context each layer should cost — lives in context budgets for long projects. Start there once the files exist.
An agent with good memory does not remember everything. It remembers the few things that change what it does next — and can show you exactly where each of them is written, who wrote them, and when they were last checked against reality.
FAQ
Where should a coding agent’s memory live: files or a database? For most individual and small-team work, plain files in the repo win: they are versioned, reviewable in pull requests, readable by every agent tool, and deletable without tooling. Reach for anything heavier only when you have a concrete failure that files demonstrably caused.
How often should agent memory be cleaned up? Monthly is a workable rhythm for active projects, plus an immediate pass whenever you catch the agent acting on a stale note — that incident means the stale item outranks the schedule.
Should the agent write its own memory files?
It should draft them; you should correct them, especially the handoff’s verified/unverified split. An agent grading its own work into memory is how unverified claims harden into facts the next session will trust. Claude Code’s auto memory makes this a live question rather than a hypothetical one — read its MEMORY.md the way you would read a junior colleague’s notes.
How big should each memory file be? Small enough to be read in full, every time. Anthropic’s own guidance for CLAUDE.md files is under 200 lines each; a handoff should fit on one screen; a decision entry should fit in three lines. If a file needs a table of contents, it is two files.
Category: Claude Code & Context Engineering · Tags: #claude-code #context-engineering #ai-agents
Next step: this article is part of Claude Code guides, which covers the full topic from first setup to advanced use.