Where my Claude Code usage went: subagents I came back to too late
For the second week in a row I had watched my Claude Code usage climb, and I had no idea why. Then I answered one of my sessions, got up for a drink, and came back a few minutes later to find my 5-hour usage far higher t
For the second week in a row I had watched my Claude Code usage climb, and I had no idea why. Then I answered one of my sessions, got up for a drink, and came back a few minutes later to find my 5-hour usage far higher than I had left it.
That was when I started looking, and what I found became Embers, a Claude Code plugin I wrote to keep subagents' prompt caches from going cold.
Claude Code keeps a transcript of every session and every subagent, and each request in it records how many tokens it read from the prompt cache and how many it wrote. I wrote a script to go through two weeks of them, 5 to 19 November. It counts a request as a cold rewrite when it writes at least 100,000 tokens to the cache and reads at least 100,000 fewer than the request before it had cached: the agent's context was there a moment ago, and now it is being paid for again. Across 134 subagents the script found 24 cold rewrites, about 8.5 million tokens in all, or roughly 350,000 per rewrite.
Each request re-sends the agent's whole context, and the part that matches the previous request's prefix is read from the prompt cache instead of being processed again. On Opus 5.5 a read costs 0.05 of the model's base input price and a write to a five-minute cache costs 1.25, so one rewrite costs as much as 25 reads of the same tokens. At list prices my 24 rewrites came to $56.17, which is 11.4% of the $494.66 that all my subagents' prompts cost in those two weeks. A subscription sends no bill: I use list prices as a proxy for weight against the usage limit, which in my data drained with cache writes.
The cache expires a fixed time after the start of the last request that touched it, and every read resets the timer (API docs). On a subscription within its plan usage, the main conversation, the session I type into, gets an hour, and subagents get five minutes. With an API key, a cloud provider, or a subscription drawing on usage credits, everything gets five minutes. So any subagent whose next request starts more than five minutes after the start of its last one rewrites its whole context, at 25 times the price of reading it.
A five-minute cache rewards coming back on time. A follow-up inside the lifetime reads the context from cache and resets the timer; one that lands after it writes the whole context back at 25 times the price of a read. Prices are Opus 5.5 multiples of the base input price.
I was not the only one paying it. In #103309 one reporter used 54% of a weekly limit in under 24 hours, and all 190 rewrites of more than 100k tokens that they checked came after the subagent had sat idle for five minutes or more, which they put down to test suites running 10 to 15 minutes. In #99808, about 92% of the rewritten tokens followed a Bash call. Both blame a subagent blocked on a tool.
The fix those threads measure is a setting, subagentPromptCacheTtl, that gives every subagent a one-hour cache. But a write to a one-hour cache costs 2 times the base input price instead of 1.25, and an agent pays that on every token it writes for its whole life, while it saves only on the requests that come after a gap of five to sixty minutes. On Opus 5.5 the hour breaks even when about 38.5% of a five-minute agent's cache writes are rewrites after such a gap. My rewrites were 24.8% of all my subagents' cache writes. That share counts every rewrite as one an hour would have caught, so the share an hour could have saved is, if anything, lower, and a blanket hour would have cost me more than the rewrites did. Other people's estimates run from 8.6% more expensive (#74318) to a 10.5% saving (a user in #99808), so the answer depends on the workload.
The setting also outranks the per-agent field, experimental: { cacheTtl: 1h } in an agent's file, and so does its environment variable, CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL. Set either one, even to 5m to spell out the default, and every agent you gave an hour of its own gets five minutes, with no warning.
The other fix in the threads works on the agent instead, and nobody in them has measured it. A comment in #99808, which says it was written by Claude Code on its own, describes a SubagentStart hook that tells every subagent to wait in steps of at most 270 seconds, so that it makes a request before its five minutes run out. The comment calls 270 seconds a margin under 300, not a measured boundary, and says it has no before-and-after comparison. Still, it was close to what I already did: some of my agents waited on long jobs in loops of just under three minutes, and their caches had stayed warm. So my plan was that rule, given to every subagent.
Not one of my rewrites came after a wait on a tool
Before building the rule, I checked it against my own 24. Not one of them followed a wait on a tool. They fell into three other groups instead.
Not one rewrite in the two weeks followed a wait on a tool. Each bar is a count from my own Claude Code transcripts, sorted into causes by a fixed rule; the left panel counts rewrites, the right panel the tokens they wrote back into the cache.
Thirteen of them, 4.9 million tokens, were work sent back late. The agent had finished and handed its report back to the main session. Five to 116 minutes later the main session sent it more work, and the resume wrote the agent's whole context back into the cache. One agent went through this four times in a single afternoon.
A finished agent makes no requests. Whatever rule it was given, it is not running when its cache expires, so it has no way to keep that cache alive. The waiting rule would have saved none of those thirteen. The one party that decides when a finished agent gets work again, and so whether that work finds a warm cache, is the main session.
Four more, 1.4 million tokens, all came from one agent that had ended its turn and was woken 1 to 11 minutes later by a system notification, most likely from its own background job (the one-minute case was probably a changed prefix, not an expiry). That is the case the waiting rule is for: an agent that waits in the foreground keeps making requests instead of ending its turn, and its cache stays warm.
The last seven, 2.2 million tokens, had no pause at all. Each followed a single request that ran 5 to 8 minutes while the model generated 28,000 to 63,000 output tokens. The lifetime counts from the start of a request, so by the time that request ended, its own cache had already expired, and the next request, sent at once, rewrote everything.
One public report had come at the biggest group from the same side. In #87106, where the reporter used 75% of a weekly limit in one afternoon, the main session kept resuming completed background subagents to hand them small follow-ups. The reporter's advice was to "prefer fresh agents with batched, self-contained briefs" and to "never drip-feed addenda". A fresh agent costs something too, though, so that advice needed numbers before a main session could act on it.
Those numbers became the main part of Embers: a short guide that a SessionStart hook gives every main session. The main session sees no clock between turns, so the guide tells it to run date when a subagent finishes. Within 240 seconds of that, five minutes less a margin for the last request's own generation time, a resume finds the cache warm. After that the cache is cold, and the cheaper path depends on the agent's size. A fresh agent with a brief that points to the earlier work's files starts at about 66k tokens before it does anything (a measured 36k baseline plus an assumed 30k of files), so a small agent is cheaper to resume even cold. Embers draws the line at 150k tokens, or 350k when the agent holds work saved nowhere else. Those two lines are my choice: docs/math.md shows they fit if a fresh agent would have to redo about 65k or 220k tokens of work over a ten-turn follow-up, and those inputs are assumptions. The main session does not have to guess the size: a background subagent's completion notice carries <subagent_tokens>, and in my sessions the last notice of each agent matched its last request to within 1%.
Some work is meant to come back, though: review rounds, or a follow-up I already know I will send later. For that, Embers adds embers:relay, a general-purpose agent with a one-hour cache set in its own file. An hour given only to the agents that come back pays its premium only where a late resume can earn it back. For an agent resumed once near the end of its work, the prices say the hour pays when more than 62.5% of such resumes come 5 to 60 minutes late; if its resumes add up to four times its final context, the bar falls to about 16%. Those bars come from the prices and an assumed resume pattern in docs/math.md; my counts do not give the share of my own resumes that come late. The agent sent back four times in one afternoon is the kind the relay is for.
The waiting rule is in Embers too, for the subagents that keep five minutes, and it is the least supported part. In my own data it could cover the rewrites after a notification, if those were background jobs: three of the four, since the fourth came only a minute later and was probably a changed prefix, not an expired cache. Tool waits never showed up, perhaps because some of my agents already waited in short loops. Its public support is thin: the reports above place rewrites after long waits, but none of them measures what a waiting rule saves, and the one comment that proposes the rule has no before-and-after comparison. I kept it on the arithmetic below. A SubagentStart hook tells each of these agents to start a long job in the background and wait for it in calls like this one, with the Bash tool's timeout set to 200000 ms:
timeout 170 bash -c 'until <done-condition>; do sleep 15; done'; echo rc=$?
The limit is 170 seconds, shorter than the public hook's 270, because the lifetime counts from the start of the request that launched the wait and because background subagents move a shell command to the background after 3 minutes (since Claude Code 2.1.318). The Bash tool's default timeout is 2 minutes, so the call also sets its own, above the loop's. At 300k tokens on Opus 5.5, one poll costs about six cents, and a rewrite instead of a read costs $1.44 more, so polling is the cheaper choice for about the first hour of a wait. Past that it costs more, about $1.30 extra on a two-hour wait, which I accept to keep one rule with no clock in it. The rule lives in hooks because a skill loads only when the model decides to use it. The hook works out each agent's real lifetime the way Claude Code does, and anything it cannot read counts as five minutes, because a rule injected by mistake costs a few hundred tokens and a missing one costs a rewrite.
Neither the waiting rule nor the guide reaches the third group. A single request that generates for more than five minutes outlives its own cache, and no rule about waiting or sending work back reaches inside one request. The relay's one-hour cache should cover it, but only for an agent that was started as a relay, and I have not measured that. Those seven rewrites were more than a quarter of mine by tokens.
The hooks also cannot see everything that sets a lifetime. While a subscription draws on usage credits, Claude Code ignores the one-hour field and drops even the main conversation to five minutes, which leaves the relay with five minutes, no waiting rule, and a guide whose one-hour figures are wrong. Managed settings the hook cannot read (server-managed, MDM, or inherited from Windows under WSL) can set the subagent lifetime to five minutes and leave the relay with neither its hour nor the rule. A wait longer than an hour goes cold for the relay too.
Whether a resume inside the hour finds its cache is still open. In #94728, resumed subagents whose history held thinking blocks rewrote their whole context inside a one-hour lifetime, and #87215 reports a similar rewrite when a parked subagent is woken by a message. I have not tested either on the current version. You can check your own relay from its transcript: ephemeral_1h_input_tokens should be above zero, and a resume 10 to 50 minutes later should show cache_read_input_tokens close to the previous context size. In my sessions since installing Embers, all 528 requests of the eight relay agents wrote to the one-hour cache.
I installed Embers on 19 November and ran the same script over the first 32 hours or so after. Across 55 subagents it found one cold rewrite: a relay sent more work 95 minutes after it finished, past its hour. Set side by side, as shares so the two windows compare:
| Before (5 to 19 November) | After (first ~32 hours) | |
|---|---|---|
| Subagents | 134 | 55 |
| Cold rewrites | 24 | 1 |
| Rewrites per subagent | 17.9% | 1.8% |
| Rewrites, share of cache writes | 24.8% | 1.8% |
| Rewrites, share of subagent prompt cost | 11.4% | 1.2% |
| Tokens written to the one-hour cache | 38,021 | 2,223,758 |
The last row is the relay at work. The after column is a day and a half of different work, with no control, so I read it as a first sign and nothing more. I will count the full week on 26 November.
Embers is MIT-licensed and lives at github.com/Ev3nt1ne/embers. It needs python3 on the PATH. To install it from a checkout:
git clone https://github.com/Ev3nt1ne/embers
claude plugin marketplace add /path/to/embers
claude plugin install embers@embers
Leave subagentPromptCacheTtl and CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL unset, or the relay loses its hour.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.