A coding agent fills its context window fast, and quality drops long before the window is full. These habits keep a session lean. Most of them also keep the prompt cache warm, which makes turns faster and cheaper.

When to use it

All the time, but especially when a session feels slower or “dumber” than at the start, when the agent forgets something you agreed on, or before you start a big task in a session that has already done a lot.

How to do it

  1. Look before you guess. Run /context to see what is using space, by category, with suggestions. To keep the number on screen all the time, set up a status line with the context percentage.
  2. Clear between unrelated tasks. /clear starts a new conversation with empty context. A backend bug and a form layout are two sessions, not one. See One task per session.
  3. Compact on purpose, at a break. /compact focus on the auth bug summarizes the conversation and keeps what you name. Do it between tasks instead of waiting for auto-compaction to hit mid-task. Compaction rewrites history, so it also costs one cache rebuild.
  4. Rewind instead of arguing. If the agent went the wrong way, /rewind (or Esc twice) goes back to an earlier point in the conversation and/or code. That beats half a session of “no, do it differently”, and it reuses the cache.
  5. Write specific prompts. “Fix the null check in src/auth.ts” makes the agent read one file; “find where we handle auth” makes it read ten.
  6. Keep lasting rules in CLAUDE.md. After compaction, the project-root CLAUDE.md is re-injected from disk, while rules you typed in chat may be summarized away. Remember that edits to CLAUDE.md apply only after /clear, /compact or a restart.
  7. Hand big research to a subagent. A subagent reads files in its own context window and sends back only a summary, so the reads never land in yours: “use a subagent to find every place we call refreshToken()”.
  8. Pick the model at the start. Switching models with /model mid-session throws away the cache.

Example

A session that fixed a login bug is now 60% full. You want to add a settings page next.

/context                      # mostly file reads from the login fix
/clear                        # new task, new context
Add a "notifications" toggle to src/pages/Settings.tsx, following the
pattern of the existing "dark mode" toggle in the same file. Run npm test.

If instead you want to continue on the login code with less noise:

/compact keep the final fix in src/auth.ts and the failing test name, drop the logs

Common mistakes

  • Waiting for auto-compaction. It fires when the window is nearly full, often mid-task, and you do not choose what it keeps.
  • Correcting over and over in the same session instead of rewinding or starting fresh with a better prompt.
  • Vague exploration prompts that make the agent read half the repo.
  • Editing CLAUDE.md and expecting the running session to see it.

Citations

  1. Best practices for Claude Code: manage your session
  2. Explore the context window
  3. How Claude Code uses prompt caching
  4. Commands
  5. How Claude Code works