codex icon indicating copy to clipboard operation
codex copied to clipboard

Control over auto-compaction parameters

Open Helg1us opened this issue 1 year ago • 31 comments

Codex CLI 0.40.0 — Auto‑compaction controls

Auto-compaction controls proposal

As a Pro subscriber who depends on long, code-heavy sessions, the hardcoded ~220k auto-compaction threshold is disruptive and should be fully optional (including a hard OFF). It collapses earlier context/code into summaries mid-task and degrades model performance.

Problem

Auto-compaction is an uncontrolled process: once ~220k is reached, the CLI compacts without consent, causing loss of working context and producing worse results than the model could otherwise deliver. This feels chaotic and effectively shortens the usable window.

Minimum (must-have)

  • Add a hard OFF switch that restores previous behavior: no automatic compaction at any token count (only on explicit user action).

Maximum (nice-to-have)

Modes and basic controls

  • off — never compact automatically.
  • on — compact automatically near the limit.
  • manual — prompt/compact only when invoked; allow a threshold to trigger a prompt.
  • smart-auto — adaptive compaction that delays until the end of the current step and preserves pinned blocks; threshold configurable.

Interface examples

CLI:

--autocompact=off|on|manual|auto --autocompact-threshold-tokens=240000

TOML:

[autocompact]
mode = "off" # off|on|manual|auto
threshold_tokens = 240000 # optional per-model override

Expected behavior when OFF

Warn near limits, but never compact unless the user explicitly requests it.

Repro (short)

Codex CLI 0.40.0; grow session to ~220k tokens → auto-compaction fires mid-task → context/code loss and quality drop.

Helg1us avatar Sep 23 '25 20:09 Helg1us

This is an essential action. Hardcoding must be removed so that users can use it in the form they desire.

ginishuh avatar Sep 23 '25 21:09 ginishuh

idk. needs to work more with it to be sure. might consider downgrading version if i notice an issue. i dont see them allowing for more token constantly or else it'll be more expensive for them to run.

Aze1338 avatar Sep 23 '25 22:09 Aze1338

I've already rolled back to 0.39.

ginishuh avatar Sep 24 '25 00:09 ginishuh

I didn't roll back. Looking at the related code change, It says the context window is 272k but iirc, in the docs, Pro users has 192k window? So I was thinking 220k made no sense. Here I think auto-compact at 220k (~81%) is reasonable. I think context-rot happens at that time and more tokens in context makes the model behave worse.

So idk what will happen for a Plus user with 128k context? they don't get this feature? that's not fair.

But I'm with you on this issue, not only for the behavior but also for the freedom and equity.

PabloLION avatar Sep 24 '25 00:09 PabloLION

gpt5-high doesn't auto compact. use that

AhnDroiid avatar Sep 24 '25 10:09 AhnDroiid

Is this auto compaction intelligent?

For example, I could tell I was reaching the context window (showed I had 20% left), so I asked the agent to make a detailed MD file for the next agent about everything we've done and everything we need to do moving forward. Once that document was completed, I mentioned the context was low, I don't know if it was just coincidence, it automatically compacted the conversation and handed that document off as the first function.

tekn0x avatar Sep 25 '25 01:09 tekn0x

Now after a test I think it's better to have it ON. my context were full on 0.39 and when it's full I get this error

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 1/5 in 215ms…

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 2/5 in 437ms…

• Analyzing nested conditional directives

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 3/5 in 857ms…

• Simplifying nested conditional macros

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 4/5 in 1.441s…

• Tracing conditional compilation blocks

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 5/5 in 3.031s…

• Adjusting debugLiveActivityPreviewPhase assignment

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 1/5 in 193ms…

• Considering debug code removal

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 4/5 in 1.505s…

• Considering debugLiveActivityPreviewPhase assignment

⚠️ stream error: stream disconnected before completion: stream closed before response.completed; retrying 5/5 in 3.354s…

PabloLION avatar Sep 25 '25 14:09 PabloLION

It looks like the auto-compact feature is triggering an infinite loop: the agent keeps running web-search, then auto-compacts, and then re-searches the same information it seems to have forgotten after the compact step. This loop continues until it’s manually cancelled, effectively draining the quota if left unattended.

There also appears to be a problem with retaining recent context after a compact. For example, it sometimes re-attempts tasks that were already completed earlier in the same session.

balboah avatar Sep 30 '25 10:09 balboah

I will submit a PR for this. stayed tuned

JakkuSakura avatar Oct 01 '25 13:10 JakkuSakura

If autocompact is not working properly and damaging the context, then compaction should be fixed. Prompt improved or only compact initial part and leave last part of context untouched. I have no idea on who is going to survive with a full context that the model won't accept, however. Only sane option I see is to trigger a request to compact with manually crafted prompt. But OFF doesn't look like a reasonable option. What are you going to do with OFF? Close the session? Possible improvements I see is to warn the user when getting close to autocompact limit, and another is to ask the user if they want to enter a custom compact prompt for autocompact.

waldoalvarez00 avatar Oct 01 '25 17:10 waldoalvarez00

When I encounter the issue, I have to enter /compact which is annoying

JakkuSakura avatar Oct 01 '25 18:10 JakkuSakura

When I encounter the issue, I have to enter /compact which is annoying

The only 2 choices when running out of context are:

1 - Compact 2 - Restart a new session

The context is limited at least with current LLMs architecture. And likely in the near future models that could have 1/2 M tokens context will have it limited as well. You just will need to compact less in the future.

Options I see useful:

Autocompact threshold configuration Configure part of the context to compact (if not 100%), maybe even prompt for this could be useful Compact prompt configuration/change Prompt the user to enter compact prompt manually when threshold reached Postpone compaction a little bit (maybe possible if there is more context beyond threshold, but there will be a point where it won't be possible to postpone)

Again: OFF <----- What is the use case for this? (Pro doesn't means unlimited context)

waldoalvarez00 avatar Oct 04 '25 08:10 waldoalvarez00

I never see auth compact triggered, it always fall in to exceed max context window after a long run and can't recover even use the /compact command.

is there a config key to enable auto compact?

sailxjx avatar Oct 13 '25 03:10 sailxjx

Is this a hallucination? I don't think there is an auto compact setting?

https://chatgpt.com/share/68f22c80-a444-8012-b6b1-239824b67d2b

atomical avatar Oct 17 '25 11:10 atomical

there is auto-compact, but the limit for it is misconfigured

_ if slug.starts_with("gpt-5-codex") => Some(ModelInfo {
            context_window: 272_000,
            max_output_tokens: 128_000,
            auto_compact_token_limit: Some(350_000),
        }),

You see it's set to 350000 but would never trigger. and even if it triggers, there are no space for the prompts to do the auto compacting and always fail

JakkuSakura avatar Oct 17 '25 11:10 JakkuSakura

Will you have plans to do this in near future? Recently I ran into a case where there was still over 20%+ context left, and the coding task was almost finished (all items in the Plan had been checked by Codex), but right at the end It got “Context compacted.” This feels like a waste of both time and tokens.

SunRunAway avatar Jan 19 '26 04:01 SunRunAway

i want this too

ronny-rentner avatar Mar 14 '26 04:03 ronny-rentner

Hello,

In our usage, context compaction does not merely “degrade” the session; it often makes it unusable.

After compaction, the agent loses essential parts of the conversation: instructions, business context, decisions already made, technical constraints, MCP, skills, and sometimes even the exact objective of the task. From that point on, it no longer follows the request properly, produces incorrect, unstable, or even broken code, and the generated changes often have to be discarded entirely.

In practice, whenever compaction occurs, we prefer to stop the session, revert the changes, and start over with the task broken down differently. This costs a great deal of time, but it is still preferable to continuing with a conversation that has become unreliable.

Two options seem essential to us:

the ability to completely disable automatic compaction;

compaction events/hooks that allow the necessary context to be explicitly re-injected.

As things stand, the issue is not a simple drop in quality: compaction makes the results unusable.

Have a good day

AhNosNimes avatar Mar 19 '26 12:03 AhNosNimes

I also need the option to disable compaction. This is the most urgent feature missing in codex.

andgate avatar Apr 08 '26 10:04 andgate

Adding support from the perspective of a long-time Claude Code user who has moved to Codex. The critical part for me is not only an OFF switch, but configurable proactive thresholds per model/project, e.g. compact at 70-85% or prompt before compacting, rather than waiting until the window is effectively exhausted.

When compaction fires too late or mid-flow, the session is more likely to drift or lose task state. A config surface like mode + threshold + prompt/manual would make long sessions safer and more predictable.

szybnev avatar May 04 '26 09:05 szybnev