/compact command
A Claude Code slash command that summarizes the current conversation to reduce context length while preserving key state.
What is /compact command?
/compact command is a Claude Code slash command that summarizes the current conversation so you can reduce context length without losing the important state. Anthropic lists it as a built-in command for compacting a conversation, with optional focus instructions. (docs.anthropic.com)
Understanding /compact command
In practice, /compact is used when a Claude Code session starts to get too long. Instead of letting the chat history grow until it becomes harder to work with, the command creates a shorter continuation that keeps the key decisions, code changes, and task context available for the next turn. Anthropic also notes that Claude Code can auto-compact when the context gets close to capacity, while /compact gives you manual control. (docs.anthropic.com)
The command is especially useful in coding workflows where the conversation contains a mix of requirements, file edits, debugging steps, and planning. A good compaction keeps the working state intact, but removes enough older detail to make room for new reasoning. You can also add focus instructions, which makes /compact more selective about what it preserves, such as code samples, API usage, or test results. That makes it easier to steer the summary toward what matters most for the next phase of work. (docs.anthropic.com)
Key aspects of /compact command include:
- Context reduction: It shortens a long session so the conversation stays usable.
- State preservation: It keeps the important task history and decisions intact.
- Manual control: You can trigger it when you decide the session needs compaction.
- Optional focus: Instructions can steer what the summary emphasizes.
- Workflow fit: It is designed for iterative, multi-turn Claude Code sessions.
Advantages of /compact command
- Frees context: Leaves room for new prompts, files, and tool output.
- Maintains continuity: Helps Claude keep the thread of the task across long sessions.
- Reduces repetition: Lowers the need to restate the full backstory.
- Supports complex work: Useful for debugging, refactors, and multi-step planning.
- Adds control: Focus instructions make the summary more task-specific.
Challenges in /compact command
- Summary quality: Important details can be lost if the session is not summarized well.
- Prompt discipline: Users still need to write clear instructions after compaction.
- Hidden assumptions: Older context may be compressed away more than expected.
- Task drift: If the focus is vague, the compacted state can emphasize the wrong things.
- Workflow timing: Compacting too late can make recovery harder than if it were done earlier.
Example of /compact command in action
Scenario: A developer has spent 40 minutes iterating on a bug in a Python service. The chat now includes stack traces, file edits, rejected hypotheses, and a partial fix.
They run /compact and add a focus instruction like “Preserve the failing test, the root cause, and the final patch plan.” Claude Code summarizes the session into a shorter state that keeps the error pattern, the files touched, and the next debugging step.
From there, the developer can continue without rehashing the entire investigation. The session stays lean, but the working memory is still useful for the next round of edits.
How PromptLayer helps with /compact command
PromptLayer gives teams a way to manage prompts, track changes, and review LLM workflows with more structure. For teams building internal tooling around Claude Code, that can make it easier to standardize instructions, compare prompt behavior, and observe how compacted sessions affect downstream outcomes.
Ready to try it yourself? Sign up for PromptLayer and start managing your prompts in minutes.