DEV Community

Cover image for How do I fix AI agents forgetting context in long sessions?
Edward Izgorodin
Edward Izgorodin

Posted on

How do I fix AI agents forgetting context in long sessions?

A rule meant for the whole session does not belong in the chat. When a coding agent forgets context halfway through a long session, look at compaction first. Once the context window is nearly full, Claude Code replaces the conversation with a summary that the model writes, and a rule you gave only in the chat survives only as far as that summary kept it. Move every rule that has to last into the CLAUDE.md at the project root, and keep Claude Code up to date.

Claude Code's memory documentation has a troubleshooting entry called "Instructions seem lost after /compact". It says "Project-root CLAUDE.md survives compaction". After /compact, Claude reads that file from disk again and puts it back into the session. Nested CLAUDE.md files and rules scoped to paths load again only when Claude next works with files they apply to.

My own working rules for coding agents live in CLAUDE.md files in my repositories, each with the date it was added and the reason. Version 2.1.311, released on 26 October, fixed Claude seeing "stale MEMORY.md and CLAUDE.md content right after automatic compaction when those files were edited earlier in the session". Before it, a CLAUDE.md you had edited during a session could reappear in its old form after the next automatic compaction.

Two later fixes are about the same moment. A "Prompt is too long" error after automatic compaction, in sessions where a very large CLAUDE.md had changed since it was last sent, was fixed in 2.1.317 on 3 November. And 2.1.328 stopped automatic compaction from "reusing the session's earlier system prompt instead of rebuilding it the way /compact does".

On 19 November 2026, the day 2.1.328 came out, I counted the changelog lines that contain the word compact in that release and the 19 before it, back to 2.1.305 on 20 October. There are 55, and in 17 of the 20 releases at least one of them begins with "Fixed". Counting by one word is crude, and two of the 55 use it in another sense, "a compact list" and "compact JSON". I left them in.

Changelog lines with the word compact in each Claude Code release from 2.1.305 to 2.1.328

Run claude --version before a long session. On anything older than 2.1.328, at least one of these three fixes is missing.

Everything above is as of 19 November 2026, when I read the changelog, the release dates on GitHub and the memory documentation. Releases after 2.1.328 are not in the count.

I work on Mnemoverse; the quotes here are Anthropic's, and the count is mine.

Top comments (1)

Collapse
 
aiapikey profile image
HAOHAOnovai •

The count measures how often compaction was worked on, which is not the same as how much forgetting went down. A changelog line is evidence someone tried; it is not evidence the symptom changed.

The direct measurement is available and cheap, and it would make the version advice much stronger: log the message list and the token count at each turn, then compute per session how many times compaction fired and what fraction of pre-compaction content survived into the next turn. That gives a survival rate you can chart against the version axis. "2.1.328 dropped survival loss from X to Y" is a much harder claim than "seventeen of twenty releases touched it", and it is the one a reader can act on.

The version check is still the right first move. It is just measuring the effort rather than the effect.