Stop Hoarding Context: Why Fresh Sessions Make AI Coding Agents Better
Long AI coding sessions degrade output quality as the context window fills up. A practical guide to session management, the /clear command, and using CLAUDE.md to maintain persistent memory across fresh sessions.
You have been coding with Claude Code for three hours. You started by scaffolding a new feature, then fixed a bug, then tweaked some styles, then asked it to write tests. Somewhere around prompt forty, you notice the output is getting worse. The agent seems confused. It is repeating mistakes you already corrected. It is ignoring instructions you gave twenty minutes ago.
So you repeat yourself. You re-explain the requirement. You paste in the same code block. You burn through tokens restating things the agent should already know. And the results are still mediocre.
This is not a bug. It is a predictable consequence of how context windows work — and one of the most common mistakes new users of AI coding tools make. The fix is counterintuitive but effective: stop trying to keep everything in one session.
How Context Windows Actually Work
Every AI coding agent — Claude Code, Cursor, Codex, Gemini CLI — operates within a context window. This is the total amount of text the model can hold in its working memory at any given moment. Think of it like a desk: there is a fixed amount of space, and everything needs to fit on it. Your messages, the agent's replies, file contents, tool outputs, system instructions — all of it shares the same space.
Claude Code's context window is roughly 200,000 tokens. That sounds enormous, but it fills up faster than you would expect. A single file read might consume thousands of tokens. Tool outputs, error messages, and the back-and-forth of debugging all accumulate. MCP server configurations alone can consume a significant chunk before you have typed a single prompt.
When the context window fills up, the system does not simply stop. It compacts — summarising older parts of the conversation to make room for new ones. The agent keeps working, but it is now working from a compressed summary of what happened earlier rather than the full detail.
This is where quality starts to erode.
The Quality Curve
Research into large language model performance shows a clear pattern: output quality degrades as context length increases. A study by Chroma Labs measuring 18 LLMs found that "models do not use their context uniformly; instead, their performance grows increasingly unreliable as input length grows." Information in the middle of long contexts is particularly likely to be missed — a phenomenon researchers call the "lost in the middle" problem.
In practical coding terms, this translates into three quality zones based on how full the context window is:
- 0-40% capacity: Clean, accurate output. Instructions followed precisely. Code is well-structured.
- 40-70% capacity: Quality begins to slip. The agent cuts corners, occasionally misses requirements, and needs more corrections.
- 70%+ capacity: Noticeably degraded output. Instructions are ignored or misinterpreted. The agent starts generating code that contradicts decisions made earlier in the session.
This is not speculation. Developers who have tracked their sessions systematically report the same pattern. Will Ness, comparing compacted sessions against fresh ones across 25 files, found that clearing context between tasks kept edits consistently in the high-quality zone, while the compacted approach pushed later work into degraded performance territory.
The phenomenon has been given a name: context rot. As the agent accumulates more information than it can effectively process, attention dilution occurs. Important constraints get buried, tool choices start to drift, and critical instructions get lost in the growing wall of text.
The Real Cost of Long Sessions
The quality degradation is bad enough, but the knock-on effects make it worse.
Rework spirals: When the agent produces lower-quality output, you spend more prompts correcting it. Each correction adds to the context, pushing quality down further. It is a vicious cycle — more tokens spent producing worse results.
Token waste: A 2025 analysis by GitClear found an eightfold increase in duplicated code blocks over two years of AI-assisted development. Copy-pasting rose from 8.3% to 12.3% of all code changes. Much of this comes from agents losing track of what already exists in the codebase and re-generating code that was already written.
Debugging overhead: Google's 2025 DORA Report correlated increased AI adoption with a 91% increase in code review time and a 9% climb in bug rates. Long, degraded sessions contribute directly to this. The agent introduces subtle issues that take longer to find and fix than writing the code from scratch would have.
The METR paradox: A study by METR found that experienced developers actually took 19% longer to complete tasks when working with AI tools than without them — contradicting their own predictions of a 24% speedup. The researchers attributed much of this to the overhead of reviewing, correcting, and reworking AI output. Long, unfocused sessions amplify exactly this problem.
The Fix: One Task, One Session
The solution is straightforward: treat each task as its own session. When you finish a piece of work, clear the context before starting the next one.
In Claude Code CLI, type /clear after completing each task. This wipes the conversation history and gives you a clean context window. Claude re-reads your CLAUDE.md file and starts fresh.
In Claude Code Desktop, start a new chat for each distinct task. Do not keep typing into the same conversation for hours.
In Cursor, press Cmd+N (Mac) or Ctrl+N (Windows) to open a new session.
The principle applies to any AI coding tool: fresh context means full attention capacity. The agent is not carrying around compressed summaries of irrelevant earlier work. Every token of that 200,000 window is available for the task at hand.
What Counts as a "Task"?
A task is a single, coherent piece of work. Some examples:
- Implement a new component
- Fix a specific bug
- Write tests for a module
- Refactor a function
- Update styles on a page
If you find yourself switching topics — moving from a feature to a bug fix, or from frontend work to backend work — that is a signal to clear and start fresh.
A useful heuristic: if you would make a separate git commit for it, it should be a separate session.
"But Won't It Forget Everything?"
This is the fear that keeps people clinging to marathon sessions. If you clear the context, surely the agent loses all the knowledge it has built up? Surely you will have to re-explain everything from scratch?
No. This is a misunderstanding of how coding agents work.
When you start a fresh session, the agent does not begin in a vacuum. It has access to your entire codebase. It can read any file. It can run commands. It can inspect your project structure, read your configuration files, and understand the current state of your code by looking at it directly.
The agent never needed to "remember" your codebase from earlier in the conversation — it needed to read the relevant files. And it will do exactly that in a new session when you give it a task.
Consider what actually matters for a coding task:
- The current state of the code — the agent reads this from your files
- What you want changed — you state this in your prompt
- Project conventions and preferences — these live in your CLAUDE.md file
None of these require a long conversation history. The first is always available on disk. The second is your new prompt. The third is loaded automatically every time.
What you do lose is the conversation about how you arrived at the current state. And that is almost always irrelevant to the next task. The agent does not need to know that you tried three different approaches to the nav bar before settling on the current one. It just needs to see the current nav bar and know what you want changed.
Persistent Memory: CLAUDE.md and Beyond
The key to working confidently with fresh sessions is making sure important context lives outside the conversation.
CLAUDE.md
This is the most important file in your project for AI-assisted development. Claude reads it at the start of every session — it is the equivalent of a project brief that never expires.
A well-maintained CLAUDE.md should include:
- Tech stack: What framework, language, and key libraries you are using
- Code conventions: Naming patterns, file structure, formatting preferences
- Common commands: How to run the dev server, run tests, build for production
- Architecture notes: Key design decisions that should not be revisited every session
If you do not have one, run /init in Claude Code to generate a starter file. Then refine it as your project evolves.
.claude/rules/ Directory
For larger projects, you can split instructions into modular rule files. Instead of one large CLAUDE.md, you create focused files like testing.md, api-design.md, or security.md inside .claude/rules/. Claude loads all of them automatically.
You can even scope rules to specific file paths using YAML frontmatter, so your React component conventions only apply when Claude is working on React files.
Auto Memory
Claude Code also maintains its own automatic memory. As it works, it saves useful context — project patterns, debugging insights, architecture notes, your preferences — to a persistent directory at ~/.claude/projects/<project>/memory/. This is loaded into every new session automatically.
You do not need to manage this directly. Claude curates it as you work. But you can tell Claude to remember something specific: "remember that we use pnpm, not npm" or "save to memory that the staging server is at staging.example.com." It will persist across sessions without you needing to repeat it.
AGENTS.md
Some teams use an AGENTS.md file — a convention that has emerged across multiple AI coding tools — to define how agents should behave in a repository. While Claude Code uses CLAUDE.md natively, you can reference an AGENTS.md from within your CLAUDE.md if your team uses one. The principle is the same: put persistent context in files, not conversations.
A Practical Session Workflow
Here is the workflow we use and recommend to clients:
1. Start fresh. Open a new session or use /clear.
2. State the task clearly. One prompt, one objective. "Add form validation to the contact page. Email should be validated with a regex. Phone number should accept UK formats. Show inline error messages below each field."
3. Let the agent work. It will read the relevant files, understand the current state, and implement the change.
4. Review and iterate. If adjustments are needed, make them in this session while the context is focused and clean.
5. Commit your work. Once the task is complete, commit the changes.
6. Clear and move on. /clear (or new session), then start the next task.
This approach has three benefits. First, the agent operates in its highest-quality zone for every task. Second, you spend fewer tokens because you are not dragging around the dead weight of irrelevant earlier context. Third, your git history ends up cleaner because each session maps to a logical unit of work.
When to Keep a Session Going
There are cases where staying in the same session makes sense:
- Iterating on the same feature: If you are refining a component and each prompt builds directly on the last, keep going. The context is relevant.
- Debugging a specific issue: If you are working through a single bug, the diagnostic context (error messages, stack traces, attempted fixes) is valuable.
- Multi-step processes: If the agent is partway through a migration or refactor that spans multiple files, let it finish before clearing.
The rule of thumb: if the next prompt directly depends on what just happened, stay in the session. If you are moving to a different concern, clear.
Key Takeaways
- Context windows have limits. Every AI coding agent degrades as the context fills up. This is not a defect — it is how the technology works. Plan around it.
- Clear often. Use
/clearin the CLI or start a new chat on Desktop after each logical task. Fresh sessions mean higher-quality output. - The agent does not forget your code. It reads your files directly. A new session does not mean starting from zero — it means starting with a clean, focused context.
- Put persistent context in files. CLAUDE.md,
.claude/rules/, and auto memory give the agent everything it needs across sessions. Conversations are temporary; files are permanent. - One task, one session. If it is worth a separate git commit, it is worth a separate session.
Stop hoarding context. Start every task fresh, and let the agent do what it does best — with its full attention on the work in front of it.
References
- Why You Need To Clear Your Coding Agent's Context Window — Will Ness
- When Coding Agents Forget: The Hidden Cost of AI Context Degradation — SmarterArticles
- Manage Claude's Memory — Claude Code Documentation — Anthropic
- Context Rot: The Emerging Challenge That Could Hold Back LLM Progress — Understanding AI
- The Context Window Problem: Scaling Agents Beyond Token Limits — Factory.ai
- Claude Code Context Management: If You're Not Managing Context, You're Losing Output Quality — Kushal Banda
Related insights
View allClaude Code Hooks: Automate Your AI Coding Workflow
Claude Code hooks run shell commands automatically at specific points in the agent's lifecycle — auto-formatting files, blocking dangerous commands, sending notifications, and enforcing project rules without manual intervention.
Prompts for Vibe Coders: A Practical Guide to Claude Code
The difference between vibe coding frustration and vibe coding success is knowing what to prompt. A practical guide to CLAUDE.md setup, effective prompt patterns, and the Frontend Design skill for non-technical builders using Claude Code.