Docs / Guides / Stop blowing your context window

Guides

Stop blowing your context window

Practical habits for keeping Claude Code context lean — what to delegate, when to compact or clear, and how to watch it in real time.

Updated Aug 14, 2026

The problem

Claude Code ’s context window includes instructions, conversation history, file reads, command output, and other tool results. Irrelevant material competes with the code and decisions needed for the current task.

As the window fills, Claude Code automatically compacts the conversation into a summary. Work can continue, but details omitted from that summary are no longer available to the model. Manage context before compaction becomes necessary.

The model has no memory — only context

The underlying model is stateless. Each API call it answers knows nothing beyond what was sent to it in that call. Training happened once, beforehand, and does not update while you work — nothing you type teaches the model anything permanently.

What feels like memory is entirely the agent’s doing, not the model’s. Claude Code re-sends the growing transcript on every turn, runs your tools, and writes durable state to disk: CLAUDE.md, the memory system, skills, and the files it edits. Continuity across a session is an illusion Claude Code maintains by re-feeding context; continuity across sessions only exists for whatever got written down somewhere the next session will read.

This is the reason context management matters at all: there is no other channel. Whatever is not currently in the context window, and not saved to a file that gets read back in, is gone.

Measure it

Run /context (opens in new tab) to see what is using the window. There is no universal 60% danger line. Act when substantial work remains and the current conversation is already carrying old tasks, broad searches, or noisy command output.

/context is a snapshot, taken on demand. For a continuous reading — updated on every reply, showing context left alongside cost, quota, and git state — see Customize the status line .

Delegate bulky investigation

Subagents (opens in new tab) run in separate context windows. Use them for work that requires many reads but only a short result:

  • Search a large tree for a pattern and report the relevant files and lines.
  • Inspect several logs and return the likely cause with supporting excerpts.
  • Review a large diff and return only correctness findings.

Give the subagent a narrow question and require a concise report. Its report still enters the main conversation, and its work still consumes tokens. Splitting small or tightly coupled steps adds coordination without saving useful context.

Let Claude run the play, then check the result

A common instinct, especially coming from a software-engineering background, is to specify every step: open this file, change this line, now run this command. That is how you would brief a junior engineer or write a script — but an agentic tool is not a script executor. It can plan, explore, and course-correct on its own, often better than a step list you write up front.

State the outcome you want, point at a skill if one already covers this kind of task, and let Claude decide the path. Then check the actual result: read the diff, run the tests, look at the output. That is not “review less” — verification still matters — it is “stop pre-deciding the route when what you actually care about is the destination.” Narrating every step both wastes your effort and burns context that could have gone toward the model doing the work.

Move recurring checks outside the conversation

Do not turn one conversation into a permanent polling loop. Put long-lived checks in a script, scheduler, or service that records its own output and alerts only on a state change. Keep the full history in logs; bring a short diagnosis into Claude Code when action is needed.

Keep project instructions lean

CLAUDE.md, auto memory, and unscoped rules consume context at startup. Keep each CLAUDE.md short and limited to facts Claude needs in almost every session: commands, non-obvious constraints, conventions, and recurring pitfalls.

Move conditional instructions into path-scoped .claude/rules/ files or on-demand skills — see below. A rule only saves context if it has paths: frontmatter; rules without one load at startup like CLAUDE.md itself. Moving text into an @ import does not save context either; imported files are expanded at startup. Anthropic’s current guidance (opens in new tab) recommends targeting fewer than 200 lines per CLAUDE.md. Block-level HTML comments (<!-- ... -->) are stripped before injection, so maintainer notes in CLAUDE.md cost nothing.

What skills actually are

A skill is a packaged, named set of instructions for one specific kind of task, stored as a file. By default only its name and a one-line description sit in context — cheap, just an index entry. When a task actually matches, the full instructions load into that turn: complete, specific expertise, on demand, instead of permanent baggage every session carries whether it needs it or not.

This is also what makes clearing a session cheaper than it sounds — see next.

A /clear can be as good as a long session

It is tempting to think a long session is more capable because it has accumulated more. Mostly what it has accumulated is task-specific history: decisions made, files inspected, dead ends explored. General competence is not stored there — it comes from training (fixed) or gets reloaded from CLAUDE.md (always on, cheap) and skills (loaded in full only when relevant, otherwise just an index entry). Clearing does not make Claude less capable.

What clearing loses is anything that exists only in that conversation and nowhere else. If a decision is already reflected in the code, a test, or a skill, a fresh session reconstructs what it needs almost immediately — often faster than continuing to drag a large, partly-stale transcript through another round of compaction. The practical rule is not “never clear,” it is: checkpoint anything load-bearing somewhere durable — a comment, a commit message, an updated skill, a short note — then clear freely.

Bound searches, reads, and output

  • Search by directory, file type, or symbol. Ask for filenames or counts before requesting matching content from a broad sweep.
  • Refer to repository code by path and line number instead of pasting it again.
  • Read the relevant range of a large file, not the entire file.
  • Send full test output and logs to a file. Inspect a summary, grep matches, or a short tail.
  • Remember that shell-mode commands prefixed with ! add their output to the conversation — and by default Claude also responds to that output, which costs the same as a normal prompt (set respondToBashCommands to false to just capture it silently).

For long commands, use Ctrl+B (opens in new tab) to move the command to the background. Background output accrues under a task ID and enters the conversation only when it is read back. Pull in the relevant excerpt; backgrounding saves nothing if you later load the entire log.

Compact with a target, clear when the task changes

/compact (opens in new tab) accepts optional focus instructions:

/compact focus on the auth refactor decisions, changed files, failed tests, and remaining work

A bare /compact uses Claude Code’s default summary. It does not discard material indiscriminately, but no summary preserves every detail — state what the next phase needs.

What compaction does and doesn't touch

Compaction does not touch what lives on disk: the project-root CLAUDE.md is re-read and re-injected after /compact, and skills reload when they next match. Nested CLAUDE.md files and path-scoped rules are not re-injected automatically — they come back the next time Claude reads a matching file. The only thing genuinely at risk is material that exists nowhere but the conversation.

/clear starts a new conversation with empty conversational context while retaining project instructions and memory. The previous conversation remains available through /resume — see Find and resume the right conversation . For a distinct task, use /clear or start Claude Code without a continuation flag — --continue loads the most recent conversation and --resume loads an earlier one, and both inherit that conversation’s full history; neither one starts at 0%.

For a quick side question about the current work, /btw answers without adding the exchange to the main conversation history. It is the inverse of a subagent: it sees your full conversation but has no tools, while a subagent has full tools but starts empty. Use /btw to ask about what Claude already knows from this session; use a subagent to go find out something new.

Model and effort, honestly

It is easy to treat model and effort choice as a dial you must get exactly right, and to second-guess every setting. It mostly is not that sensitive.

Effort controls how much the model reasons before answering — low is faster and cheaper for straightforward work, higher effort digs deeper on hard problems. Levels run low/medium/high/xhigh plus a session-only max, and which ones exist depends on the model; /effort with no argument opens a slider, /effort auto returns to the model’s default. That default is high on every model that supports effort (one exception: Opus 4.7 defaults to xhigh). Running at high is not an unusual or heavy setting — it is the baseline everyone starts from. Use the default for most work, and treat effort changes as a deliberate override, not a per-task decision. Raise it when you actually observe a problem — Claude skipped a file, did not run the tests, or gave up partway through a multi-step task. Lower it for mechanical, low-ambiguity work where you are paying for reasoning you do not need.

Model works the same way: reach for a larger model (Opus, Fable) when a task is genuinely hard — subtle bugs, unfamiliar domains, real architecture decisions — or when a smaller model had all the context it needed and was still confidently wrong. Use a smaller, cheaper model (Sonnet, Haiku) for routine, mechanical work where the extra capability is not buying you anything.

Neither lever is where most of the leverage is. Anthropic’s own model/effort guidance says this explicitly: these are not major levers — the quality of your prompts and the context you give Claude typically matters more than which model or effort tier you picked. If you are not sure, leave the defaults alone and spend the effort improving the instructions and context instead.

Cheat sheet

SymptomFix
Unsure what is filling the windowRun /context; set up a status line for a persistent reading
Large search, log review, or diff inspectionUse a subagent and request a concise report
Explaining every step instead of stating the goalSay what you want, let Claude choose the path, check the result yourself
Noisy tests or long-running commandWrite output to a file; background it and inspect only relevant excerpts
Relevant task, cluttered conversationRun /compact with explicit focus instructions
Starting unrelated work, or worried /clear loses too much/clear — checkpoint anything load-bearing first, then clear freely
Bloated startup contextPrune CLAUDE.md, memory, imports, and unscoped rules; move conditional instructions into a skill
Repeating the same checkMove it to an external scheduler or service
Second-guessing every model/effort choiceLeave the defaults; only change effort when you see a concrete failure, and put the saved effort into better prompts and context instead

Treat the context window as a working set. Keep only what the current task needs.

Where to go next

Source: content/guides/context-optimization.md · maintained in the nuilab-agenticai repository.