# Codex Auto Compact: When It Triggers, and How to Control It

> Codex compacts a long conversation automatically at 90% of the model's context window: 244,800 tokens on the 272K models in Codex 0.166.0. Where that number comes from, what model_auto_compact_token_limit can and can't change, why there's no off switch, how to compact between turns instead of mid-task, and what to do when compaction loops or loses your task.

By Tahir Nazir · Published 2026-11-06 · Updated 2026-11-06
Canonical: https://www.its-tahir.com/blog/codex-auto-compact

**Codex auto compacts a conversation when it reaches 90% of the model's context window. In the model catalog bundled with Codex 0.166.0, most models have a 272,000-token window, so compaction starts at 244,800 tokens (your live catalog can differ; [check yours](#how-do-i-check-my-own-codex-compaction-threshold)): Codex shows `Compacting context`, replaces older turns with a summary, and then shows `Context compacted`. There is no setting that turns it off. `model_auto_compact_token_limit` can only make it start earlier; to make it start later, raise `model_context_window`. To keep it from interrupting work, run `/compact` yourself or set `model_post_turn_compact_threshold_percent` so it compacts between turns.**

| You want | Set this in `~/.codex/config.toml` |
| --- | --- |
| Compact earlier, at a fixed token count | `model_auto_compact_token_limit = 150000` |
| Compact later | `model_context_window = 500000` (up to the model's maximum) |
| Compact between turns, not mid-task | `model_post_turn_compact_threshold_percent = 70` |
| A different summary | `compact_prompt = "..."` or `experimental_compact_prompt_file = "path"` |
| Compact right now | Type `/compact` |
| Turn auto compaction off | Not possible in Codex 0.166.0 |

*Last reviewed 6 November 2026, against Codex 0.166.0. The threshold rules come from the 0.166.0 source code on GitHub and the bundled model catalog; settings from OpenAI's configuration reference; on-screen messages from the 0.166.0 Linux package, which I downloaded from npm and searched without running it. I couldn't run a signed-in Codex session for this post, so the numbers are what the code computes, not measurements from my own sessions.*

![When Codex auto compacts, Codex 0.166.0. Default 272,000-token window, the most common size in the bundled catalog: auto compact at 244,800 tokens (90%), hard cap that always compacts at 258,400 (95%). model_context_window = 872000: auto compact at 784,800. model_post_turn_compact_threshold_percent = 70: turn-end compact at 180,880, 70% of the 258,400 usable window. There is no setting that turns auto compaction off.](https://www.its-tahir.com/blog/codex-auto-compact/codex-compaction-thresholds.svg)

*Auto compaction starts at 90% of the context window. The only ways to move it later are a bigger window or a manual /compact before you reach it.*

## What is auto compact in Codex?

**Auto compact is Codex summarizing its own conversation when the context window is nearly full, so a long task can keep going.** Every request Codex sends to the model includes the conversation so far. When that history gets close to what the model can accept, Codex replaces earlier turns with a shorter summary and carries on. In the CLI you see two lines:

```text
Compacting context
Context compacted
```

The same thing happens when you type `/compact`; auto compaction just picks the moment for you. Codex can compact at three points in a turn: before it sends your next message, in the middle of a turn when a tool result pushes the context over the limit, and (if you turn it on) after a finished reply.

## When does Codex auto compact?

**Codex auto compacts when the active context reaches 90% of the model's context window: 244,800 tokens on a 272,000-token model in Codex 0.166.0, and 450,000 on a 500,000-token one.** As a rule: the threshold is the smaller of `model_auto_compact_token_limit` and 90% of the context window, and reaching 95% of the window always forces compaction. This is the function in Codex 0.166.0 that sets it:

```rust
pub fn auto_compact_token_limit(&self) -> Option<i64> {
    let context_limit = self
        .resolved_context_window()
        .map(|context_window| (context_window * 9) / 10);
    let config_limit = self.auto_compact_token_limit;
    if let Some(context_limit) = context_limit {
        return Some(
            config_limit.map_or(context_limit, |limit| std::cmp::min(limit, context_limit)),
        );
    }
    config_limit
}
```

So it returns the smaller of that 90% figure and any limit set for the model or in your config. The model catalog bundled with 0.166.0 sets no per-model limit, so for every listed model the 90% figure is the threshold:

| Model (bundled catalog, 0.166.0) | Context window | Auto compact at | Maximum window |
| --- | --- | --- | --- |
| gpt-6.1-sol*, gpt-6-sol, gpt-6-luna, gpt-6-astra | 272,000 | 244,800 | 872,000 |
| gpt-5.6-sol, gpt-5.6-terra, gpt-5.6-luna | 272,000 | 244,800 | 872,000 |
| gpt-6.1-terra | 500,000 | 450,000 | 872,000 |
| gpt-5.5 | 272,000 | 244,800 | 272,000 |

\* Two caveats. Codex downloads a fresh catalog from OpenAI and caches it, so the live values for your account can differ from the bundled file. In one user's cache from 18 October 2026, quoted in [#55008](https://github.com/openai/codex/issues/55008), gpt-6.1-sol had a 500,000-token window, which puts its threshold at 450,000. And there's a second, harder limit: 95% of the window (258,400 tokens on a 272K model) is what Codex treats as usable, and reaching it forces compaction regardless of your settings.

Some summaries put the threshold at 40 to 50% of the window, or about 220,000 tokens. In Codex 0.166.0 it is 90% of the window, which is 244,800 tokens on a 272K model.

## How do I check my own Codex compaction threshold?

**Open `~/.codex/models_cache.json` (or `models_cache.json` in your `CODEX_HOME`), find your model's entry, and take 90% of its `context_window`.** That file is the catalog Codex downloaded for your account, so it beats any table, including mine. Three fields matter:

- `context_window`: the window Codex uses. Your threshold is 90% of it.
- `auto_compact_token_limit`: if it's set, the threshold is the smaller of this and the 90% figure. In the bundled catalog it's `null` for every model.
- `max_context_window`: the most you can raise `model_context_window` to.

If you set `model_context_window` or `model_auto_compact_token_limit` in `config.toml`, those override the file, with the same 90% cap.

## How do I change the auto compact threshold?

**`model_auto_compact_token_limit` moves it earlier; `model_context_window` moves it later.** Both go in `~/.codex/config.toml`.

To compact earlier, for example to keep each request smaller and cheaper:

```toml
model_auto_compact_token_limit = 150000
```

Codex uses the smaller of your number and 90% of the window, so a value above 244,800 on a 272K model changes nothing. Raising this setting is not a way to stop compaction.

To compact later, give the model a bigger window, up to the maximum listed for it in the catalog:

```toml
model_context_window = 500000
```

Codex caps this at the model's `max_context_window` (872,000 for most current models in the bundled catalog, 272,000 for gpt-5.5), and the 90% rule applies to the new size: 450,000 tokens for a 500K window, 784,800 for 872K. Keep in mind that a bigger window lets each request near the end of a long session carry more tokens, which uses more of your plan or costs more on an API key. A [context window checker](https://tools.its-tahir.com/context-window-checker) helps you see whether a big file or log fits before you paste it in.

There's also `model_auto_compact_token_limit_scope`. The default, `total`, counts the whole active context against the limit. `body_after_prefix` counts only what's been added since the last compaction's carried-over prefix, which is for setups that want each compaction window to have a fixed budget.

## Can I disable auto compact in Codex?

**No, not in Codex 0.166.0.** There's no config key, flag or slash command that turns it off for your sessions. The source does have an off switch, a thread setting called `AutoCompactionDisabled` in `codex-rs/core/src/compact.rs`, but only the app server sets it, for threads it starts internally. Nothing in your config can. An issue asking for more control over auto-compaction ([#4106](https://github.com/openai/codex/issues/4106)) has been open since September 2025.

What you can do instead:

1. **Push it later** with a bigger `model_context_window`, as above.
2. **Choose the moment** by running `/compact` at a natural break, before Codex reaches the threshold mid-task.
3. **Move it between turns** with `model_post_turn_compact_threshold_percent` (next section).
4. **Start a new thread** when a task is done, instead of stretching one conversation across several tasks.

The error for a request that doesn't fit in the window reads:

```text
Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.
```

## How do I compact manually?

**Type `/compact` and confirm.** Per OpenAI's docs, "Codex replaces earlier turns with a concise summary, freeing context while keeping critical details." Doing it yourself after a task is finished, or before you start a new one in the same thread, means the summary covers finished work instead of half-done work.

Don't confuse it with `/recap`, which summarizes the conversation for you to read and leaves the context as it is.

## How do I stop compaction from losing my task?

**Compact between turns, and keep the task somewhere a summary can't drop it.** A recurring complaint in Codex's issue tracker is that a summary written mid-task loses the current instruction, after which the agent asks what you'd like to work on or repeats finished steps ([#57608](https://github.com/openai/codex/issues/57608), [#57806](https://github.com/openai/codex/issues/57806), [#57983](https://github.com/openai/codex/issues/57983)). Three settings help:

- **`model_post_turn_compact_threshold_percent`.** It's off by default. Set it to compact "after a final response when active context reaches this percentage of the usable context window." At 70 on a 272K model, that is 70% of the 258,400-token usable window, so Codex compacts at 180,880 tokens once a reply is finished, usually well before the mid-task threshold. The normal limits still apply if a single turn grows past them.

  ```toml
  model_post_turn_compact_threshold_percent = 70
  ```

- **`compact_prompt`** (or `experimental_compact_prompt_file` for a longer one) replaces the instructions Codex uses to write the summary. Tell it to keep the current task, open decisions and file paths word for word.
- **`PreCompact` and `PostCompact` hooks** run your own command around each compaction, for example to save the current plan to a file before the summary replaces it.

The habit that works in any version: keep the task and its acceptance criteria in a file in the repo (a `TASK.md`, or a section in `AGENTS.md`), and tell Codex to re-read it after compaction. A file isn't part of the conversation, so no summary can lose it.

## Why does Codex compact too early?

**Usually the threshold is lower than the number you're comparing it with.** Common reasons:

- **The window is smaller than the model's maximum.** Most current models run with a 272K window even though the catalog allows up to 872K, so compaction at about 245K is expected ([check yours](#how-do-i-check-my-own-codex-compaction-threshold)).
- **An old build.** Codex 0.166.0 (29 October 2026) "preserved reasoning-token accounting across resume and fork to avoid premature compaction." If compaction started early after `codex resume` or a fork, update first. [#59081](https://github.com/openai/codex/issues/59081) reports the same symptom on the Windows desktop app, on a build without that fix.
- **The display and the trigger measure different things.** Reports such as [#55126](https://github.com/openai/codex/issues/55126) describe compaction while the IDE still showed about half the context free. The trigger counts active context against 90% of the window, so a remaining-context figure based on a different total can look misleading.
- **Images.** Since 0.150.1, remote compaction counts retained images toward its token budget and trims older ones.

## Why is Codex stuck compacting, or compacting in a loop?

**Compaction itself failed or didn't free enough room.** When remote compaction fails, the 0.166.0 build logs `Error running remote compact task`, and a failed turn-end compaction logs `Post-turn compaction failed; preserving the completed turn`. Open reports describe compaction that leaves the next request no smaller, so it runs again and again ([#59534](https://github.com/openai/codex/issues/59534)), and remote compaction errors that stop further replies in the desktop app ([#59475](https://github.com/openai/codex/issues/59475)).

What to do:

1. **Update.** Codex 0.164.0 stopped failed compactions from leaving partial summaries in the history. That doesn't fix every loop (#59534 was still open on 5 November 2026), but it rules out one cause.
2. **Find what's refilling the context.** If it compacts, refills straight away and compacts again, a large tool output or file read is the usual cause. Ask for smaller reads, or narrower command output.
3. **Start a new thread** with a short written summary of where the task stands. A fresh thread costs less than a loop that burns usage on repeated summaries.

## Is Codex compaction local or remote?

**It depends on the model provider.** The configuration reference describes a provider capability, `remote_compaction`: `v2` "sends compaction triggers through Responses", and `unsupported` "uses local compaction." Codex reports both kinds in its `task.compact` metric, split by type `remote` or `local`. Since 0.162.0 you can set the capability for custom Responses-compatible providers, and since 0.147.0 Amazon Bedrock supports remote compaction.

## How is Codex auto compact different from Claude Code's?

| | Codex (0.166.0) | Claude Code |
| --- | --- | --- |
| Default trigger | 90% of the context window (244,800 on a 272K model) | The model's context limit, about 967K tokens on 1M-context models ([details](/blog/claude-code-keeps-compacting)) |
| Change the trigger | `model_auto_compact_token_limit` (earlier only), `model_context_window` (later) | `/autocompact`, or `autoCompactWindow` |
| Turn it off | Not possible | **Auto-compact** in `/config`, or `DISABLE_AUTO_COMPACT` |
| Manual | `/compact` | `/compact` |

If you use both, the Claude Code side is in [Claude Code keeps compacting](/blog/claude-code-keeps-compacting), and its compaction during breaks is covered in [Compacted while idle](/blog/claude-code-compacted-while-idle). For how each tool decides what it may run without asking, see [Codex auto mode](/blog/codex-auto-mode).

## What changed in Codex compaction recently?

**Between 0.147.0 and 0.166.0 (August to October 2026), Codex brought remote compaction to Bedrock and custom providers, made failed compactions clean up after themselves, and fixed early compaction after resume.**

| Version | Date | Change |
| --- | --- | --- |
| 0.147.0 | 7 Aug 2026 | Remote compaction for Amazon Bedrock |
| 0.150.0 | 26 Aug 2026 | Compaction fixed for Amazon Bedrock models |
| 0.150.1 | 27 Aug 2026 | Remote compaction counts retained images toward its token budget |
| 0.155.0 | 17 Sep 2026 | Accepted prompts are saved even when compaction fails before a turn starts |
| 0.162.0 | 8 Oct 2026 | Remote compaction capability configurable for custom Responses-compatible providers |
| 0.164.0 | 23 Oct 2026 | Failed compaction no longer leaves partial summaries in history |
| 0.166.0 | 29 Oct 2026 | Reasoning-token accounting kept across resume and fork, to avoid premature compaction |

The dates are the dates on [Codex's GitHub releases](https://github.com/openai/codex/releases).

**Key takeaways**

- Codex auto compacts at 90% of the model's context window: 244,800 tokens on the 272K models in Codex 0.166.0's bundled catalog, 450,000 on gpt-6.1-terra.
- model_auto_compact_token_limit can only lower that point. To compact later, raise model_context_window, up to the model's maximum (872,000 for most current models).
- There's no setting that turns auto compaction off in Codex 0.166.0. Run /compact yourself at a natural break instead.
- Set model_post_turn_compact_threshold_percent (for example 70) to compact after a finished reply instead of mid-task, where summaries lose the current instruction.
- Keep the task in a file Codex re-reads after compaction; no summary can drop it.
- If compaction starts early after resume, update to 0.166.0; since 0.164.0 a failed compaction no longer leaves a partial summary behind.

## FAQ

## Related

- [Codex auto mode: how to turn on Approve for me, and why it still asks](/blog/codex-auto-mode)
- [Claude Code keeps compacting: four causes and their fixes](/blog/claude-code-keeps-compacting)
- [Claude Code "compacted while idle": what it means and how to turn it off](/blog/claude-code-compacted-while-idle)
- [Why Claude Code uses so many tokens](/blog/why-claude-code-uses-so-many-tokens)
- [Codex Crashpad 'pending' eating your Mac's disk](/blog/codex-crashpad-pending-disk-space)

## FAQ

### When does Codex auto compact?

When the active conversation reaches 90% of the model's context window. In the model catalog bundled with Codex 0.166.0, most models have a 272,000-token window, so auto compaction starts at 244,800 tokens; gpt-6.1-terra has a 500,000-token window, so it starts at 450,000. Codex refreshes that catalog from OpenAI, so the live values can differ. A lower model_auto_compact_token_limit makes it start earlier.

### How do I change the Codex auto compact threshold?

Set model_auto_compact_token_limit in ~/.codex/config.toml to compact earlier. It can only lower the threshold: Codex uses the smaller of your value and 90% of the context window. To compact later, raise model_context_window instead, up to the model's maximum in the catalog (872,000 tokens for most current models in Codex 0.166.0). The 90% rule then applies to the bigger window.

### Can I disable auto compact in Codex?

No. Codex 0.166.0 has no config key or flag that turns automatic compaction off for your sessions; the code can only disable it internally for certain threads the app server starts. The closest you can get is a later threshold, by raising model_context_window, and compacting at moments you choose with /compact.

### How do I compact manually in Codex?

Type /compact in the CLI and confirm when Codex offers to summarize the chat so far. It replaces earlier turns with a concise summary to free context. /recap is different: it summarizes the conversation for you without compacting the context.

### Why does Codex lose my task after compaction?

Because compaction replaces earlier turns with a summary, and a summary written mid-task can drop the current instruction. Several Codex issues report exactly this. Setting model_post_turn_compact_threshold_percent (for example to 70) makes Codex compact after a finished reply instead of in the middle of work, and keeping the task in a file the agent re-reads survives any summary.

### Why does Codex compact so early?

Usually because the threshold is lower than the number you're looking at. Auto compaction starts at 90% of the context window, and the window can be smaller than a model's advertised maximum. Codex 0.166.0 also fixed reasoning tokens being counted wrongly after resume or fork, which caused premature compaction, so update first.

### What does 'Codex ran out of room in the model's context window' mean?

It's Codex's error for a request that no longer fits in the model's context window: 'Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.' Run /compact, or start a new thread and carry the task over in a short written summary.

### Is Codex compaction local or remote?

Both exist. A model provider's remote_compaction capability decides it: v2 sends compaction through the Responses API, and unsupported uses local compaction. Codex counts the two separately in its task.compact telemetry metric, by type remote or local.

## Sources

- [Configuration reference: model_auto_compact_token_limit, model_context_window, compact_prompt (Codex docs)](https://learn.chatgpt.com/docs/config-file/config-reference)
- [Slash commands: /compact and /recap (Codex docs)](https://developers.openai.com/codex/cli/reference)
- [ModelInfo::auto_compact_token_limit, the 90% rule (openai/codex, rust-v0.166.0)](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/protocol/src/openai_models.rs)
- [Context window token status and the hard cap (openai/codex, rust-v0.166.0)](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/core/src/session/context_window.rs)
- [Bundled model catalog, models.json (openai/codex, rust-v0.166.0)](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/models-manager/models.json)
- [Codex releases on GitHub (0.147.0 to 0.166.0)](https://github.com/openai/codex/releases)
- [@openai/codex 0.166.0 (npm)](https://www.npmjs.com/package/@openai/codex)
- [Automatic compaction loses active task (openai/codex #57608)](https://github.com/openai/codex/issues/57608)
- [Model catalog cache, models_cache.json (openai/codex manager.rs, rust-v0.166.0)](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/models-manager/src/manager.rs)
- [AutoCompactionDisabled (openai/codex compact.rs, rust-v0.166.0)](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/core/src/compact.rs)
- [GPT-6.1 Sol auto-compacts at ~200k despite a displayed 475k window, with a models_cache.json snapshot (openai/codex #55008)](https://github.com/openai/codex/issues/55008)
- [Auto-compaction while IDE shows 51% context remaining (openai/codex #55126)](https://github.com/openai/codex/issues/55126)
- [Remote compaction v2 can loop (openai/codex #59534)](https://github.com/openai/codex/issues/59534)
- [Control over auto-compaction parameters (openai/codex #4106)](https://github.com/openai/codex/issues/4106)
