Fork of karanb192/cache-tax: Claude Code prompt-cache keepwarm and cold-send guard, plus handoff on cache expiry
  • TypeScript 90.1%
  • HTML 9.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-01 13:28:12 +02:00
.claude-plugin Add a keep_warm tool so the model can arm keepwarm before waiting 2026-10-01 13:23:14 +02:00
.github/workflows Extract cache-tax 2.1.2 into its own repository (#1) 2026-09-18 23:47:05 +05:30
commands Route the keepwarm command fallback to the keep_warm tool 2026-10-01 13:23:31 +02:00
docs/assets Replace outdated cache explainer video (#8) 2026-09-20 17:08:40 +05:30
hooks Add a keep_warm tool so the model can arm keepwarm before waiting 2026-10-01 13:23:14 +02:00
site Site: receipt social card, favicon, og:image on own domain (#5) 2026-09-20 17:08:49 +05:30
tests Add a keep_warm tool so the model can arm keepwarm before waiting 2026-10-01 13:23:14 +02:00
.gitignore Extract cache-tax 2.1.2 into its own repository (#1) 2026-09-18 23:47:05 +05:30
LICENSE Extract cache-tax 2.1.2 into its own repository (#1) 2026-09-18 23:47:05 +05:30
README.md Add a keep_warm tool so the model can arm keepwarm before waiting 2026-10-01 13:23:14 +02:00
tsconfig.json Extract cache-tax 2.1.2 into its own repository (#1) 2026-09-18 23:47:05 +05:30

cache-tax

Keep Claude Code's prompt cache warm during breaks.

On Fable 5.1, a one-hour cache write costs 80x a cache read per token: $20 versus $0.25 per million tokens. Run /keepwarm before stepping away. If you return cold, the guard stops your send once with the estimated rewrite price.

Install · How it works · Costs and limits · Commands

Real recording of cache-tax stopping a cold send with a $6.61 estimate

A two-line recap in a 330k-token session triggered a $6.61 estimate. After resending, the reported cache write was $6.28. Recorded on 2.1.1; current wording says “up to” the estimated token count. These are API-equivalent costs, not extra subscription charges.

Install

Install this fork

claude plugin marketplace add https://forge.elephanthand.com/turnercore/cache-tax.git
claude plugin install cache-tax@turnercore
CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude

Needs early-access function hooks, a one-hour cache and Claude Code left running. Pings cost tokens, including uncapped output.

claude plugin marketplace add karanb192/claude-code-mods
claude plugin install cache-tax@claude-code-mods

Start Claude Code with function hooks enabled:

CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude

In a warm session, run /keepwarm to arm six hours. Use /keepwarm 90m for a shorter window, or /keepwarm off to stop.

Check the main cache lifetime. Included subscription usage defaults to one hour. API-key, usage-credit and cloud-provider sessions default to five minutes, so set promptCacheTtl to "1h" for those billing paths. A 50-minute timer cannot protect a five-minute main cache. See Limits for the fork evidence.

No function hooks yet? The hook version warns by default, refuses once with CACHE_TAX_BLOCK=1, and provides a standalone status-line countdown. Warming is part of this Mod.

How it works

Before a break: /keepwarm arms a bounded window. After 50 idle minutes, a timer inside Claude Code sends one tool-less fork over the session's transcript. It reads usage after every ping and stops if reads are zero or writes reach 10% of reads. There is no background transcript-polling loop.

When you return cold: for a context of at least 50,000 tokens, the guard drops your ordinary message once when its one-hour clock says cold. It shows the estimated rewrite price. Resend to continue, or /clear and start from a note.

A detected cold write automatically arms at least three hours of keepwarm. /cache-tax shows the current cache estimate, warming window and this session's cold-write tally.

Windows belong to individual sessions. An already-cold session waits for your next turn before pinging. A window ends at its deadline; /keepwarm off also cancels it and clears the always setting.

A recorded warming ping

Real keepwarm status showing a 75k-token cache read at $0.02

Sonnet 5, 16 September 2026: the status reported 75k tokens read at $0.02. Nothing appeared in the conversation during the live run. This capture used the one-minute testing interval on 2.0.0; its card's $0.45 rewrite estimate used an older Sonnet price row, versus $0.30 for the same context with the current row. The two-cent ping is a receipt for that session, not a fixed price.

Why a short message can cost so much

When the cached prefix expires, even “give me a recap” can trigger a rewrite of the old context before the answer. The 80x figure compares Fable 5.1's one-hour write and cache-read rates; it is not a claim of 80x total savings.

Watch the 23-second overview. Its designed scenes label Fable 5.1 list prices; every terminal and status-line pixel comes from the real refusal recording and keep-warm receipt above. The refusal was recorded on 2.1.1 and the keep-warm crop on 2.0.0.

New to caching? Anthropic explains how Claude Code uses it.

Commands

/keepwarm               keep warm for six hours
/keepwarm 90m           keep warm for a window of your own (also 2h30m, 6h)
/keepwarm always        arm a six-hour window at every session start, remembered across sessions
/keepwarm 6h every 2m   pinging every two minutes; a testing knob, floor 1m, forgotten after this window
/keepwarm status        the line the status slot shows
/keepwarm off           stop, forget the window, and turn always off
/cache-tax              the card
/cache-tax guard warn   show the price and send (the hook's default)
/cache-tax guard refuse drop a cold send once, the resend goes through (this mod's default)

T3 Code and other hosts that list commands from the init message show them as /cache-tax:keepwarm and /cache-tax:cache-tax; both names run the same commands.

The refusal, verbatim:

cache-tax: the prompt cache went cold 2h00m ago. Sending this re-writes up to 200,502 tokens at $20/MTok = $4.01 (a warm turn would have cost $0.05). Send it again to pay it, and keepwarm will then hold the cache for 3h00m. Or /clear and start from a note.

The figure is an upper bound. The context count the engine reports for a resumed session is the last response's input, cache read, cache write and output together, and the resume payload carries no separate output count to take off; on one 15-day-old session the refusal said 330,316 tokens and $6.61 and the write that followed was 314k tokens, $6.28.

The status slot while keepwarm is armed reads keepwarm 5h10m left · ping in 37m · last ping read 200k $0.05, on a cold cache keepwarm 6h00m left · cold now, first ping 50m after the next turn, and after a stop keepwarm stopped: the ping read 0 and wrote 180k tokens ($3.60), the cache was already gone.

Handoff before cold

For sessions with at least 50,000 context tokens, the mod replaces the final warm ping with a tool-less handoff fork, or schedules one without a keepwarm window. It runs 50 minutes after the last warm request (the custom every interval when set), even if the window ends sooner. Main requests reset the timer; each idle stretch gets at most one handoff. /keepwarm off stops pings but still allows this handoff.

At fire time it reads ~/.claude/skills/handoff/SKILL.md and asks for markdown following its Write section, source auto; a missing skill uses embedded instructions. The reply is saved with $.fs.write to <main checkout>/.handoffs/YYYYMMDD-HHMM-auto.md (UTC), resolving the checkout through git's common directory from the session cwd, with cwd as fallback. The status slot shows the countdown and then the saved path. Start a fresh Claude or Codex session from that file.

A successful fork refreshes the guard's one-hour clock and stops warming, even if saving fails. A later cold refusal or warning names the file. Continuing in the same session deletes it once the user's prompt passes; a refused send preserves it. File deletion uses rm through the host process API. Handoff state, like the request clock, stays in memory.

Work done

The model gets two tools. mcp__cache-tax__keep_warm arms the default six-hour window, the same as a bare /keepwarm, so a model can keep the cache warm before it ends a turn to wait on background work. mcp__cache-tax__work_done is for when a thread's work is finished: the PR merged, the task delivered, nothing left pending. The session's keepwarm window ends and no idle handoff is scheduled; a cold write in the closing turn does not arm the three-hour window either. The next user message puts everything back as it was. The always switch is untouched, so the next session still arms.

T3 Code and other SDK hosts

T3 Code builds its command list from a probe that disables hooks and reads the init message, which comes before session.start registers the mod's commands. The plugin therefore also ships commands/keepwarm.md and commands/cache-tax.md, listed as /cache-tax:keepwarm and /cache-tax:cache-tax, and the mod answers those names too. Without function hooks those files only tell you to turn them on.

T3 Code stops a thread's session when the thread is settled or archived, which ends the pings. In an SDK host the mod also drops the session's window at session.end, so resuming the thread does not bring it back. A snoozed thread keeps its session alive and the mod cannot see the snooze; /keepwarm off or work_done covers it.

SDK hosts print every status update as a chat message, so there only the saved handoff path is shown.

Both forms installed

The hook and the mod share a name and a job, so having both means two guards on every cold send. The mod checks at session start whether the hook's /cache-tax:status command exists and says so once. Keep one. The hook stays for people who have not turned on function hooks. One thing to know before uninstalling the hook: the 🧊 row in a status line wired to cache-tax.js comes from the hook's files, and a mod cannot draw into the status line.

What it can reach

Validated on Claude Code 2.1.285:

❯ ./register.ts hooks: session.start, classic.SessionStart, command.run{command=keepwarm|cache-tax:keepwarm}, command.run{command=cache-tax|cache-tax:cache-tax}, prompt.submit, turn.step, turn.complete, tool.call{tool=mcp__cache-tax__work_done}, session.end, session.compact
❯ ./register.ts calls: $.clock.after (via arm), $.clock.now, $.command.list, $.command.register, $.env.get, $.fs.read (via handoff), $.fs.write (via handoff), $.model.fork (via handoff, ping), $.process.run (via deleteHandoff, handoff), $.session.cwd (via handoff), $.session.id, $.session.model, $.session.usage, $.store.delete (via prune, startWindow, stop), $.store.get, $.store.set, $.tool.register, $.ui.log, $.ui.status (via showStatus)

Reach L2, drives Claude. Sees every prompt you type, every model request's timing and every answer's token counts.

Threat model for cache-tax (reach L2, drives Claude)
1. Reads:    of each prompt, whether it starts with a slash and nothing else (the text is passed on untouched, never kept, never logged); the time; the token counts and model id the engine already holds on turn.complete and on the fork's reply; the resume fields Claude Code computes for settings hooks; the command list, the session id and, when a resumed session's fields carry no model id, the session's model once at start; from its own $.store, the keepwarm deadline and the ping period keyed by session id, plus the global always switch and guard mode; the local handoff skill, HOME, session cwd and git common directory when writing a handoff
2. Runs:     warm pings inside a keepwarm window and at most one handoff fork per idle stretch for contexts of at least 50k tokens, one request per ping period (50 minutes unless the testing knob set it, floor 1 minute), the handoff may run outside the window; never onto a cache the mod already knows is cold, never after a readback that read nothing or wrote at least a tenth of what it read
3. Sends:    nothing leaves the machine except the fork, an API request over the session's own transcript with a fixed ping prompt or the handoff skill and write instruction
4. Persists: in $.store, the keepwarm deadline and the ping period under this session's id, and the always switch and the guard mode for every session; a window that has ended is deleted at stop and at this session's next start, together with the bare keys of a 2.1.0 store; another session's keys are never deleted here, because a read followed by a delete cannot be made atomic against that session renewing its window, so a session that armed keepwarm and never came back leaves two small keys behind; the auto handoff is saved in the main checkout's .handoffs directory (cwd if git resolution fails), and deleted on continuing; its path and the session's cold-write tally live in memory and die with the session
5. Hostile input: the only text it parses is the argument of its two commands, matched against a duration regex and five literals; of the prompt text only the first non-blank character is inspected, for a slash; the optional local handoff skill supplies instructions to a tool-less fork; fork results are normalized before reading usage; a refusal only ever drops the user's own message, and the resend is unconditional; if a hook throws, the engine skips it and the message enters unguarded, with one dim line

Cost and the plan-limit question

Prices are the list table, where Fable 5.1 reads at $0.25, writes the 1h tier at $20 and answers at $50 per million tokens; Sonnet 5 has its own row ($0.20, $4, $10). On an API key the arithmetic is plain. A ping bills the cache read, its own few uncached tokens at the base rate, and whatever the model says back at the output rate; the figure in the status slot counts all of it, since a fork takes no output cap and a model at high effort may think before it says "warm". A comeback after the lapse is a write. The card prints the read-only upper bound for your model, 80 pings on Fable 5.1, with the idle that covers at the current ping period; a real ping costs a little more than a read, so the true break-even sits below that number. On a subscription the dollars are a yardstick, not the bill, and how a cache read weighs against the 5-hour and weekly limits is not documented anywhere I could find. Watch the rate-limit row of your status line during the first window.

Limits

  • Function hooks are early access; nothing loads without CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1, and the API can change between releases.
  • The 50-minute schedule needs a one-hour main cache. Included subscription usage has it by default. Other billing paths need promptCacheTtl set to "1h".
  • One transcript-verified return at the default interval, Fable 5.1 on a subscription, 19 September 2026: /keepwarm armed at minute 42 of a break, the ping fired at minute 50 and read 157k, and the message sent at minute 62 read 156,886 tokens from cache and wrote 2,255. Claude Code's own cache countdown reset to 58 minutes after the ping, so the fork carried the one-hour TTL. One run, one configuration: it shows the ping kept that cache alive past the hour, not net savings for every setup. Claude Code documents forks in its non-main request bucket, normally five minutes. In this run, the fork still read the one-hour session prefix and reset the native countdown. That is observed behavior in one configuration, not proof that every provider handles the buckets identically.
  • A ping that reads warm proves the cache was warm then. Prefix changes can invalidate the cache regardless of time. The exact effect depends on the model and when Claude Code applies the change; see Claude Code's cache behavior.
  • Resume fields let the guard check the first ordinary send after resuming. If those fields are absent, the first turn seeds its clock and context.
  • The refusal's token count is an upper bound: it is the context the engine reports for the last response, which on a resume includes that response's output, and the mod has no separate output count to subtract.
  • The cold-write tally is per session and in memory; /clear empties it.
  • Context size is the engine's live window figure. A turn's own usage is its responses summed, which on a ten-step turn is ten reads of the context, so it is only the fallback where the host reports no live figure.

Local development and persistent setup

From a local checkout, load it for one session:

CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude --plugin-dir .

To keep it on, add this to ~/.claude/settings.json, which also loads the hooks module of every other installed plugin that ships one:

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

Prove it on your own session

The mock-clock tests prove the timer, the guard and the scoring, not that a fork hits the main cache. One ping proves that, and it costs a cache read plus the answer. In any warm session:

> Reply with one word: ready
> /keepwarm 1h every 1m

Watch the status slot after a minute. last ping read 20k $0.01 reports the fork's readback. keepwarm stopped: the ping read ... and wrote ... means the readback did not meet the warm threshold; the loop has stopped. A null reply shows stopped: the engine did not send the ping, either the snapshot was cold or the API call failed instead. Finish with /keepwarm off. This short check establishes a cache read, not retention across the default 50-minute interval.

Tests and typecheck

Run from the repository root:

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

The 55 tests use a mock clock and engine. They cover:

  • Guard: refuse once and resend, warn mode, slash commands, small contexts, resume seeding and cold-write scoring.
  • Warming: command defaults, always/off, idle resets, usage-based stopping, cold-window expiry and delayed timers after sleep.
  • Handoff: final-ping replacement, no-window scheduling, skill reload/fallback, checkout/cwd paths, clock refresh even on save failure, path-bearing guards, deletion on continue and fork result normalization.
  • Session state: isolated store keys, restored windows, legacy cleanup, clear/compaction resets and subagent isolation.
  • Pricing and display: model matching, output and uncached input costs, context counts, duration formatting and the read-only break-even figure.

These tests do not validate server-side cache retention. See the live check.

Typecheck

Generate declarations by running /plugin-types inside a Claude Code session opened in this repository. Then, from the repository root:

npx -p typescript tsc -p .

Generated declarations live in .claude/types/. Keep them untracked.