Skip to content

Chapter 3 of 205 min readBy Deepak GuptaPublished

Context Window Mastery

Nearly every Claude Code best practice traces back to one constraint: the context window fills up, and output quality drops as it does. Engineers who internalize this get consistent results. Engineers who ignore it blame the model.

What fills the window

The context window is everything Claude can see at once. In a session it holds:

  • The system instructions and tool definitions
  • Every CLAUDE.md file that loaded at startup
  • Descriptions of available skills, subagents, and MCP tools
  • Every message you sent and every reply
  • Every file Claude read, in full or in part
  • Every command output, test log, and error trace

The last three grow fastest. One verbose test run can consume more context than an hour of conversation. Run /context at any time to see what is taking the space.

Why quality degrades

As the window fills, three things happen. Early instructions get less attention relative to recent material. Stale information, such as a file Claude read before you changed it, competes with current information. Failed attempts sit in context and quietly bias the next attempt toward the same mistake.

The practical rule from experienced users: on serious work, keep the window under roughly 30 to 40 percent full. Above that, reset or compact before starting anything important.

The four commands

Command What it does Use it when
/clear Wipes the conversation; CLAUDE.md reloads You switch to an unrelated task
/compact Summarizes the conversation and continues The task is long but on track
/rewind (or Esc, Esc) Jumps back to an earlier message and can restore code An attempt went sideways
/context Shows what is consuming the window Responses feel slower or vaguer

You can steer compaction with instructions, for example /compact keep the API contract decisions and the failing test names. A generic compaction keeps generic things.

Rewind beats correction

This is the habit with the biggest payoff in this chapter. When Claude takes a wrong turn, most people type a correction: "No, don't do it that way, use the repository pattern instead." The failed attempt and the correction now both sit in context, and the model is reasoning around its own mistake.

Instead, rewind to the message before the wrong turn and resend a sharper version of your original request. The bad attempt disappears. The next try starts clean, and it usually lands. One limit: rewind restores files Claude changed with its edit tools, not files changed only by shell commands, so keep git as your real safety net.

Warning

Pitfall: Correcting the same error three times in a row. If two corrections have not worked, the context is poisoned. Rewind or clear, then restate the task with what you learned.

Decision rubric

  1. New, unrelated task? /clear.
  2. Same task, going well, window over a third full? /compact with guidance on what to keep.
  3. Same task, last answer was wrong? Rewind to before it, rephrase, retry.
  4. Wrong twice after rewinding? /clear, write a short spec in a file, and start a fresh session that reads the spec.
  5. Session from yesterday you need again? claude --resume and pick it.

The handoff file

For work that spans several sessions, keep a plain markdown file such as docs/plans/feature-x.md. Ask Claude to update it at the end of each session with decisions made, current status, and open questions. The next session starts by reading that file instead of replaying yesterday's conversation.

This is the single best tool for long features. A two-hundred-line plan file costs a fraction of the tokens of the conversation that produced it, and it is far more precise.

Isolation as context management

Two features exist largely to protect your context.

  • Subagents run in their own windows and return only a summary. A subagent can read forty files to answer "where is the session timeout configured?" and hand back three lines. Chapter 6 covers them.
  • Git worktrees let you run separate sessions on separate checkouts at the same time, each with its own clean context. Chapter 12 covers parallel work.

Common context leaks

  • Pasting an entire 5,000-line log instead of the failing section
  • Letting Claude read a huge generated file, lockfile, or minified bundle
  • Hooks that print verbose output after every edit
  • Asking for full-file rewrites when a targeted edit would do
  • Keeping one session open all day across unrelated tasks

Add large generated files to your deny list or mention them in CLAUDE.md as files to never read.

Tip

What to do this week

  • Run /context at the start and middle of three sessions and note the biggest consumers.
  • For the next five wrong turns, rewind instead of correcting.
  • Start one plan file for a multi-day task and have Claude maintain it.

Key takeaways

  • Output quality drops as the context window fills; check it with /context.
  • Rewind to before a wrong turn instead of stacking corrections.
  • Use /clear between unrelated tasks and steer /compact with what to keep.
  • Carry multi-day work in a plan file, not in a long-running conversation.