Try free

Last updated 13 October 2026

Compare /compact and /clear in Claude Code

Context management prevents session decay

Your experience with Claude Code usually starts with high speed and precise answers. You run the claude command in your project folder and the agent immediately understands your file structure using the Glob and Read tools. However, as your session grows, the context window fills with every file the agent has read, every Bash command output, and every line of code it has modified. You might notice the agent starts to repeat itself or takes longer to process a simple request. This bloat happens because the agent is trying to keep the entire history of the session in its immediate memory.

When the context window is near its limit, the agent has to work harder to find relevant information among the hundreds of lines of previous conversation. This leads to higher token usage and increased latency. You might see the agent mention that it is reaching a limit, or you might simply feel the degradation in quality. Managing this context is a core skill for a developer using a command line agent. You have two primary tools to handle this within a session: the compact command and the clear command. Knowing which one to choose depends on whether you want to preserve the current state or start from a clean slate.

Effective token management is not just about saving money, though the /cost command will show you that long sessions are expensive. It is about maintaining the reasoning capabilities of the agent. A cluttered context window can lead the agent to hallucinate or ignore specific project instructions you placed in your CLAUDE.md file. By proactively using slash commands to manage history, you keep the agent sharp and ensure its Edit and Write operations remain accurate throughout your workday.

Summarizing history with the compact command

The /compact command is your first line of defense against a bloated context window. When you run this command, the agent looks back over the entire conversation. It identifies the key actions taken, such as which files were edited and what the final state of the code is. It then generates a summary of the session so far and replaces the long, verbose history with this condensed version. This frees up space in the context window while allowing the agent to remember what it has already accomplished in the current folder.

This command is particularly useful during long refactoring tasks. If you have been moving functions between several files using the Read and Edit tools, the history is full of intermediate steps that are no longer relevant. By using /compact, you keep the knowledge of the new architecture but discard the step by step logs of how you got there. This maintains the thread of the project without the overhead of thousands of lines of previous tool outputs. You can even observe the PreCompact hook event if you have configured hooks in your .claude/settings.json file to trigger specific actions when a summary occurs.

Keep in mind that while /compact saves space, some fine details from early in the conversation might be lost in the summary. If you had a very specific discussion about a edge case that is not reflected in the code itself, the agent might not recall the nuances after a summary. However, for most development workflows, this is an acceptable trade-off to keep the session running fast. You should use /compact when you are still working on the same high level task but feel the agent is becoming sluggish or the token count is getting too high for comfort.

Resetting the state with the clear command

The /clear command is a more drastic measure compared to a summary. When you type /clear inside a session, you are telling Claude Code to forget everything that has happened since you started the current session. This completely empties the context window. It does not delete your files or undo any changes made by the Edit tool, but it removes the agent's memory of the previous conversation. It is effectively a fresh start within the same working directory, saving you from having to quit the CLI and restart it.

You should use /clear when you are pivoting to a completely different task. For example, if you just finished debugging a memory leak in the backend and now you want to work on a CSS alignment issue in the frontend, the agent does not need the context of the memory leak. In fact, keeping that old context might confuse the agent, leading it to suggest backend changes for a frontend problem. A quick /clear ensures that the next prompt you submit is processed with a 100 percent fresh context window, which is the most efficient way to work for distinct, unrelated tasks.

Another benefit of /clear is the immediate reduction in cost for subsequent prompts. Since the history is gone, the input tokens for your next request will only include your new prompt and any files the agent needs to read from scratch. You can verify this by running the /usage command before and after you clear the session. While /compact reduces tokens significantly, /clear brings them down to the absolute minimum required for the next operation. This makes it the preferred tool for developers who work in short, intense bursts on separate features.

Using project instructions to maintain continuity

One concern when using the /clear command is losing the project specific rules you have established. This is where the CLAUDE.md file becomes essential. Located in your repo root, this file serves as the permanent memory for the agent. Even after you run /clear, the agent will re-read CLAUDE.md when it starts its next task. You should use this file to store your build commands, testing patterns, and code style preferences. This ensures that the agent always knows how to run your project, regardless of how many times you reset the session history.

If you have instructions that only apply to a specific part of your tree, you can place a CLAUDE.md file in a subdirectory. The agent will respect these local instructions when working in that folder. For global preferences that you want to apply across every project on your machine, you can use the ~/.claude/CLAUDE.md file. By leaning on these files, you make the /clear command much safer to use. You no longer have to worry about the agent forgetting that you use semicolons or that you prefer the TodoWrite tool for tracking technical debt.

A well maintained CLAUDE.md file acts as a bridge between sessions. It allows you to move between /compact for ongoing work and /clear for new tasks without the friction of re-explaining your environment. Every time you find yourself giving the same instruction to the agent twice, that instruction belongs in your markdown configuration files. This practice transforms Claude Code from a stateless tool into a deeply integrated member of your development team that understands your unique workflow constraints.

Monitoring resource usage during long tasks

To make informed decisions about when to use /compact or /clear, you need to monitor your resource consumption. The /cost command provides a real time view of the financial impact of your current session. If you see the cost per prompt rising steadily, it is a clear signal that the history is becoming too long. Similarly, the /usage command shows you the breakdown of tokens used. High token usage directly correlates with the amount of history the agent must process every time you hit Enter.

You can also use the /status command to get a high level overview of the current session state. This includes information about the model being used and any active MCP servers configured in your .mcp.json file. If you notice that the agent is frequently using the WebSearch or WebFetch tools, be aware that these tools can pull in a significant amount of external text, which fills the context window much faster than local code reads. In these cases, you might need to use /compact more frequently to keep the session manageable.

Your permission settings also play a role in how you manage the session. If you are in bypassPermissions mode, the agent might perform many Read and Edit operations in quick succession without waiting for your input. This can lead to a very long history in a short amount of time. If you prefer to have more control, you can switch to the plan mode by using Shift+Tab. This allows you to review the agent's intended actions before it executes them, giving you a chance to run /compact or /clear before the history gets out of hand.

Managing multiple project contexts simultaneously

For developers handling complex systems, a single terminal session is often not enough. You might be jumping between a library, its documentation, and an implementation project. Managing these different contexts by constantly switching folders and using /clear can be exhausting. This is where adhdTerminal offers a more organized approach. It is a native app for macOS and Windows, made by MINDBALANCER OU in Estonia, that lets you run many Claude Code sessions side by side as tiles on a wall.

Each tile in adhdTerminal represents a separate claude session with its own persistent context. You can have one tile dedicated to a long refactoring project where you use the /compact command to keep the history alive, while another tile is used for quick bash commands or experiments that you /clear frequently. The tiles are color coded and named, allowing you to see at a glance which session is busy working and which one is waiting for your next instruction. This visual separation reduces the mental load of remembering which context is active in which terminal tab.

The app also includes a free iPhone app that mirrors your wall of tiles over an end to end encrypted link. This means you can monitor a long running task while away from your desk or even send a prompt to a specific tile to start the next step of a build. When you restart your computer, the tiles reopen in the same place and resume the same session, preserving the state of your work. This persistent, multi tile environment makes the choice between /compact and /clear less about managing a single bottleneck and more about optimizing specific workflows across your entire project suite.

Practical workflow for high pressure debugging

In a high pressure debugging scenario, your priority is speed and accuracy. Start your session by running claude and focus on reproducing the bug. Use the Bash tool to run tests and the Grep tool to find relevant code. Once you have a lead and the agent has read several large files, run /compact. This ensures the agent remembers the bug reproduction steps but doesn't get bogged down by the initial file searches and failed test runs. It keeps the context tight on the solution path.

If you discover that the bug is actually caused by a third party dependency, you might use WebSearch to find documentation. This will add a lot of tokens to your session. After you have the information you need and have implemented a fix, this is a good time to run /clear. You have finished the debugging task, and your changes are already saved to disk. Clearing the session now prepares the agent for the next task, such as writing a regression test, without the distraction of the research history.

Finally, always finish a session by checking your work. You can use the /review command to have the agent look over the changes it made during the session. If you have already used /clear, the agent will look at the current state of the files on disk and compare them to your project instructions in CLAUDE.md. This final check ensures that even with a reset context, the quality of your code remains high and follows your established standards. By alternating between summary and reset, you maintain a fast, cost effective, and accurate development environment.

Questions people ask

Does the clear command delete the history of my session permanently?

Yes, once you run /clear, the history of that specific conversation is removed from the active context window and cannot be recovered within that session. However, any changes made to your files using the Edit or Write tools remain on your disk. If you need to see what happened previously, you would need to check your shell history using Ctrl+R or look at your git commits.

How can I automate the compacting process?

While you cannot set a specific token count for automatic compacting in the standard settings, you can use the PreCompact hook to monitor when the agent feels a summary is necessary. You can also manually trigger /compact whenever you feel the agent's responses are slowing down. For most developers, running it after every major milestone in a task is a good habit to maintain performance.

Can I use the clear command to switch between different projects?

The /clear command resets the context but keeps you in the same working directory. If you want to switch to a completely different project, it is better to quit the current session with Ctrl+C, change directories in your terminal, and run the claude command again. This ensures that the agent correctly identifies the new repo root and loads the appropriate CLAUDE.md file for that specific project.

Read next

More on claude code basics·All posts