Make context window and auto-compaction settings model-aware

Open 💬 6 comments Opened Mar 28, 2026 by GraviyDev
💡 Likely answer: A maintainer (github-actions[bot], contributor) responded on this thread — see the highlighted reply below.

What feature would you like to see?

Model-aware context window and auto-compaction settings.

Problem

Codex currently allows global configuration like:

model_context_window = 1000000
model_auto_compact_token_limit = 500000

This works for a large-context model such as gpt-5.4, but becomes misleading or unsafe when switching to a smaller-context model such as gpt-5.4-mini.

The user can end up in a state where:

  • the UI still appears to reflect the large configured context window
  • the selected model actually has a smaller context limit
  • auto-compaction does not happen soon enough
  • the session fails with a context window exceeded error

Proposal

Please make context handling explicitly model-aware.

Possible approaches:

  • support per-model overrides in config
  • automatically clamp model_context_window to the selected model's true maximum
  • automatically clamp model_auto_compact_token_limit to a safe threshold under the selected model's limit
  • show both:
  • configured context window
  • effective context window for the currently selected model
  • show a warning when the configured values exceed the selected model's actual supported window

Why this matters

This is especially important for users who intentionally use:

  • gpt-5.4 with a very large configured context window
  • gpt-5.4-mini for cheaper or faster work in the same app

Right now the config model and the active UI-selected model can drift apart in a way that is hard to reason about.

Expected outcome

Switching models should never leave the user with stale or unsafe context settings.

The app should either:

  • transparently adapt the effective limits, or
  • explicitly tell the user that the current context settings are incompatible with the newly selected model.

View original on GitHub ↗

6 Comments

github-actions[bot] contributor · 5 months ago

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #16139

Powered by Codex Action

GraviyDev · 5 months ago

Related bug report: #16139

mahdikayvan · 3 months ago

@GraviyDev Hey — I know this kind of issue too well. It is very close to the agent reliability problem I’m working on.

I’m building AgaveCore, and I’d like to let a few early people use it for free while I shape it into something stronger.

It helps when a coding agent starts to drift, loses important context, or gives you an output you do not fully trust.

In practice, it works through two habits:

  • tell the agent to remember-progress or remember-this when something important should not be lost
  • use consult-brain when the current task needs more grounded feedback on what to do next

Over time, as useful journals, decisions, and failures pile up, the product should help the agent make sharper decisions and take better actions with less drift.

If this sounds relevant, reply here and I’ll send the exact steps. You can also use the email or Telegram on my profile.

Cheers,
Mads.

chigkim · 3 months ago

I'm using local model, and I can only afford 64k context window.
I set model_context_window inside profile like this:

[profiles.test]
model_provider = "llamacpp"
model = "Qwen3.6-27B"
model_context_window = 64000

Then if I run /status, I get this:

│ Context window: 86% left (45.9K used / 258K) │

How can I set the context window for the profile properly?
Thanks!

chigkim · 3 months ago

Nevermind, I asked Codex to look codex repo, and it said model_context_window is top level config.
It worked if I set it on the top!

kinzirva · 1 month ago

Additional deterministic evidence from macOS / Codex CLI 0.145.0 with gpt-5.6-sol.

Environment

  • Codex CLI: 0.145.0
  • Surface: local CLI / TUI
  • Model: gpt-5.6-sol
  • Reasoning effort: high
  • Platform: macOS
  • A custom model_catalog_json entry declared:
  • context_window = 1048576
  • max_context_window = 1048576
  • effective_context_window_percent = 95
  • Codex therefore reported an effective window of 996147.
  • The built-in model cache for the same model declared 272000.

Observed behavior

Four separate tool-heavy threads failed with context_window_exceeded after the last successful request reached approximately:

  • 364559 input tokens
  • 369007 input tokens
  • 369869 input tokens
  • 371440 input tokens

The next request failed with:

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

The affected rollouts contained no compacted or context_compacted event before failure. The client was waiting for the locally advertised ~996K threshold while the request path rejected the conversation much earlier.

This made the thread unrecoverable: even a short follow-up asking for a summary failed with the same error.

Workaround and compact smoke test

A safe local workaround was:

model_auto_compact_token_limit = 220000
model_auto_compact_token_limit_scope = "total"

I also ran a deterministic compact smoke test using the same model and CLI, temporarily overriding the threshold to 32000:

  1. First turn reached 37128 input tokens.
  2. The same thread was resumed with a short second prompt.
  3. The persisted rollout recorded both:
  • compacted
  • context_compacted
  1. The resumed turn completed successfully.

This confirms the compact machinery itself works when the threshold is below the real request boundary. The failure is the unsafe split between configured/catalog context metadata and the actual accepted context.

Suggested fix

  1. Treat the server/provider maximum as authoritative and clamp any custom catalog context_window to it.
  2. Compute the effective auto-compact limit as the minimum of:
  • configured compact limit,
  • server-advertised effective context,
  • a safe preflight limit that reserves output/reasoning headroom.
  1. Before sending a request, estimate the normalized prompt and trigger compaction when it cannot fit the active provider limit, even if local metadata says otherwise.
  2. On context_window_exceeded, perform one emergency compact-and-retry before making the thread unrecoverable.
  3. Show configured window, server-effective window, and active compact threshold separately in /status and diagnostics.
  4. Emit a warning when model_catalog_json advertises a window larger than the selected provider accepts.

This appears to be a concrete GPT-5.6 instance of the model-aware context handling requested in this issue.