Lesson 326 of 2116
Context Rot — Why Long Sessions Get Stupid
Long agent sessions degrade in predictable ways. Learn what context rot looks like, why it happens even with million-token windows, and the compaction discipline that keeps quality high.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1Bigger Window, Same Brain
- 2context rot
- 3compaction
- 4context window
Concept cluster
Terms to connect while reading
Section 1
Bigger Window, Same Brain
Claude 4.7 has a million-token window. GPT-5 has 400k. You can hand them an entire codebase. They will read it. They will also forget half of it by the time they finish writing the answer. Long context is necessary, not sufficient — and it actively degrades reasoning quality past a point.
The long-context performance curve
Compare the options
| Context used | Typical behavior | Quality |
|---|---|---|
| 0-30k tokens | Sharp, follows instructions, recalls everything | Best work |
| 30k-100k | Slight blurring of details from early turns | Still strong |
| 100k-300k | Mistakes earlier file contents, makes up function names | Mixed |
| 300k-1M | Confidently wrong about half of what you fed in | Roll the dice |
Symptoms of context rot
- The agent re-asks for a file you pasted 20 turns ago
- It contradicts a constraint you set at the start of the session
- It mixes up function names from two different files you discussed
- It claims a test is passing when you pasted the failure 5 minutes earlier
- It replies more verbosely as the session lengthens (a tell that it is uncertain)
Why it happens
Attention costs scale roughly quadratically with context length. To stay tractable, models use various sparse attention tricks. These work great in benchmarks ("find the needle in the haystack") but degrade in real reasoning where you need to combine ten facts spread across the haystack. The model can still find each one. Combining them is harder.
Compaction: deliberately throwing away
The model writes its own handoff note. Then you start fresh and paste it in.
# Mid-session compaction prompt for Claude Code, Cursor, Codex
"Pause. Summarize this session into a working brief:
1. What is the goal? (one sentence)
2. What constraints have we agreed on? (bullets)
3. What files have we touched and how? (file -> change)
4. What is the current bug or open question?
5. What should NOT be touched?
Return only the brief. I'll start a fresh session with it."Persistent memory beats long context
A 200-line CLAUDE.md, AGENTS.md, or `.cursor/rules` file at the project root is read on every session. It survives compaction. It survives session resets. Anything you find yourself repeating to the agent — conventions, scripts, gotchas — belongs in that file, not in the context window.
A 30-second check that prevents most multi-hour disasters.
# Session hygiene checklist (run mentally, every 30 min)
1. Has the agent forgotten any earlier constraint? -> compact + re-state it.
2. Are we still on the original goal? -> re-state goal, drop tangents.
3. Has the file state diverged from what the agent thinks it is? -> show `git status`, force re-read.
4. Is the agent repeating itself? -> compact and reset.
5. Have we been at this >1 hour with no commit? -> hard reset.When NOT to compact
- You're 90% done with a delicate refactor — one more push beats a reset
- The session is below 50k tokens and the agent is still sharp
- You're debugging a bug that requires the full trace history to understand
“Long context is a runway, not a destination. Land before you crash.”
Key terms in this lesson
The big idea: bigger context windows are a tool, not a free pass. Treat every session as a perishable resource. Compact aggressively, persist conventions in project-memory files, and start fresh when the agent's recall starts to slip. The work you save is your own.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Context Rot — Why Long Sessions Get Stupid”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 10 min
Long-Context Strategies: When The Window Fills Up
Even with massive context windows, real Claude Code sessions fill up. The strategies for keeping context healthy are the difference between a 10-minute session and a 4-hour grind.
Creators · 14 min
Local Coding Models Need Smaller Loops
Ollama and local models can help with coding, but they need tighter context, smaller tasks, and clearer tool-call formatting than frontier cloud models.
Creators · 35 min
AI for Debugging Stack Traces
Use AI to interpret cryptic stack traces and locate the failing line.
