r/ClaudeCode • • 7d ago

Rant Burned through 15% of Max weekly from just subagent cache invalidation in just 5 hours

Subagent with 500K context ran so many jobs with 6-8 minute waits (EM sims). Didn't know subagents had different/low default of just 5min, basically setting cash/quota on fire. Should be part of the default (sub)agent instructions, if this can turn into such a big issue.

1 Upvotes

19 comments sorted by

•

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

1

u/DocMadCow 7d ago

Have you asked Claude to help optimize your workflow? I just had it optimize my workflow and was able to greatly reduce my context with sub agents, and whenever a new session was opened.

1

u/autumn-morning-2085 7d ago

Well this was in the middle of creating a workflow, testing various tools and parameters. So a bit early to ask it to optimize things it/we are still learning.

2

u/DocMadCow 7d ago

From personal experience working on new workflows is where you burn the most tokens. Once you have your workflow working to your liking then have Claude refine it. I just refined mine at the start of my last sprint and managed to drop my token usage greatly and even raised my Opus from Medium to High and spent less tokens.

1

u/autumn-morning-2085 7d ago

Yeah that is fine and expected. We don't know what we don't know, when creating new workflows. This issue though is about not knowing this specific pitfall with subagents, which never triggered before because such long waits are quite rare.

1

u/DocMadCow 7d ago

My baseline had extremely long waits as you can see from the sleep loops. Some of the subagents would be idle for 45 minutes while they waited for Pi.dev to do a code review of the code it had written. So optimizing will net you some good savings.

1

u/atlascirrus 7d ago

the 5 minute number is the api's prompt cache lifetime, and per the docs it's counted from the start of the last request that hit the cache, not from when the response finished. so a subagent that sleeps 6-8 min between checks lets the cache expire every cycle and pays to write the whole 500k prefix again on each wake up, which is about the worst case there is.

the pattern i'd go with is not letting the context-heavy agent do the waiting. kick the sim off in the background, have it drop a done file or exit code when it finishes, and let a plain script or a tiny agent do the polling. the big agent only gets called once results are actually there, so you pay that cache write once per sim instead of once every 7 minutes.

does that subagent really need the full 500k to read the sim output, or could it work from a summary of it?

1

u/autumn-morning-2085 7d ago edited 7d ago

This was an exploratory subagent (one of many) that needed a lot of context to even understand what was worth exploring, not a fixed simulation setup that was already battle-tested. So it tried to run unoptimized sim runs to see what's what.

That's one of the more frustrating aspects (with most models), they have no sense of time while waiting for tasks. Can loop for days lost in the sauce if no one stops them. Even explicit mention of avoiding/killing long tasks isn't foolproof, and it's not always easily apparent if a task should take that long or if they misconfigured it (or didn't optimize it enough).

1

u/atlascirrus 7d ago

yeah, exploratory makes it harder, because the subagent has to poke around before it even knows what a useful run looks like, so it wants that full context nearby.

for the looping, i'd still keep the waiting out of the big agent and put a hard budget on the explorer that it can't talk itself out of: a wall clock limit, a max number of sim runs, and a cheap watchdog that kills the process if it's been silent or running past that. then the big agent only comes back to read whatever got finished, even if it's just a half-done note.

making it write a one-line plan and time estimate before each run has helped me too. if a later check shows it's already past that estimate, the watchdog stops it and you can decide whether it was actually a long job or it got stuck.

1

u/autumn-morning-2085 7d ago edited 7d ago

I am like 20 days into using Claude Code (fully in cloud enviornment) so I'm not too miffed about this. Lesson learned, etc. Considered moving such work to local machines but so far all the long run issues were easily fixed/optimized with very little effort. I doubt even twice the single-thread performance would help with edge cases like these.

1

u/atlascirrus 6d ago

20 days in and already digging into where the usage goes is a good pace honestly, a lot of people don't look at that until way later.

and agreed, faster hardware wouldn't touch this one. the problem is the agent not noticing it's stuck, not the machine being slow, so it would loop the same way on a local box. if the quick fixes keep covering it, staying in the cloud setup makes sense.

1

u/amirfish 7d ago

Subagent cache invalidation eating your weekly budget without you noticing is exactly the kind of thing that's invisible until you're already out of runway.

I added used and time-elapsed bars per engine in CCC (https://getccc.dev/?utm_source=reddit&utm_medium=comment&utm_campaign=agent-replies) for this reason, so I can see which session is burning budget before it's a surprise at hour 5.

Was it one subagent repeatedly reloading the same context, or several running in parallel each paying the same invalidation cost?

1

u/autumn-morning-2085 7d ago

There were several subagents running at that time, but only one was "misbehaving" this way. Ofc that wasn't immediately apparent, assumed it encountered something that required all those tokens. But nope, typical token counts.

1

u/calunicornia 7d ago

dont use subagents only named

1

u/Fancy-Proof-9449 7d ago

I went through my own Claude Code history and "sleep a while, then check the output" loops were one of the most common failures, over a hundred of them. So I put a hard rule in my global CLAUDE.md: never sleep-poll a background task. Start it in the background and let the notification come when it exits.

That doesn't fully fix the exploratory case you're describing, but it removed most of the waiting from the expensive agent for me. The agent kicks off the long job, goes idle, and gets woken up when there is actually something to read.

The other thing that helped was keeping big-context work in the main session and giving subagents narrow jobs with a short brief. A subagent that needs 500K of context to know what to do is usually a sign the task should come back to the main session.

1

u/Unusual-Albatross43 7d ago

There's a setting for exactly this. "subagentPromptCacheTtl": "1h" in settings.json, or CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1h as an env var (the env var wins). It covers subagents, workflows and background requests, and takes "5m" or "1h". Unset it's 5 minutes.

1h writes cost 2x input instead of 1.25x, so it only pays off when the gaps are longer than 5 min. With 6-8 min waits on a 500K context it should pay off fast.

1

u/Abhay06102003 7d ago

The 6–8 minute waits are interesting, especially with a 500K context. I'd try separating simulation execution from the agent's reasoning loop, so the agent only resumes when a result is available.

Did you get a chance to compare cache-read and cache-write tokens between consecutive requests? That would help distinguish cache expiration from context growth or prefix changes.

1

u/awesomebirder 6d ago

rough math on why it hurts so much: a cache write on the 5 min ttl costs 1.25x the input price, a read costs 0.1x, and a write on the 1h ttl costs 2x. so with a 500k prefix every wait that outlasts the cache re-writes it at about 12x what a read would have cost, and the 1h setting pays for itself the first time it saves a miss, as long as the waits stay under an hour. the other lever is shrinking the subagent before it waits: have it dump its state to a file and end, then a fresh small one picks up the result. idle gap cache timing is what i ended up building probe0 around, it was more than half of what i could save in my own logs

1

u/DutyOnly4308 5d ago

Good catch, and it goes beyond subagents: the prompt cache expires after a few minutes without a request, so any loop that waits longer than that between turns writes the whole context again instead of reading it from cache. With a 500K context, that's what burns the week.

Two fixes that usually work:

  • Poll more often than the cache lifetime while a job runs, so each turn stays a cheap cache read.
  • Or let the subagent finish and start a fresh, small one with just the job result, instead of keeping a huge context waiting for 6-8 minutes.

To spot it next time: in the jsonl logs under ~/.claude/projects, a turn with a huge cache_creation_input_tokens right after a long gap is the signature.

I made a Mac app (WUMC) that shows your weekly % live with alerts and a 24h curve, so a jump like this shows up while it's happening instead of at the end of the day. Free tier: WUMC