
conPACT を作りました:タスク完了後に Claude Code が自分のコンテキストを圧縮し、プロンプトキャッシュの期限切れ前にアイドル中のセッションの圧縮を提案
conPACT は Claude Code のモッドです。タスク完了後に MCP ツール経由でセッションのコンテキストを自動的に圧縮し、プロンプトキャッシュが切れる前にアイドル中のセッションの圧縮を促します。Claude Code 2.1.284 以降ではプロセス内のプラグイン hooks モッドとして動き、Remote Control 経由では Stop hook で /compact を送ります。Codex と ChatGPT Desktop にも対応しています。作者によると支出は約 4.7% 減り、ピーク時のコンテキストも小さくなりました。MIT ライセンスです。

この mod について
作者は conPACT について紹介しています。MCP ツール queue_compaction を使って自己圧縮をキューに入れ、最終回答の後に本物の /compact として圧縮を実行します。チャット履歴は残したまま、ライブのコンテキストを要約します。
焦点の指示と最小サイズを指定できます。キャッシュ期限の 5 分前にトーストが表示され、今すぐ圧縮するか、セッションごとの自動圧縮にするかを選べます。プロンプトの上には状態行も表示されます。
作者は Claude Code で開発し、九月中旬から自分の開発セッションで使っています。Claude Code 2.1.284 以降の新しいプロセス内プラグイン hooks を使うモッドとして組み込まれ、Remote Control 経由の場合は Stop hook で /compact を送ります。任意のサイドカーを使えば ChatGPT Desktop(Codex)にも対応し、Stop hook 経由で Codex CLI にも対応します。
作者の重要な学びは次のとおりです。/compact はターンの途中では送れないため、圧縮はターンの終了後に行われ、かつ最大 1 回です。サイズより、タイミングのほうが重要です。
187,978 回の API 呼び出しを計測した結果、conPACT による 134 回の圧縮はすべてキャッシュが温かい状態で行われました。一方、手動の 121 回のうちキャッシュが温かかったのは 30 回です。標準価格で計算すると、圧縮 1 回あたり約 $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]

