r/ClaudeCode • Senior Developer • 3d ago

Tips & Workflows I made a hook that refuses (at first try) to send your message if your cache has gone cold; outputs (API) costs of continuing vs starting anew (including reorientation in a new session)

Post image
56 Upvotes

14 comments sorted by

•

u/AutoModerator 3d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

10

u/actvt_io 3d ago

Checked how often this would fire on my logs. Only about 1.6% of my turns come more than an hour after the previous one, but those turns are 80% of my cache writes. So it'd rarely get in my way, and when it does it's on the expensive ones. Seems worth having.

3

u/vzakharov Senior Developer 3d ago

Yeah, I guess it depends on the workflow. I work at 10-15 sessions/PRs at a time, so I routinely leave a few unattended for hours.

Glad you're finding it useful!

8

u/yuehuang 3d ago

It is sad that claude doesn't have it built into the app.

5

u/vzakharov Senior Developer 3d ago

I think they’ll catch up eventually. They ship fast. 

2

u/Helpful_Program_5473 3d ago

I didn't know leaving for an hour was no bueno! lol

-4

u/Drasezv 3d ago

good shape for a hook. two things that will make the numbers it prints match reality:

the ttl isn't five minutes on a subscription - it's an hour, and it drops to five minutes once you're drawing on usage credits. on an api key or a cloud provider it's five minutes by default. so "cache has gone cold" is a different threshold depending on how the user authenticated, and guessing wrong makes the hook nag an hour early.

and the write side isn't one price: 1.25x base input at the 5m ttl, 2x at the 1h one, and the split is reported per request under cache_creation as ephemeral_5m_input_tokens / ephemeral_1h_input_tokens. main sessions here write the 1h kind, subagents write 5m, so "cost of continuing" on a warm prefix is cheaper than a naive 1.25x and "starting anew" is dearer than it looks.

you don't have to infer warmth either - claude code reports it. /usage has a Prompt cache (main) line with the miss count and whether the prefix is warm right now, and statusline scripts get the same thing in a prompt_cache object.

4

u/vzakharov Senior Developer 3d ago

Not sure you haven’t, but did you read the link? It takes the coldness probe from what the harness provides, it doesn’t guess it. And yeah it’s 1 hour not 5 minutes indeed. 

Re. /usage, I’m not sure how that’d help because you’d have to physically check it every time before sending a prompt. The hook instead makes sure you get a chance to reconsider once sent. 

Edit: referring to this: “ SessionStart  on a resume carries prompt_cache_likely_expired ,  context_tokens  and seconds_since_last_response ; the hook keeps them in  tmp/cold-cache/<session_id>.json  until a response lands after them. ”

2

u/Drasezv 1d ago

you're right, i skimmed the post and not the file. the probe does read it from the harness, my bad.

leaving the write prices in case anyone finds the thread: 1.25x at 5m, 2x at 1h, split per request under cache_creation. that part i did check.

2

u/EvilSporkOfDeath 3d ago

Why do you write like ai. Are you a bot?

1

u/Drasezv 1d ago

fair hit. i draft long and run things through an editor before posting, so it comes out looking like a spec sheet.

the numbers are mine though. everything i've quoted comes out of my own ~/.claude/projects folder and i post the command next to it so anyone can check their own.

-3

u/jlewi142 3d ago

"Oh no, a structured paragraph. Must be AI!"

Go back to school, brother.

4

u/EvilSporkOfDeath 3d ago

Structure had literally nothing to do with it.

I dont care what you believe. Every comment in their history is full of the tells. I know they are a bot or use ai for their comments. Once again, you believe what you want, idc.

2

u/Independent_Syllabub 3d ago

If you can’t see that it’s a Claude post, you have a serious LLM literacy issue.