codingBy HowDoIUseAI Team

What Does /compact Do in Claude Code? (Context Management Explained)

The /compact command compresses your conversation history into a summary, freeing up tokens without losing context. Here's when and how to use it.

Long coding sessions eat tokens fast. Eventually Claude Code starts losing track of early decisions, repeating itself, or hitting context limits. The /compact command fixes this by compressing your conversation history into a focused summary — keeping what matters, dropping what doesn't.

How does /compact work?

When you type /compact, Claude Code takes your entire conversation history and summarizes it. The full back-and-forth gets replaced with a condensed version that preserves key decisions, code context, and important details. Your session continues with this summary as the new starting point.

Think of it like Bubble's "Page Loaded" workflow — when compact runs, it resets the conversation state but carries forward the essential data, like passing a serialized object to a new page instead of recreating everything from scratch.

When should you run /compact?

The best time to compact is at around 60% context utilization — before quality starts degrading. Most people wait until they see warnings at 80–95%, but by then Claude is already struggling to track details from early in the session.

Signs you need to compact:

  • Claude repeats work it already did earlier
  • Responses feel slower or less focused
  • You get context window warnings
  • You've been going for 30+ minutes on a complex task

Claude Code also has automatic compaction — it triggers when usage hits approximately 83.5% of the context window (~167K tokens on a 200K window). But running it manually at 60% gives better results because the summary is higher quality when there's less to compress.

Can you control what /compact preserves?

Yes. You can pass custom instructions to tell Claude what to prioritize during summarization:

/compact Focus on the database schema changes and API endpoints we discussed
/compact Preserve all code snippets and variable names

Without instructions, Claude uses its best judgment about what's important. Adding instructions is especially useful when you know you'll need specific details later — exact error messages, function signatures, or architectural decisions.

What do you lose when you compact?

The trade-off is precision. Compaction summarizes, and summaries lose detail:

  • Exact error messages get paraphrased
  • Specific variable names might get generalized
  • Nuanced back-and-forth discussions get condensed to conclusions
  • Early decisions lose their reasoning context

This is different from /clear, which wipes everything. /compact is lossy compression — you keep the gist but lose some fidelity.

How does /compact affect costs?

Compaction directly reduces token usage. After compacting, every subsequent message sends the shorter summary instead of the full history, which means:

  • Lower per-message costs (fewer input tokens)
  • Faster responses (less context to process)
  • More room for new work before hitting limits again

For a session that's used 150K tokens of context, compacting might reduce it to 20–30K tokens — an 80%+ reduction that lets you keep working for another hour.

/compact vs /clear vs /btw — which one when?

CommandWhat it doesUse when
/compactSummarizes history, keeps contextLong sessions, approaching limits
/clearWipes everything, fresh startSwitching tasks entirely
/btwSide question, no history impactQuick one-off questions

Use /compact when you want to keep working on the same task but need to free up space. Use /clear when you're done with one task and starting something completely different. Use /btw when you just need a quick answer without affecting anything.