BrainGrid
How-tos

Claude Code Memory: The State File Pattern (and Where It Falls Short)

How Claude Code memory actually works, why auto memory goes stale, and the state file pattern that gives your coding agent durable context across sessions.

BrainGrid Team
11 min read
Claude Code Memory: The State File Pattern (and Where It Falls Short)

Giving your coding agent a memory feels like the obvious fix for the most annoying thing about AI coding: every session starts from zero. So you turn on Claude Code's auto memory, or you stuff a CLAUDE.md full of context, and for a week it feels solved. Then the agent does something baffling, and you trace it back to a note it wrote to itself three weeks ago that is no longer true. The memory did not make the agent smarter. It made the agent confidently wrong, and harder to debug.

That is the claim worth sitting with. More memory is not the same as more reliability. A coding agent that remembers everything you ever told it, including the things that have since changed, is not a teammate with experience. It is a teammate working from a stale runbook nobody updated.

#How Claude Code memory actually works

Start with the mechanics, because the term "memory" hides three different things. Claude Code has a few layers, and conflating them is where most of the confusion comes from.

The first layer is the CLAUDE.md file, the one you write by hand. It lives in your repo, it holds your conventions, your build commands, and your house rules, and the agent reads it at the start of every session. This is the layer you control. The second layer is auto memory, where Claude saves notes for itself as it works, debugging discoveries, commands that worked, decisions it made, so it can accumulate knowledge across sessions without you typing anything. The official docs describe it plainly: auto memory lets Claude accumulate knowledge across sessions without you writing anything. The third layer is the live conversation, the context window, which evaporates the moment the session ends.

The distinction matters because the layer that feels most effortless, auto memory, is the one you control least. It writes itself, in a Claude-specific location, in the background, and it keeps doing so whether or not the things it recorded are still accurate.

#The reframe: stale memory is worse than no memory

Here is where the obvious fix turns on you. A real practitioner, posting the day before this was written, put the problem more sharply than any vendor would:

I've seen so many times that stale memory caused the agent to make bad decisions. The information it puts into those memory files is inherently stale in a constantly-evolving project, and the agent would often trust the memory instead of looking up the latest information.

@kunchenguid on X

Read that twice, because it inverts the whole premise. The danger is not that the agent forgets. The danger is that it remembers, and trusts the memory over the code in front of it. In a project that changes daily, which is every project worth building, a note from two weeks ago is not context. It is a confidently stated falsehood the agent will act on without checking.

The same post raises a second cost that is easy to miss: auto memory stores into a Claude-only location that other agents do not share. If you build with Claude Code today and want to try Cursor or Codex tomorrow, that accumulated memory does not travel. You have quietly built lock-in into your own workflow, one auto-saved note at a time. The portable alternative people land on is an AGENTS.md-style file, plain markdown in your repo that any agent can read.

So the instinct to "give the agent a memory" splits into two very different moves. One is letting a tool silently accumulate notes you never review. The other is deliberately writing down the durable facts about your project in a place you own and update. The first feels effortless and rots. The second takes work and compounds.

#The state file pattern

The pattern experienced builders converge on is not a database or a vector store. It is a file. A short, deliberately curated state file that holds the small set of things that are true about your project and unlikely to change session to session: the architecture, the conventions, the commands, the decisions you have already made and do not want re-litigated. The agent reads it at the start of every run. You, not a background process, decide what goes in it.

The contrast with auto memory is the whole point.

Auto memory: the tool writes notes to itself as it works, in its own location, and trusts them later. You never see most of them, and stale entries quietly steer the agent wrong.

State file: you write the durable facts, in your repo, in plain markdown. You can read it, diff it, and delete a line the moment it stops being true.

A state file is to a coding agent what a README is to a new hire. It does not try to capture everything that happened. It captures what matters and stays current because a human owns it. The 200-line cap people complain about in auto memory is not really the limitation; the limitation is that nobody is curating. A good state file is short on purpose. The discipline is in what you leave out.

This works, with one honest caveat. A state file is durable context, not a definition of done. It tells the agent how your project is built and what to assume. It does not tell the agent what this specific feature is supposed to do, or how you will know it worked. Memory, even good memory, is background. It is not a spec. And that gap is exactly where AI builds go sideways: not because the agent forgot your conventions, but because nobody ever wrote down what "done" meant for the thing it was building.

#Where memory ends and a spec begins

This is the line that matters, and it is the one the memory discourse keeps walking up to without crossing. Models are stateless. Your product is not. The job is to put the durable truth of your product somewhere outside the conversation, where it survives the session, survives the model upgrade, and survives the switch from one agent to another.

A state file is the lightweight version of that idea: durable context the agent reads every time. But context tells the agent how to work, not whether it succeeded. The heavier, more valuable version is a requirement with acceptance criteria, the durable record of what each feature is meant to do and the evidence that proves it did. That is the gap BrainGrid closes. Instead of hoping an auto-saved note still holds, you describe the feature in plain language and the Planning Agent turns it into a requirement: the conventions and context captured as the product's background, the task as a scoped requirement, the constraints as explicit acceptance criteria. That requirement is durable and yours. When you build, against Claude Code, Cursor, or Codex in your own repo or the Builder Agent in a managed sandbox, the agent works from that record, and verification checks the result against the same criteria you set. Every decision, criterion, and verification accumulates per product, so the next session does not start from zero and does not start from a stale guess either. It starts from a record you trust because you can read it.

The difference from auto memory is the difference between a note an agent wrote to itself and a spec a human wrote on purpose. One you hope is still true. The other you verified. This is the same move we make from the prompting side in structured prompting is just a spec: the durable artifact is the written definition of what you want, not the conversation that produced a result once.

#What this means for you

If you build with Claude Code right now, the practical move is two-part. First, be deliberate about memory: keep a curated state file (or CLAUDE.md) with your durable conventions, and treat auto memory with suspicion, review what it stores, or turn it off, rather than trusting notes you never read. Second, and this is the part the memory tools cannot do for you, stop relying on memory to carry the thing it was never meant to carry. Your conventions belong in a state file. What "done" means for each feature belongs in a spec with acceptance criteria, checked with evidence. The first keeps the agent oriented. The second keeps it honest.

The builders who get reliable results from coding agents are not the ones with the most memory. They are the ones who decided, on purpose, what their agent should treat as true, and then verified the rest. Memory is the background. The spec is the contract. Confuse the two and you get an agent that remembers everything and is sure about nothing.

#FAQ

#How does Claude Code memory work?

Claude Code has three distinct layers. The CLAUDE.md file is one you write by hand in your repo; it holds your conventions, commands, and rules, and the agent reads it at the start of every session. Auto memory is separate: Claude writes notes to itself as it works, saving build commands, debugging discoveries, and decisions across sessions without you typing anything, stored in a Claude-specific location. The third layer is the live conversation context, which is lost when the session ends. The layer you fully control is CLAUDE.md; auto memory writes itself in the background.

#What is the difference between CLAUDE.md and auto memory?

CLAUDE.md is human-authored and curated: you decide what goes in it, you can read and edit it, and it lives in your repo in plain markdown. Auto memory is agent-authored: Claude Code accumulates its own notes as it works, in its own location, and references them in future sessions without you reviewing them. The practical trade-off is control versus effort. CLAUDE.md takes work to maintain but stays accurate because a human owns it. Auto memory is effortless but can go stale in a fast-changing project and steer the agent toward decisions based on outdated notes.

#Should I turn off Claude Code auto memory?

It depends on how fast your project changes. In a stable codebase, auto memory can save you from re-explaining setup. In a project that changes daily, stale auto-memory notes are a known failure mode: the agent may trust an outdated note over the current code. Many experienced users prefer to keep durable facts in a curated CLAUDE.md or AGENTS.md file they control, and either review auto memory periodically or disable it, rather than trusting notes they never see. The portable, agent-agnostic alternative (AGENTS.md) also avoids tying your accumulated context to one tool.

#What is the state file pattern for AI coding agents?

The state file pattern is keeping a short, human-curated file in your repo that holds the durable facts about your project: architecture, conventions, commands, and decisions already made. The agent reads it at the start of every session, so it does not start from zero, and because a human owns and updates it, it does not go stale the way auto-saved notes do. It is the lightweight way to give a coding agent persistent context. Its limit is that it describes how your project works, not what a specific feature is supposed to do; for that you need a requirement with acceptance criteria.

#Can Claude Code remember context across sessions?

Yes, in two ways with different reliability. A CLAUDE.md or AGENTS.md file gives it durable context you control on every session. Auto memory gives it self-written notes that persist but can drift out of date. Neither is a substitute for a record of what each feature is meant to do. To carry that across sessions reliably, you want the intent captured as a durable spec with acceptance criteria, outside the conversation, so the next session starts from a verified record rather than a stale note or an empty context window.

BrainGrid turns your project's durable context and intent into a spec your coding agent builds against and is verified by, so no session starts from zero or from a stale note. Try it at braingrid.ai.

About the Author

The BrainGrid team building tools to help developers ship better software with AI.

Want to discuss AI coding workflows or share your experiences? Find me on X or connect on LinkedIn.

Get Started

Ready to build without the back-and-forth?

Turn messy thoughts into engineering-grade prompts that coding agents can nail the first time.

Describe what you want to build