ByteCosts
context-compaction-prompt-cache-cost.mdx

AI Coding Agents

What context compaction does to prompt cache cost

On an API key, context compaction adds a billed summary request, and the first turn with the shorter history rebuilds the conversation cache. Compact when the remaining work saves enough on repeated reads to repay both costs, or when you need context space. A warm prefix makes the summary request cheaper.

On an API key, context compaction adds a billed summary request, then the first turn with the shorter history rebuilds the conversation cache. Compact when the remaining work saves enough on repeated reads to repay both costs, or when you need context space. A warm prefix makes the summary request cheaper.

Why a shorter history can cost more

A cache entry covers an ordered prefix through a cache boundary. Imagine tools and stable instructions followed by exchanges A, B, and C. If the next request appends D, the old history is still in the same position. If the next request removes A and then appends D, the conversation starts at B. B and C were present in the previous request, but the earlier entry represented them after A. A rolling window that drops the oldest exchange on every turn changes the start of the history every time it advances.

The effect depends on where you cached the fixed part. A usable entry ending after the stable tools and instructions can still match when the history changes. An entry ending deep in history can only match through a boundary that remains eligible and identical. If the stable part never had its own usable entry, retaining it in the payload alone doesn’t ensure any billed cache reads. Anthropic describes looking for entries created by earlier writes, and OpenAI points to the first changed token when compaction rewrites a prefix. See the Anthropic API caching guide and OpenAI API caching guide.

History policyWhat the request doesCost to watch
Append while you have roomAdds the latest exchange after the existing historyCached reads of a growing history and writes for newly eligible content
Trim the oldest messages as the window advancesMoves the start of conversation history forwardReprocessing the retained tail whenever the window advances, with possible reuse of a separate fixed prefix
Generate a new summary frequentlyReads the history for a summary, then changes the earlier history sent on later turnsSummary input and output plus recurring history rebuilds
Summarize once, then appendBuilds a new conversation prefix and preserves it across later turnsOne summary and rebuild, followed by reads of a shorter prefix while the cache stays warm

Compare trimming the oldest messages with writing a summary based on what the agent needs to retain. Trimming can discard a decision that later messages still refer to. A summary can preserve the decision, but you pay for the model to generate it and must check that it retained the right details. A policy that rewrites an early summary on every turn also means later messages sit behind a changing prefix. Leaving a summary fixed for a stretch of follow-up work gives the rebuilt history time to be reused. See the context compression and prompt caching comparison for how to treat the two inputs to the decision.

What Claude Code bills around /compact

As of November 30, 2026, Claude Code documents a separate summary request. It repeats the system prompt, tools, and history being summarized, then adds a summarization instruction. While the original prefix is warm, that request reads it from cache. If you run /compact after the cache lifetime has elapsed, the summary request reprocesses the history as uncached input. That difference comes before the first turn with the shorter history. See Claude Code prompt caching.

The first regular turn after compaction rebuilds the conversation layer. Claude Code reloads project context from disk and caches it together with the new conversation, so unchanged project files can still contribute to that write. The system prompt layer can be read from cache if the prompt is current and the entry is available. Budget for the portions that actually rebuild, including reloaded project context, rather than estimating the rebuild from the summary length alone.

Claude Code can also compact a long conversation while it is idle on a one-hour TTL. The documented setting idleCompaction: false disables this behavior on version 2.1.290 or later. Whether the summary request can read a warm prefix depends on the main conversation’s TTL. The default is one hour within included subscription usage and five minutes on API keys, cloud providers, or usage credits. Compaction requests follow the separate “everything else” TTL for their own writes. That bucket defaults to five minutes, with a one-hour exception for server-controlled helpers within included subscription usage. Before compacting after a break, check the main conversation’s warm or cold status and active TTL in /usage.

For a separate comparison of a proxy’s approach to timing rewrites around cache state, see Headroom’s cache mode and cold recompaction.

Estimate the break-even turn on Sonnet 5.5

Suppose you’ve finished a task and are deciding whether to compact before the next turn. Use Claude Sonnet 5.5 at the committed ByteCosts API rates per million tokens. Ordinary input costs $2, output $10, cache reads $0.10, and five-minute writes $2.50. Anthropic documents one-hour writes at twice the ordinary input price, which gives a derived Sonnet 5.5 rate of $4 per million. The table uses five-minute writes, the default for Claude Code’s main conversation on an API key. See the ByteCosts pricing index for current rates.

Assume a warm 10,000-token system and tools prefix plus 80,000 tokens of reusable project context and conversation history. After compaction, assume the shorter project and conversation layer is 20,000 tokens, including the summary. The summary call adds 1,000 ordinary input tokens for instructions and produces 1,000 output tokens. In each branch, assume each regular turn adds 1,000 new cache-write tokens, including on the rebuild turn, and the next turn can read those tokens from cache. Both branches do the same regular work with equal ordinary input and regular output costs, which cancel out of the comparison. Assume enough context space to keep appending and closely spaced requests so the relevant entries stay warm.

Multiply each token bucket by its rate, add the charges, and divide by 1,000,000. For the summary, that’s (90,000 × 0.10 + 1,000 × 2 + 1,000 × 10) / 1,000,000 = $0.021. On the append branch, the next turn reads 90,000 tokens and writes 1,000 for an estimated $0.0115. On the compact branch, the next turn reads the stable 10,000 and writes 21,000, which costs an estimated $0.0535. Including the summary, you spend $0.0745 through that first regular turn. The append branch is ahead by $0.063 at that point.

On each subsequent turn, the compacted branch reads 60,000 fewer tokens and writes the same new tail as the append branch. The estimated saving is 60,000 × 0.10 / 1,000,000 = $0.006 per turn. To reproduce the totals, start with the first-turn costs above. On regular turn two, the append branch reads 91,000 tokens and the compacted branch reads 31,000. For each additional turn, add 1,000 to both read counts and keep the write count at 1,000.

Regular turns after decisionKeep appending, estimatedCompact now, estimatedLower estimated cost
1$0.01150$0.07450Keep appending
6$0.07050$0.10350Keep appending
12$0.14460$0.14160Compact now

You need eleven further turns after the initial rebuild to recover $0.063 at $0.006 per turn, so compaction wins on the twelfth regular turn under these assumptions. The balance is sensitive to the size of the retained context, whether the fixed prefix survives, and how much work is left. At a higher cache-write rate with the same read rate, a larger rebuild takes longer to repay. If you’ve already hit a context constraint, the append branch may be infeasible even if its estimated cost is lower.

If you set promptCacheTtl to 1h on an API key, change the main conversation’s write rate to $4 and keep the same warm summary assumptions. The first append turn costs an estimated (90,000 × 0.10 + 1,000 × 4) / 1,000,000 = $0.013. The first compacted turn costs an estimated (10,000 × 0.10 + 21,000 × 4) / 1,000,000 = $0.085, plus the $0.021 summary. That leaves $0.093 to recover. Both branches still write the same number of tokens after the rebuild, so the saving remains $0.006 per turn. Compaction first costs less on regular turn 17.

The cold summary alone would be an estimated (91,000 × 2 + 1,000 × 10) / 1,000,000 = $0.192 if the entire old prefix had expired. Comparing a cold compaction decision requires repricing the append branch and the next turn’s fixed prefix too. The warm break-even point only applies to the warm case above. Use the same three columns in your own estimate for ordinary input, cached reads, and writes, then add output, including the summary. You can reuse the method in the compression break-even guide.

To see why the same retained size can yield a different bill, reuse the warm example and assume a trim on every turn keeps the project and conversation layer at 20,000 tokens before the new 1,000-token tail. If every trim changes the history from its first message and the whole layer is written again, each turn costs an estimated (10,000 × 0.10 + 21,000 × 2.50) / 1,000,000 = $0.0535 before regular output and other ordinary input. With one compaction and then an append, the second turn costs $0.0056 on those same buckets. The difference comes from how often the retained layer is rewritten. A separate, matching breakpoint inside that layer could change the outcome. When you benchmark a trimming policy, look at what survived as a cache read after the trim and whether the rewritten history had a chance to earn any reads before the next trim.

Decide when to compact your own session

  • Keep appending through a short follow-up. If you have enough room and the relevant history is warm, estimate the few remaining read charges first. A large warm prefix can be cheap to keep for that task.
  • Compact at a task boundary when there’s more work ahead. Preserve active decisions, changed files, and unresolved checks in your summary instructions. Compare the expected shorter reads to the summary and rebuild cost, then use the updated session to continue the work. See managing context in long coding sessions.
  • Measure a session over several turns. Record /usage and the active TTL before the summary, after the first regular turn, and after several follow-ups. Compare per-model token deltas as well as the main cache line’s expected rebuild counter. Remember that the main cache summary covers the main conversation, while the session’s per-model totals are cumulative. When you can observe individual requests, record the summary call separately.
  • Treat expired history as a fresh estimate. If you return to a cold session, price the full history processing on each available branch. Decide which context still helps the next task before paying to process it.

Sources

Interactive report

  • The Five-Minute Clock: A quick review keeps a prompt cache warm. A longer break can turn the next question into another setup charge. Follow the same coding session through both clocks.

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.

What context compaction does to prompt cache cost. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/context-compaction-prompt-cache-cost/

Sources