Lesson 5 of 7 · 14 min · Checked on 6 October 2026
Context, compaction and memory
By the end of this lesson you will be able to:
- Explain why a long session starts to go worse
- Choose between compacting the conversation and starting a new session
- Save the state of the work in a file and use the tools' memory without depending on it
Jorge spent the afternoon with the agent putting together the sign-ups for the music school. It started well. At seven in the evening, the agent proposes a database, even though they had agreed not to. At eight, it redoes a function that was already done and tested.
The agent is not tired. The sheet where it sees the conversation is full.
The concept: the context window
The model only sees what is in the context window: your messages, its replies and everything it read along the way. If you took the Artificial Intelligence course, you already know it from the lesson “Why it forgets and why it makes things up”.
In an agent, the window fills up much faster than in a chat. Every file it opens and every result of a command goes in there. An hour exploring code can take up more than a day of conversation.
The Claude Code documentation is direct: performance gets worse as the window fills up. The agent starts to “forget” instructions from the beginning and to make more mistakes. That is what happened to Jorge.
Compact or start fresh
You have two ways out.
Compacting swaps the conversation for a summary of it. You carry on with the same work, with less weight. You can say what must survive:
/compact keep the list of changed files, the decisions taken and the command for the tests
The summary loses details. Claude Code also compacts on its own when the window is almost full; after that, it reads CLAUDE.md and the automatic memory from disk again.
Starting fresh gives you an empty conversation. It is the best choice between different tasks, and when you have already corrected the agent twice on the same problem without results. In that case, the conversation is full of wrong attempts that pull it toward the same mistakes. Start another, with a better request that includes what you learned (lesson 1/04).
Tip
Rule of thumb: same task and a heavy conversation, compact. New task, or the agent going round in circles, start fresh.
One task per session
The Claude Code best-practices guide calls the most common mistake the “kitchen sink session”: you start one task, ask a question about something else, go back to the first. The window fills up with topics that do not help.
One task, one session. When it is done, tests passing (lesson 2/01), commit, new session.
Write the state in a file
What has to survive goes in a file, not in the conversation. Before you stop:
Before we finish, write in the file ESTADO.md, as a list and in at most 15 lines: what is done, what is left, the decisions we took and why, and the command to run the tests.
(ESTADO means “state”.) Read the file, correct what is wrong and commit. In the next session:
Read ESTADO.md and tell me in three lines where we left off. Then propose the first step of what is left, without changing any files.
If you do not want a file, ask at least for a summary before you close: “Summarise in five lines what we did and what is left.” Copy it into the first request of the new session.
Memory: what passes from one session to the next
There are three different places.
CLAUDE.mdorAGENTS.md(lesson 1/07). You write it. It is read in every session. It is for rules.- Automatic memory. The agent writes it, on its own: your preferences, the corrections you made to it.
- The state file. You write it, with the agent's help. It stays in the project and in Git.
Both tools warn about the same thing: memory is context, not a guaranteed rule. Codex's says to keep mandatory rules in AGENTS.md. Claude Code's says that, to truly block an action, you use a hook (lesson 2/03).
Watch out
Automatic memory can save things that are wrong, out of date or that you did not want saved. Review it now and then and delete what is not right. Never tell the agent a password “so it remembers”.
How it is done today
This is how it looks on 6 October 2026; your screen may differ. Look for the button with the same name.
Claude Code. /context shows how much of the window is taken. /compact compacts, with or without instructions. /clear starts an empty conversation. Automatic memory comes switched on: /memory shows the memory files and has the switch to turn it off. The notes are kept in ~/.claude/projects/<project>/memory/, and the start of MEMORY.md is read in every session. If you say “remember that…”, it goes to memory; if you say “add to CLAUDE.md…”, it goes to CLAUDE.md.
Codex. /status shows, among other things, the session's token use. /compact summarises the conversation and /new starts a new one. Local memories come switched off and are marked as experimental. To switch them on, add this to ~/.codex/config.toml:
[features]
memories = true
After that, /memories controls whether the conversation uses memories. They are kept in ~/.codex/memories/.
Tip
In CLAUDE.md you can say what compaction should keep, for example: “When compacting, always keep the list of changed files and the command for the tests.”
Exercise
How I know it worked in the new session, without you explaining anything, the agent says the function and the three tests are done and tells you what is left. The window taken is much smaller than before step 5.
If it didn't work
/contextdoes not exist: you are in Codex; use/status.- The agent cannot find
ESTADO.md: you opened it in another folder. Check withpwd(lesson 1/03). ESTADO.mdcame out huge: repeat the request with “in at most 15 lines”./memoriesdoes not appear in Codex: memories are switched off. See the configuration above.
Sources:
Videos
In the specified section: how the context window works, the /context command, one task per conversation, saving learnings in CLAUDE.md, /clear and auto-compact.
The context window explained: tokens, limits, the lost-in-the-middle problem, memory management, compaction and the context cost of MCP servers.