Same Word, Different Mechanism: Claude Code vs Codex Context Compaction¶
For / Key Points
For: Engineers using Claude Code or Codex CLI for multi-hour sessions who need to understand what can disappear after compaction.
Key Points:
- Claude Code combines tool-result paging, selective clearing, and full summarization
- Codex rebuilds history around recent user messages and a summary
- The two tools preserve different things, so the risky place to store decisions is different
A long refactoring session hits the context limit and the interface says the context was compacted. Immediately afterward, the agent seems to forget a decision that felt settled. That can happen in both Claude Code and Codex CLI, but what disappeared is not the same.
The question is: after compaction, what do Claude Code and Codex preserve, and what do they discard? For Codex, the answer can be checked directly in the open-source compact.rs and config definitions.12 For Claude Code, the public evidence is asymmetric: official docs explain user-facing behavior, while internal constants come from independent analyses of TypeScript source that was accidentally shipped in the npm package in late March 2026.678 That distinction matters when assigning confidence.
Claude Code Tries to Preserve Before Summarizing¶
Claude Code does not jump straight to summarizing the whole conversation. Public source analyses describe a staged pipeline: large tool results are first moved out of the live context, older tool results are then selectively cleared, and full summarization is used only when those steps are not enough.67
The first move is closer to paging than summarization. Instead of stuffing a huge command output or search result into the conversation, Claude Code stores the body on disk. The live context keeps a short preview and a file path, so the model can fetch the full text again with the Read tool if needed.
Victor Dibia's analysis reports a per-tool maxResultSizeChars threshold, with a default of 50,000 characters.6 Outputs above that threshold are replaced with roughly a 2 KB preview and a persisted path. Read is treated as an exception, because persisting Read output and then asking Read to fetch it again would create a loop.6
At this stage, the information has not been lost. It has moved from the conversation to the file system. The important design move is separating the body from the reference.
Old Tool Results Are Selectively Cleared¶
If paging is not enough, Claude Code starts clearing older tool_result content. The target is not the entire conversation. It is mostly old tool output.
Public analyses call this microcompaction.7 When the prompt cache is still warm, Claude Code reportedly uses a cache_edits path to clear old results while preserving the cached prefix as much as possible. When the cache is cold, it can clear content inline instead.
Anthropic's platform docs show the same broader strategy at the API level. Context editing supports tool result clearing for agentic workflows with heavy tool use, removing old tool results once the conversation grows beyond a configured threshold.12 That does not prove the API feature and Claude Code internals are identical, but it confirms the design pattern is officially supported.
This stage can lose information. Large results that were paged to disk can be recovered by reference. Smaller results that were never persisted may simply be gone from the live conversation. Claude Code's advantage is not that it loses nothing; it is that it tries to make the loss more targeted before falling back to a full summary.
Full Summarization Is the Last Resort¶
Claude Code eventually performs full conversation summarization too. Public analyses suggest that with a 200K-token context model, auto-compaction triggers in the low-to-mid 80% range of the window.8 That should be treated as an implementation snapshot, not a stable contract.
The full summary is produced by a forked summarization process, then the session is rehydrated with recent files, plan state, skills, and related context according to the public analyses.78 So the post-compaction state is not just a summary. It is a summary plus selected working materials.
The official docs make the same operational point from the user side. Claude Code best practices say auto compaction summarizes important details such as code patterns, file states, and key decisions, and that users can run /compact <instructions> for more control.9 The memory docs also state that a project-root CLAUDE.md is re-read after /compact, while conversation-only instructions can disappear.10
/compact Preserve auth design decisions, target file paths, and pending tasks.
The practical lesson is direct. Do not keep durable constraints only in chat. Put them in CLAUDE.md, AGENTS.md, or a design note, and use manual compaction instructions at task boundaries.
Codex Rebuilds History Around User Messages and a Summary¶
Codex compaction is more direct. When the threshold is reached, it generates a summary, discards the old history, and rebuilds the conversation around recent user messages plus that summary.1
The source code makes the preservation rule explicit:
const COMPACT_USER_MESSAGE_MAX_TOKENS: usize = 20_000;
collect_user_messages extracts user messages, and build_compacted_history fills the replacement history from the newest user messages backward up to the 20,000-token limit.1 Messages beyond the budget are truncated or dropped. Existing summaries are detected with SUMMARY_PREFIX, so summaries do not simply stack forever.1
The inverse is just as important. Assistant messages and tool_result bodies are not preserved as original history in the replacement transcript. If an architectural decision exists only as an assistant suggestion or as an interpretation of tool output, it survives only if the summary captures it. Codex structurally prioritizes the user's stated intent, not the assistant's intermediate context.
Codex Also Has a Remote Compaction Path¶
Reading only the local summarization path is not enough for Codex. OpenAI's API reference exposes POST /responses/compact, which compacts a conversation and returns a compacted response object.3 The Codex Prompting Guide also describes calling /compact when the context grows large and notes that the endpoint can return encrypted_content for future requests.4
The Codex CLI source contains a remote-compaction branch and a /responses/compact endpoint constant.1 Simon Zhou's prompt-injection experiment suggests that the server-side compactor produces an encrypted blob that is later decrypted and injected into subsequent context.5 Because that is an independent probe rather than official documentation, it is best treated as an informed inference.
There are also configuration knobs. config/mod.rs defines model_auto_compact_token_limit for auto-compaction thresholds and tool_output_token_limit for how much tool output is retained in the context manager.2
model_auto_compact_token_limit = 150000
tool_output_token_limit = 16000
That second setting is not the same as Claude Code's disk paging. It limits what enters the retained context; it does not turn output into a recoverable file reference.
The Preserved Object Is Different¶
The main difference is not a small implementation detail. The two tools make opposite bets about what deserves to remain verbatim.
Claude Code tries to move tool result bodies out of the live context and keep recoverable references. It also re-reads project memory such as CLAUDE.md after compaction. This assumes that work state lives partly in the file system and project artifacts, not only in the transcript.
Codex preserves recent user messages up to a fixed budget and folds the rest into a summary. Assistant messages and tool results become summary material. This assumes that the user's explicit intent is the most important verbatim artifact.
| Dimension | Claude Code | Codex CLI |
|---|---|---|
| Shape | Paging, selective clearing, full summarization67 | Replacement history built from user messages and summary1 |
| First protected object | Large tool-result bodies | User-message text |
| Easy-to-lose content | Small old tool results, chat-only constraints | Assistant decisions, tool results, older user messages |
| Cache handling | Public analyses describe a cache_edits path7 | Replacement makes old prefixes harder to preserve |
| Summary visibility | Easier to steer with /compact instructions9 | Remote path can involve encrypted content35 |
| Practical mitigation | CLAUDE.md, design notes, manual /compact | File-based decisions, explicit user instructions, threshold tuning |
The takeaway is not that one approach is universally safer. The risky storage location is different.
In Claude Code, constraints mentioned only in conversation can disappear after a full summary. In Codex, decisions proposed by the assistant or inferred from tool results are unlikely to remain verbatim. For long-running work, important decisions need to live outside the chat transcript in both systems.
"Claude Code Remembers Better" Is Too Strong¶
It is tempting to say Claude Code remembers longer and Codex forgets more. That conclusion is too strong. There is no public benchmark, as of July 10, 2026, that compares the two under the same task, model quality, context budget, and compaction schedule.
The design supports a narrower claim. Explicit user requirements are structurally easier for Codex to preserve because recent user messages have a dedicated 20,000-token budget.1 Intermediate assistant decisions and tool-output interpretations may fare better in Claude Code because full summarization is delayed by paging and selective clearing.
The practical rule is the same across tools:
- Put durable decisions in
AGENTS.md,CLAUDE.md, or a design note - After compaction, ask the agent to restate the plan, target files, and unfinished tasks
- Save large outputs as logs or analysis files instead of relying on chat history
- Compact manually at clean task boundaries instead of waiting for emergency compaction
Compaction is also moving from CLI harness behavior into API and model behavior. OpenAI exposes /responses/compact.3 Anthropic exposes beta server-side compaction.11 OpenAI's Codex Prompting Guide describes first-class compaction support as part of recent Codex model improvements for long-running reasoning and longer conversations.4
So memorizing today's harness constants is not enough. The durable habit is to understand what the official contract preserves, then move project decisions into artifacts that survive any transcript rewrite.
Summary¶
Claude Code combines tool-result paging, selective clearing, and full summarization. It tries to keep recoverable references and rehydrate working context after compaction.
Codex rebuilds history around recent user messages and a summary. It preserves explicit user intent more directly, but assistant messages and tool results survive only through summarization.
The operational answer is simple: do not entrust long-running task memory to chat history alone. Claude Code benefits from CLAUDE.md and manual /compact instructions. Codex benefits from explicit user-level decisions and file-based state.
Related Articles¶
- The Loop Becomes the Unit of Work: Long-Running Task Design in OpenAI's Codex White Paper
- What Is Harness Engineering: What Do You Actually Build?
openai/codex
codex-rs/core/src/compact.rshttps://github.com/openai/codex/blob/main/codex-rs/core/src/compact.rs ↩↩↩↩↩↩↩openai/codex
codex-rs/core/src/config/mod.rshttps://github.com/openai/codex/blob/main/codex-rs/core/src/config/mod.rs ↩↩OpenAI API Reference, "Compact a response" https://platform.openai.com/docs/api-reference/responses/compact ↩↩↩
OpenAI, "Codex Prompting Guide" https://developers.openai.com/cookbook/examples/gpt-5/codex_prompting_guide ↩↩
Simon Zhou, "Investigating how Codex context compaction works" https://simzhou.com/en/posts/2026/how-codex-compacts-context/ ↩↩
Victor Dibia, "Inside Claude Code" https://newsletter.victordibia.com/p/inside-claude-code ↩↩↩↩↩
brtkwr.com, "What we can all learn from the Claude Code source" https://brtkwr.com/posts/2026-04-01-what-we-can-all-learn-from-the-claude-code-source/ ↩↩↩↩↩↩
"How Claude Code Manages Infinite Conversations in a Finite Context Window" https://oldeucryptoboi.com/blog/context-compaction-deep-dive/ ↩↩↩
Anthropic, "Best practices for Claude Code" https://code.claude.com/docs/en/best-practices ↩↩
Anthropic, "How Claude remembers your project" https://code.claude.com/docs/en/memory ↩
Anthropic, "Compaction" https://platform.claude.com/docs/en/build-with-claude/compaction ↩
Anthropic, "Context editing" https://platform.claude.com/docs/en/build-with-claude/context-editing ↩