Video Lesson: Compact conversations in Claude Code

This lesson is being re-recorded

A new video is coming soon. The written lesson below is from the previous version, so some info may be out of date.

Lessons

Compact conversations in Claude Code

Claude Code compact conversations enable long session context management and persistent long running coding tasks.


Lesson Content

When your context window fills up during a long session, Claude Code will automatically compact the conversation. This summarizes what's happened so far, and moves it to the start of a new conversation so you can continue working without losing the context of what you were working on.

How compaction works

Compaction isn't just a simple summary of what you talked about. Claude Code intelligently retains the information that matters, including what files you're working on, the decisions you've made, and the state of your current task, while discarding the back-and-forth that led you there.

Earlier versions of Claude Code would simply summarize the conversation, which sometimes lost important context. The newer approach is smarter, because it preserves the right details so long-running tasks don't lose their thread.

This is part of what makes Claude Code effective for extended sessions. Even if your context window maxes out, it can compact and resume, picking up right where it left off -- effectively working for hours without forgetting what it's doing.

When to compact manually

Claude Code does automatically compact your conversation if you're running low on context space, but there may be times when you'll want to compact a bit earlier.

If you're really deep into a debugging session and notice the context eerily low, and just about to tell Claude to work on something additionally, it may be a good situation to run a manual compact before you hit the limit.

Just run /compact. You can also pass instructions as an argument to this command if you want, with specific context that you'd like retained, such as certain architectural decisions or constraints that you've established.

There's also a more targeted option tucked away in the /rewind menu. Pick a checkpoint and choose Summarize, and Claude Code will compress the conversation up to that point without rolling back any of your code, keeping you in the same session. It works a lot like /compact, but it lets you draw the line yourself, so everything before the checkpoint gets condensed while your recent work stays fully intact.

Preventing early task abandonment

Claude Code is aware of its context limits, and as the context window fills up, it might try to wrap things up early rather than risk running out of space when it's in the middle of a task.

You can override this behavior by adding the following to your system prompt in CLAUDE.md:

Your context window will be automatically compacted as it approaches its limit, allowing you to continue working indefinitely from where you left off. Therefore, do not stop tasks early due to token budget concerns. As you approach your token budget limit, save your current progress and state to memory before the context window refreshes. Always be as persistent and autonomous as possible and complete tasks fully, even if the end of your budget is approaching. Never artificially stop any task early regardless of the context remaining.

This tip is from the official docs, and tells Claude Code to trust the compaction system and keep working. It unlocks more persistent, autonomous execution for long-running tasks rather than bailing out prematurely.

For good measure, always add this paragraph to the bottom of your CLAUDE.md file.