ClaudeMods
☰
ZH-TW
● 0 人在線上 · 瀏覽 0 次
贊助提交作品
GitHub 儲存庫 · 發布者 mediavee

cold-cache-guard

一個 Claude Code mod,會在提示快取過期的大型對話再次送出前,先詢問要怎麼處理。

已翻譯

關於這個 mod

cold-cache-guard 是 Claude Code 外掛,會在提示快取過期的大型對話準備重新送出前詢問使用者。當對話閒置超過提示快取壽命(訂閱預設一小時、API 金鑰預設五分鐘)時,下一則訊息會把整個上下文當成快取寫入重新處理;在長工作階段裡,這則短訊息的成本可能超過一天其他操作的總和。此模組會在冷啟動恢復(claude --resume、--continue、/resume)以及閒置後第一次輸入提示時介入,讓使用者選擇直接送出、先壓縮、重新開始(/clear)或取消,並顯示閒置時間、token 數與價格估算。非人工輸入的訊息(其他工作階段、排程任務、-p 執行)不會被攔截;對話框支援 Claude Code 的 language 設定(目前支援英文與法文)。透過 plugin marketplace 加入並安裝,且需要啟用 early access 的 function hooks;可設定 ttlMinutes(預設 60)與 minTokens(預設 30000)。

安裝

請先查看作者 README,確認 marketplace 與外掛名稱;指令可能隨儲存庫結構而變動。

claude plugin marketplace add mediavee/cold-cache-guard
claude plugin install cold-cache-guard
原文 / README

cold-cache-guard

A Claude Code mod that asks what to do before a large conversation whose prompt cache has expired is sent again.

When a conversation sits idle past the prompt cache lifetime (one hour on a subscription, five minutes on an API key by default), the next message re-processes the whole context as a cache write. On a long session that one short message can cost more than the rest of the day. cold-cache-guard stops at that moment and lets you choose:

 ☐ Cache
│ Cold prompt cache: last answer 17h38 ago, ~109k tokens to cache again (~$0.54). What now?
❯ 1. Send as is
  2. Compact first
  3. Start over (/clear)
  4. Cancel

It asks in two places:

  • On a cold resume (claude --resume, --continue, /resume), before you type anything: keep the session as is, compact now, or start over. The idle time, the token count and the price estimate are Claude Code's own.
  • On the first prompt typed after an idle spell in a session that stayed open, the case a resume never sees: send as is, compact first, start over, or cancel. Compact, start over and cancel put your prompt back in the box.

Messages that are not typed by a person (another session's message, a scheduled task, a -p run) go through untouched. The dialog follows Claude Code's language setting (English and French so far).

Install

cold-cache-guard is a mod: a plugin built on Claude Code's function hooks, which are in early access. It needs function hooks enabled; it was built and tested on Claude Code 2.1.283.

claude plugin marketplace add mediavee/cold-cache-guard
claude plugin install cold-cache-guard@cold-cache-guard

Then enable function hooks, either for one launch:

CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude

or for good, in ~/.claude/settings.json:

{ "env": { "CLAUDE_CODE_ENABLE_FUNCTION_HOOKS": "1" } }

Sessions started before that change do not load it.

Configure

/plugin configure cold-cache-guard@cold-cache-guard, or the config menu:

| Option | Default | Meaning | | --- | --- | --- | | ttlMinutes | 60 | How long an idle conversation stays cached, for the prompt check. Set 5 on an API key, a cloud provider or usage credits unless you raised promptCacheTtl. Resumes use Claude Code's own estimate instead. | | minTokens | 30000 | Below this context size, nothing is asked. |

How it works

  • A classic.SessionStart hook reads what Claude Code reports on a resume (seconds_since_last_response, context_tokens, prompt_cache_likely_expired, estimated_cache_write_usd) and asks while the session loads.
  • A turn.complete hook records when the last answer arrived, in the session's $.state, so a reload of the mod keeps it.
  • A prompt.submit hook compares that time with ttlMinutes and the live context size with minTokens, then asks through $.ui.ask. A prompt hook cannot compact or run /clear while it holds the prompt, so those run right after the prompt is dropped, and the prompt comes back to the box rather than being resubmitted.
  • A choice holds until the next answer, so a cancelled prompt asks again and an accepted one does not.

Limits

  • Function hooks are early access: the API can change with any Claude Code release. If a hook fails, Claude Code skips it and the prompt goes through as if the mod were not there.
  • The prompt check uses your ttlMinutes, not the live cache state, which mods cannot read yet. A wrong setting means asking too early or too late.
  • Anything typed into the terminal counts as a person's prompt, including text a terminal multiplexer or an orchestrator sends by keystrokes.
  • On Pro and Max plans, Claude Code's own "Resume from summary" dialog can also appear for a resumed session over 100k tokens. Answer "Don't ask me again" there if you prefer this one.
  • Changing the model or the effort level also rebuilds the cache; Claude Code already asks before those while the cache is warm, so this mod does not.

Related

cache-tax takes another approach to the same cost: it keeps the cache warm with periodic pings while you are away, and refuses a cold send once with its price.

Develop

claude plugin validate .claude-plugin/plugin.json
claude plugin test .

To type-check, run /plugin-types in a Claude Code session opened in this folder (it writes .claude/types), then npx -p typescript tsc -p ..

License

MIT

更多類似作品