The break-even framing is the useful part here: a blanket 1-hour TTL only pays when ~38.5% of writes are post-gap rewrites, and yours were 24.8%, so "just raise the TTL" would have cost you more. Did the 24 rewrites cluster behind specific tools (long test runs, builds), or were they spread across agents? If they cluster, a per-agent choice - long TTL only for agents that wait on slow Bash calls, default for the rest - might beat both options. Thanks for publishing the script method and the real numbers instead of a vibe.
Thanks! They didn't cluster behind tools at all. None of the 24 came after a wait on a tool. By cause they were:
13 resumes of an agent that had already finished and handed back, 5 to 116 minutes later
7 single requests that generated for over five minutes and outlived their own cache
4 wake-ups by a notification after the agent had ended its turn
My guess for the zero is that the agents waiting on long jobs are already trained to poll in loops under three minutes, which kept them warm.
So I ended up with the per-agent split you describe, but it asks a different question: will the main session come back to this agent later? Those agents get the relay, with a one-hour cache set in its own file. Everything else stays at five minutes.
I still added a few lines for tool waits, to be sure, even though my agents already tended to keep themselves warm. A hook tells every five-minute subagent to start long jobs in the background and poll in steps under three minutes. The same rule covers the notification cases, because an agent that keeps polling never ends its turn. An agent blocked on a 10–15 minute test run, as in #103309, is probably cheaper polling than paying 2x on every write, for up to about an hour of waiting.
The 7 long responses are the case no rule reaches. A single response that runs past five minutes outlives its own cache, and nothing can poll from inside it. Only a one-hour cache on that agent covers it, and I haven't measured that yet.
That breakdown is more useful than the original question, thanks. The 13 resumes stand out: 5 to 116 minutes later, a one-hour cache only rescues part of that range anyway. For the long tail, it might be cheaper not to resume at all: have each subagent end with a short handoff note (what it did, files touched, open questions), and when the main session needs it again, start a fresh agent from that note instead of rewriting the whole old context. You'd pay for a small prompt instead of a full cache write.
For the 7 long responses, one thing to try before the one-hour cache: tell those agents to write big outputs in several smaller tool calls (one file or section per call). Each call is a new request that hits the cache inside the five-minute window, so the generation never outlives it. Would be curious whether that brings the 7 down.
Mostly, yes, and that's what the main part of Embers does. When a subagent finishes, the main session runs date. Within 240 s it resumes the agent. After that it resumes only if the agent is under 150k tokens (350k if it holds work saved nowhere else; both configurable, and the defaults were a choice, checked against the cost math). Otherwise it starts a fresh agent with a brief that points to the earlier work's files. A fresh agent isn't free (about 66k tokens before it does anything, by my estimate), so a small agent is still cheaper to resume even when its cache is cold. Your hand-off note would make the fresh path better: if every subagent ended by writing one, the brief would have something reliable to point at. Claude suggested this too, but I chose not to: it would mean a file per agent, and I'm already swamped in files (I'm publishing another article on Monday, on another project, about that problem). The agent's final report already lands in the main session's context, so the brief can carry it from there.
Splitting the output is a good idea that reaches the 7 long responses, and the arithmetic holds. If each piece takes a couple of minutes, the next call lands inside the five minutes and pays a cache read, 0.05 of the input price, instead of a 1.25 rewrite. The catch is that the model can't see its own generation time, so the rule would have to be about size: no single tool call above some number of output tokens. The 7 produced 28k to 63k tokens in 5 to 8 minutes, which gives a rough scale for the limit. I'll look into it: first what those 7 responses were writing, then where the limit would sit.
Makes sense to keep it in the final report rather than adding a file per agent. On the size rule: since the model can't see time but can see what it's about to write, you could phrase it as a unit instead of a token number: "never write more than one file, or more than ~300 lines, in a single tool call; for anything longer, write it in sections and append." Models follow "one file, N lines" much more reliably than an output-token budget they can't count. Looking forward to the Monday article.
@ev3nt1ne Fair, I had not counted the 5 to 8 minute generations. The lifetime starts when the request starts, so a long answer can outlive its own cache before it lands. That splits the rewrites cleanly: a quiet gap means the prefix changed, a long request means the clock ran out.
we've been looking at a different source of usage: repeated reads while agents wait for a reply. After a usage monitor projected 104.9% for one Claude Code account's week, our manager agent set three rules: wait for the notification instead of re-reading, put everything for the same recipient in one message, and compact the context at a natural pause (one session was at 227k tokens). I don't know whether our polling issue relates to your cache rewrites. Did the rewrites cluster around tasks that were waiting on a human?
Yes, mostly. I went back and checked. Before Embers I had 24 cold rewrites in two weeks. 13 of them were work sent back to a finished subagent, and in every one the manager was waiting on me. The other 11 had no human in them. 7 were single responses of 28k to 63k output tokens that ran 5 to 8 minutes, so the cache expired during the request itself. The other 4 were agents woken by a background job's notification more than 5 minutes later.
So I'd say our two problems share a trigger, a person in the loop, but the costs run in opposite directions. Your agents stay active while they wait, and each re-read pays a cache read on the whole context. Mine sat idle and paid a full cache write when they came back. At list prices a 5-minute cache write costs 1.25x input and a read 0.1x, so one cold rewrite costs about as much as 12 reads of the same context. That makes the polling interval what matters. A check every 4 minutes or so is cheap insurance if it keeps a cache warm through a long wait. Checking every 30 seconds costs most of a rewrite every 5 minutes. Waiting for the notification is the right default, as long as you accept that a long wait ends with one rewrite.
Coming back late is where the cache bites. Once the TTL lapses, the next turn writes the whole prefix again at the write price, which costs more than a warm read. Was the jump the cache expiring, or the subagent's output growing?
The cache expiring. The script only counts a request if it writes at least 100k tokens and reads at least 100k fewer than the request before it had cached. Growing output doesn't look like that: the old prefix is still read from cache and only the new turns get written, so reads stay high. In a rewrite the reads collapse and the whole context is written again, which is the expiry you describe.
Two cases are less clean. One rewrite came a minute after the last request, too soon for an expiry, so most likely the prefix changed. And seven had no gap at all. A single request spent 5–8 minutes generating output, and since the lifetime counts from the start of the request, the cache had expired by the time the request finished. So output did play a part there, through the time it took rather than its size.
Building cachly (MCP): your coding agent remembers your codebase and every decision made — not just the chat. Follows you between editors, stays when someone leaves. I post what I learn shipping it.
Useful breakdown, especially the 25x between a cache write and a read.
One thing that moved my numbers more than caching was which model a subagent runs on. Five of mine only read files for an inventory. Nobody had set a model, so they inherited the session's most expensive one: about 400,000 tokens for reading work. Since then every subagent type has its model pinned in its definition, and a check reports any drift from the plan.
Do your transcripts show how many of the 134 subagents ran on the session model only because nothing else was set?
We're a place where coders share, stay up-to-date and grow their careers.
The break-even framing is the useful part here: a blanket 1-hour TTL only pays when ~38.5% of writes are post-gap rewrites, and yours were 24.8%, so "just raise the TTL" would have cost you more. Did the 24 rewrites cluster behind specific tools (long test runs, builds), or were they spread across agents? If they cluster, a per-agent choice - long TTL only for agents that wait on slow Bash calls, default for the rest - might beat both options. Thanks for publishing the script method and the real numbers instead of a vibe.
Thanks! They didn't cluster behind tools at all. None of the 24 came after a wait on a tool. By cause they were:
My guess for the zero is that the agents waiting on long jobs are already trained to poll in loops under three minutes, which kept them warm.
So I ended up with the per-agent split you describe, but it asks a different question: will the main session come back to this agent later? Those agents get the relay, with a one-hour cache set in its own file. Everything else stays at five minutes.
I still added a few lines for tool waits, to be sure, even though my agents already tended to keep themselves warm. A hook tells every five-minute subagent to start long jobs in the background and poll in steps under three minutes. The same rule covers the notification cases, because an agent that keeps polling never ends its turn. An agent blocked on a 10–15 minute test run, as in #103309, is probably cheaper polling than paying 2x on every write, for up to about an hour of waiting.
The 7 long responses are the case no rule reaches. A single response that runs past five minutes outlives its own cache, and nothing can poll from inside it. Only a one-hour cache on that agent covers it, and I haven't measured that yet.
That breakdown is more useful than the original question, thanks. The 13 resumes stand out: 5 to 116 minutes later, a one-hour cache only rescues part of that range anyway. For the long tail, it might be cheaper not to resume at all: have each subagent end with a short handoff note (what it did, files touched, open questions), and when the main session needs it again, start a fresh agent from that note instead of rewriting the whole old context. You'd pay for a small prompt instead of a full cache write.
For the 7 long responses, one thing to try before the one-hour cache: tell those agents to write big outputs in several smaller tool calls (one file or section per call). Each call is a new request that hits the cache inside the five-minute window, so the generation never outlives it. Would be curious whether that brings the 7 down.
Mostly, yes, and that's what the main part of Embers does. When a subagent finishes, the main session runs date. Within 240 s it resumes the agent. After that it resumes only if the agent is under 150k tokens (350k if it holds work saved nowhere else; both configurable, and the defaults were a choice, checked against the cost math). Otherwise it starts a fresh agent with a brief that points to the earlier work's files. A fresh agent isn't free (about 66k tokens before it does anything, by my estimate), so a small agent is still cheaper to resume even when its cache is cold. Your hand-off note would make the fresh path better: if every subagent ended by writing one, the brief would have something reliable to point at. Claude suggested this too, but I chose not to: it would mean a file per agent, and I'm already swamped in files (I'm publishing another article on Monday, on another project, about that problem). The agent's final report already lands in the main session's context, so the brief can carry it from there.
Splitting the output is a good idea that reaches the 7 long responses, and the arithmetic holds. If each piece takes a couple of minutes, the next call lands inside the five minutes and pays a cache read, 0.05 of the input price, instead of a 1.25 rewrite. The catch is that the model can't see its own generation time, so the rule would have to be about size: no single tool call above some number of output tokens. The 7 produced 28k to 63k tokens in 5 to 8 minutes, which gives a rough scale for the limit. I'll look into it: first what those 7 responses were writing, then where the limit would sit.
Makes sense to keep it in the final report rather than adding a file per agent. On the size rule: since the model can't see time but can see what it's about to write, you could phrase it as a unit instead of a token number: "never write more than one file, or more than ~300 lines, in a single tool call; for anything longer, write it in sections and append." Models follow "one file, N lines" much more reliably than an output-token budget they can't count. Looking forward to the Monday article.
@ev3nt1ne Fair, I had not counted the 5 to 8 minute generations. The lifetime starts when the request starts, so a long answer can outlive its own cache before it lands. That splits the rewrites cleanly: a quiet gap means the prefix changed, a long request means the clock ran out.
we've been looking at a different source of usage: repeated reads while agents wait for a reply. After a usage monitor projected 104.9% for one Claude Code account's week, our manager agent set three rules: wait for the notification instead of re-reading, put everything for the same recipient in one message, and compact the context at a natural pause (one session was at 227k tokens). I don't know whether our polling issue relates to your cache rewrites. Did the rewrites cluster around tasks that were waiting on a human?
Yes, mostly. I went back and checked. Before Embers I had 24 cold rewrites in two weeks. 13 of them were work sent back to a finished subagent, and in every one the manager was waiting on me. The other 11 had no human in them. 7 were single responses of 28k to 63k output tokens that ran 5 to 8 minutes, so the cache expired during the request itself. The other 4 were agents woken by a background job's notification more than 5 minutes later.
So I'd say our two problems share a trigger, a person in the loop, but the costs run in opposite directions. Your agents stay active while they wait, and each re-read pays a cache read on the whole context. Mine sat idle and paid a full cache write when they came back. At list prices a 5-minute cache write costs 1.25x input and a read 0.1x, so one cold rewrite costs about as much as 12 reads of the same context. That makes the polling interval what matters. A check every 4 minutes or so is cheap insurance if it keeps a cache warm through a long wait. Checking every 30 seconds costs most of a rewrite every 5 minutes. Waiting for the notification is the right default, as long as you accept that a long wait ends with one rewrite.
Coming back late is where the cache bites. Once the TTL lapses, the next turn writes the whole prefix again at the write price, which costs more than a warm read. Was the jump the cache expiring, or the subagent's output growing?
The cache expiring. The script only counts a request if it writes at least 100k tokens and reads at least 100k fewer than the request before it had cached. Growing output doesn't look like that: the old prefix is still read from cache and only the new turns get written, so reads stay high. In a rewrite the reads collapse and the whole context is written again, which is the expiry you describe.
Two cases are less clean. One rewrite came a minute after the last request, too soon for an expiry, so most likely the prefix changed. And seven had no gap at all. A single request spent 5–8 minutes generating output, and since the lifetime counts from the start of the request, the cache had expired by the time the request finished. So output did play a part there, through the time it took rather than its size.
Useful breakdown, especially the 25x between a cache write and a read.
One thing that moved my numbers more than caching was which model a subagent runs on. Five of mine only read files for an inventory. Nobody had set a model, so they inherited the session's most expensive one: about 400,000 tokens for reading work. Since then every subagent type has its model pinned in its definition, and a check reports any drift from the plan.
Do your transcripts show how many of the 134 subagents ran on the session model only because nothing else was set?