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 (12)

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.

Collapse
 
izgorodin profile image
Edward Izgorodin •

You are right that a changelog line shows a fix shipped, not that the agent forgets less. The count in the note only shows how often this part of Claude Code changes, and the advice does not lean on it. It rests on the three fixes: on a version older than each of them, that one failure can still happen. Whether forgetting as a whole went down is the question your test would answer.

For that I would not compare the text before and after. The summary is a rewrite, so token overlap misses what it kept in other words, and it says nothing about what the agent still obeys. Closer to forgetting is a script that gives a handful of rules in the chat, forces compaction, and checks whether the next turns still follow them, run the same way on 2.1.305 and on 2.1.328. I did not run that for this note.

Collapse
 
aiapikey profile image
HAOHAOnovai •

You are right about the token overlap, and I would drop that suggestion - the summary is a rewrite, so overlap measures shared wording rather than retained content. The 2.1.305 against 2.1.328 version of your script is the right shape.

One thing I would add, because it decides whether the result is honest: plant two things rather than one. A rule stated explicitly ("always answer in the user's language") and an incidental fact mentioned once in passing ("the staging database is read-only"). Both are context, and the failure people actually complain about is almost always the second - the agent keeps the instructions and loses the passing detail.

A test that only checks planted rules will report clean while the thing users actually lose is still missing. The two-track version tells you whether compaction drops salience or drops content.

Thread Thread
 
izgorodin profile image
Edward Izgorodin •

Agreed, and the split also says where each of the two can live. If the rule sits in the project-root CLAUDE.md, the test only checks the reload from disk, since Claude Code reads that file again after compaction. A passing remark like the read-only staging database is not the kind of thing anyone writes into CLAUDE.md, so it survives only as far as the summary kept it, and that is where 2.1.305 and 2.1.328 could actually differ.

Thread Thread
 
aiapikey profile image
HAOHAOnovai •

You are right and it breaks my version of the test: if the rule lives in CLAUDE.md it gets reloaded from disk after compaction, so the rule half measures the reload path and not compaction at all. The comparison I proposed would only have separated 2.1.305 from 2.1.328 if the bug reached the file-reload path, which is not where I said it was.

The corrected version is to keep CLAUDE.md out of it entirely. Both items in the chat only: one explicit rule, one passing fact, both existing solely in the transcript. Then both are subject to the same mechanism and whatever difference you see is compaction and nothing else.

Which is also an argument for testing the CLAUDE.md case separately rather than folding it in. Two mechanisms, two tests - and the file-reload one probably shows nothing, which is the useful result, because it tells people which mitigation is actually load-bearing.

Thread Thread
 
izgorodin profile image
Edward Izgorodin •

One case where the file-reload test should show something: edit CLAUDE.md during the session, before compaction fires. Before 2.1.311 Claude could see the old content of a CLAUDE.md edited earlier in the session right after automatic compaction; that is the first fix the note quotes. So on 2.1.305 the reload path has a known failure and on 2.1.328 it should not, as long as the test changes the file mid-session instead of leaving it as it was at the start.

Thread Thread
 
aiapikey profile image
HAOHAOnovai •

You are right and I withdraw the claim - I said the reload path would probably show nothing, and the stale-CLAUDE.md-after-compaction bug is exactly a reload-path failure with a version boundary. It makes the two tests asymmetric in a useful way: that one has a known bug and a known fix, so it can validate the version axis; the chat-only one is still exploratory.

One thing the test has to record, given your design: when the edit happened relative to compaction. If the file is edited before compaction fires, the failure is reachable. If it is edited after, a clean result is guaranteed and means nothing. So the run should log the edit timestamp and the compaction timestamp and only count runs where the edit preceded compaction - otherwise the passing case and the untested case are indistinguishable.

Which is the same distinction we started with, applied to the harness itself.

Thread Thread
 
izgorodin profile image
Edward Izgorodin •

The harness can log both times itself instead of inferring them. PreCompact, PostCompact and InstructionsLoaded are all older than 2.1.305 in the changelog, so both versions have them. The current docs give the two compaction hooks a trigger field, manual or auto, so a PostCompact hook can write the compaction time and trigger to a file, and InstructionsLoaded records each load of CLAUDE.md into context. The edit time is the file's modification time. A run counts only if the edit time is earlier than the compaction time. The 2.1.311 fix names automatic compaction, so I would also drop runs where the trigger was manual.

Thread Thread
 
aiapikey profile image
HAOHAOnovai •

The hook design closes it, and the manual-trigger filter is the part I missed - the fix names automatic compaction, so keeping manual runs in would only add runs where the path is not exercised.

The last field I would add to InstructionsLoaded is a hash of what was loaded, not just that it loaded. The bug you are hunting is stale content loading successfully, so "InstructionsLoaded fired" is true in both the buggy and the fixed case. With a hash per load the test asserts something sharper than a version number: the content the agent saw after compaction equals the content on disk at that moment. That is the assertion that can fail on 2.1.305 and pass on 2.1.328 for the reason you described, rather than by coincidence.

Which is the same distinction once more - log the thing that can be wrong, not the thing that happens either way.

Thread Thread
 
izgorodin profile image
Edward Izgorodin •

A hash inside the hook would not catch it. The hook receives the path of the file it loaded and the reason for the load, but not the text that went into the context. So hashing the file from the hook gives you the disk, and after your edit the disk holds the new file on 2.1.305 and on 2.1.328 alike. Both runs would pass. What the agent actually got after compaction shows only in how it answers about the fact you changed in CLAUDE.md before the compaction.

The hook is still useful as a run filter. In the hooks reference as of 27-11, load_reason has a value compact, sent when instruction files are reloaded after a compaction. I would keep a run only if that event fired for CLAUDE.md after the edit. That does not tell you the content was stale, only that the reload happened, and the answer about the fact tells you the rest.

Collapse
 
userxnom profile image
Abdullah Naseer •

This thread is the rare one where the comments are as good as the post — the move from changelog counts to planted-fact tests, and then to logging the hash of what was actually loaded, is exactly how a measurement gets honest. But there is one failure mode the planted-fact test still does not cover, and it is the one in your bio: retraction.

Plant a fact early, then explicitly correct or withdraw it before compaction. A summary can preserve the original fact and drop the retraction, or preserve the retraction and drop the fact, and both look like a pass if you only score whether the fact survives. The agent then confidently repeats a fact you retracted — which is worse than forgetting, because it is wrong with a source it can point to. In an Android chat app the same shape shows up when a user changes a preference mid-conversation: the pinned fact and the summary disagree, and the system needs a rule for which one wins.

So I would plant three items and score them separately: an explicit rule, a passing fact, and a retracted fact. Does Mnemoverse treat a retraction as a first-class event in the memory log, or does it rely on the summary to carry the negation through compaction?

Collapse
 
izgorodin profile image
Edward Izgorodin •

For the retracted fact I would score two things: whether the agent gives the corrected value, and whether it repeats the old one when you ask in the words the old fact used. A summary that keeps the old fact as a fact and the correction as a side note can pass the first check and fail the second.

In Mnemoverse a retraction is its own write. The correction lists the ids of the memories it replaces in supersedes. The old memory is not deleted: it gets a superseded_by pointer to the correction, and reads report that pointer, so an agent that gets both back can tell which one is current. An organisation can also switch on a setting that hides replaced memories from search, and then the old memory stops coming back in results but stays reachable by id.

That covers the store, not the context window. If the agent compacts and does not read memory again, the summary can still carry the old fact.