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) 钩子,阈值可在配置菜单中设置。通过 /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.

