1.7 Context Management Best Practices
Your AI coding agent is only as good as the context it's working with. This section teaches you to manage your context window like a professional — keeping your agent sharp, focused, and productive throughout long development sessions.
Why Context Management Matters
Every interaction with an AI model accumulates tokens: your prompts, the model's responses, tool outputs, file contents read into context, and error messages. The context window — the model's "working memory" — is finite. And unlike a simple hard limit where things just stop working, the reality is worse: output quality degrades gradually as context fills.
This means your agent can slowly become less effective without any obvious signal that something is wrong. It still responds. It still writes code. But it starts missing details, ignoring instructions, and producing subtler bugs. Understanding this dynamic — and knowing how to counter it — is one of the most important skills you'll develop in this program.
The "Dumb Zone"
Practitioners have a name for the experience of watching a capable AI agent become progressively less useful mid-session: the "Dumb Zone." The term was coined by Dex Horthy (HumanLayer) and amplified by Matt Pocock, capturing the frustration of an agent that was performing brilliantly ten minutes ago but now seems to have forgotten how to code.
This isn't imagined — it's well-documented in research:
Research Findings
| Study | Key Finding |
|---|---|
| Liu et al., 2024 (TACL) — "Lost in the Middle" | Models disproportionately ignore information placed in the middle of long contexts, with >20% accuracy drops on QA tasks |
| Du et al., 2025 | 13.9–85% performance loss attributable to context length alone; degradation begins as early as ~7K tokens |
| Chroma Research — "Context Rot" | Named and characterized the gradient nature of degradation — it's progressive, not a cliff |
| Anthropic documentation | Acknowledges that "performance degrades as context fills" |

Source: Chroma Research — Context Rot
The chart above shows performance across multiple models (Claude Sonnet 4, GPT-4.1, Qwen3-32B, Gemini 2.5 Flash) on a repeated-words task as input length increases. All models degrade toward ~0.5 (random chance) by 10K+ tokens. Claude holds the longest but still drops sharply. The takeaway: no model is immune to context rot.
You'll see guidance (including in this program) to "compact around 40–60% utilization." This is a practitioner heuristic based on observed quality degradation patterns — not a precise, peer-reviewed threshold. The actual point of degradation varies by model, task complexity, and the nature of the context content. The principle holds: don't wait until your context is full to act.
Your Context Management Toolkit
Claude Code provides several mechanisms for managing context. Here's when and how to use each one.
/compact — Summarize and Continue
The /compact command summarizes your current conversation to free context space while preserving key decisions, state, and progress.
/compact
You can also provide focus instructions to guide what the summary preserves:
/compact focus on the database schema changes and migration plan
When to use: When your status line shows context usage climbing past 40–60%, or when you notice the agent starting to lose track of earlier decisions. Compact before quality degrades, not after.
/compact faithfully summarizes whatever is in your context, including failed attempts, wrong conclusions, and error loops. If your context is polluted, compaction serializes that pollution into the summary that guides all future work. See Intentional Compaction below for how to clean your context before compacting.
/rewind (Esc+Esc) — Selective Undo
The /rewind command (or the Esc+Esc keyboard shortcut) lets you undo back to a specific point in the conversation and compact from there. This is more surgical than /compact — it lets you discard a wrong direction entirely.
/rewind
When to use: When the agent has gone down a wrong path and you want to back up to before the mistake. Rather than trying to correct accumulated errors, rewind to a clean state and redirect.
/rewind isn't just an undo button — it's a context quality tool. Every wrong path the agent explores leaves tokens in your context that will survive compaction. By rewinding past failed attempts before you compact, you ensure only validated, correct work gets summarized. Think of /rewind as protecting the quality of your next /compact.
/clear — Full Reset
The /clear command wipes the entire conversation history and starts fresh.
/clear
When to use: When switching to an entirely unrelated task, or when context has become so polluted that compaction won't help. Starting fresh is often faster than fighting accumulated confusion.
Status Line — Your Early Warning System
If you configured your status line in 1.2 Verify Installation, you have a live context utilization indicator at the bottom of your terminal. This is your dashboard — glance at it regularly.
When to use: Always. The status line is passive monitoring. When you see context usage climbing into yellow/red territory, that's your cue to compact or clear.
CLAUDE.md — Persistent Context That Survives Compaction
Your project's CLAUDE.md file is loaded into context at the start of every session and after every compaction. This makes it the ideal place for instructions that must always be present: coding conventions, project architecture notes, workflow rules, and behavioral guidance.
When to use: For any instruction you find yourself repeating across sessions or after compaction. If you've told the agent the same thing three times, put it in CLAUDE.md.
@ File References — Precision Context Loading
Instead of letting the agent read entire files into context, you can use @ references to load specific files precisely when needed.
@src/components/Header.tsx What props does this component accept?
When to use: When you need the agent to work with specific files but want to minimize unnecessary context consumption. This is especially valuable when working with large codebases where reading the wrong files can quickly eat your context budget.
Intentional Compaction — Curate Before You Compact
The tools above — /compact, /rewind, /clear — are powerful individually. But the real skill is knowing how to combine them. This is the idea behind Intentional Compaction, a concept articulated by Dex Horthy at HumanLayer.
The Problem with Naive Compaction
When you hit /compact after a messy session — one full of failed attempts, error loops, and wrong directions — the summarizer doesn't know which parts were mistakes. It faithfully condenses everything, including wrong conclusions and abandoned approaches. Your compacted context now carries that misinformation forward, subtly poisoning every subsequent interaction.
Not all context problems are equal. Dex Horthy identifies a clear hierarchy of harm:
| Context Problem | Severity | Why |
|---|---|---|
| Incorrect information | Worst | Wrong conclusions survive compaction and actively mislead the agent into repeating mistakes |
| Missing information | Bad | Gaps can be re-discovered by reading files or asking questions; wrong conclusions cannot be easily detected |
| Too much noise | Tolerable | Verbose but at least accurate — the signal is still in there somewhere |
This hierarchy is why naive compaction is dangerous: it converts noise (tolerable) into incorrect information (worst) by summarizing wrong paths as if they were valid work.
The /rewind → /compact Workflow
The fix is simple: curate your context before you compact it.
- Recognize a dead end — the agent went down a wrong path, a fix didn't work, or you realize the approach was flawed
/rewindback to before the mistake — surgically remove the bad tokens from context- Continue from the clean checkpoint — redirect the agent on the correct path
/compactwhen ready — now the summary contains only validated, correct work
This two-step pattern — rewind then compact — ensures your compacted context is high-quality. You're not just saving space; you're curating what the agent remembers.
Serialized External State (Advanced)
For long-running tasks, there's an even more powerful technique: serialize your progress to a file before clearing context entirely.
- Ask the agent to write a
progress.md(or similar) capturing:- The goal and chosen approach
- Completed steps with outcomes
- Current state and next steps
- Any key decisions or constraints discovered
- Run
/clearfor a completely fresh context - Start a new session with
@progress.mdto reload only the curated state
This is the basis of Dex Horthy's Frequent Intentional Compaction (FIC) methodology — rather than letting context grow until it degrades, you proactively serialize state to disk and restart with a clean window. If this sounds familiar, it should: it's exactly what the SDD workflow does at every step, as described in the next section.
The concepts of Intentional Compaction and Frequent Intentional Compaction (FIC) were developed by Dex Horthy at HumanLayer. For a deeper dive, see the Advanced Context Engineering for Coding Agents repository.
SDD as a Context Management Strategy
Here's something that may not be obvious yet: the Spec-Driven Development workflow you set up in the introduction isn't just a development methodology — it's a context management strategy.
Each SDD step serializes state to an external artifact:
| SDD Step | Artifact Created | Context Implication |
|---|---|---|
| Specification | spec.md | Decisions captured in a file — safe to compact |
| Task Breakdown | Task list with proof artifacts | Plan exists outside context — safe to compact |
| Implementation | Code + tests committed to git | Work is saved — safe to compact |
| Validation | Validation report | Results recorded — safe to compact |
Each step creates a natural compaction checkpoint. After completing any SDD step, you can safely compact (or even clear) your context and reload from the artifact. The spec file, task list, or committed code contains everything the agent needs to continue.
This was an intentional design choice. SDD workflows create "save points" — external records of state that free you from depending on conversation history. Treat each SDD step as an opportunity to reset your context.
Best Practices
- Watch your status line — compact proactively around 40–60% utilization, before quality degrades
- Start fresh rather than fighting accumulated context — if the agent seems confused or is ignoring instructions, a
/clearand fresh start is often faster than trying to course-correct - "After two failed corrections, start fresh" — this is Anthropic's own guidance; if the agent isn't responding to feedback, the context is likely too polluted
- Use subagents for investigation — subagents run in their own context windows, keeping your main session clean while exploring tangential questions
- Use Plan Mode to reduce token consumption — Plan Mode limits the agent's ability to make changes, reducing the tokens generated during exploration and planning phases
- Configure PreCompact hooks to preserve critical instructions — hooks can inject must-keep context before compaction runs, ensuring nothing critical is lost
- Break long tasks into phases with explicit context boundaries — don't try to implement an entire feature in one session; use SDD steps or other natural breakpoints to segment work
- Use context markers to detect instruction loss — add a directive to your CLAUDE.md requiring the agent to include a specific emoji (e.g., 🤖) at the start of every response. When the marker disappears, your agent has lost its instructions — time to compact or clear. This technique, popularized by Lada Kesseler, complements the status line: the status line monitors token count while context markers monitor instruction adherence. Liatrio's SDD workflow incorporates context markers as a best practice — check this project's own
CLAUDE.mdfor a working example. For complex workflows, use different markers for different instruction layers so you can see at a glance exactly which instructions were lost.
Treat your context window like RAM, not a hard drive. It's fast, powerful working memory — but it's finite and volatile. Your files, specs, and CLAUDE.md are your "disk storage." Save state to disk (artifacts, commits, CLAUDE.md) and keep your RAM (context) lean and focused on the immediate task.