What is compaction? Claude Code, Codex, Cursor and Grok

By

Compaction replaces older chat turns with a shorter summary so a coding agent can keep going. How Claude Code, Codex, Cursor and Grok Build each run it.

Compaction is a coding agent replacing older turns with a shorter summary, so the same session can continue when the context window is close to full. The summary keeps the main points. It drops detail.

Prompt caching, compression, and compaction is the post that owns the cost side: what the summary does to the next request, and why a worse cache-hit rate can still be a cheaper turn. This entry is the short definition, plus the command each tool documents.

How compaction works

Every new model call sends the conversation again. File reads and command output stay in that history until something removes them. Compaction is that removal. The agent writes a stand-in for the old turns and keeps working in the same chat.

A new chat is the other move. Compaction tries to carry the task forward. /clear or a fresh session drops it.

What compaction does to a coding agent sessionOlder turns, file reads and command output fill the chat. Compaction replaces that history with a shorter summary. The agent continues the same session, and some instruction files are read from disk again.Turns, file readsand command outputfill the chatCompaction writes ashorter summaryThe agent continuesthe same session

What comes back after the summary depends on the tool. Claude Code’s context page lists it. The others document the command and say less about the leftovers.

Compaction in each tool

Claude Code Codex Cursor Grok Build
By hand /compact, with optional focus text /compact, then confirm /summarize in the CLI, alias /compress /compact, with optional context text
A summary that does not shrink the chat Not the same command /recap Not documented Not documented
On its own As the window gets close to full. /autocompact sets an earlier point Not a token threshold in the config reference checked today When the window gets close to full At auto_compact_threshold_percent, 85 by default
What the docs say survives System prompt, output style, root CLAUDE.md, auto memory, the plan file “Key points” in the /compact description A “Summarized conversation” slice in the context breakdown Not documented

Claude Code

The glossary calls compaction automatic summarization as the window approaches its limit. /compact runs it now. /compact focus on the API changes tells the summary what to keep.

The context page says the automatic pass works the same way. /autocompact 500k sets how full the window gets before that pass. /rewind can summarize from a chosen message, or up to it. /clear is the cut between unrelated tasks.

After compaction, the system prompt and the output style still apply. The project-root CLAUDE.md, unscoped rules, and auto memory are read from disk again. The plan file from plan mode is read again. Path-scoped rules and nested CLAUDE.md files were part of the message history, so they come back when a matching file is touched, not as a block that sits there forever. Skill descriptions are rebuilt from the skills you have now. Claude Code also re-reads up to five files it read or edited, most recently modified first. A file over 5,000 tokens comes back as a path, not the contents.

See Explore the context window and the glossary.

OpenAI Codex

/compact summarizes the visible chat to free tokens. Codex asks you to confirm, then replaces earlier turns with a summary. /recap writes a summary of the conversation and does not compact the context. /status shows how much context is left.

In config.toml, compact_prompt is an inline override for the history compaction prompt. experimental_compact_prompt_file loads that override from a file. For a custom model provider, model_providers.<id>.capabilities.remote_compaction is unsupported for local compaction or v2 to send compaction through the Responses API. Omit it to keep the provider default.

The config reference checked for this entry does not list a token count that triggers automatic compaction. See Slash commands and the configuration reference.

Cursor

Every chat shares the model’s context window. When that window gets close to full, Cursor compresses older parts of the conversation into a summary. The ring next to the prompt shows how full the window is. Open it and one slice is “Summarized conversation”, separate from the conversation itself.

The editor prompting page does not name a slash command for doing this by hand. In the Cursor CLI, /summarize reduces context, and /compress is an alias. See Prompting agents and CLI slash commands.

Grok Build

/compact compacts conversation history. The command is written as /compact [context], so you can pass context text with it. The commands page does not say how that text changes the summary. /context shows current usage.

Automatic compaction uses auto_compact_threshold_percent under [session] in ~/.grok/config.toml. The default is 85, on a scale from 0 to 100. A custom model’s context_window setting is what drives the timing of that pass. /compact-mode is a different command. It toggles a denser layout. It does not summarize the chat.

See Modes and commands and the settings reference.

Details checked against each vendor’s documentation in November 2026.

A real example

A long debugging chat in Claude Code, once the early test logs are in the way and the next edit still depends on them:

/compact focus on the failing test name, the root cause, and the files we changed

The same job in the Cursor CLI is /summarize. In Codex it is /compact, and you confirm when asked. In Grok Build it is /compact plus any context text you want attached.

Put the requirement that must not disappear into a file the agent can read again. AGENTS.md and a short project note outlive a summary. That is the point of context engineering.

When to use compaction

Use it when the session is the right one and the old logs are the problem. A bug investigation that already found the cause is the usual case. Keep the cause, the files, and the test name, and drop the early failed runs.

Skip it when the next task is unrelated. /clear in Claude Code, /new in Codex or Grok Build, and /clear in the Cursor CLI start fresh, which is cheaper than summarizing a chat you are about to abandon.

Skip it when you only want to reread the thread. Codex /recap writes that summary and leaves the context in place. Compaction replaces the earlier turns.

Pitfalls

Claude Code path-scoped rules feel permanent until /compact. They were loaded into the history when a matching file was touched, and the summary takes them with it. The root CLAUDE.md is read again. A rule you need after compaction belongs there, or in a file with no paths: frontmatter.

Codex /recap and /compact sit next to each other in the command list. One leaves the context alone. The other replaces earlier turns. Confirming the wrong one is how a chat loses the logs you still wanted.

Grok’s /compact-mode only changes padding. The command that shortens history is /compact.

The caching post is the other trap. Right after compaction the next request can miss the prompt cache, because the prefix changed. Fewer tokens can still cost less. Read that post before you compact to “fix” a hit-rate meter.

  • Context window: The context window is everything a coding agent can see at once. How to check it, free it and keep it small in Claude Code, Codex, Cursor and Grok Build.
  • Context engineering: Context engineering is choosing what an agent sees before it edits. How Claude Code, Codex, Cursor and Grok Build load rules, files and skills.
  • Tokens and usage limits: A token is the unit a model reads and writes. A usage limit is how much of that work a plan includes. How Claude Code, Codex, Cursor and Grok count it.
  • Sessions: A session is a saved coding-agent conversation you can resume later. How Claude Code, Codex, the Cursor CLI and Grok Build store, name, resume and fork one.

All AI glossary entries →

Want me to talk about your product? You can sponsor this site.