Mastering Claude Code Context Management: /clear and /compact

If you've ever noticed Claude giving repetitive answers or misreading your intent after a long session, you've already felt the context window closing in. This guide is for developers who use Claude Code daily and want to stop losing response quality to token bloat — without resetting their entire workflow every time.

1. Why this matters now

The context window is the invisible ceiling of every AI-assisted coding session. Claude doesn't have a running memory — it reads the entire conversation from top to bottom every single time it responds. As your session grows, so does that reading cost, and eventually the model starts dropping important details just to fit everything in.

In Claude Code, the terminal header gives you a live readout:

● Context: 47,832 / 200,000 tokens (23%)

That number looks comfortable at 23%, but after a few hours of refactoring, reading files, and iterating on code, you'll hit 70–80% faster than you expect. Past that threshold, response quality degrades noticeably — Claude starts second-guessing earlier decisions, repeating suggestions it already made, or missing recently established constraints.

The real pain isn't just slow responses. It's wrong responses that look confident. You iterate based on bad output, dig deeper into a flawed solution, and only realize something is off 20 minutes later. Getting ahead of context overflow prevents that entire class of problem.

2. The core idea

Two commands cover the full spectrum of context management in Claude Code: /clear for a clean break, and /compact for a compressed continuation. Choosing between them comes down to one question — does the rest of your session depend on what happened before?

Situation Best command
Switching to a completely different task /clear
Continuing the same refactor or debug session /compact
Long uninterrupted session you want to autopilot autoCompact setting

Think of it like working in a notebook. /clear tears out all the pages and starts fresh. /compact takes everything you've written, distills it into a one-page summary, and glues that to the front — then discards the rest.

The summary approach works because most of what fills your context window mid-session is scaffolding: exploratory messages, partial attempts, back-and-forth clarification. The actual decisions and constraints fit in a fraction of that space.

3. How to implement it

Using /clear

Run this when you're done with a task and moving on:

/clear

That's it. The conversation history is gone, context drops to zero, and Claude starts the next session with no baggage. The cost is total — every decision, every agreed-upon constraint, every file path you discussed is wiped.

What to do before you run it: if there's anything you'll need again, write it into your CLAUDE.md first (more on that below). Don't rely on re-explaining things from memory at the start of the next session.

Using /compact

When you need to keep going but the context is getting heavy:

/compact

Claude summarizes the entire conversation — decisions made, files changed, open questions — and replaces the full history with that compressed version. You keep working from where you left off, but the token cost resets:

● Context: 8,200 / 200,000 tokens (4%)
  ↑ Compacted from 142,000 tokens

The default summary covers most cases, but you can tell Claude exactly what must survive the compression:

/compact include the list of files modified so far and the reason for each function change

This is useful during a long refactor where the rationale behind specific decisions matters — the kind of thing that's easy to compress away but painful to reconstruct later.

Enabling auto-compact

If you'd rather not think about this at all during deep work sessions, add this to ~/.claude/settings.json:

{
  "autoCompact": true,
  "autoCompactThreshold": 0.75
}

autoCompactThreshold sets the trigger point. At 0.75, Claude Code automatically runs /compact the moment your context hits 75%. You stay in flow, no manual intervention needed.

Keeping project context alive through resets

Here's the gotcha I hit early on: after running /clear, I kept spending the first five minutes of every new session re-explaining the project structure. The fix is a CLAUDE.md file at the project root.

# Project Overview
Next.js 14 e-commerce app. Uses App Router throughout.

# Code Conventions
- TypeScript strict mode everywhere
- Components live in src/components/
- API routes live in src/app/api/

# Active Work
- Refactoring payment module from Stripe to Toss Payments

Claude Code reads this file automatically at the start of every session. Run /clear as aggressively as you want — the project context is always loaded fresh. This turns CLAUDE.md into persistent memory that survives every reset.

Keep it current. Stale CLAUDE.md content is worse than no content, because Claude will confidently operate on wrong assumptions.

A realistic workflow

# Start your session
$ claude

# Context climbs to 70%+ during a long refactor
/compact include current file modifications and remaining TODOs

# Task complete, switching to something unrelated
/clear

# New task — CLAUDE.md reloads project context automatically
> Help me add the new checkout flow

4. What to watch in production

Don't wait until context is critical. Running /compact at 70% is proactive. Running it at 95% means Claude is already struggling to summarize coherently — the summary quality degrades when there's too much noise to compress cleanly.

Verify the compact summary before continuing. After running /compact, ask Claude to confirm what it retained: What do you know about the changes made so far? If something critical is missing, correct it before you go further. One follow-up question here saves you from building on an incomplete foundation.

autoCompact doesn't know your session boundaries. It will compact mid-refactor, mid-debug, even mid-explanation. That's fine for most cases, but if you're in the middle of reasoning through a complex problem, the compression can lose the thread. Consider keeping autoCompact off during particularly nuanced sessions and running /compact manually with instructions.

CLAUDE.md scope matters. The file in the project root is project-wide. If you're working in a monorepo and different subdirectories have different conventions, you can place a CLAUDE.md in each subdirectory. Claude Code picks up the nearest one relative to where you're working.

On token cost: /compact does use tokens to generate the summary — it's a real model call. In most sessions this is negligible, but if you're watching API costs carefully and running many short sessions, /clear is the zero-cost option.


Context management is one of those Claude Code habits that separates a smooth 4-hour session from one that degrades into frustration by hour two. /clear and /compact are both single commands — the hard part is building the reflex to use them at the right time rather than pushing through a context that's already overloaded.

Next: once you've got context management dialed in, the logical next step is structuring your CLAUDE.md to act as a proper project spec — not just a note, but a source of truth that shapes how Claude approaches every task in that repo.


🐦 Faster updates on X: @baegseungh7061
📚 More in this series: Code Intro
💌 Subscribe: Follow on X or grab the RSS

댓글