Lesson 6
Context Management Commands
AI-generated
Context Management Commands
Explain why context management matters for Claude Code performance
Use /clear to reset between unrelated tasks
Use /compact to compress context while preserving important information
Use /rewind to restore previous conversation and code states
Recognize signs that your context is getting full
The context window is Claude's working memory. It holds everything from your current session: your messages, Claude's responses, files Claude has read, and output from commands. This window has a limit. When it fills up, Claude's performance degrades. Responses become slower, less accurate, and sometimes Claude forgets earlier instructions.
Managing context is one of the most important skills for using Claude Code effectively. The commands in this lesson help you keep context clean and Claude performing at its best. You will learn when to clear, when to compact, and how to rewind when things go wrong.
Think of context like a desk. If you pile papers on it all day without organizing, eventually you cannot find anything. These commands help you keep the desk tidy.
Before we look at the commands, let us understand what we are managing.
The context window holds:
Your messages to Claude
Claude's responses
Files Claude has read
Output from terminal commands
Tool results and intermediate steps
Every piece of information adds to the context. A single debugging session might add thousands of tokens as Claude reads files, runs commands, and traces through code.
Claude Code shows context usage in the status line (if configured) or through the /cost command. You might see something like "Context: 45% used" or a token count. As this number grows, pay attention.
Signs your context is getting full:
Claude responds more slowly
Claude seems to forget things you mentioned earlier
Responses become less precise or more generic
You see a warning about context limits
When this happens, it is time to use one of the context management commands.
The /clear command is the simplest and most powerful context tool. It wipes everything and gives you a fresh start.
After running this, your conversation history is gone. Context usage drops to nearly zero. Claude Code is ready for a new task as if you just started.
When to use /clear:
When switching to an unrelated task
After finishing a task and before starting something new
When Claude seems confused or stuck
When you see context warnings
A common mistake is treating one Claude Code session like a continuous workday. You ask about authentication, then ask about the database, then ask about the frontend. Context fills with information about all three topics, even though you only care about the current one.
The better pattern: finish a task, run /clear, start the next task with full context available.
/clear has two aliases that do the same thing: /reset and /new. Use whichever feels natural.
Sometimes you do not want to lose everything. You are mid-task and need to free up space, but you want Claude to remember key decisions or code patterns. That is what /compact does.
When you run this, Claude Code summarizes the conversation. Important information is preserved in a compressed form. Less important details are dropped. Context usage decreases while continuity is maintained.
You can add instructions to focus the summary:
These instructions tell Claude what matters most. The summary emphasizes what you specified.
When to use /compact:
Mid-task when context is growing but you need continuity
Before a complex operation that will read many files
When you want to preserve decisions but drop exploration history
The tradeoff: compaction is not perfect. Some nuance may be lost. If Claude later seems confused about something that happened before compaction, you might need to remind it.
Auto-compaction: When you approach context limits, Claude Code automatically compacts to make room. This uses default behavior. If you want more control, run /compact manually with specific instructions before auto-compaction kicks in.
The /rewind command is like an undo button for your entire session. Every action Claude takes creates a checkpoint. You can go back to any of these checkpoints.
This opens an interactive menu showing checkpoints. You can:
Restore conversation only (keep code changes)
Restore code only (keep conversation)
Restore both conversation and code
Summarize from a selected message
Press Escape twice as a shortcut to open the rewind menu.
When to use /rewind:
Claude made changes you do not want
You went down a wrong path and want to back up
You want to try a different approach without losing the original
A change broke something and you need the previous state
This is powerful for experimentation. You can tell Claude to try something risky, knowing that if it fails, you can rewind and try differently.
Important: Checkpoints only track changes Claude makes. If you edit files yourself outside of Claude Code, those changes are not checkpointed. /rewind is not a replacement for Git.
Two commands help you understand what is in your context.
`/cost` shows token usage:
You see how many tokens the session has used, broken down by category. This helps you understand if context is becoming a problem.
`/context` shows a visual representation:
This displays a colored grid showing context usage. It also suggests optimizations if Claude detects potential issues, like context-heavy tools or memory bloat.
Check these commands periodically during long sessions. They help you decide when to clear or compact.
Here are patterns that work well:
Clear between unrelated tasks. If you finish working on the login feature and switch to the payment system, run /clear. The login context does not help with payments.
Compact mid-task. If you are deep in debugging and context is growing, run /compact Focus on the bug we found in UserService. This keeps relevant information while freeing space.
Rewind instead of arguing. If Claude made bad changes and you find yourself explaining why they are wrong, it is often faster to /rewind and rephrase your original request.
Name sessions before clearing. If you might want to return to a conversation, run /rename auth-investigation before /clear. The named session is saved and you can resume it later.
Start fresh when confused. If Claude seems lost or keeps making the same mistake, /clear often helps more than continued explanation. A fresh start with a better-phrased prompt beats fighting with polluted context.
The context window holds your conversation, files Claude read, and command output
Performance degrades as context fills up
/clear wipes everything for a fresh start; use between unrelated tasks
/compact summarizes while preserving important information; use mid-task
/rewind restores previous states; use when Claude made unwanted changes
Check /cost and /context periodically to understand context usage
When in doubt, /clear and start fresh with a better prompt
Practice context management with these steps:
Start Claude Code in a project and ask several questions to build up context.
Run /cost to see your token usage.
Run /context to see the visual representation.
Run /compact Focus on the most recent question and observe how context changes.
Ask Claude to make a small change to a file.
Run /rewind and explore the checkpoint options. Restore the code to before the change.
Run /clear to reset everything.
Run /cost again and notice how it dropped to nearly zero.
This exercise builds intuition for when each command is appropriate.
https://code.claude.com/docs/en/best-practices - Context management patterns and when to clear
https://code.claude.com/docs/en/checkpointing - How /rewind and checkpoints work
https://code.claude.com/docs/en/context-window - Understanding what fills the context window