SmartCompact is a context compaction tool for coding agents, designed to solve the problem of high costs caused by context bloat in long sessions. By intelligently judging when to compact, it compacts early at pause points after a piece of work is completed, thereby reducing costs without losing results.
Purpose
Coding agents (such as Claude Code, Pi, and Codex CLI) re-read the entire context on every request in long sessions, making every step expensive. Native auto-compaction usually triggers only when the context approaches its limit (for example, Claude Code approaching 1 million tokens, or Codex configurable up to 800k), and it ignores timing, often compacting in the middle of a work task. SmartCompact instead chooses the right time to compact early, saving cost.
Key capabilities
- Compact at the right time: Once the context exceeds the allowed line (default 200k tokens), if work has been completed since the last compaction (commit, passing tests, task completion) and there are no file changes, compact at the end of the turn.
- Never compact mid-way: Decide only at the end of a turn, never compact in the middle of a turn; if tests or builds fail or errors recur, wait; also require the user to be idle for 3 minutes to avoid interrupting discussion.
- No limits of its own: If no work has been completed, the agent is usually deep in a large task, where a large context is valuable; SmartCompact does not intervene and lets the platform's native compaction handle it under the original rules.
- Progress ledger: After compaction, hand file changes, the latest commit and test results, user requirements and decisions, and the agent's last reply back to the model to fill the information gap.
- Avoid pointless compaction: If compaction frees insufficient space, skip it.
Usage
- Claude Code (2.1.295 or later): Takes effect in a new session. Use
/smartcompact pauseto close in the current session. Plugin options includeallowLineTokens、minFreedTokens、compactIdleSeconds, leave blank to use the default value. - Pi: With default settings, compact at the end of a turn (agent_settled) when work is completed. If reserveTokens is raised so that Pi's native compaction happens earlier, SmartCompact decides based on work status whether to defer. Command-line flags:
--smartcompact-allow-line、--smartcompact-min-freed、--smartcompact-idle-seconds. - Codex CLI (0.162 or later, Node.js 22.18 or later): Trust the plugin's 4 hooks on the next launch. The hooks find Node.js 22.18+ themselves (PATH, nvm, Homebrew, or setSMARTCOMPACT_NODE); if none is found, do nothing. When updating, runcodex plugin marketplace upgrade smartcompactand then run thecodex plugin addcommand again. If an older version was previously installed withnode scripts/codex.ts install, runnode scripts/codex.ts uninstallonce in that clone to avoid the hooks running twice. Codex hooks cannot defer compaction mid-turn, so SmartCompact decides at the end of the turn and, if compaction is warranted, starts compaction after the session is idle via Codex's shared background server (used by default in the interactive UI).codex execBy default, no shared server is used, and Codex App (ChatGPT.app) currently cannot either; in this case, SmartCompact only hands back the progress ledger, and compaction follows Codex's native rules. If you want earlier compaction, you can add Codex's own at the top of config.tomlmodel_post_turn_compact_threshold_percent: after the turn ends, compaction occurs when the context exceeds this percentage of the available window (31 is approximately 260k of an 872k window). - Viewing results: Claude Code and Pi use
/smartcompact(or/smartcompact report) to display the allowance line, native compaction point, observed work-completion signals, and cost estimates;/smartcompact pauseandresumeare turned off/on in the current session. The model can also call the checkpoint tool to record user requirements and decisions, which are returned with the ledger after compaction. Codex usesnode ~/.codex/plugins/cache/smartcompact/smartcompact/<version>/adapters/codex/report.ts <session ID>(Node.js 22.18+); records are stored in~/.codex/smartcompact/.
Use Cases
Intended for developers using Claude Code, Pi, or Codex CLI for long-running continuous programming tasks, especially scenarios where the session context grows to hundreds of thousands of tokens. Across 18–60 consecutive programming tasks, costs were reduced by 7–44% without losing hidden acceptance test results; the longer the session, the greater the savings. In the longest run (60 tasks, with native sessions reaching 820k–960k tokens), the model missed 6 of 180 recall questions about details mentioned earlier.
Limitations
- The evaluation is based on a small sample from a single codebase and a single task type, so the results may not be generalizable.
- After compaction, the model may forget details mentioned casually earlier; the progress ledger records only files, commits, tests, and the requirements and decisions the model chooses to record; user requirements have a separate section, and when they exceed 30 entries, only the oldest are discarded.
- It does not change Claude Code's own settings; native automatic compaction remains near 1 million. Version 0.1.3 removed the previous behavior of moving the compaction point to about 260k via
CLAUDE_CODE_AUTO_COMPACT_WINDOWto move the compaction point to about 260k. - For smaller context windows (such as 200k), native compaction triggers first (Claude Code at about 167k), and SmartCompact does not act. Under Codex's default 272k window, native compaction is at about 245k; SmartCompact only takes effect between 200k and 245k, so savings are limited unless you increase
model_auto_compact_token_limit. The allowance line can be lowered, but lower lines have not been evaluated. - Uninstallation: Claude Code uses
/plugin uninstall smartcompact@smartcompact; Pi usespi remove npm:smartcompact; Codex usescodex plugin remove smartcompact@smartcompact.
Project Structure
plugin/: Claude Code plugin,plugin/src/core/stores platform-independent decisions, signals, ledgers, and cost estimates for reuse by each adapter.adapters/pi/、adapters/codex/: Pi and Codex adapters.eval/: A/B evaluation scripts (one each for Claude, Pi, and Codex); for the task format, seeeval/day.py. The tasks and hidden tests come from an internal project and are not public.docs/EVALUATION.md: A/B run methods and results;DESIGN.md: design notes;docs/TEST-REPORT.md: full test report (the latter two are in Chinese).
License: MIT, see LICENSE.