Claude Code Cache Keepalive — Development skill for Claude Code
Keep the Claude Code prompt cache hitting: what burns it, stable-prefix configuration, and TTL keepalive for long sessions and agents.
How to install Claude Code Cache Keepalive
This entry records only its repository, not the path inside it, so there is no
exact command to give. Open lllq-123/claude-code-cache-keepalive and copy the folder into
~/.claude/skills/, or the file into ~/.claude/agents/.
What Claude Code Cache Keepalive does
Keep the Claude Code prompt cache hitting: what burns it, stable-prefix configuration, and TTL keepalive for long sessions and agents.
Alternatives in Development
- Dao Code — Open-source TypeScript terminal coding agent for DeepSeek-V4 — builds on DeepSeek's strong price-performance a 1.3k ★
- Compact 서비스 (대화 요약) — 관찰 위치: Claude Code 내부 아키텍처 변수: NO_TOOLS_PREAMBLE, BASE_COMPACT_PROMPT, PARTIAL_COMPACT_PROMPT, PARTIAL_COMPACT 149 ★
- Post Compact Reminder — Claude Code hook that detects context compaction and injects a reminder to re-read AGENTS.md, preventing post 40 ★
README
claude-code-cache-keepalive
让长会话和自动化 agent 的 Claude Code prompt cache 保持命中:哪些行为会烧掉缓存、怎么配置让前缀稳定、以及 TTL 到期前的轻量保活。
[English README](README.en.md)
[!WARNING] 非官方社区笔记,不隶属于 Anthropic。数字来自 Claude Code 2.1.175 / 2.1.258 的真实 API usage;CC 版本升级后行为可能变化,请重新验证再信。 姊妹项目:[claude-code-turn-anchor](https://github.com/lllq-123/claude-code-turn-anchor) (已于 2026-09-02 改为每 15 个 prompt 稀疏追加,不再改写 transcript)。
先读这个:缓存崩塌是自愈的,肉眼看不见
最反直觉、也最重要的一条:**砸缓存的事故会自己恢复,所以你几乎不可能 "当场看到"它。**
砸掉的那一轮把整个前缀按缓存写入价(1.25× 或 2×)全量重算,**下一轮就恢复 99% 命中,之后一路正常**。你听说缓存出了问题、跑去看当前命中率——一切正常。 于是结论变成"没有问题"。实际上钱(或订阅额度)在那一轮已经烧掉了。
**盯当前值永远抓不到崩塌,要看历史序列。**从 session transcript 里拉:
jq -r 'select(.type=="assistant" and .message.usage) |
[.timestamp,
.message.usage.cache_read_input_tokens,
.message.usage.cache_creation_input_tokens] | @tsv' \
~/.claude/projects//.jsonl
健康的长会话里 `cache_read` 稳定爬升、`cache_creation` 只有每轮新增的一小条。 序列中间突然出现一行 `read=0 / create=一大坨`——那就是一次崩塌, 发生过、自愈了、被你错过了。
缓存机制一页纸
- 请求前缀按 tools → system → messages 顺序拼接;缓存是前缀逐字匹配: 从第一个不一致的字节起,后面全部作废重算。
- 计费(Anthropic API):缓存写入 1.25×(5 分钟 TTL)或 2×(1 小时 TTL), 缓存读取 0.1×。订阅(Pro/Max)用户烧的不是账单,是 5 小时窗口的额度—— 一样疼。
- TTL 是滑动的:每次命中都重置计时。
- 判读三条(排查时最有用):
read = 0、create巨大 → 最前排就变了(tools 段或 system 段), 典型:工具表变化、system prompt 变化;read = 一个中等小值、create巨大 → 最前排完好,分叉在 messages 靠前处,典型:历史被改写过、MCP 工具异步接入时序差;- 一轮的 usage 全是 0 → 大概率是被打断的轮,那是"没数据", 不是崩塌,别当病查。
原则:不变的放前面,会变的放后面
前缀按序匹配意味着:**一个会变的字节,位置越靠前,烧掉的越多。**
反例:把当前时间放进 system prompt 开头 → 每轮全量重算,缓存等于不存在。
一个真实案例:Claude Code 的 system reminder 里有一个 `currentDate` 字段, **每个进程启动时计算一次**,位置在 prompt 最前排。常驻交互进程没事 (进程活着日期不重算;跨天时 CC 往**末尾**追加一条 date-change 通知, 前缀不动——这是正确示范)。但"每轮起新进程 resume"的自动化架构会在 跨零点后的第一轮触发日期重算 → 前缀第一个字节就失配 → 整份全量重写。 同样的字段,两种架构下代价完全不同。
每轮 resume 架构下这个问题有两条出路:
- 认了它:代价是每天跨零点后第一轮全量重写一次,时间点可预期。 上下文不大的话这是最省心的选择;
- 根治(风险自负的非官方手段):patch 客户端二进制,把那个启动时 计算的日期字段清空,日期信息改由轮末 hook 注入提
Related Skills
Cc Cache Keepalive
Keep the most recently used Claude Code prompt cache warm without polluting the original session.
Claude Prompt Cache Keepalive
Agent skill for Claude Code: keep the 1-hour prompt cache warm while you are away, with a ladder of background
Cache Keepalive
Keeps an idle Claude Code session's prompt cache warm, so the session you come back to does not start cold
Kvguard
Local proxy & TUI that catches 1-token prompt/KV cache prefix misses and auto-heals requests for Anthropic, Op
Claude Code Cache Kit
One setting, the cost math and a checklist for Claude Code prompt caching (5-min vs 1-hour TTL)
Cato Plugin
Cut Claude Code token costs by 35-80% with prompt-cache TTL tracking, pre-read structural indexing, noisy CLI
Related Agents
9remote
Long-running AI coding sessions keep working after you leave the desk, but checking on them from a phone usual
Claudehut DB Reviewer
Persistence and data-access performance review — JPA/R2DBC mappings, fetch strategy, transaction boundaries, m
Cache Prefix
Sistema: data-agents — Plataforma de Engenharia de Dados