—compactions
—overhead cost
—total run cost
Where compaction pays off — sweeping the trigger
Compact too early and you fire many events; compact too late (or never) and every step re-reads a huge context. The total-cost column usually has a minimum in the middle.
| Compact at | Compactions | Overhead | Total run cost |
|---|
The housekeeping nobody budgets for
An autonomous agent has no long-term memory of its own run, so it keeps the whole working context in the prompt: the system instructions, the tool definitions, every tool result it has seen and every step it has taken. That context grows monotonically, and long before it overflows the model's window the agent has to compact — hand the entire context to the model, get back a compressed summary, and carry on from the summary. Compaction is what keeps a multi-hour agent alive, but it is not free: summarising a 150,000-token context bills 150,000 input tokens for that one event, and a run that compacts three or four times can spend a fifth to a third of its total tokens on this alone. It is a cost that never appears in a per-call price and never shows up in a naive token estimate, which is exactly why agent bills come in higher than the math on the back of the envelope. The way to control it is to tune when you compact and how small you reset to, and to bill the re-read at a discount with caching or a cheaper summariser. Size the multi-turn side on the multi-turn conversation cost calculator, the whole loop on the agent loop budget calculator, and the caching win on the prompt caching savings calculator.
Agent Loop Budget CalculatorMulti-Turn Conversation CostAI Agent Step CostContext Window CostPrompt Caching Savings
How this calculator works
It simulates the run step by step. Context starts at the base size and grows each step; when it crosses the trigger, a compaction fires — the whole context is billed as input plus a summary output — and context resets. It sums the productive cost (reading context to do each step) and the compaction overhead separately, then sweeps the trigger so you can find the total-cost minimum.
Frequently asked questions
What is context compaction and why does it cost money?
A long-running agent accumulates context — tool results, messages, reasoning — until it approaches the model's context window. To keep going it compacts: it feeds the entire current context to the model and asks for a compressed summary, then continues from that summary instead of the full history. The catch is that summarising means re-reading everything, so each compaction event bills the whole context as input tokens plus a small output for the summary. A single compaction of a 150,000-token context costs the same as 150,000 input tokens, and a long agent that compacts several times can spend a fifth to a third of its total tokens on this housekeeping alone.
How many times will my agent compact?
It depends on how fast context grows and where the trigger sits. If each step adds a few thousand tokens of tool output and the agent compacts when it reaches, say, 140,000 tokens then resets to a 25,000-token summary, it will compact roughly every time it climbs that gap again. A run that adds 8,000 tokens per step and compacts at 140k will fire a compaction about every fifteen steps. This calculator simulates the run step by step, counts the compactions, and separates the productive cost from the compaction overhead so you can see the split.
How do I reduce compaction overhead?
Three levers move it. Compacting later (a higher trigger) means fewer, bigger compactions but more expensive productive steps because the context you re-read every turn is larger — there is a genuine sweet spot rather than always-higher-is-better. Resetting to a smaller summary buys more runway between compactions. And routing the summarisation itself to a cheaper model, or using prompt caching so the re-read prefix is billed at a discount, cuts the per-event cost directly. The table on this page sweeps the trigger threshold so you can see how total cost bends, and never compacting at all is shown too — usually the worst option because context then grows without bound and every step pays for it.
Is compaction cheaper than just using a bigger context window?
Often yes, because the alternative to compaction is carrying the full history on every single step, and input cost grows with the square of the run length when nothing is ever trimmed. Compaction caps that growth: after each summary the per-step context drops back down, so productive cost stays roughly linear instead of quadratic. You pay a lump sum at each compaction event, but you avoid re-reading an ever-larger transcript thousands of times. For very short runs the overhead is not worth it; for long autonomous agents it usually pays for itself many times over, which this calculator lets you confirm for your own numbers.