DEV Community

Cover image for How to Tell What Claude Code and Codex Forget When They Compact a Session
Mayur Agnani
Mayur Agnani

Posted on Edited on Originally published at theaicommit.com

How to Tell What Claude Code and Codex Forget When They Compact a Session

When a coding agent runs low on memory, it keeps what you told it and what it changed, and throws away nearly everything it only read. You can predict the loss, measure it, and tell the summary what to keep.

Engineer's view. This is a process restart with a handoff note. Whatever the process wrote down or saved to disk comes back. Whatever sat only in memory is gone, and nothing errors. A coding agent's memory is its conversation, so values it read but never wrote down vanish, and it must read them again.

TL;DR. A compacted agent keeps your instructions and its own edits, but loses most of what it only read. Whether a fact survives depends on how it entered the session, so you can predict the loss and tell the summary what to keep.

Explain Like I'm 5

You are building a huge model castle, and your table is full. So you ask a friend to write a
short note about the castle, then you clear the table.

The note says what you were told to build and which towers you finished. It does not list every
piece you looked at in the box. Those are still in the box, so you can look again.

But some pieces you borrowed for a minute, and they went back to a friend's house. If the note
does not mention them, you will not remember them, and you cannot go look.

So before you clear the table, tell your friend what to put in the note.

The Problem

You have shipped this bug already, in a system with no model in it.

Your service held a session cache in process memory. A deploy restarted it. The health check went
green and the error rate stayed flat. Then users started logging in again, because the sessions
never reached disk. Nothing crashed. The process simply came back without the state that had only
lived in memory.

A coding agent has the same split, and it restarts in the middle of your work.

Its working memory is the conversation. When that fills, the agent runs compaction: it
replaces the conversation with a summary and carries on. In my test session, 78,642 tokens became
2,632. That is 97% gone in about twenty seconds, and the session looks healthy afterward.

The agent then keeps working from the note. If a detail is missing, you see no error. You only see
it read the same file twice, or ask something you already answered. Worse, the detail may have
come from a test run or a web page that no longer exists.

You could treat this as bad luck. It is not. What survives follows a rule.

The fix: know which lane each fact is in, tell the summary what to keep, and write anything you
cannot regenerate to a file before compaction runs.

Read the full post on The AI Commit

How to Tell What Claude Code and Codex Forget When They Compact a Session carries the rest: the fix and why it works, the code to change for each role, how to tell it worked, when it is the wrong tool, and an interactive model you can play with.


Originally published at theaicommit.com, a daily AI engineering lab for software engineers. The original has an interactive version you can play with, and the full runnable code is on GitHub.

Top comments (0)