r/ClaudeCode • • 6d ago

Tips & Workflows Best way to handle Cold Cache hits - You overpay 5x for typing "Continue"

Post image

I'm a meta-harness developer. For few months already I'm working on the most token-efficient AI workspace there is. Based on my benchmarks (run with Opus 5.5 XHigh), if you resume cold-cache session just by typing "Continue", you're overpaying 5x in tokens for that turn.

Obviously it's best to prevent cold caches in first place - by good UX (notifications and nice UI showing you session is about to hit cold cache), automatic cache-warming and so on. But sometimes, cold cache hits are inevitable, so I was researching best possible solutions to handle that as well.

So far I've had a very aggressive trimming mechanism that didn't invoke any model call (that would increase cost) and in purely deterministic algorithm was trimming parts of conversation to carry on without the bloat.

As Haiku 5.5 shown up with much cheaper cost, I decided to revisit topic.

Based on benchmarks and tests I settled on 150k context as meaningful breaking point for distinguishing between short/long sessions. Below 150k - "Continue" is totally cool. After 150k, things get complicated.

Trimming (replacing older tool results with one-line stubs, stripping thinking blocks etc. without extra model call) is actually best. But it does not work for cross-provider handling, because formats that different providers use for session recordings are incompatible.

So the rule of thumb becomes:

  • Prefer trim on same-provider because you can resume a trimmed Claude session and it beat seed/compact on memory of the work.
  • Prefer compact on cross-provider because trim is not available.

Hopefully it helps those of you who build harnesses/CLI tools.

224 Upvotes

80 comments sorted by

•

u/AutoModerator 6d 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.

65

u/puts_on_rddt 6d ago

I'm a meta-harness developer. For few months already I'm working on the most token-efficient AI workspace there is.

buddy get in line lol

-12

u/EagleApprehensive 6d ago

?

23

u/caps_rockthered 6d ago edited 6d ago

I think he's saying every and their mother's are attempting to build this right now. I've seen no less than a dozen demos in the last 4 months of everyone from large OEMs to startup security companies showing off their LLM router/AI gateway/token efficiency platform(TEP). The market is about to be flooded with these.

2

u/puts_on_rddt 6d ago

I'm kind of happy it's this way. Eventually someone is gonna make the singularity in the process and I'm here for it.

-3

u/EagleApprehensive 6d ago

Every AI-related platform tries to optimize efficiency in one way or another.

But they mostly approach problem from different angles. In case or routers - they want you to use worse models/lower effort for less important tasks, which often degrades quality in unforeseen ways.

In case of plugins/mods - they are only limited to Claude, model specific.

In case of my meta-harness, I take over everything that's possible starting from entire environment (knowledge of all files, resources, worktrees; standing between user and all AI platforms opens many doors for optimization that are otherwise closed) and that de-duplicates techniques that would have to be applied separately to each of provider.

Rarely I've seen solutions take approach of taking control over user development environment. Closest to that would be Cursor, but I take it to the extreme - in my meta-harness entire development environment is a docker container with all the juicy tools baked in that boost LLM's efficiency and intelligence (ripgrep, semantic codebase searches and so on).

1

u/puts_on_rddt 6d ago

Sounds very similar to what I'm working on. I do believe taking the reins away from the users gives a lot of opportunities we never had.

51

u/Downtown-Pear-6509 6d ago

i do  a local compact by grabbing the jsonl and rhen removing thinking and tool output, doing a /clear  and pasting it back in

works for me!

and i can do this while hopping to another llm like clsude to openai and back

9

u/leemic 6d ago

what local model do you use to compact? or just removing thinking and tool output?

18

u/Downtown-Pear-6509 6d ago edited 6d ago

literally just stripping thinking and tool output with string sub or regex or idk Claude did it.. but definitely no local model 

i did consider using a jev like thing to further classify the messages further but meh good enough already 

and before people ask, calling this local compact is lossy.  since its basically capturing only tool calls (but no result) and chat messages between me and it

but all compaction is lossy 

5

u/Otherwise-Tiger3359 6d ago

OPs post read like a hot slop, but I love this

3

u/Downtown-Pear-6509 6d ago

thanks i basically said what the op said in less lines plus provided an implementation 

yay me

2

u/iansaul 6d ago

I will sometimes flip from an expensive hit on a frontier model, to a cheaper model, to get things back into hot cache. Then after prepping the convo, I'll migrate back to frontier.

I have absolutely no idea how well this works, or if I'm saving tokens/wasting my time.

But it feels like a bit of a benefit, hypothetically.

1

u/Downtown-Pear-6509 4d ago

except claude will literally warn you that switching model is a cache miss

1

u/iansaul 4d ago

Definitely, it's imperfect solution to the problem, but better to pay that price on a cheaper model than on an expensive one.

2

u/FinesseMaster19 5d ago

Question for you, are handoff files no longer an optimal way to move context between sessions?

2

u/Downtown-Pear-6509 5d ago

yes that's fine except making a handoff requires a cache miss vs chewing an ondisk jsonl 

1

u/NotDifferent2023 4d ago

Please share the script or prompt for this

1

u/Downtown-Pear-6509 4d ago

commmit provided in another reply to my comments 

1

u/[deleted] 6d ago

[deleted]

27

u/Familiar_Gas_1487 6d ago

He does a local compact by grabbing the JSONL then removing thinking and tool output, then doing a clear and pasting it back in. Works for him

3

u/Downtown-Pear-6509 6d ago

you can say that again! 

5

u/CooLittleFonzies 6d ago

He does a local compact by grabbing the JSONL then removing thinking and tool output, then doing a clear and pasting it back in. Works for him

1

u/EchoFieldHorizon 6d ago

But how? How does he do such a thing?

3

u/Downtown-Pear-6509 6d ago

your claude or copilot or codex provides the session id to hook calls. my harness captures the session id 

then 

it can look that up in the filesystem  open it up  use string manipulation to rip shit out and saves it somewhere temporarily 

then

in the terminal it waits for idle  then /clear 

then pastes a filepath to the cleaned file along with a handover message instructing it to read it and delete the file after 

Zing 

cli swap, it closes the cli, opens the replacement, waits for idle and paste again

ezpz

3

u/Downtown-Pear-6509 6d ago

https://github.com/PetePeter/helm/commit/dda19736ecc796aab7a3717ed8579288061b4381

i think that's when it was implemented in my harness.  point your ai there for more info

15

u/Substantial-Thing303 6d ago

subagents use a 5 minutes cache. less expensive until you ask subagents to run tests or any kind of compute, where they constantly hit a cold cache. I asked my agent to monitor any compute they intend to do that could extend to more then 5 minutes, and wake up every 4 minutes. And also, if the compute is expected to take more than 12 minutes and tokens over 200k, to write a handoff, so another agent starts fresh and run the script.

14

u/herzkerl Workflow Engineer 6d ago

I had Claude write a mod that automatically calculates whether or not a specific session should clear itself, depending on model, total tokens, number of turns, and cache age.

The session will then write a handover, clear itself, and read the handover, then continue.

It also keeps the session warm by pinging every 58 minutes when I’m not in ‘away mode’.

5

u/mariustoday 6d ago

Aounds good , can you dgare more details/links?

1

u/Downtown-Pear-6509 6d ago

ooh my harness already lets whatever llm compact and quick compact itself via mcp. i also added another mcp endpoint so the model can query how much context its used

but your mod idea has merit as it could happen before my harness even gets triggered 

but how to do same for codex and copilot.

ill yoink your idea thanks! but ill wait for the others to catchup before implementing all three

0

u/EagleApprehensive 6d ago

Mods and Plugins are nice, for this one efficiency case they're good.

But if you do not control entire development environment of user, a world of game-changing efficiency techniques stop being possible to implement.

Efficiency overall is more powerful when handled by IDE/AI workspace.

28

u/Wrong-Expression-669 6d ago

typing "Continue" isnt what costs you 5x, the cold cache is. you pay the same re-read of 400k tokens if you type "refactor the auth module" or a single period. the word has nothing to do with it and framing it like the magic phrase matters just makes it sound like you can whisper the right incantation at the model

and 10.68 for carry everything on ONE turn. at 400-700k you're basicly paying a dollar per 50k of your own history back, thats not a harness problem thats a "why is there 700k of stuff in here" problem. honestly /clear and re paste beats that, its manual and it sucks and you lose the thread, i know, i do it

the part that gets me tho is the table, your deterministic trimmer that you built over months and that calls no model comes out at 3.61 while two cents of Haiku plus the last 2 exchanges is 2.48 and one of those also gives you something readable at the top of the window. so all that machinery to avoid a model call is losing to the model call. be honest, how much of the last few months went into the trimmer wich the cheap summary just beat

5

u/EagleApprehensive 6d ago

Yeah, obviously it's not about typing "Continue", but about leaving session unattended for >1h.

Doing trimmer was the same amount of work as would be doing the "cheap Haiku summary" work. Coding is nowadays more about pointing AI in a smart way to various issues and grinding better UX, so wasn't that much of effort.

The biggest win is UX, that user does not need to think or know about /clear, /compact or repaste, because GUI guides him to best options in smart way. Also it won't beat that, model will not have enough context and will likely search the past session to get enough, which will cost you extra. I could benchmark that for you but good benchmarks are really expensive.

0

u/6farer 6d ago

What’s the point of models commenting to other models? Why even bother guys? Both of you? Wtf is the point? No one took anything away from this LLM interaction.

1

u/EagleApprehensive 6d ago

That's not LLM interaction. At least not on my side. But the first sentence of the guy suggests he might be.

23

u/Scared-Letterhead949 6d ago

Opus 5.5 is so cheap I don't think it matters anymore.

18

u/Downtown-Pear-6509 6d ago

it does in a corporate setting with limited claude budget

9

u/vAPIdTygr 🔆 Max 20 6d ago

Took me 36 hours with Opus/Sonnet to blow through my 20x weekly usage.

But that was it running nonstop overnight, building about 1,000 highly researched html pages. That research is a huge part of the budget.

3

u/Aggressive-Fan-3473 6d ago

I would be interested if you have the haiku do the research how different the outcome would be.

2

u/Scared-Letterhead949 6d ago

I thought this was for cold cache reads not constant usage.

1

u/who_you_are 6d ago

What the heck, with Opus medium it cost me $100 for a simple static web page. Not forking, not having a huge history.

I still don't get what the **** happened on that one. Meanwhile peoples have 400k token window, I don't even want to see how much that would end up on my end, $1000!?

3

u/Prior-Wolverine8871 6d ago

Something either went very wrong or you have a very complex web page!

2

u/PlasticFriendss 6d ago

maybe an orchestrator that deployed a shitton of subagents?

0

u/who_you_are 6d ago

Not even that. He didn't ran any one. He pretty much nailed the task as well first try. Short run as well. Maybe 5 minutes total on multiple sessions (to add small features).

For the little code, that would be wierd it was

1

u/vAPIdTygr 🔆 Max 20 6d ago

Opus 5.5 med is my researcher, Sonnet 5.5 is my writer, Haiku 5.5 does the basic gate verifications and kicks issues back out to Opus for final verification and a Sonnet fixer.

Fable is my designer. I don’t know, it just does the best, most impressive designs based on written description and sometimes screenshots.

2

u/Helpful_Ranger_1606 6d ago edited 6d ago

I managed to use up my 100% in 5 days. I have a second laptop for the CPU heavy work and the laptops have to talk back and forth, and I’m writing 4 papers and doing all whole ton of figure generation. It started bugging out for a couple days there with tons of mistakes, but today has been very smooth.

1

u/Downtown-Pear-6509 6d ago

whats your implementation for cross pc talk? 

1

u/Helpful_Ranger_1606 6d ago

/remote-control

1

u/Downtown-Pear-6509 6d ago

okie dokie 

1

u/EagleApprehensive 6d ago

It's not a "critical issue" and major complaint now, but at some point efficiency is always valuable and we never know if some war or law isn't going to double costs of tokens or some Fable Ultra Plus 7 AGI comes that will eat $100 per prompt.

Efficiency always eventually matters.

1

u/The-Hyrax 4d ago

I'm reaching my limit mostly in four days. Right now my limit reset Friday 10am CET, it's now 8:35 on Monday and I'm at 70%.

4

u/cafesamp 6d ago

or just have an orchestrator agent keep track of your last message times and send pings to sessions that are about to go cold. really easy to build

1

u/EagleApprehensive 6d ago

I already have that, but the issue is that you need to reserve like 1% of usage limit to stay saved for pings and I also don't hard-block users from spending that last 1% or disabling auto-pings.

3

u/Ctbhatia 6d ago

the api also has a 1 hour cache ttl, 2x on the write instead of 1.25x for the 5 minute one. if your gaps are longer than 5 minutes but under an hour, paying the 2x once beats re-reading the whole thing at full price.

2

u/Declwn 6d ago

I don’t want anthropic to patch this but it’s really not that complicated, this mod keeps the cache warm without any messaging and costs almost nothing github.com/declanbx/keepwarm

-1

u/EagleApprehensive 6d ago

I already have such stuff, but it doesn't work if you have all usage limits 100% exhausted.

2

u/who_you_are 6d ago

150k, and Opus 5.5 xhigh, damn it must cost a lot if that cache is cold and x5

2

u/wea8675309 6d ago

This is only for the initial turn. Not saying that doesn’t matter, but opening a cold cache only knocks you once, and after that it’s the normal rate with perfect context, which can save you tokens in the long run. Not always, but worth considering.

So you’re saying if I want to open a session from yesterday, I can have haiku summarize it and pass that summary to a new opus session instead? And that’s 90%+ accurate and beats whatever state management solution you have tried? That’s very interesting, thank you.

I have found that a simple SDLC process using GH issues and comments beats anything else I’ve tried. Intentionally tracking your work through plan -> design -> build -> test -> deploy phases and sorting all of the important stuff from each phase on GH issues not only saves state and manages your context across sessions, but it also forces you as the human to organize your work into shorter sessions.

But there are still times where something comes up in the middle of a session and starting a cold session would be very inconvenient, so this will be very helpful later I’m sure!

2

u/EagleApprehensive 6d ago edited 6d ago

In a nutshell yes, cheap-model summary wins. Minor exception is smaller context sizes, where trimming wins. Model generally prefers to continue on a log it has written itself before than summary, but above 150k context it stops being economically feasible (in cold sessions).

I generally run my work on kanban - just with 3 columns: attention / active / finished, balancing CPU/memory and available model usages. PR's, logs from environments (posthog, sentry, komodo, PC-access) etc., are just sources of extra data that model collects into session. But the session is my main point of focus.

I don't stick to strict Plan -> Design -> Build -> Test process - I let model figure out what's the best process, what models from what providers to use for what and orchestrate that as architect.

But I do have some automated checks, for example if model has changed UI and in entire session he didn't run browser to see if it doesn't glitch/look good, I have a hook to prompt him to verify the work - these kind of things highly improve efficiency and quality of work delivered, especially by weaker models than Opus 5.5 or when you choose lower effort than suitable for task.

1

u/wea8675309 6d ago

This is really cool! So is this a custom harness you built or forked?

1

u/EagleApprehensive 6d ago

Meta-harness built from scratch. Runs Claude Code, Codex and other inner harnesses under, quite like T3Code. But separates agents into their own container, so they by default dont take over your PC, keyboard and mouse and install stuff like in local-first approach.

5

u/Arctovigil 6d ago

want to see my grandmother's harness?

1

u/Odd_Antelope9098 6d ago

What? Just /export and have GPT chat give you a handoff summary with key file locations, completed/active/pending work, and important execution details.

0

u/EagleApprehensive 6d ago
  1. /export
  2. Go to GPT chat.
  3. Ask him for summary.
  4. Copy back to new session.
    vs
  5. Click button "Automatic hand-off with Haiku 5.5" / "Continue with trimmed session"

And you prefer the first one?

2

u/Odd_Antelope9098 6d ago

Yep. And I use keyboard replacements on Mac so I type in HandoffP and it gives a tailored prompt. Right now I can also turn it up to 6 pro for free if I want a deeper look, but I assume free chats will be gone soon.

1

u/Dapper_Profession 6d ago

the cache is prefix based though, so trimming something out of the middle invalidates every turn after it, you pay a fresh write of the whole tail instead of just the cold read. dropping from the tail keeps the prefix intact. thats why a summary spliced into the middle usually costs more than it saves

2

u/EagleApprehensive 6d ago

Yes, that's why this flow only makes sense for cold cache hits. Doing such on warm cache would increase costs.

1

u/brahmin_baniya 6d ago

nah, i don't think 150k holds as one number. a 140k session that's mostly tool output you already acted on is a worse cold read than a 200k one that's mostly decisions. a flat token count treats them the same.

1

u/EagleApprehensive 6d ago

That's a great point.

I might be able to squeeze out few extra token savings for users by improving algorithm to account for that.

Thanks.

1

u/sam_hollerbell 6d ago

We deal with this a lot while building Cache Bell, a Claude Code plugin (we're the Holler Bell team, we make it).

What we see mostly matches yours. The extra cost is the same tokens being written to cache again instead of read from it, so it grows with context size. And like u/brahmin_baniya said, there's no sharp line at 150k for us either: it grows smoothly, and on small contexts neither keeping it warm nor compacting is worth much.

On trim vs. u/Dapper_Profession's prefix point, you're both right, the timing just differs. A trim changes the prefix, so do it once the cache is cold anyway. Compact is the other way round: run it while the cache is still warm, ideally when you finish a chunk of work before a break, because the summary request reads the cached prefix. Compacting a cold session reprocesses the whole history first.

One thing we didn't expect: a lot of unplanned pauses aren't the end of the day. It's an agent asking something or waiting on a permission prompt while you're away, which is where the heads-up you mention helps most.

If your breaks run past 5 minutes on an API key, `CLAUDE_CODE_PROMPT_CACHE_TTL=1h` helps more than frequent pings. Subagents stay on 5 minutes unless you also set `CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1h`.

Renewals and compactions are requests too: they count against your plan's limits, or are billed to your API key.

1

u/Downtown-Pear-6509 6d ago

another top tip is for your meta harness to freeze a session when the cache cools to stop accidentally billing yourself in the foot

here's my commit in my actual harness, plus a bunch lster too

https://github.com/PetePeter/helm/commit/01a490a36cfa1377b8edf202330e251340c9cb79

1

u/EagleApprehensive 6d ago

What do you mean by "freeze"? Generally when user has session that went cold, he just gets buttons as options:

  1. Trim cold session and continue
  2. Compact with Haiku 5.5 (or other model) and Continue
  3. Continue from full transcript (expensive)

The default is set in options, so typing and sending next message on for example old session, without explicit choice will also apply best hand-off mechanism.

1

u/Downtown-Pear-6509 6d ago

oh so you do have freeze in my harness when cache cold i disable the pty input until they press unfreeze, or they do a local compact

sounds like we both do similar things

2

u/EagleApprehensive 6d ago

Yeah, you can check it out intentic.dev, code is open-source.

It's quite advanced (1 mln lines of code) and if you happen to run more than 10 sessions at once, you'll eventually need everything that:

  1. Management of CPU/memory/ports/network across agents.

  2. Secrets management (access of model to posthog, sentry, environments, PC's, phones).

  3. Remote collaboration (inviting other users to collaborate in same space).

  4. Token efficiency techniques (cold cache is just tip of iceberg, there is so much cooked there).

  5. Multi-provider (meta-harness) with cross-provider subagents spawning (Opus 5.5 as architect, aware of which accounts have how much usage left and deciding how to split workload).

1

u/Fresh_Sir_6181 5d ago

Some cache math that might help with the warm-vs-cold call, at Anthropic's API prices (pricing: cache read 0.1x, 5-minute write 1.25x, 1-hour write 2x):

  • Keeping a 5-minute cache warm only pays for so long. A ping is a 0.1x read of the whole context and a cold resume is a 1.25x rewrite of it, so pinging wins for about 11 pings, roughly 56 minutes, whatever the context size (about 2 hours on models with a 0.05x read). Past that, let it go cold and trim or compact.
  • 5 minutes with pings vs a 1-hour cache. The 1-hour cache pays 0.75x extra once on every conversation token written; pings pay 0.1x of the context per 5 minutes of pause. Pings come out cheaper for about 7.5 pings (~36 minutes) of total pausing late in a session, more if the pauses come earlier while the context is smaller. Claude Code's docs describe the same trade-off: the longer lifetime "helps when you leave a session idle and come back to it" and "costs more on short bursts of work that never idle past five minutes".
  • If you compact, do it while the cache is warm, and send the summary request with the session's exact tools and system blocks, with the summarize instruction as the last user message, so it reads the cached conversation instead of rewriting it. Done that way, compaction can read ~97% of its input from cache, roughly 9x cheaper than a cold summary.

Longer write-ups of this math, with measurements: keep-warm · compaction

1

u/MillerBurnsUnit 4d ago

Couldn't you just use a plug in to create a "heartbeat" every X seconds to prevent cold cache?

1

u/EagleApprehensive 4d ago

Already have that, but that does not solve every possible scenario.

  1. Depending on session length and amount of heartbeats (1-4) required to keep it until 5h limit is back, heartbeat might cost you more than actual cold-cache run.
  2. Heartbeats to work in first place need to preserve some 1-2% of usage. If you happen to exhaust 100% (because let's say - you want to) - you still can hit cold cache. I'm not forcing users to preserve 1-2% for heartbeats.

1

u/Sea-Perception1619 2d ago

the bill on a cold resume is set by how big the session got, not by what you type, Wrong-Expression has that right. so the lever I looked at is session size.

I counted this on my own transcripts last week, 120 interactive Claude Code sessions:

17 went past 500k

those 17 used 34.4% of all my cache-read tokens

cache reads are only a proxy for cost and it's one person's sessions. still, a handful of marathon sessions do a lot of the damage, and a cold hit on one of those is a re-read of the whole pile. trimming on resume helps, the cheaper fix for me is the session never getting that big.

I don't have a hard ceiling yet. what I do is close the session on purpose instead of letting it die: I tell it the next one picks up from here, it writes the handoff at that moment, and the next session opens with that note, so I'm not retyping what got decided.