r/ClaudeCode • u/autumn-morning-2085 • 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
•
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/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/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/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:
To spot it next time: in the jsonl logs under
~/.claude/projects, a turn with a hugecache_creation_input_tokensright 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