dtinth/claude-code-mods/tree/main/polite-compaction
polite-compaction
一個 Claude Code Mod,在提示列上方顯示 context token 用量和工作階段成本,並要求代理在自動壓縮前刷新備註。
關於這個 mod
polite-compaction 是 dtinth-mods 市集中的 Claude Code 外掛。它在提示列上方顯示灰色狀態列,包含目前的 context token(ctx X / Y (Z%))以及與 /cost 相符的工作階段成本。另外,它會協助代理在壓縮過程中保留備註:每回合結束後讀取 context 大小,一旦 context 超過可設定的門檻(預設 300000 token),就傳送提示給代理,要求它在壓縮前刷新所有重要內容;代理回覆 <ready-for-compaction/> 後,再執行一般壓縮。它只在主要工作階段使用 session.start、session.measure、turn.complete 和 ui.render (AbovePrompt) hook,門檻可在設定選單中指定。透過 /plugin marketplace add dtinth/claude-code-mods 和 /plugin install polite-compaction@dtinth-mods 安裝。需要支援 early-access function hooks(Mod API)的 Claude Code 建置版本。
安裝
請先查看作者 README,確認 marketplace 與外掛名稱;指令可能隨儲存庫結構而變動。
claude plugin marketplace add dtinth/claude-code-mods claude plugin install polite-compaction
原文 / README
polite-compaction
Part of claude-code-mods.
A Claude Code mod. It does two jobs:
- It shows the context size and the session cost in a gray line above the prompt.
- It helps the agent keep its notes through compaction.
Why
Auto-compact can run in the middle of a task. The summary then loses small details. This mod asks the agent to save what it needs, in its own words and to a place of its own choice, before the conversation is compacted.
What you see
ctx 123k / 1000k (12%) · $4.56
ctx: tokens in the context now, the size of the context window, and the percentage used.$: the cost of the session so far, the same figure/costshows.- The line is gray. It is hidden while a survey shows.
How the flush works
- After each turn, the mod reads the context size.
- When the context reaches the threshold (default 300k tokens), the mod sends
this prompt: "Context window reached 300k tokens. The conversation will be
compacted soon. If there is anything to flush, please do so now, so you can
continue well after compaction. When you are ready, end your reply with
<ready-for-compaction/>". - The agent saves what it needs. The prompt does not name a place.
- When a reply ends with
<ready-for-compaction/>, the mod runs a normal compaction (no custom instructions).
Scope:
- The mod works on the main session only. It does not measure or ask sub-agents. Their context is separate, and they do not get the flush prompt.
- The mod acts only between turns. A turn is one run of the model, from a prompt to the final answer, and it can include many tool calls. The mod reads the context size when the turn ends. It does not inject the prompt in the middle of a turn. The prompt goes in as a new turn, after the session is idle.
- A long turn can pass the threshold, and also the auto-compact point, before the mod gets a chance to ask. Keep the threshold well below auto-compact.
Rules:
- The mod asks one time for each crossing of the threshold. It arms again when the context falls below the threshold.
- If you interrupt the turn, or the turn ends in an error, the mod does not ask again until after the next compaction.
- Compaction cannot start while a turn runs. The mod tries again every 500 ms, up to 20 times.
- Set the threshold below the auto-compact point. If auto-compact runs first, the flush does not happen.
Settings
| Setting | Default | Meaning |
| --- | --- | --- |
| thresholdTokens | 300000 | Context size at which the agent is asked to flush. |
Change it in the config menu. The mod reloads with the new value.
Hooks used
| Event | Use |
| --- | --- |
| session.start | Set the first text of the line. |
| session.measure | Update the line. Ask the agent at the threshold. |
| turn.complete | Look for <ready-for-compaction/>. Start compaction. |
| ui.render (AbovePrompt) | Draw the line. |
Files
.claude-plugin/plugin.json manifest and the threshold setting
hooks/hooks.json names the hooks module
hooks/register.tsx the mod
hooks/register.test.ts test
types/index.d.ts type of the value the line keeps in $.state
Install
This repository is a Claude Code marketplace named dtinth-mods. In Claude
Code, run:
/plugin marketplace add dtinth/claude-code-mods
/plugin install polite-compaction@dtinth-mods
From a shell, use claude plugin marketplace add dtinth/claude-code-mods and
claude plugin install polite-compaction@dtinth-mods. The source can also be a
local path to a clone of the repository.
Develop
claude plugin validate .
claude plugin test .
To load the mod in a terminal session, run claude --plugin-dir <path to this folder>.
Limits
- The mod needs a Claude Code build that has function hooks (a mod API). That API is in early access and can change.
- The test uses a stand-in for compaction. A real compaction is checked by hand.

