AI Coding Agents
Headroom cache mode and cold-prefix recompaction
Headroom’s default cache mode keeps the frozen prompt prefix byte-identical and compresses fresh content so the provider can still reuse the earlier prefix. Cold-prefix recompaction is a separate, opt-in feature that can rewrite retained history when Headroom judges the cache cold. Test that judgment against observed cache reads before enabling the feature on valuable sessions.
What the default mode freezes
A coding agent can send the same prior messages and tool results repeatedly while adding one new result or instruction at a time. Headroom’s cache mode distinguishes a frozen prefix from the live zone. Its README describes keeping the frozen prefix byte-identical and applying compression to new bytes, such as fresh tool output and the latest turn. That gives the upstream provider a chance to match the prefix it saw previously. An upstream cache hit still depends on the provider and the particular request.
Say an agent runs tests and receives a verbose log on every attempt. On one request, the new log is a candidate for compression before it gets sent to the model. On the next request, the portion already frozen needs to stay in the same form for the provider to reuse it. Reprocessing an earlier log because a later attempt has a similar error could change the old prefix. Cache mode confines compression to the new material during normal operation. The Headroom review covers the proxy, its compression transforms, and recovery of original content.
The memory behavior depends on the provider path. The README says cache mode skips automatic --memory injection for Anthropic /v1/messages to keep the provider prefix stable. It recommends --mode token when that path needs automatic memory context. Switching to token mode gives up cache mode’s frozen-prefix protection. The README describes OpenAI Chat, OpenAI Responses, and Gemini placing memory at the live-zone tail. If your workflow depends on memory, evaluate agent behavior as well as token counts. Hold the memory configuration fixed while measuring cold recompaction so that changes in automatic context injection do not confound the result.
What CacheAligner actually does
Headroom describes CacheAligner as a detector for volatile content that could break a provider’s cached prefix. It flags such content and leaves the prompt as it received it. The compression pipeline can transform fresh content separately. That division tells you where to look when the same session repeatedly sends a changing value near the beginning. Investigate the component that constructs the request and use a prefix diff to confirm whether the expected shared section is stable.
| Mechanism | Behavior described by Headroom | What to verify in a trace |
|---|---|---|
| Frozen prefix | Preserves bytes already frozen in cache mode | The outgoing prefix is identical across ordinary warm turns |
| Live-zone compression | Compresses fresh input such as a new tool result | New content is smaller and remains sufficient for the task |
| CacheAligner | Flags volatile content without rewriting the prompt | An apparent early change can be traced to where the prompt was assembled |
| Automatic memory injection | Skipped in cache mode for Anthropic Messages, appended to the live-zone tail on the other documented paths | The agent receives the memory context you expect for that path |
| Cold-prefix recompaction | Allows a rewrite when the opt-in hook judges a prefix cold | A candidate rewrite occurs only in cases your test intended to treat as cold |
How the opt-in hook decides a prefix is cold
Headroom’s latest release as of November 30, 2026 was v0.45.0, published November 22. PR #2555, merged July 25, 2026, describes a cold-prefix decision based on idle time exceeding the TTL plus a margin. Its behavior table pairs Claude and Codex, which use encrypted reasoning, with deduplication and removal of superseded reads across the prefix. For plain-text reasoning on Kimi, GLM, and DeepSeek, the cold-turn action drops the full reasoning block. The implementation summary puts whole-prefix recompaction in the Anthropic handler and reasoning compaction in the OpenAI Chat handler. Check the handler behavior and order of transformations in the build you run.
PR #2557, merged the same day, documents HEADROOM_COLD_RECOMPACT and HEADROOM_CACHE_TTL_LEARN and says the flags remain off by default. The project described cold recompaction as a candidate to default on for Claude Code once TTL detection is validated in the field. Cache mode can be active with the cold hook still disabled.
The pull requests describe reading TTL signals from Claude Code configuration or the request and learning from observations where applicable. The configuration notes documented through PR #2557 give a static 300-second fallback for OpenAI/Codex and Kimi when there is no TTL signal. Use that proxy estimate to pick idle gaps for testing, and check provider usage to see whether the upstream API actually read cached tokens on each request.
Why the last request can give the wrong TTL
Draft PR #4511 was opened October 27, 2026 and remained open as of November 30, 2026. Its author reported a Claude Code main conversation using a one-hour cache and an idle-compaction request carrying the five-minute default. The PR describes that request arriving after about 50 to 57 minutes of idle time while the one-hour prefix can still be warm. It reports that comparing the idle time to the shorter TTL can cause the hook to recompact a warm prefix. A rewrite that changes bytes can then force a cache write.
The reported mismatch requires a one-hour main-conversation TTL and a five-minute compaction TTL. Claude Code requests the hour for the main conversation by default on a Claude subscription within included usage. Usage credits, API keys, and cloud providers default to five minutes. API-key and cloud users can opt into the hour by setting promptCacheTtl to 1h. Compaction defaults to five minutes in the separately configurable “everything else” bucket. To confirm a one-hour write for the main conversation, check ephemeral_1h_input_tokens in usage.cache_creation on a turn that writes the cache.
The draft proposes judging coldness with the larger of the request TTL and the longest TTL the tracker observed for that conversation. That describes the proposed change in the open draft. In a test of your own, include a previously warmed long-TTL prefix and a later request carrying a short TTL before the long window has elapsed. Compare the same sequence with the hook enabled and disabled. If the baseline reads cached tokens while a rewrite in the enabled run causes a write, you have reproduced the cost exposure for that trace.
A second test uses the same workflow after a long enough idle gap for the baseline also to miss. This controls for the possibility that you would have paid for a new prefix anyway. Keep the order of preceding requests and their timing consistent in both comparisons. A warm-up request or additional follow-up can change which prefix is available when the timed request arrives.
Classify the first resumed request and the next few requests separately. A rebuild can look expensive on the resume and then make later turns cheaper. The opposite pattern is also possible if a changed prefix has to be written again before many requests read it. If you can reproduce the workload at several idle gaps, see whether the hook’s decisions cluster near the observed expiry or whether some experiments lose reads well before your baseline does. That distinction guides whether to investigate TTL detection or the economics of compression itself.
Compare the rebuild cost with the savings
The decision is between keeping the existing history and rewriting to a shorter one. Treat all totals here as estimates and use token counts and rates from your actual session. Suppose the original prefix contains P tokens, the rewritten prefix contains Q tokens, and P is larger. Let W and R be the write and read prices per million tokens for the selected model and lifetime. Assume the rewritten prefix is written once, later eligible requests read their respective prefixes, and new input and output costs are equal in both runs.
On a warm turn, keeping the prefix costs P × R / 1,000,000. Rewriting costs Q × W / 1,000,000 under the assumption that the full prefix must be written again. The difference is the initial estimated cost of rewriting a warm prefix. Each later matching request saves (P − Q) × R / 1,000,000 under these assumptions. If there is an initial premium, divide it by the per-read savings to estimate how many later hits it takes to recover that premium. Long-lived, heavily reused prefixes give you time to recover it. A session that ends immediately after the rewrite gives you none.
For a Sonnet 5.5 estimate, assume the warm prefix has 150,000 tokens and the rewrite has 60,000. Use $2 for ordinary input, $0.10 for cache reads, and $2.50 for five-minute writes, all per million tokens. The one-hour write rate is $4 per million, derived from twice the ordinary input rate. Keeping the warm prefix costs 150,000 × $0.10 / 1,000,000 = $0.015. A fresh five-minute write costs 60,000 × $2.50 / 1,000,000 = $0.15, a premium of $0.135. Each subsequent hit on the shorter prefix saves 90,000 × $0.10 / 1,000,000 = $0.009. Recovering that premium takes 15 later hits. A fresh one-hour write costs $0.24 and takes 25 later hits to recover the $0.225 premium against the same warm read. See the pricing index for current rates.
If both runs start cold and put their full eligible prefixes back in cache at the same write rate, the corresponding comparison is P × W / 1,000,000 versus Q × W / 1,000,000. With the same token counts and five-minute writes, the shorter prefix saves 90,000 × $2.50 / 1,000,000 = $0.225 immediately. Changes in the model’s behavior, future hits, and additional writes can alter either result. Use the cache-bust break-even guide to compare cases where those assumptions differ. Record actual read, write, and ordinary input tokens for the control and experiment, and use the same output-quality checks for each.
Use a resumed task that actually depends on earlier context. For example, ask the agent to continue investigating a failing test whose diagnosis is already in the conversation. Check whether it remembers the relevant result or reruns work to recover the missing details. Count those extra calls in the session comparison along with any savings on the rewritten prefix.
Test the flag on a small set of sessions
- Record a baseline in default cache mode. Choose a representative workflow with repeated tool results and a long-lived history. Capture request times, the location of the frozen boundary, provider token usage, and task results. Keep the cold hook off for this control.
- Replay with cold recompaction enabled. Use the documented opt-in
HEADROOM_COLD_RECOMPACT=1in the experimental run. Include short pauses, longer gaps beyond the relevant TTL, and the mixed-TTL scenario described in the draft. Keep the agent and model settings the same. - Classify what happened on each resume. Check whether the outgoing frozen prefix changed and whether the provider billed a read or a write. Compare against the corresponding control turn. Use the cache hit rate guide to aggregate tokens without giving a tiny hit the same weight as reuse of most of a long turn.
- Keep the opt-in only where it pays. Check the estimated cost over the whole resumed session and verify that the agent still completes the task with the information it needs. If your traffic seldom resumes after expiry, or if the mixed-TTL test shows warm rewrites, keep the default and tune the live zone first.
Sources
- Headroom README, cache mode and pipeline (Headroom, accessed 2026-11-30)
- PR #2555, model-aware cold-prefix hook (Headroom, accessed 2026-11-30)
- PR #2557, cold-prefix flags and TTL observation log (Headroom, accessed 2026-11-30)
- Draft PR #4511, longest observed conversation TTL (Headroom, accessed 2026-11-30)
- Headroom releases (Headroom, accessed 2026-11-30)
- Claude Code prompt caching (Anthropic, accessed 2026-11-30)
- Claude API prompt caching (Anthropic, accessed 2026-11-30)
How this note was produced
Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.
Headroom cache mode and cold-prefix recompaction. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/headroom-cache-mode-cold-recompaction/