Context Hygiene: Mastering /clear and /compact in Claude Code
Every message you send to Claude Code carries a hidden passenger: the entire conversation that came before it. The model is stateless, so the client re-sends the full transcript on every turn. That means a context window you never tidy isn't just messy — it's a recurring line item on your bill. This first lesson is about the cheapest optimization there is: throwing away tokens you no longer need.
The Challenge
Picture a two-hour session. You start by debugging an auth middleware, drift into a database migration, then end up styling a React component. By message fifty, the model is still re-reading the stack trace from message three on every single request. None of that auth context helps with CSS — but you pay for it anyway, token-for-token, turn after turn.
This is context bloat, and it compounds in three ways:
- Cost. Input tokens scale with transcript length. A conversation that has grown to 120K tokens costs roughly 10× more per message than the same task started clean at 12K.
- Latency. More input tokens means more time to first token. Bloated context feels sluggish.
- Quality. Irrelevant history dilutes attention. The model is more likely to confuse the migration schema with the auth flow when both are crammed in the window.
The instinct is to keep one long-running session because "it already knows everything." That instinct is what's draining your budget.
The Solution
Claude Code ships two commands for managing the window. They solve different problems — using the wrong one wastes the technique.
/clear — the hard reset
/clear wipes the conversation history entirely. The next message starts from an empty window (your CLAUDE.md and project context still load — only the chat transcript is dropped). Use it at task boundaries: the moment you finish one unit of work and start something unrelated, clear.
# Finished the auth fix. The migration has nothing to do with it.
/clear
# Fresh window. Next prompt is cheap again.
> Now help me write the users-table migration.The rule of thumb: one task, one context. If you can't explain why the previous discussion helps the current task, clear it.
/compact — the lossy save
Sometimes you do need continuity — you're deep into a feature, the window is filling, but the history still matters. /compact asks the model to summarize the conversation so far into a dense recap, then replaces the raw transcript with that summary. You keep the thread of the work at a fraction of the token cost.
# 90K tokens deep in a refactor, still going. Don't lose the plot —
# just shrink it.
/compact Keep the new repository interface and the files still left to migrate.Passing focus instructions after /compact is the move most people miss. Left to its own devices the summary is generic; tell it what to preserve and the recap stays surgical. Everything you don't name is fair game to drop.
/clearwhen the past is irrelevant./compactwhen the past is relevant but bulky. Reaching for compact at a real task boundary just pays a summarization tax to keep junk.
Pro-Tip
Don't wait for auto-compact — it's the most expensive compaction you can run. Claude Code triggers an automatic compaction when you approach the context limit. By definition that fires when the window is at its fullest, so the summarization pass itself reads a maximal transcript — you pay top dollar for the one compaction you didn't choose, at a moment you didn't pick.
Experienced users compact proactively, on their own cadence: at natural checkpoints (a passing test suite, a committed change), while the window is still half-empty and the summary is cheap to produce. Pair that with a discipline of clearing between tasks and you almost never hit the auto-trigger at all.
One caveat that pays for itself: if you rely on prompt caching, remember that editing or summarizing the early part of the conversation invalidates the cached prefix. Compact at boundaries where you were going to break the cache anyway (a new sub-task, a fresh file set) — not in the middle of a tight cached loop.
Metrics
A representative three-task session — auth fix, migration, component styling — run two ways. "Before" is one unbroken context; "After" clears at each task boundary. Numbers are illustrative but the ratio is the point.
Technique | Input tokens / final msg | Session input total
-------------------+--------------------------+--------------------
One long context | ~118,000 | ~2,400,000
/clear per task | ~14,000 | ~430,000
-------------------+--------------------------+--------------------
Reduction | ~88% | ~82%Roughly an 8× cut on the per-message cost of the final task, and over 80% off the session's total input spend — from one command, typed at the right moments. No model downgrade, no quality trade-off. Just refusing to re-send tokens that stopped being useful.
Context hygiene is the foundation the rest of this series builds on. Next up: the full roadmap — including the token budget mental model that makes the meter legible before you spend.