Context Compaction in Claude Code: What It Is, How to Use It, and When to Avoid It

Jun 22, 2026

A practical guide to managing long sessions in Claude Code without losing the context that matters.


01 — What it is

What is context compaction?

Every AI model has a context window — a maximum amount of text it can hold in memory at once. In long working sessions, that window fills up fast. Context compaction is Claude Code's built-in solution: it summarizes your conversation history into a compact prose summary, then carries that summary forward so the model can continue working without losing the thread of what you were doing.

Think of it as a smart handoff note — it preserves the decisions made and the direction you're heading, without dragging along every line of dialogue that led there.

Key distinction

Context compaction produces a prose summary, not a JSON snapshot. It captures meaning and intent, not raw data structures.


02 — How to use it

How does context compaction work?

In Claude Code, run the /compact command to trigger compaction. Claude will summarize the conversation history up to that point. For best results, include a short instruction telling it what to preserve — the core decisions, architectural choices, open problems — and what it can safely discard.

Example

/compact — Focus on the API design decisions we settled on, the file structure we agreed on, and the two open bugs. Skip the debugging back-and-forth we already resolved.

When you start a new session, Claude automatically reads the compacted summary from the previous one. You don't need any special command — just continue prompting as normal. The model picks up the summary on its own.


03 — The tradeoffs

The risks of over-relying on it

Context compaction is not a free pass to run a session indefinitely, and understanding why requires separating two distinct failure modes. The first is context pollution: when a single session spans too many unrelated requests, the resulting summary ends up blending those threads together. Claude can no longer tell what decision belonged to which task, and its outputs start to reflect that confusion. The second failure mode is summary overload, which happens when you compact repeatedly across multiple sessions. Each new session inherits a denser history than the last, and over time the accumulated noise makes it harder for the model to surface the right context at the right moment.

Watch out

Avoid using /compact repeatedly within the same sprawling session as a shortcut. If your session has become too broad, the better fix is to start a focused new session — not to compact your way through the noise.


04 — Best practices

When and how to use it well ?

Context compaction works best when it's used intentionally, not reflexively. The right moment to reach for /compact is when your session is approaching its context limit and there's real, meaningful work worth preserving — a set of architectural decisions, a bug you've narrowed down, a direction you've aligned on. It's equally useful as a deliberate checkpoint before you branch into a new phase of work, letting you carry forward only what's relevant and leave the rest behind.

What it can't do is rescue a session that's already scattered. If your context window is full of mixed, unrelated requests, compacting won't clean that up — it'll just compress the mess into a smaller mess. In those cases, the right call is /clear, which wipes the session entirely and gives you a clean slate. Use compaction to preserve good work; use clear to escape bad sessions.