compactor
Agent-controlled auto-compaction for Claude Code. The agent holds auto-compaction during
fragile work, such as debugging chains or multi-file refactors, and releases it at a safe
breakpoint. It can leave a handoff note that comes back after compaction.
What "control" means
Claude Code can't be told to compact early, by hooks or by the model. So compactor controls
when compaction is allowed:
- Below roughly your threshold, nothing compacts (Claude Code starts a little early; see
Known Claude Code behaviors).
- At or above it, compaction proceeds unless the agent holds it. Releasing lets it through
at the next automatic check. The exception: by default, while a background subagent runs,
compactions are held until the safety ceiling (see the same section).
- A safety ceiling (90% of the window by default) overrides any hold, so a session never
runs into the hard limit. It counts tool output that arrived since the model's last reply,
and lets compaction through early when one more turn as large as the last would reach it.
Requirements
- Claude Code with plugin support
python3 (3.9 or newer) on your PATH. It uses the standard library only. On Windows, make
sure python3 resolves, for example through the Python launcher or an alias.
The test suite runs in CI on Linux, macOS and Windows (Python 3.9–3.13). Live sessions have
been checked on Linux only, so reports from macOS and Windows are welcome.
Install
/plugin marketplace add rhwendt/compactor
/plugin install compactor@compactor
Enable
compactor is inactive until you set a threshold, which also pulls auto-compaction below the
model's default. Add it to ~/.claude/settings.json:
{ "env": { "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "350000" } }
Set the threshold below the model's context window, for example 350000 on 1M-context
models or 150000 on 200k models. The safety ceiling is always a percentage of the window,
so a threshold at or above it leaves the agent no room to hold.
Restart Claude Code. The agent is told about the commands at session start.
Per project or per session
The plugin and the threshold are set separately, and each can be global, per project, or (for
the threshold) per session.
Where the plugin is installed. Pass --scope when installing from a shell in the project
folder:
claude plugin marketplace add rhwendt/compactor --scope local
claude plugin install compactor@compactor --scope local
| Scope | Active in | Recorded in |
|---|
user (default) | all your projects | ~/.claude/settings.json |
project | this project, for everyone using the repo | .claude/settings.json (committed) |
local | this project, just you | .claude/settings.local.json (not committed) |
Where the threshold is set. Put the env block in the matching settings file:
| To enable compactor for | Set CLAUDE_CODE_AUTO_COMPACT_WINDOW in |
|---|
| every project | ~/.claude/settings.json |
| one project, everyone | the project's .claude/settings.json |
| one project, just you | the project's .claude/settings.local.json |
| one session | claude --settings '{"env":{"CLAUDE_CODE_AUTO_COMPACT_WINDOW":"350000"}}' |
Project settings override your user settings, and --settings overrides both. A value in any
settings file also beats one from your shell, so CLAUDE_CODE_AUTO_COMPACT_WINDOW=500000 claude
or an export in your shell profile has no effect while a settings file sets the variable.
The COMPACTOR_* options below can go in the same env blocks.
For example, to use compactor in one project only, install it with --scope local and add
this to that project's .claude/settings.local.json:
{ "env": { "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "350000" } }
Updating
Claude Code caches an installed plugin by version. To pick up a new release, run both
commands, then start a new session:
claude plugin marketplace update compactor
claude plugin update compactor@compactor
How the agent uses it
| Command | Effect |
|---|
compactor hold "<why>" | Blocks auto-compaction until released. A reason is required. |
compactor release [--note "<text>"] [--file <path>] | Allows compaction again, optionally saving a handoff note. |
compactor note "<text>" [--file <path>] / --clear | Sets or clears the handoff note. With --file, the last 40 lines of that file (up to 4 KB) come back with the note after compaction. |
compactor status [--json | --line] | Shows the hold, the note, running subagents, background tasks, and context usage. |
Only the main agent can hold, release or write notes. Subagents and agent-team teammates share
its session, so a PreToolUse hook refuses their compactor hold/release/note calls
(compactor status is allowed).