Files
gabrielvidalandClaude Opus 5.5 4842063c04 chore: stand-alone repo (moved out of ai-agent tools/cache-score)
- the repo root is both the plugin and its marketplace:
  /plugin marketplace add https://git.gabvdl.xyz/gabrielvidal/cache-score.git
  /plugin install cache-score@cache-score
- URLs point here; calibration doc linked on the ai-agent
- curl install of the bare script, package.json (npm test, bin), MIT LICENSE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:55:46 +01:00

138 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# cache-score
How well do you keep Claude Code's prompt cache warm, and what does it cost
you when you don't? A Claude Code plugin with three parts:
- **`cache-score`**: a report with a score out of 100, every message you sent
into an expired cache, what those cost in list dollars and in % of your plan,
and when you tend to come back too late.
- **A status line**: `● cache 42m · hit 98 % · 331K` while the cache is warm,
a `/compact` hint in its last 10 minutes, and
`○ cold 2h · next msg re-sends 331K ≈ $2.46 (1.1 % 5h) · new task? /clear is free`
once it has expired.
- **A nudge**: the first message you send into an expired cache of 100K+
tokens is held once with what it would cost. Press ↑ then Enter to send it
anyway.
One Node file (`scripts/cache-score.mjs`, Node ≥ 18, no dependencies), MIT.
It started in the [ai-agent](https://git.gabvdl.xyz/gabrielvidal/ai-agent), whose
"fresh context" nudge does the same inside that app. It
reads your local transcripts and the plan your Claude Code login caches in
`.claude.json`. It never reads a token and sends nothing anywhere.
## Install
In Claude Code:
```
/plugin marketplace add https://git.gabvdl.xyz/gabrielvidal/cache-score.git
/plugin install cache-score@cache-score
/cache-score:setup
```
The nudge works as soon as the plugin is installed. `/cache-score:setup` adds
the status line to `~/.claude/settings.json` (backup in
`settings.json.bak-cache-score`). If you already have a status line, it keeps
yours and puts the cache line under it. It also links a `cache-score` command
in `~/.local/bin`. `cache-score uninstall` puts your old status line back.
Without the plugin, the file works on its own:
```
curl -fsSL https://git.gabvdl.xyz/gabrielvidal/cache-score/raw/branch/main/scripts/cache-score.mjs -o cache-score.mjs
node cache-score.mjs # the report
node cache-score.mjs install # status line + a `cache-score` command (no nudge without the plugin)
```
## Why it matters
- On a Claude subscription, Claude Code writes the conversation to the cache
with a **1-hour lifetime**, and every request that hits it restarts the hour.
On an API key, on extra usage once you're past your plan's limit, and for
subagents, it's **5 minutes**
([docs](https://code.claude.com/docs/en/prompt-caching.md#which-ttl-each-request-gets)).
- A warm turn reads the context at 0.05× the input price on Opus 5.5 and
Sonnet 5.5 (0.1× on older models). A message sent after expiry writes it
again at **2×**. On a 300K Opus thread that's ≈ $2.40 instead of ≈ $0.06.
- `/compact` reads the history from cache while it's warm. Once the cache has
expired, it re-processes everything uncached
([docs](https://code.claude.com/docs/en/prompt-caching.md#compacting-the-conversation)).
So compact **before** you step away, not after. `/clear` costs nothing.
- Claude Code ≥ 2.1.290 compacts a conversation over 200K tokens by itself
just before the hour runs out (`idleCompaction`), as long as the terminal
stays open and the machine stays awake. A closed laptop lid defeats it.
## The report
```
cache-score [--days 30] [--list] [--json] [--no-color] [--tier T] [--dir D]
```
- **Score**: of all the context your messages re-sent, the share that came
from cache. Each message counts in proportion to its context, so a cold
return on a 400K thread weighs 20× one on 20K.
- **Cold return**: a main-conversation request sent after the cache's lifetime
that re-wrote what the previous request had cached. The rule is Claude Code's
own (`prompt_cache.misses`): more than 5 % and at least 2,000 tokens short,
with no compaction in between. The TTL comes from each request's
`cache_creation.ephemeral_1h/5m` split, so 5-minute sessions are judged on 5
minutes. Its cost is the re-written tokens × (write rate − read rate).
- **Other cache breaks**: same rule, but within the lifetime. Causes are a
model switch, an MCP or tool change, or a Claude Code upgrade.
- **Compacting before you left**: what the cold returns would have cost if
the thread had been compacted while still warm. Each return then replays
≈ 58K, measured on 24 real compactions in the ai-agent.
- Forked sessions are counted once. Subagent calls count toward spend, but
not as cold returns (they keep their own 5-minute caches).
## What 1 % of your plan is worth
The plan meters usage in list-price dollars: cache reads at their price,
writes at theirs. Fitting the % Claude reports against transcript spend gives
the exchange rate ([how](https://git.gabvdl.xyz/gabrielvidal/ai-agent/src/branch/main/docs/usage-calibration.md)):
| tier (`userRateLimitTier`) | plans | list $ per 1 % of the week | per 1 % of 5 h | source |
|---|---|---|---|---|
| `default_claude_max_5x` | Max 5×, Team premium seat | **$12.80** | **$2.14** | measured, Nov 2026 (R² 0.81 / 0.88) |
| `default_claude_max_20x` | Max 20× | **$25.90** | $5.60 (Oct; no 5 h window since) | measured, Nov 2026 (R² 0.89) |
| `default_claude_ai` | Pro | ≈ $3.70 | ≈ $0.43 | estimate |
| anything else | Team standard, Enterprise… | Pro estimate | | guess |
**Other tiers don't scale by their name.** A Max 20× week holds about **2×** a
Max 5× week, not 4× (its 5-hour window held about 2.6×). So multiplying one
measurement by "N×" gives the wrong answer. That's why the Pro row is only an
estimate, from Anthropic's published weekly hours (Pro ≈ 1/3.5 of Max 5×) and
5-hour windows (1/5).
**So every install calibrates itself.** Each time the status line renders, it
receives the plan's own `rate_limits` (5-hour and weekly %). It keeps a
reading in `~/.claude/cache-score/samples.jsonl` whenever the reading moves.
The report divides the list dollars your transcripts spent between readings
by the points the window rose. It uses that rate instead of the seed once
there are 12 points of the week (30 of the 5-hour window).
Usage from other machines or claude.ai moves the % without leaving
transcripts here, so the calibrated rate can only come out low (pessimistic).
The plan also weighs Fable about 2× and Sonnet about 1.5–2× per list dollar
compared with Opus. The figures are good to ±30 %.
## Settings
| env | default | |
|---|---|---|
| `CACHE_SCORE_NUDGE=0` | on | turn the nudge off |
| `CACHE_SCORE_NUDGE_MIN_TOKENS` | `100000` | smallest context worth asking about |
| `CACHE_SCORE_DATA` | `~/.claude/cache-score` | readings, the status line's copy |
The nudge leaves `-p` runs and apps that resume sessions alone. It never holds
a slash command or a `!` shell line, and it doesn't fire after an idle
compaction.
## Development
```
npm test # node --test, no dependencies
claude plugin validate .
claude --plugin-dir . # try it without installing
```