DEV Community

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

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

Edward Izgorodin on November 25, 2026

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 compact...
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.

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.