ByteCosts
context-compression-vs-prompt-caching.mdx

AI Coding Agents

Context Compression vs Prompt Caching for Coding Agents

Context compression reduces the tokens a coding agent sends, while prompt caching lowers the rate charged for reusable input. Compress new tool output before it enters repeated history and keep the cached prefix stable. When a rewrite replaces cheap cache reads with fresh input or cache writes, a smaller request can cost more.

Price the prefix and the new work separately

Once a coding agent has read files and run tools, its next request may carry the same instructions, tool definitions, and prior results along with a new result. A prompt cache can give some of that repeated input a lower rate. Those tokens still belong to the request’s context. If the context is approaching its limit, shortening the history can keep the session usable even when cached history is cheap to read. How LLM providers price prompt caching

Compression changes what the model receives. It can replace a long result with a compact view, omit duplicate information, or summarize part of a conversation. Caching changes how reusable context is billed. A compression ratio measures the change in length, while a cache-read rate prices the surviving tokens that the provider can reuse. When you evaluate them together, track how compression moves tokens among the billed categories.

For this estimate, use mutually exclusive buckets: fresh input, cache reads, cache writes, and generated output. For each bucket multiply tokens by its USD rate per million, then sum those products and divide by a million. If your provider exposes an inclusive input total alongside cache usage, map its fields to disjoint buckets before applying the formula. Otherwise a cached token could enter the estimate twice.

The committed ByteCosts rates checked on November 30, 2026 for Anthropic’s claude-sonnet-5-5 are $2 for fresh input, $0.10 for cache reads, $2.50 for cache writes, and $10 for output, all per million tokens. Apply the actual model and cache billing categories for your account when you repeat the calculation. Current provider rates

A smaller request can raise the bill

Assume a coding turn arrives with a reusable prefix of 100,000 tokens and a fresh 10,000-token tool result. Assume three alternative paths for that same turn. The ordinary request reads the existing prefix and sends the result as fresh input. A live-output compressor keeps the prefix and reduces the result to 2,000 tokens. A history rewrite instead replaces the reusable prefix with a 20,000-token summary that cannot reuse the existing prefix in this scenario and leaves the fresh result untouched.

Hold model output constant and omit its charge from the comparison. Assume the result itself is billed as fresh input, with the replacement summary billed as either fresh input or a cache write in the separate rewrite cases below. These are estimated input costs for one turn under the stated assumptions.

Request pathAssumed input bucketsEstimated input cost
Reuse the prefix as is100,000 read and 10,000 fresh(100,000 × $0.10 + 10,000 × $2) ÷ 1,000,000 = $0.030
Compress only the new result100,000 read and 2,000 fresh(100,000 × $0.10 + 2,000 × $2) ÷ 1,000,000 = $0.014
Rewrite history as fresh input20,000 replacement and 10,000 result, both fresh(20,000 × $2 + 10,000 × $2) ÷ 1,000,000 = $0.060
Rewrite history and write the summary to cache20,000 write and 10,000 fresh(20,000 × $2.50 + 10,000 × $2) ÷ 1,000,000 = $0.070

The live-output path saves $0.016 in estimated input cost. The fresh history rewrite sends 30,000 tokens instead of 110,000 and doubles the estimated input cost. The same tool result contributes $0.020 in both the ordinary and rewritten requests. The increase comes from swapping a $0.010 cache read for a $0.040 fresh summary. If the prefix had already expired, price the original 100,000 tokens in the bucket that would actually be billed. The warm-prefix comparison would no longer describe the decision.

The immediate break-even test is simple. If the suffix and output stay fixed, multiply the old affected prefix length by the cache-read rate. Divide by the price of the replacement bucket to find the maximum replacement length at the same input cost. In this estimate, 100,000 cached tokens cost the same as 5,000 fresh tokens or 4,000 cache-write tokens. A 20,000-token replacement exceeds both thresholds. If a leading stretch still hits the cache after the rewrite, apply the test only to the affected portion and include any additional fresh or written input that follows it.

Estimate how long the replacement will survive

The rewrite may still pay back over later requests. Keep the same example and assume the 20,000-token replacement is written once, then reused at the cache-read rate on every following turn. Each subsequent prefix read costs an estimated $0.002 instead of $0.010, saving $0.008 per turn. The initial write costs $0.050 compared with the original $0.010 read. The $0.040 extra charge takes five later cache hits to recover at $0.008 per hit. The shared fresh input and output cancel under these assumptions.

Use the payback test at the point where the agent would rewrite. A planning agent near the end of its work might have few requests left. An implementation agent that will run through several edit and test cycles has more chances to reuse the replacement. A switch to a separate phase, tool configuration, or model can change the reuse pattern. A cache write also buys less if the summary is rewritten before it can be read repeatedly.

Add the cost of the summarization call if one generates the replacement. Include any retrieval calls if the model needs omitted details, and count the follow-up model requests carrying those details. If the new representation adds turns, recalculate the sequence with their actual usage. The payback row above assumes the same continuation and gives a threshold you can compare with a trace of a real task. Reversible context compression

When the existing prefix is already close to filling the available context window, compare against a realistic alternative such as starting a new phase with a handoff. The alternative might need extra file reads to reconstruct the task state. Account for those reads and the time spent getting back to a usable response. A rewrite that raises the next request’s cost can still be the cheaper way to finish if the other path requires repeated reconstruction.

Keep the reused prefix stable

Start at the point where content first enters the prompt. Compress a log or structured tool result before it becomes part of repeated history. Preserve that transformed representation on later requests. If a tool instead reprocesses yesterday’s result with today’s query and produces different text, move that new view into the fresh portion or test the change as a deliberate new cache generation.

Headroom’s README describes a live-zone design that compresses fresh tool output and the latest turn while keeping the frozen prefix byte-identical. Its CacheAligner flags volatile content and does not rewrite prompts. Its Compress, Cache, and Retrieve mechanism stores originals locally so the model can request details later. Verify the shipped integration’s behavior in usage logs for your client and provider. Headroom

Billion Context describes model-directed folding of message ranges into summaries and a tool to restore a range on demand. The project describes its incremental approach as prefix-cache friendly and recommends investigating sustained hit-rate changes. An open issue title reports a cache hit rate collapsing for a generation and returning on the next request. That title documents a reported symptom, with its cause and applicability to other sessions unestablished by the evidence. Billion Context · Cache-hit fluctuation report

When you control prompt assembly, keep stable instructions and stable tool definitions together ahead of volatile metadata where the integration permits. Inspect the effective request sent by the client, including any injected tools. Look for changes in tool order and definitions, timestamps inside stable instructions, and old messages rewritten in place. When you need an updated instruction or tool set, account for the new write or fresh-input charge and measure subsequent reads with the new configuration.

Plan when to change a warm prefix

Mark the boundaries where the session expects its reusable prefix to change anyway. A model switch or a handoff to a separate agent can create a natural point to start with a smaller history. If you use a handoff, require it to carry the relevant decisions, evidence locations, and outstanding checks. Retest the cost of recovering those details in the new session. A handoff that appears cheap when only its initial request is counted can require expensive reconstruction afterward.

For a continuing warm session, start by comparing neighboring outbound requests. Identify the first changed block and distinguish an appended result from a replacement earlier in the history. If the provider reports only aggregate read and write counts, pair those figures with the request diff to see which change could account for the observed movement. A read drop on a request with several concurrent changes cannot identify the compressor’s contribution on its own.

If an agent already performs its own compaction, identify the requests immediately before and after that event. Treat cost at the window limit as a separate test from normal incremental use. A compressor that helps the agent postpone compaction can pay through fewer handoffs or rereads even if it produces only modest savings on an isolated warm request.

Check the rates on a complete coding task

Compare a baseline with the compressor on a representative task from the same starting state. For each request, log the model, the billed buckets, the turn number, and which rule changed the payload. Record the cache conditions at the start of each run. Compare a cold start with a cold start and examine warm continuation separately so a preexisting cache cannot explain the difference.

Watch for a drop in read tokens accompanied by a rise in writes or fresh input. A drop in read tokens can also happen because the new prefix is legitimately smaller and still hits. Compare the behavior turn by turn. A rewrite may show a one-time write spike followed by cheap reads. Repeated spikes after tiny edits to tool output point to a different cost pattern. A tool dashboard’s count of removed tokens cannot distinguish those patterns without billing data.

The independent tokbench pilot, with N=1 per arm, reports that Headroom removed 342K tokens on the wire while its run took 237 turns against native’s 180 and recorded more billed input. Its pilot provider had no prompt-cache discounts. The pilot illustrates how real reductions on individual requests can coexist with higher input usage across the run. This example’s cache calculation needs separate verification on a cache-discounting provider. tokbench

Finish the comparison with the same task acceptance checks, including any extra turns needed to retrieve evidence or repair the result. If live-output compression lowers the cost of accepted work and keeps latency within budget, use it where fresh results dominate. If the expensive part is long history close to the window limit, compare the one-time rewrite and expected reuse with the cost of a clean handoff. A stable warm prefix with little new input gives you less money to recover from rewriting. Measuring coding agent token savings

Sources

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.

Context Compression vs Prompt Caching for Coding Agents. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/context-compression-vs-prompt-caching/

Sources