Context Window Hygiene skill for Claude Code
Keep long sessions sharp instead of slowly forgetful. Treats the context window as working memory that degrades when stuffed with dead ends and raw logs. Claude checks /context, trims fixed costs like unused MCP servers, scopes every read and command output, keeps the plan and key facts in a file, and picks the right reset: clear for a new task, compact with keep and drop instructions, rewind after a wrong turn, or a fresh session when corrections stop sticking.
- name
context-window-hygiene - size161 lines · ≈1,850 tokens
- files1
- licenseMIT
- updated
What Claude sees before loading it
This description is the only part of the skill that sits in context all the time. Claude reads it to decide when to load the full playbook below.
description: Keeps Claude Code sessions sharp by managing the context window, choosing between /clear, /compact with focus instructions, /rewind, subagents, and a fresh session, scoping file reads and command output, keeping plans and state in files, and trimming always-loaded costs like MCP servers and memory files. Use when a session feels slow or forgetful, Claude repeats mistakes or contradicts earlier decisions, auto-compact is approaching, switching tasks, or the user asks how to manage tokens or context. Try asking
- This session has gotten slow and keeps forgetting what we decided — fix it
- Should I /clear or /compact before starting the next part of this migration?
- Why is my context already a third full before I type anything?
Use it when
- A long session starts repeating mistakes or forgetting constraints
- Auto-compact is approaching during multi-step work
- Switching from one task to an unrelated one
- Fixed overhead from MCP servers or memory files looks large
Skip it when
- You need a note for a different person or a later day — use session-handoff-notes instead
- The work has not been planned yet — use task-decomposition-plans first
How to check the result
After a reset, Claude restates the goal, current step, constraints and next action correctly from the plan file, and /context shows ample headroom.
The common mistake it prevents
Running /clear or letting auto-compact fire before writing the plan, decisions and exact error text to disk, so the details that mattered are gone.
The context window is working memory. Output quality drops as it fills with stale exploration, dead-end attempts, and raw logs, long before it is technically full. A hygienic session holds the current goal, the decisions that constrain it, and the relevant code, and keeps everything else on disk where it can be reloaded on purpose.
Tools
| Command | Effect | Use when |
|---|---|---|
/context | Shows what is consuming the window (system, tools, MCP, memory, messages) | First step whenever something feels off |
/clear | Drops the conversation; starts clean in the same directory | Switching to an unrelated task |
/compact [instructions] | Replaces history with a summary, steered by your instructions | Same task, long history, decisions still matter |
/rewind (or Esc twice) | Restores an earlier checkpoint of the conversation and/or code | A wrong path was taken; undo it instead of arguing past it |
| Subagent | Does the noisy work in its own window; returns a summary | Wide searches, log triage, verbose test runs |
New session (claude) | Fully fresh start | Context feels poisoned; corrections stop sticking |
claude --continue / /resume | Reopen a previous conversation | Picking up work deliberately, not by default |
Auto-compact triggers near the limit. Do not let it pick the moment during delicate work; compact or reset yourself at a natural checkpoint.
Decision rules
- New, unrelated task ->
/clear. Carrying the previous task’s files and decisions only adds noise. - Same task, history long, approach settled -> write state to a file, then
/compactwith explicit keep/drop instructions. - Last few turns went the wrong way ->
/rewindto before the detour. A conversation containing three failed approaches keeps steering toward them. - Two corrections in a row did not stick, or Claude contradicts a decision made earlier -> the context is polluted. Write the correct state to a file and start a fresh session from it.
- Need to explore something large -> delegate to a subagent (built-in
Explorefor codebase search) and keep only its summary. - Approaching auto-compact in the middle of a multi-step change -> finish the current atomic step, update the plan file, then compact.
Workflow for long tasks
- [ ] 1. Check /context at the start; trim fixed costs if large
- [ ] 2. Put the plan in a file (docs/plans/<task>.md or .claude/plans/)
- [ ] 3. Work in steps; after each step, update the plan's status lines
- [ ] 4. Scope every read and command output
- [ ] 5. At checkpoints: compact with instructions, or clear and reload the plan
- [ ] 6. At the end: record outcomes in the plan or a handoff note
1. Trim fixed costs
Everything listed before your first message is paid on every turn:
- MCP servers: each server’s tool definitions sit in context. Disable servers not needed
for this repo (
/mcp, or remove them from project config). - Memory files: root CLAUDE.md should be short (see
memory-file-architect); large@importsload eagerly. - Skills: only names and descriptions load up front, but dozens of long descriptions add up.
4. Scope reads and output
| Instead of | Do |
|---|---|
| Reading a 3,000-line file | grep -n for the symbol, then read the relevant line range |
| Reading every file in a directory | List it, read the 2-3 that matter |
| Full test suite output | Run the affected tests; quiet reporters; failures only (pytest -q -x, vitest run path --reporter=dot) |
cat of a log | tail -n 100, grep -n ERROR, or jq filters |
| Printing a large JSON API response | `jq ‘.items[] |
| Pasting a whole stack trace from 5 services | The first error and the deepest frame in project code |
| Reading lockfiles, build output, minified bundles, snapshots | Do not; query them with grep if needed |
Delegate anything whose raw output is large and whose conclusion is small.
5. Compact with instructions
The default summary keeps generalities and loses specifics. Tell it what matters:
/compact Keep: goal (migrate billing to Stripe Payment Intents), decisions (no
backfill; webhook-first), files changed so far, the exact failing test names and
error text, next step (step 5 in docs/plans/billing.md). Drop: exploration of the
legacy PayPal module and the abandoned queue approach.
A CLAUDE.md line such as “When compacting, always preserve the current plan file path, modified files, and failing test names” applies to every compaction, including the automatic one.
Clear-and-reload alternative (often cleaner than compacting):
/clear
Read docs/plans/billing.md. Steps 1-4 are done and committed. Continue with step 5.
Plan file shape
# Billing migration plan
Goal: replace Charges API with Payment Intents; no customer-visible change.
Decisions: webhook-first; idempotency keys per order; no backfill of old charges.
## Steps
- [x] 1. Add PaymentIntent service wrapper (commit a1b2c3)
- [x] 2. Webhook handler for payment_intent.succeeded (commit d4e5f6)
- [ ] 3. Switch checkout to PaymentIntents behind flag `pi_checkout` <- current
- [ ] 4. Remove Charges code paths
## Facts to keep
- Test webhook secret is in 1Password "stripe-test"; never in code
- `pnpm test:billing` runs the relevant suite in ~40 s
Symptoms of a degraded session
- Re-reading files it already read, or asking for information given earlier.
- Reintroducing a bug that was fixed, or an approach that was rejected.
- Ignoring a constraint stated early in the session.
- Long, meandering answers; slower responses.
- Fixes that oscillate between two wrong versions.
Any two of these: stop, write state to disk, and reset.
Gotchas
/cleardiscards everything not written to disk. Save the plan, decisions, and exact error strings first.- Compaction loses precise details (paths, line numbers, error messages, command flags) unless told to keep them.
- Pasting huge blobs (full logs, whole files, screenshots of text) consumes more than reading a targeted slice with a tool.
- Subagents that return raw dumps move the bloat back into the main thread. Cap their
output (see
subagent-design). - Habitually resuming yesterday’s session drags in stale context. Start fresh from a handoff note unless the old conversation is genuinely needed.
- Arguing with a wrong approach for several turns leaves every failed attempt in context. Rewind instead.
Verification
/contextafter trimming and after each reset shows messages, not fixed overhead, as the main consumer, and plenty of headroom.- After a
/clearor compaction, Claude can state the goal, the current step, the constraints, and the next action correctly from the plan file alone. - The same class of mistake does not recur after the reset.
Output
When performing a reset for the user, leave this checkpoint (in the plan file or reply):
**Checkpoint:** before /clear at step 3 of docs/plans/billing.md
**Saved to disk:** plan status updated; failing test `checkout uses payment intent` + error text recorded
**Fixed costs trimmed:** disabled 2 unused MCP servers (-11k tokens); CLAUDE.md imports unchanged
**Resume with:** "Read docs/plans/billing.md and continue with step 3." About this skill
Written and maintained by the Claude Skills editors to the same editorial standard as every skill in the directory: a selective trigger, a real workflow, known pitfalls and a verification step. First published , last updated . MIT licensed.