Context compaction in ai coding agents fires late, keeps little, and costs a pause you can measure. We read every compaction three agents recorded on one machine, 69 events in all. Claude Code fired at about 97% of a 1M window, Codex CLI between 75% and 95% of 258,400 tokens, and both cut the conversation to a median of roughly 17,000 to 18,000 tokens. By the end you will know when each agent compacts, what it keeps, how long you wait, and the one thing Antigravity's logs cannot tell you. Measured 2026-10-09 from logs written by Claude Code 2.1.270–2.1.295, Codex CLI 0.152.1–0.160.1, and Antigravity between 2026-09-04 and 2026-09-17. Compaction also resets the prompt cache, which is where much of the cost of AI coding tools is decided.
Key takeaways
- Claude Code compacts late. 10 of 11 events fired between 966,933 and 983,408 tokens on 1M-context sessions.
- Codex CLI compacts in a band, not at a line. 38 events fired at 75.1% to 94.6% of a 258,400-token window, median 87.5%.
- Both keep about the same amount. Median after compaction: Claude Code 16,726 tokens, Codex CLI 18,256.
- The wait is real, and in Claude Code it changed. 38–129 seconds through 2.1.285; under a second on 2.1.289 and 2.1.295, with a 7–9 second gap before the next reply.
- Antigravity's log truncates its own summary at about 4,121 bytes, so only its cadence is measurable: a median of 324 steps between compactions.
The short answer
Every agent here compacts automatically, near the top of its window, and keeps under 20,000 tokens in the median case. What differs is how close to the edge they run, how long the pause is, and how much of the event they write down. Claude Code records exact before and after counts. Codex CLI records the window and the fill around the event. Antigravity records the summary but not its size.
For Claude Code alone, with the boundary record and the reload in detail, read Claude Code compaction explained. This page is the cross-agent comparison that one cannot make.
Where the events came from
Nothing was triggered for this article. Each agent already writes its own record of compacting, and our census script reads them, keeping only numbers.
| Agent | Where the record lives | What it records | Events |
|---|---|---|---|
| Claude Code | ~/.claude/projects/**/*.jsonl, compact_boundary | Tokens before and after, duration, trigger | 11 |
| Codex CLI | ~/.codex/sessions/**/*.jsonl, compacted | Window, fill before, tokens after, duration | 41 |
| Antigravity | ~/.gemini/antigravity/brain/…/transcript/*.jsonl, CHECKPOINT | The kept summary, truncated | 17 |
node scripts/compaction-census.mjs
# → Claude Code: 11 events
# → Codex CLI: 41 events
# → window { n: 38, min: 258400, median: 258400, max: 258400 } fill { n: 38, min: 0.751, median: 0.875, max: 0.9455 }
# → Antigravity: 17 events
The Claude Code sample is the smallest for a reason: its transcripts are deleted after 30 days, so the census only sees September 13 onward. Codex logs from September 4 were still on disk.
When context compaction fires
Claude Code waits until the window is nearly full. All 11 events were automatic. Ten fired between 966,933 and 983,408 tokens on 1M-context sessions, about 97%. The eleventh fired at 1,096,728, above the nominal window, and is recorded as unexplained rather than explained away.
Codex CLI fires across a band. Every event with a recorded window was on 258,400 tokens. The request before compaction held between 75.1% and 94.6% of it, median 87.5%. The check runs between requests, so one large tool result can carry the context well past the point where compaction would otherwise have started. Codex documents the threshold as model_auto_compact_token_limit in its configuration reference; we left it at the default.
The practical difference is headroom. Claude Code gives you nearly the whole window before it summarises. Codex starts earlier, and how much earlier depends on what the last turn returned.
What survives the summary
Both agents keep a similar amount, far less than they dropped.
| Agent | Before (median) | After (median) | After (range) |
|---|---|---|---|
| Claude Code | 969,292 tokens | 16,726 tokens | 9,270 – 186,036 |
| Codex CLI | 87.5% of 258,400 | 18,256 tokens | 13,624 – 80,014 |
That is a reduction of about 98% in Claude Code and about 92% in Codex CLI in the median case. Whatever you need after compaction has to be in the summary, in a file the agent re-reads, or in a project instruction file. What fills a context window in the first place decides how much of that is yours to control.
The wait, and a change in Claude Code
Compaction is a model call, and you wait for it.
| Agent | Versions | Duration |
|---|---|---|
| Codex CLI | 0.152.1 – 0.160.1 | 32.2 – 270.3 s, median 72.2 s |
| Claude Code | 2.1.270 – 2.1.285 | 37.7 – 128.8 s |
| Claude Code | 2.1.289, 2.1.295 | 0.0 s and 0.2 s |
The last row is new. On the two most recent Claude Code events, the recorded duration was under a second, and the transcript timestamps agree: the boundary record landed 0.6 to 0.7 seconds after the previous record, and the next reply arrived 7 to 9 seconds later. Those two events also kept more, 41,203 and 44,676 tokens, against 9,270 to 31,770 on every earlier event but one.
The Claude Code changelog for 2.1.286 says the compaction spinner's "timer now starts when compaction begins". It contains no entry that explains a near-instant compaction. Two events is a pattern worth watching, not a documented change, and we report it that way.
Antigravity logs less than it keeps
Antigravity compacts too. Its transcript records a CHECKPOINT step whose content opens with "Resuming from a compaction", followed by the summary the agent continues from. We found 17, a median of 324 steps apart (range 140 to 1,913).
What the log does not give is the size of that summary. Every one of the 17 records is marked truncated_fields: ["content"], and every one is cut at 4,120 or 4,121 bytes. A constant size across 17 different conversations is a logging cap, not a summary length. There are no token counts in the transcript either, so for Antigravity this census can measure how often it compacts, not how much it keeps.
Working with context compaction in ai coding
These follow from the measurements rather than from any vendor's advice.
- Put durable facts in files, not in the conversation. In the median case, more than 90% of the conversation is gone after compaction. A project instruction file is reloaded; a decision made forty turns ago is a line in a summary, if that.
- Compact on your own schedule before a large task. Claude Code runs to about 97% before it acts. Starting a big refactor at 80% guarantees a compaction in the middle of it. Manual control is covered in the /compact command guide.
- Expect a pause in Codex. A median of 72 seconds, and once 270, is long enough to look like a hang.
- Read your own logs. The census script needs no network and no account, and it prints only numbers.
What did not work
Antigravity's summary size was the measurement we wanted, and the log will not give it. The first pass reported a median summary of 4,121 bytes. The figure was too constant to be real, and the truncation flag confirmed it was a logging cap.
Codex fill is missing on 3 of 41 events. Three compactions had no token count before them in the log, so 38 events carry a fill and all 41 carry a kept count.
Claude Code's sample is cut by its own retention. The census can only see 30 days, because the transcripts before that were deleted. A census run on a fresh install will see fewer events, not more.
What we are not claiming
This is one machine, one developer and three agents, with event counts of 11, 41 and 17. The work behind the events was not controlled, so this does not compare summary quality, task success or cost between agents. The Claude Code duration change rests on two events. Antigravity's kept size is unknown, not small.
Conclusion
Context compaction in ai coding is automatic in all three agents, and it removes about 92–98% of the conversation in the median case. Plan for it: keep anything that must survive in a file, compact before a big task, and expect a minute-long pause in Codex. The table above lists where each agent keeps its record, if you want to look at your own. We replay this page's figures from a saved fixture with an internal check script, which also re-reads the changelog.
Frequently asked questions
What is context compaction in AI coding agents?
When does Codex CLI auto compact?
When does Claude Code compact the context?
How long does context compaction take?
Does Antigravity compact the context?
Muhammad Kashif
Founder and editor of Devventa, covering AI coding assistants, Next.js and the modern AI development stack.





