
我做了 conPACT:任務完成後 Claude Code 自動壓縮自己的上下文,並在提示快取過期前提議壓縮閒置的工作階段
conPACT 是一款 Claude Code 模組,任務完成後透過 MCP 工具自動壓縮工作階段的上下文,並在提示快取過期前提示壓縮閒置的工作階段。它以行程內的外掛 hooks 模組形式運作於 Claude Code 2.1.284 以上版本,透過 Remote Control 時則改用 Stop hook 送出 /compact,也支援 Codex 與 ChatGPT Desktop。作者回報約節省 4.7% 花費、尖峰上下文變小,採 MIT 授權。

關於這個 mod
作者介紹 conPACT,它透過 MCP 工具 queue_compaction 排入自我壓縮,讓壓縮在最終回答之後以真正的 /compact 執行,在摘要即時上下文的同時保留聊天紀錄。
它支援焦點指示與最小大小設定。快取到期前五分鐘會跳出提示,讓使用者選擇立即壓縮,或為該工作階段開啟自動壓縮。提示列上方還有一列狀態。
作者用 Claude Code 開發,並從九月中旬起在自己的開發工作階段中使用。它以模組形式整合,使用 Claude Code 2.1.284 以上版本新的行程內外掛 hooks;透過 Remote Control 時,則由 Stop hook 送出 /compact。它也能透過選用的 sidecar 支援 ChatGPT Desktop(Codex),並透過 Stop hook 支援 Codex CLI。
作者的重點心得:/compact 無法在 turn 進行中送出,所以壓縮只會在 turn 結束後執行,且最多一次。時機比大小重要。
以 187,978 次 API 呼叫為樣本,conPACT 的 134 次壓縮都在快取仍有效(warm cache)時完成,手動壓縮 121 次中只有 30 次在快取有效時執行。以標價計算,每次壓縮約為 $5.67 對 $0.20。尖峰上下文的中位數從 501k 降到 337k tokens;冷啟動恢復時重新讀取的中位數從 493k 降到 255k tokens;冷啟動在花費中的占比從 6.4% 降到 3.8%,而且沒有重工的代價。
需求:Python 3.11 以上,只使用標準函式庫,可在 Windows、Linux、macOS 執行,採 MIT 授權。GitHub:https://github.com/st0nebridge/conPACT
安裝
安裝方式請查看原始來源。
原文 / README
Long Claude Code sessions cost you twice. The context keeps growing after the work that needed it is done, and if you come back after the prompt cache has expired, your next message re-reads all of it uncached. /compact fixes both, but only if you type it at the right moment. I usually didn't. What it does When a piece of work is finished, Claude queues a compaction of its own session through an MCP tool ( queue_compaction ). It runs after the final answer, as a real /compact : your chat history stays visible and only the live context is summarised. It can take a focus ("keep the plan and the open decisions") and a minimum size. When a big session sits idle, a toast appears five minutes before the cache expires and offers to compact it now, or always for that session. A row above the prompt follows the request: queued, compacting, then what it came to. How Claude Code was used I built it with Claude Code, and it has been compacting its own development sessions since mid-September. In Claude Code 2.1.284+ it ships as a mod (the new in-process plugin hooks), so the session compacts itself and draws the row above the prompt. Without the mod, a Stop hook sends /compact over Remote Control. It also works for ChatGPT Desktop (Codex) through an optional sidecar, and for the Codex CLI through a Stop hook. What I learned You can't send /compact mid-turn. A busy session receives it as plain text and it never runs. So the tool only records the request, and the compaction happens after the turn ends: always after the final answer, and at most once. When you compact matters more than how small. I measured it over 187,978 of my own API calls (12 days with conPACT, 80 before). All 134 conPACT compactions ran on a warm cache, against 30 of the 121 I'd typed by hand. A cold compaction re-reads the whole context at the cache-write price first, so that's roughly $5.67 against $0.20 per compaction at list price. Median peak context per session fell from 501k to 337k. The idle toast does its job: a cold restart now starts from a compacted context. The median re-read on a cold resume fell from 493k to 255k tokens, and cold resumes' share of spend fell from 6.4% to 3.8%. No rework penalty. Claude re-reads some files after a compaction, but the next prompt was a correction 5.5% of the time, against 6.1% in ordinary turns. Net: about 4.7% of spend saved at list price (1.8–13%, depending on what you assume I'd have done otherwise). It's modest, and the "after" period is only 12 active days, but every compaction saves more than it costs. Python 3.11+, standard library only. Windows, Linux and macOS (the test suite runs on all three; most of my live use is on Windows). MIT licensed. GitHub: https://github.com/st0nebridge/conPACT Feedback welcome, especially from anyone on macOS or Linux. submitted by /u/stonebrigade [link] [comments]

