How Codex and Claude Code Compress Context: Mechanisms, Experiments, and Performance
The article analyzes Codex's opaque, encrypted compaction items versus Claude Code's transparent summaries, explains trigger mechanisms, details a reverse‑engineering prompt‑injection experiment, and presents a benchmark where native server compression achieves 100% accuracy while plain text summaries lag behind.
For a long time Codex's context compression has been a black box: the API returns an opaque, encrypted compaction item whose contents are hidden, leading to speculation that it stores latent‑space vectors.
A recent X post revived the discussion, claiming that Codex uses server‑side encrypted compression when running OpenAI GPT models, enabling effectively unlimited context for long tasks, whereas other harnesses lack this ability and suffer performance loss.
Automatic compression is triggered when context usage approaches a preset threshold, occurring before a hard window overflow. Both Claude Code and Codex also allow manual triggering, so the system shortens old content early to free space.
When calling the OpenAI Responses API, developers can set compact_threshold in context_management. For example, the official demo sets the threshold for gpt-5.3-codex at 200 k tokens; once exceeded, the server runs an in‑stream compression, returns an encrypted "compaction item", cuts old context, and continues inference. The documentation merely labels the item as opaque.
Codex CLI, however, uses a different entry point: the client monitors token count and, upon reaching the threshold, initiates server compression. Remote v2 appends a compaction_trigger to the request, while remote v1 calls a separate compact endpoint, so the API’s compact_threshold does not directly map to the CLI’s internal trigger.
Anthropic introduced a similar server‑side compression in January. Its Messages API applies the compact_20260112 policy with a default 150 k token threshold. When triggered, the API first generates a dialogue summary as a compaction block placed at the start of the reply, which is then fed back in the next round.
The key difference is that Anthropic’s summary is plaintext: the default summary prompt is public (it asks the model to record state, next steps, and lessons learned) and can be replaced via the instructions parameter. A pause_after_compaction switch lets the API pause after compression, returning recent messages unchanged before continuing.
In Claude Code one can request the system to return the summary after a /compact operation, illustrating the transparent nature of its compaction.
Thus, Anthropic provides a readable summary, while OpenAI returns a locked black box.
Codex CLI is open source, and its compression follows two paths: for non‑OpenAI models it performs local summarization (the prompt and hand‑off prompt are visible in the source); for GPT‑series models it uses the server path and receives the encrypted blob.
In March, Kangwook Lee demonstrated a reverse‑engineering method using prompt injection. By embedding a fake SYSTEM NOTE containing a keyword, the compressor copied that content into the summary; a second API call then extracted it. The resulting blob turned out to be a plain LLM summary encrypted with Fernet, not a latent‑space representation. The injected prompt began with "You are performing a CONTEXT CHECKPOINT COMPACTION" and matched the server‑side prompt used for non‑OpenAI models.
Alexis Gallagher (Pi community developer) initially believed the encryption hid advanced techniques, but after the reverse‑engineering concluded it was just an ordinary summary, he revised his view: the stronger performance of Codex compression may stem from the model itself rather than hidden tricks.
Gallagher later built the pi-openai-server-compaction extension to plug Codex‑style server compression into Pi and ran a benchmark.
The benchmark compared native server compression with a text‑summary approach under equal token budgets. Using the same GPT‑5.6 Sol model, both methods processed synthetic software‑project dialogues (~35 k tokens) containing 325 state items and many distractors. After compression, 75 questions were answered and scored by exact string match.
Results: native compression answered all 900 questions correctly, matching the full‑context baseline; the text‑summary approach answered 745 correctly (82.8%). A dense‑summary prompt performed even worse at 76.7%.
Text summaries struggled on task‑continuation questions (only 35.6% correct vs. 100% for native compression). Prompt‑engineering could not fully recover the loss; a balanced summary preserved early facts but dropped later task state, while a task‑prioritized summary kept task state but reduced early factual accuracy to 15%.
Gallagher’s report concludes that the encrypted blob does not contain latent‑space data; an encrypted, model‑optimized text summary explains everything. Both statements hold: the server does essentially the same work as a client‑side summary, but the encrypted version offers higher fidelity, while Claude’s transparent summary offers more control.
From personal use, Codex’s app with a 258 K window shows almost lossless performance after automatic compression.
OpenAI documentation hints that a compaction item carries key state and reasoning to the next round. Since chain‑of‑thought reasoning is never exposed, encryption may be intended to hide non‑public information.
In short: Codex trades transparency for fidelity with a black‑box compaction, whereas Claude trades fidelity for control with a transparent summary.
Claude Code’s CLI compression follows a five‑layer waterfall: >50 k characters of tool results are dropped from context; cache_edits removes old tool output from the server cache; a nine‑section session note is continuously maintained and used as a zero‑cost summary; if needed, a full LLM summary is performed; after compression the system re‑reads up to five recent files with a 50 k token budget. Codex employs a similar re‑read of the five most‑recent edited files with the same budget.
Claude now ships with a 1 M token context window, though the community typically manually compacts at 500–600 K tokens.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
