Research-backed calculator guide
Agent Context Compaction Cost: Find the Break-Even Threshold
Calculate when an AI agent should compact its context by comparing repeated context reads, the compaction pass, summary tokens, cache rates, and post-compaction rework.
Quick answer
Agent context compaction is worth paying for when the input-token cost you avoid on later turns is greater than the cost of the compaction request, the summary it produces, and any rediscovery or rework caused by information loss. There is no universal “compact at 80%” answer. The break-even threshold depends on context growth per turn, number of turns remaining, cache-read price, compaction-model price, summary size, and the value of exact history to the task.
A useful first-pass model is:
net saving = avoided reread cost − compaction cost − post-compaction rework cost
For a long-running agent, measure these terms from traces rather than treating the context-window limit as the billing threshold. Anthropic describes compaction as replacing older turns with a server-written summary, while Microsoft’s Agent Framework documents several alternatives, including tool-result compaction, sliding windows, summarization, and truncation.[1] [5] Those choices have different cost and fidelity profiles.
In the worked example below, 120 turns start with 30,000 tokens and add 2,000 tokens per turn. At an illustrative cached-input rate of $0.30 per million tokens, compacting after turn 40 reduces repeated context-read cost enough to outweigh a $0.375 compaction pass. The modeled saving is about $1.98 per 120-turn session, before assigning a dollar value to quality loss or engineering rework. Change the assumptions and the threshold changes with them.
Run your scenario
Turn this framework into an editable estimate.
Use dated, source-linked assumptions and see cost per request, successful task and month.
What compaction costs
Compaction is not free deletion. The system has to read some or all of the history, produce a summary, and replace the old messages with that summary. Anthropic’s documentation says the older turns are replaced by a summary and distinguishes on-demand compaction from threshold-triggered compaction.[1] A custom client-side summarizer adds the same basic cost components, even if the API does not provide a native feature.
Price the event in separate meters:
- Compaction input: tokens sent to the summarizer or compaction endpoint. This can be a fresh input charge, a cached read, or a mixture.
- Summary output: tokens generated by the summarizer. A longer structured summary may preserve more state but costs more.
- Rebuilt context: the summary and retained tail are sent on subsequent turns. Those tokens are not the same as the raw history that was removed.
- Rediscovery and rework: extra tool calls, file reads, retries, or human review caused by details that did not survive the summary.
- Cache transition: rewriting history can change the prefix that a prompt cache recognizes. Cache behavior must be measured after the compaction boundary rather than assumed.
Prompt caching changes the arithmetic but does not make context free. The Claude documentation describes cache reads, cache writes, and ordinary input as separate usage fields, with total input represented as cache_read_input_tokens + cache_creation_input_tokens + input_tokens.[2] Its pricing page states that cache writes and reads have different multipliers from base input pricing.[3] Other vendors use different meters, TTLs, and cache semantics, so substitute your provider’s rates.
Compaction also addresses a reliability problem. Anthropic’s context-engineering guidance treats context as a finite resource with diminishing marginal returns and describes compaction as a way to continue a long-horizon task with a smaller, high-signal context.[4] A cheaper token bill is not a win if the agent loses a required file path, repeats a completed investigation, or creates a defect that a reviewer must repair.
The break-even formula
Let the following variables describe one agent session:
S= starting context tokens before the measured work begins.g= average new context tokens added per turn.n= number of remaining turns.p_r= price per input token for the context portion being reread. Use a cache-read rate only when telemetry confirms those tokens are cache reads.p_o= output-token price, if output is included in the comparison.o= average output tokens per turn.C_in,C_out= compaction input and summary output tokens.p_cin,p_cout= the applicable prices for that compaction call.R= expected post-compaction rework cost, including extra tokens and operational cost.
Without compaction, the repeated context tokens are approximately:
T_no_compact = nS + g × n(n − 1) / 2
The corresponding context-read cost is T_no_compact × p_r. If every turn also produces o output tokens, add n × o × p_o.
If compaction occurs after k turns and reduces the active history to S_c tokens, split the session into two sums:
T_compact = kS + g × k(k − 1) / 2 + (n − k)S_c + g × (n − k)(n − k − 1) / 2
Then add the event itself:
C_compact_total = T_compact × p_r + C_in × p_cin + C_out × p_cout + R
Compaction is economically favorable when C_compact_total < C_no_compact. If you are comparing strategies rather than a single event, calculate this over a representative workload and include the probability of retries, failed tasks, and human intervention.
Do not mix tokens and dollars in the same threshold calculation. First capture usage fields from traces. Then apply the rate card for the model, service tier, cache status, and region. The AI token cost calculator is useful for checking the token-to-dollar conversion, while AI API invoice reconciliation helps compare the modeled meters with billed usage after the fact.
Worked cost example
This example is intentionally transparent rather than a vendor quote. It uses a cached-input planning rate of $0.30 per million tokens, an illustrative output rate of $15 per million tokens, and no separate infrastructure charge. The $0.30 rate matches the cache-hit rate shown for Claude Sonnet 4.6 in the pricing documentation checked on 2026-11-02, but the example is not a recommendation to use that model or rate.[3]
Assumptions:
- 120 total turns.
- 30,000 tokens at the beginning of turn 1.
- 2,000 tokens added to the active history per turn.
- 1,000 output tokens per turn.
- One compaction after 40 turns.
- Compaction replaces the old history with a 12,000-token summary.
- The compaction request reads 110,000 tokens and generates a 3,000-token summary.
- No quality penalty is assigned in the first pass; rework is shown separately as a limitation.
| Step | Inputs and assumptions | Token calculation | Estimated cost at stated rates |
|---|---|---|---|
| No compaction, context reads | 120 turns; 30,000 start; 2,000 added each turn | 17,880,000 read tokens | $5.364 |
| No compaction, generated output | 120 turns × 1,000 output tokens | 120,000 output tokens | $1.800 |
| Compacted run, ordinary context reads | 40-turn pre-compaction sum + 80-turn post-summary sum | 10,040,000 read tokens | $3.012 |
| Compaction input | One pass over 110,000 tokens | 110,000 input tokens | $0.330 |
| Compaction summary | One 3,000-token summary | 3,000 output tokens | $0.045 |
| Compacted run, generated output | Same 120 × 1,000 output tokens | 120,000 output tokens | $1.800 |
| Total comparison | No compaction: $5.364 + $1.800; compacted: $3.012 + $0.330 + $0.045 + $1.800 | $7.164 vs. $5.187 | $1.977 saved, 27.6% |
The input-only saving is larger: $5.364 − ($3.012 + $0.330 + $0.045) = $1.977, or about 36.9% of the no-compaction input bill. The full-session saving is 27.6% because both strategies generate the same illustrative output. The difference between the two percentages is why a team should report which meter it is optimizing.
This example assumes every reread is charged at the cached rate. If a cache miss makes the 110,000-token compaction input or a later turn use the base input rate, the result is worse for compaction. If the agent loses context and spends two extra turns rereading files, add those turns to R; if it creates a failed deployment, price that operational consequence separately. The AI agent budget guardrail and P95 calculator can help model the tail rather than only the average session.
How to choose a threshold from telemetry
Start with a fixed observation window, such as 100 to 500 completed sessions. Record context tokens, cache-read tokens, cache-write tokens, fresh input, output, compaction events, summary length, turns after each event, tool retries, and task success. Keep model and pricing changes as separate cohorts.
Next, plot cumulative input cost against context size. A rising curve alone does not prove compaction is beneficial. Add the quality signals that reveal information loss: repeated file reads, repeated searches, reverted edits, user corrections, failed tool calls, and time to recover after a summary. Microsoft’s documented strategies support a staged design in which old tool results are collapsed before a more lossy summarization step.[5] That is often a better experiment than comparing only “never compact” with “summarize everything.”
Test several candidate thresholds around the point where the marginal cost of carrying context exceeds the combined compaction and recovery cost. For each candidate, report:
- dollars per successful task, not only dollars per turn;
- p50 and p95 context-read tokens;
- compaction frequency and summary size;
- failure or correction rate after compaction;
- rework tokens and human minutes;
- cache-hit rate before and after the boundary.
A threshold that minimizes token spend but increases failed-task recovery is not the cheapest operating policy. Conversely, keeping raw history forever can be wasteful when the task mostly needs decisions, constraints, and current state rather than verbatim tool output.
When not to compact
Do not summarize away information that must be reproduced exactly unless it is retained somewhere retrievable. Examples include signed payloads, legal wording, migration checksums, schema fragments, credentials references, and exact patch context. Use an external state file, object store, or durable event log for facts that must survive compaction. The active summary should point to that source of truth rather than pretending to contain every detail.
Do not use a vendor’s default threshold as an accounting truth. A framework may trigger on tokens, turns, or message groups. Microsoft’s documentation shows that strategy choice affects what is preserved, and that a tokenizer changes whether a trigger measures tokens or another unit.[5] Confirm the actual usage records and the actual boundary behavior in your runtime.
Finally, do not treat a larger context window as a free substitute for policy. Anthropic’s guidance recommends curating the smallest useful set of high-signal tokens, and its compaction documentation provides a way to replace older turns while retaining recent ones.[1] [4] Larger capacity can delay a boundary; it does not remove the repeated-read meter or the risk of stale context.
FAQ
Is there a standard agent context compaction cost?
No. Compaction cost is the sum of the provider’s input and output meters for the compaction request plus the cost of sending the resulting summary and retained tail on later turns. Cache status, model, region, service tier, summary length, and implementation all change the result. Use the formula above with your usage export instead of a universal price.
When should I compact an AI agent?
Compact when the expected cost of carrying the next set of turns, plus any context-quality penalty, exceeds the compaction pass and expected recovery cost. Start with a few candidate thresholds around your measured inflection point. A threshold based only on “80% of the window” is a starting experiment, not a conclusion.
Does prompt caching eliminate the cost of long context?
No. It can lower the per-token rate for a matching prefix, but the cached tokens are still read and metered. Anthropic’s pricing documentation lists cache reads separately from base input, and its caching guide explains that changes before a breakpoint can prevent a match.[2] [3] Measure cache reads and misses around every compaction boundary.
Is compaction the same as truncation?
No. Truncation removes older message groups without asking a model to preserve their meaning. Compaction summarizes older content and can keep a recent tail. A sliding window keeps a bounded set of recent turns. Microsoft documents these as different strategies with different preservation behavior.[5]
How do I account for context summarization cost in a monthly budget?
Count compaction events per month and multiply each event by its measured input and output token charges. Add the post-event summary and retained-tail reads, then add expected rework tokens and human review minutes. Feed those totals into the AI Agent Cost Calculator alongside ordinary model, tool, retrieval, retry, and observability spend.
Limitations
The worked example is a sensitivity model, not a forecast of your bill. It assumes linear context growth, one compaction event, a stable cache-read rate, and no quality penalty. Real agents have bursty tool results, cache TTL expiry, retries, parallel branches, and model-specific tokenization. Vendor prices and compaction APIs change; recheck the rate card on the date you run the analysis. The search benchmark was recorded using a web-result interface that did not expose a reliable sponsored-versus-organic label, so its ten listings are documented as observed result listings with that limitation.
Primary sources checked
Pricing and product behavior can change. These sources support the time-sensitive claims in this guide; verify current provider terms before committing spend.