Pan1127

Cc Cache Keepalive — Development skill for Claude Code

Development community

Keep the most recently used Claude Code prompt cache warm without polluting the original session.

How to install Cc Cache Keepalive

This entry records only its repository, not the path inside it, so there is no exact command to give. Open Pan1127/cc-cache-keepalive and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

What Cc Cache Keepalive does

Keep the most recently used Claude Code prompt cache warm without polluting the original session.

Alternatives in Development

  • Caveman Micro — 6-line caveman micro prompt (85 tokens) that outperformed the original 552-token skill 162 ★
  • Compact 서비스 (대화 요약) — 관찰 위치: Claude Code 내부 아키텍처 변수: NO_TOOLS_PREAMBLE, BASE_COMPACT_PROMPT, PARTIAL_COMPACT_PROMPT, PARTIAL_COMPACT 149 ★
  • Pair Verify — Use after you fix a bug and before you claim it fixed — it spawns ONE skeptic sibling session via CCC that mus 130 ★

README

Claude Code Cache Keepalive

一个只为“最近使用的 Claude Code 会话”续热提示缓存的小工具。

它不会把保活消息写回原会话:每次调用都使用 `--fork-session` 和 `--no-session-persistence`。它会禁用全部内置工具和 MCP,只在返回随机 nonce 且 Claude Code 报告了缓存 token 时记录成功。

工作方式

  1. 从 sessions.json 找出 last_used 最新的会话。
  2. 若对应 tmux 窗口正忙,本次自动跳过。
  3. 距离最近活动满 50 分钟后,从原会话创建一个不持久化的临时分叉。
  4. 验证回复和 cache_read_input_tokens / cache_creation_input_tokens。
  5. 只把状态和 token 计数写入独立的 status.json。

这不能保证供应商永远保留缓存,也不会减少输出 token;它只是尽量避免活跃会话的 提示缓存因一小时无请求而失效。每次保活仍会产生少量输入和输出用量。

要求

  • Linux、Python 3.10+
  • 已登录且可用的 Claude Code CLI
  • tmux(仅用于判断交互窗口是否正忙)
  • 一个由你的会话管理器维护的 sessions.json

安装

sudo install -d -m 0755 /opt/cc-cache-keepalive
sudo install -d -o claude -g claude -m 0750 /var/lib/cc-cache-keepalive
sudo install -m 0755 cc_cache_keepalive.py /opt/cc-cache-keepalive/
sudo install -o claude -g claude -m 0640 .env.example /etc/cc-cache-keepalive.env
sudo install -o claude -g claude -m 0640 sessions.example.json /var/lib/cc-cache-keepalive/sessions.json
sudo install -m 0644 cc-cache-keepalive.service cc-cache-keepalive.timer /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now cc-cache-keepalive.timer

以上命令假设运行用户和用户组都是 `claude`;如果不是,请在命令、环境文件和 service 的 `User=` 中统一替换。 把 `/etc/cc-cache-keepalive.env` 中的路径改成实际值。 `sessions.example.json` 展示了输入格式;`last_used` 是 Unix 时间戳。

接入会话管理器

每次用户真正使用某个 Claude Code 会话时,由你的程序原子更新 `sessions.json`:

{
  "窗口标识": {
    "sid": "Claude Code 会话 UUID",
    "tmux": "可选的 tmux 会话名",
    "model": "auto",
    "last_used": 1788192000
  }
}

`sid` 和 `last_used` 必填。没有使用 tmux 时可以省略 `tmux`;脚本仍会保活,但无法判断 该交互窗口是否正忙。写文件时应先写同目录临时文件,再用原子重命名替换,避免定时器 读到半截 JSON。

检查

systemctl list-timers cc-cache-keepalive.timer
journalctl -u cc-cache-keepalive.service -n 30 --no-pager
cat /var/lib/cc-cache-keepalive/status.json

成功状态会包含 `status: "success"`、最近成功时间和缓存 token 数。其他旧会话会标记 为 `paused`,因此不会同时保活多个窗口。

隐私与安全

  • 不要提交 .env、sessions.json、status.json 或任何 .jsonl 会话记录。
  • 保活调用通过