Context compaction in Claude Code and Codex¶
A coding agent sends the model the whole conversation on every response, so the context only grows. When it nears the model's limit, the harness has the model summarise the session and carries on from the summary. That is a compaction. After it, the agent knows only what the summary kept.
agent-top 0.30 draws the context of every session as a timeline, with each compaction marked.
Why the context only grows¶
A model keeps no state between requests. For each response the harness sends everything again: the system prompt, the tool definitions, memory files such as CLAUDE.md or AGENTS.md, every message, and every tool result still in the conversation. A file the agent read on its third response is sent again on every later response until something removes it.
So the prompt size of a session climbs with each response, and each step up is whatever was added since the last one: a reply, a tool result, a message of yours. Every token in it is paid for again on each response, at the cache-read price when the prompt cache holds and at the full input price when it does not. Context by source shows that cost per tool.
Before the prompt reaches the model's context window, the harness compacts.
What a compaction does¶
Every harness does roughly the same thing:
- Send the conversation to the model with an instruction to summarise it: what the task is, what has been done, what was decided, what is left.
- Replace the conversation with that summary.
- Continue, sending the fixed parts (system prompt, tools, memory files) plus the summary.
After a compaction the agent works from the summary. Exact tool output and files it read early on are gone unless the summary kept them.
Claude Code¶
According to its docs, Claude Code compacts on its own when the conversation reaches the model's context limit. A model with a 1M window compacts before it fills, at about 967K tokens by default. The autoCompactWindow setting or the /autocompact command sets a different point, and CLAUDE_AUTOCOMPACT_PCT_OVERRIDE can lower it to a percentage of that window. /compact asks for one by hand and takes optional instructions on what the summary should focus on. /context shows how full the context is.
What survives a compaction: the system prompt and output style still apply, the project's CLAUDE.md and auto memory are read again from disk, and up to five recently changed files are re-read. PreCompact and PostCompact hooks run around it, with the trigger as manual or auto.
It records each compaction in the session's transcript, a JSONL file under ~/.claude/projects. A system line with subtype: "compact_boundary" carries compactMetadata with the trigger (auto or manual), preTokens (the context size before) and postTokens, followed by the summary itself as a user line marked isCompactSummary.
From the 32 Claude Code sessions on the machine this post was written on:
- 17 compacted at least once, 60 times in all, every one of them automatic.
- The size before ranged from 200k to 968k tokens, the two documented thresholds. The same model id runs with a 200k or a 1M window, and the transcript does not say which.
postTokensis the summary alone. The first prompt after a compaction was about four times it (median 66.8k against a 16.9k summary), because the system prompt, tools and memory files are sent again with it.
Codex¶
Codex's compaction is in its open source, codex-rs/core/src/compact.rs (read at rust-v0.171.0). It compacts automatically once the context reaches 90% of the model's window, or earlier if model_auto_compact_token_limit in its config is lower. model_post_turn_compact_threshold_percent makes it compact at the end of a turn instead of mid-turn, and /compact asks for one by hand. /status shows the context left.
The summary prompt asks for "a handoff summary for another LLM that will resume the task": progress and decisions, constraints and user preferences, what remains, and any data needed to continue. Codex keeps your most recent messages, up to about 20,000 tokens of them, and adds the summary after them, prefixed with a note that "another language model started to solve this problem and produced a summary of its thinking process". compact_prompt in the config replaces the summary prompt.
Codex writes the model's window into every session as model_context_window, after taking 5% off as headroom. The sessions agent-top has seen say 258,400, which is 95% of 272,000; automatic compaction comes at 244,800. The source also writes a compacted line with the summary when it compacts. None of the 132 Codex sessions on this machine has one yet, so agent-top does not read it until it has a real one to test against.
Side by side¶
| Claude Code | Codex | |
|---|---|---|
| Automatic | at the context limit (about 967K on a 1M window), or autoCompactWindow |
at 90% of the window, or model_auto_compact_token_limit |
| By hand | /compact [instructions] |
/compact |
| Summary prompt | focus instructions on /compact |
replaceable with compact_prompt |
| Kept beside the summary | system prompt, tools, CLAUDE.md and memory, up to five recent files |
system prompt, tools, its rebuilt initial context, your recent messages up to about 20k tokens |
| Recorded | a boundary line with trigger, size before and summary size | a compacted line with the summary (none seen yet) |
| Window in the transcript | no | yes, model_context_window |
OpenCode records its compactions as a summary message. Gemini CLI has no compaction record that agent-top has seen. Kodelet stores only the context's current size, so it has no timeline.
What agent-top shows¶
agent-top reads each response's prompt size from the transcript and draws it, one point per response. Press c on a session in the live view, or open a session in agent-top ui:

A compaction the harness recorded is a solid rule, and the table under the chart gives its trigger, the size before, the summary size, the first prompt after, and how many responses it took to fill. When the harness recorded nothing, as with Codex today, a prompt that falls below half its previous size is marked as an inferred compaction with a dashed rule. When the harness writes the window, a dotted line marks it.
What to look for:
- Steep steps. One tool result that added tens of thousands of tokens. It is re-read on every response until the next compaction. Ask for a line range,
greporheadinstead of the whole file. - The floor after each compaction. The first prompt after is the fixed cost of every response. A high floor means large memory files or many MCP tool definitions, and it leaves less room before the next compaction.
- Compactions close together. A stretch that fills in a handful of responses means the agent keeps working from summaries. Start a fresh session, or move the work to a subagent.
Each subagent starts from an empty context and gets its own timeline: Tab in the live view, or the Agent list in the web view. agent-top sync keeps all of it in the local store, so you can query compactions across sessions with agent-top sql.
Reading the context timeline explains each mark and column.
Try it¶
brew upgrade agent-top # or: cargo install agent-top
agent-top # select a session, press c
agent-top sync && agent-top ui
The first sync after upgrading re-reads every transcript to fill the new tables, so it takes longer than usual.