Harness Notes

Compaction

Updated 2026-11-24,

On this page

What each harness does when the conversation outgrows the context window: when it compacts, what it keeps, and how you can steer it.

When it happens

QuestionCodex CLIchecked at 0.162.1
Automatic compaction, and when

Yes. Before each turn, after each model step that needs a follow-up, and when you switch to a model with a smaller window, Codex compares the active context (the last reported input + output token count) to an auto-compact limit of 90% of the model's context window (or model_auto_compact_token_limit if lower) and compacts when it is reached; it also force-compacts at the usable window, 95% of the context window. For the bundled 272,000-token models that means compaction at about 244,800 tokens, with a hard ceiling at 258,400.

model_post_turn_compact_threshold_percent (default 0, off) adds an optional extra check after a final answer.

0.166.0 keeps track of whether the last reported token count already included reasoning from earlier turns, and saves that with the thread. Resumed and forked threads no longer count that reasoning twice, which could trigger automatic compaction too early. Older saved threads without this information keep the previous estimate.

0.167.0 release fix, traced at 0.167.2: retained output from failed or usage-less responses remains in the token estimate until a usable server count covers it. History tracks the last usage-covered item boundary; total usage adds the estimated tail. Completion updates that boundary only when token_usage is present. This changes counting, not the configured compaction threshold; no runtime probe.

Change the trigger or turn it off

Partly

Set model_auto_compact_token_limit in config.toml to compact earlier; with the default model_auto_compact_token_limit_scope = "total" it is capped at 90% of the context window, while "body_after_prefix" counts only tokens added since the carried-over prefix. model_post_turn_compact_threshold_percent (0-100) also compacts after a final answer once that percentage of the usable window is used. There is no setting to turn automatic compaction off: the 95% usable-window guard always applies.

model_context_window can raise the window up to the model's maximum (872,000 tokens for most bundled models), which moves both thresholds.

Compact by hand

Partly

Type /compact in the TUI (it can run even while a task is in progress). It takes no arguments, so you cannot tell a single /compact what to keep; persistent instructions only go through compact_prompt, which applies to local compaction only.

See how full the context is

The TUI footer shows NN% context left, and /status shows NN% left (used / window). You can add a context item to the footer with /statusline. The percentage treats the first 12,000 tokens as baseline and measures against the full context window, so it reaches 0% later than auto-compaction fires (90%).

What it does

QuestionCodex CLIchecked at 0.162.1
What replaces the conversation

With OpenAI (and Azure Responses) providers, Codex uses remote compaction: it sends the history plus a compaction trigger through the Responses API, and the server-side model returns one encrypted compaction item. With other providers it runs locally: the current model gets the summarization prompt (or compact_prompt) and writes a handoff summary, which is stored as a user message beginning with a fixed "Another language model started to solve this problem..." prefix. Either way the history is replaced by retained user messages, freshly rebuilt initial context, and the summary or compaction item. After that Codex warns that long threads and repeated compactions reduce accuracy.

Custom providers can choose with model_providers.<id>.capabilities.remote_compaction = "v2" or "unsupported".

With the token_budget feature enabled, compaction resets the context window without summarizing.

What survives

Partly

Codex rebuilds the initial context after compaction: base and developer instructions, AGENTS.md, environment and permissions context, and the skills and plugins catalog. Local compaction keeps your most recent user messages, up to about 20,000 tokens. Remote compaction keeps your user messages and short non-final sub-agent messages (each up to 10,000 tokens), up to about 64,000 tokens total, newest first. Assistant replies, tool calls and tool outputs are dropped and survive only through the summary.

Images in retained messages may be dropped (only their text is kept) when the token budget is exceeded.

Trimming without summarising

Partly

Yes, separately from compaction: each tool or function output is truncated when it is stored in history, to 10,000 tokens by default for bundled models. You can change this with tool_output_token_limit (or per MCP tool with mcp_servers.<id>.tools.<tool>.output_token_limit). Before remote compaction, Codex also shortens old function-call outputs if needed so the request fits the window. There is no separate setting to prune old turns.

The full history afterwards

Yes. The session rollout under ~/.codex/sessions/ is append-only: compaction adds a compacted record with the replacement history (and, for local compaction, the summary text in message) after the original turns, which remain. Remote compaction summaries are encrypted, so you cannot read them; codex resume restarts from the latest compaction point.

Shaping it

QuestionCodex CLIchecked at 0.162.1
Customise the summary or hook in

Set compact_prompt (inline) or experimental_compact_prompt_file (a file path) in config.toml to replace the summarization prompt; the inline value wins, and both apply only to local compaction. PreCompact and PostCompact hooks run around both manual and auto compaction (matcher on trigger: manual or auto). A hook returning continue: false stops the turn, but it cannot change the summary.

When a request overflows anyway

Partly

If a normal request still overflows, the turn ends with "Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying." Codex then marks the context as full, so the next turn compacts before sending, but it does not retry the failed request. If the compaction request itself overflows during local compaction, Codex drops the oldest history items one at a time and retries.

Codex compacts and retries in the same turn only for Guardian auto-review sessions whose review budget is exhausted.

0.164.0 source: local compaction works on a cloned history and installs replacement history after a successful response; background take_ready drops a closed result channel instead of later awaiting it, returning no candidate to the foreground compaction path. Existing trigger/provider conditions remain independently audited; no runtime preservation probe.

Notes and sources

Codex CLI, 36 sources
Automatic compaction, and when, 10
Change the trigger or turn it off, 4
Compact by hand, 3
See how full the context is, 4
What replaces the conversation, 7
What survives, 3
Trimming without summarising, 5
The full history afterwards, 4
Customise the summary or hook in, 5
When a request overflows anyway, 8