r/ClaudeCoding • • 4d ago

Idle Claude Code sessions silently re-pay their whole cache when you come back. A keep-warm hook cut that ~87% across my 53 sessions.

**TL;DR:** (but read if you like saving $ and maximizing your limits...) When a long Claude Code session sits idle past its cache lifetime (1 hour in my sessions) and you come back, your next message pays to rebuild the whole conversation's cache. A tiny automatic reply every ~50 minutes avoids that. Measured from my own session logs since I've been using this...I have a membership, and it still factors in for tokens not wasted, but for API-equivalent costs: across 53 sessions, 474 pings cost ~$67 vs. ~$520 of cold-cache rebuilds.

Eg. of long session alone: 35 pings, ~$3.50, avoided ~$25.
"One ping costs ~1/40th of a full cache rebuild."

**The problem many seem to miss...**

Claude Code caches your conversation so each message doesn't re-pay for everything before it. Reading that cache is cheap, but it expires. If you walk away longer than its lifetime (dinner, sleep, a long build or training run) and then reply, the entire conversation is written to the cache again at the expensive rate. On a big session that's a real hit every time you come back.

**What I set up (the gist)**

One Claude Code **Stop hook**, with no cron or external timer. Claude Code can run a Stop hook in the background and wake the session when the hook exits with code 2 (`asyncRewake`). Mine starts a small watcher at the end of every turn. It checks the session transcript once a minute, and after 50 idle minutes it exits with a short instruction: reply with exactly "warm · 9:16 AM" (the script stamps the time, since the model has no clock), no tools, no file changes. That ~15-token reply re-reads the cache, which resets its 1-hour timer, and the end of that turn starts the next watcher.

Safety rails:

- Stops after 20 consecutive pings (~17 hours) until I send a real message, so a forgotten session can't ping forever. (In practice it never triggered: I always came back or closed the session first.), just there in case you totally space out, go touch grass for a few days and forget about it...it's not pinging/costing you perpetually...

- One watcher per session, and it dies with the session, so closing a session ends the pings.

- Global and per-project off switches.

- Logs each ping's `cache_read` / `cache_creation`, which is how I know every ping was a cache hit.

**The numbers** (Based on Opus 5.5 pricing: cache read = 0.05x input, 1-hour cache write = 2x input)

- One ping costs ~1/40th of a full cache rebuild.

- One long session: 35 pings over 8 idle stretches (1 to 12 hours) = **$3.50**; rebuilds avoided = **$25.23**. Every ping was a cache hit (big read, ~335 new tokens written).

- All 53 sessions I have historically: pings **$67**, rebuilds avoided **$520**. Only ~$11 of the $67 went unused: $9 in sessions I never returned to, $2 where I came back within the hour anyway.

- Break-even is ~40 pings on Opus 5.5, so with the 20-ping cap, any return within ~17 hours is a win. On models where cache reads cost 0.1x, the cap sits right at break-even.

**When it does NOT help**

- **You leave a session open and never come back.** Those pings are wasted. Close sessions you're done with: a closed session never pings, and the cap limits any one you forget, but it's always better to start fresh in general, the problem is starting over it not always optimal for a given instance, hence this solution...

- **You come back within the hour.** The cache was still warm, so that ping wasn't needed. Unavoidable (the hook can't know when you'll return), but tiny: $2 across ALL my sessions.

- **You come back between 50 and 60 minutes.** A ping fired at the 50-minute mark, but the cache would still have been warm when you returned, so that one ping wasn't needed. Come back sooner and no ping fires at all ($0). The hook can't know when you'll return, so this can't be avoided, but it's tiny: 14 such pings, ~$2, across ALL my sessions.

- **Your session uses the 5-minute cache.** This relies on the 1-hour cache; a 50-minute ping does nothing for a 5-minute one. Check yours (below).

- **On a subscription,** the dollar figures are API-equivalent, not actual charges. The benefit there is stretching your usage limits. Anthropic doesn't publish exactly how cache reads and rebuilds count against plan limits, so read the numbers as ratios. The savings assume your plan weighs a cheap cache read far less than a full rebuild, as API pricing does, so your limits stretch further.

**The bigger lesson: session size**

My long session for eg. had grown to ~740K tokens. Every ordinary call re-reads the whole thing (on average ~386K tokens per call over 267 calls), and that re-reading cost ~$20, about as much as the keep-warm saved in that session. Keep-warm is great for an important session you can't restart yet. But when you can, start a fresh session (with good notes or a handoff summary) so each call re-reads far less.

**How to check your own sessions**

Claude Code keeps transcripts as JSONL under `~/.claude/projects/<project>/`. Each assistant entry has `message.usage`:

- `cache_read_input_tokens` / `cache_creation_input_tokens`: a healthy resume shows a big read and a tiny write. A large `cache_creation_input_tokens` on the first message after a break is the expensive rebuild this avoids.

- `cache_creation.ephemeral_1h_input_tokens` vs `ephemeral_5m_input_tokens`: tells you whether your session uses the 1-hour or the 5-minute cache.

As with most stuff, have Claude Code set this up for you and ask me anything you want. Happy Coding!

41 Upvotes

12 comments sorted by

9

u/txgsync 4d ago

Literally "/loop 15m report current status" in Claude Code.

4

u/cleverestx 2d ago

/loop does keep the cache warm. Mine just does it better.

Why?
It only fires when I'm actually away. It waits for 50 minutes of no activity, then sends one tiny ping. While I'm working it does nothing. /loop 15m fires 4 times an hour whether I'm there or not. Messy.

As mentioned, the ping is tiny. It's a one-word reply with no tools. "Report current status" makes Claude write a whole report every 15 minutes, which piles up in the conversation all night. If you leave yours running...OOPS.

Also, It's automatic. It's a global hook, so it runs in every session without me typing anything. And it stops on its own if I don't come back for about 16 hours, instead of running for 30 days, saving from that left running rare case (we all forget things, sometimes.)

And before anyone says "just do /loop 50m": /loop converts the interval to cron, and 50 minutes doesn't fit cron, so it gets rounded to every 60 minutes. The cache expires at 60 minutes, so that's cutting it too close and some pings will land after it's already gone.

so...literally not.

3

u/benfinklea 4d ago

I’m gonna try it

1

u/cleverestx 2d ago

Let me know how it goes!

2

u/Honkey85 4d ago

Are projects sessions or is every thread in the project a session?

1

u/cleverestx 2d ago

Every thread is its own session. A project in Claude Code is basically just the folder you're working in, and each conversation in it gets its own session ID and transcript. The cache works the same way: each conversation has its own, so two threads in the same project don't keep each other warm.

The hook runs per session, so every open thread gets its own little timer and gets pinged on its own schedule. Close a thread and its timer dies with it, so old conversations you've walked away from for good don't keep burning usage.

1

u/johnerp 3d ago

I just have a rule that states to set a monitor to fire every 59m, summarising the session and be explicit calling out anything needed from me.

1

u/cleverestx 2d ago

Sounds sort of like mine, just more of a token cost/usage per hour. The summary is nice, I would just rather ask cause because I might be a few hours away from it, and I just would want the token hit on the hour I am back... but I can see how that would be useful.

2

u/johnerp 2d ago

It’s all cached input tokens, and very few output, for me it feels like it’s made a huge impact. I didn’t monitor anything but my usage is going further, and I don’t get these huge jumps in usage randomly.

1

u/cleverestx 2d ago

At least we are all trying to improve this....I think 99% of people are just wasting the money/usage in these scenarios.

1

u/archpoke 2d ago

Good numbers, and the break-even maths is clear. The $9 you lost in sessions you never returned to is the case I think about most, and the 20-ping cap is a reasonable guard against it.

I build a free plugin, Rightsize (MIT), that takes the same idea and decides per break instead of using a fixed cap. It keeps a record of how long you usually stay away, sends a keep-warm request only while a return is likely enough to cover its cost, and sends at most 2 per break. It stops when you type, when a request misses the cache, or when keeping warm would cost more than coming back cold. If its own requests cost more than they saved, it pauses itself. The same plugin also picks the model and effort for each prompt: https://github.com/archid21/rightsize

Your point about session size matters too. For that, Rightsize shows when the cache goes cold, what a cold return costs and how full the context is, with a compact or handoff button.

1

u/cleverestx 2d ago

Cool I'll check it out. Thanks for sharing.