codex icon indicating copy to clipboard operation
codex copied to clipboard

CLI crashes with “server_error: The server had an error while processing your request” when context window is nearly exhausted

Open alokwhitewolf opened this issue 1 year ago • 2 comments

When using the @openai/codex CLI interactively with a very large conversation history, once the API context window is nearly full (e.g. “1% context left”), the CLI throws an internal server error and exits rather than gracefully warning or trimming the context.

Reproduction Steps

  1. Install the latest CLI:
    npm install -g @openai/codex
    
  2. Start an interactive session:
    codex
    
  3. Enter or paste a very long prompt (or build up many turns) until you see:
    — 1% context left
    
  4. Continue chatting or issue another prompt.
  5. Observe the CLI crash with a stack trace similar to:
    file:///…/codex/dist/cli.js:443
    …pg.NEWLINE_REGEXP=/\r\n|[\n\r]/g;function Fwe(e,t){…}
    …
    [Error]: The server had an error while processing your request. Sorry about that!
        at e.a [as iterator] (…/cli.js:443:1514)
        at process.processTicksAndRejections (node:internal/…)
        at async I0.run (…/cli.js:460:2356) {
      status: undefined,
      headers: undefined,
      request_id: undefined,
      error: {
        type: 'server_error',
        message: 'The server had an error while processing your request. Sorry about that!',
      }
    }
    

Expected Behavior

  • Detect that the context window is exhausted (or about to be) and either:
    1. Prompt the user to trim or clear history (e.g. suggest /clear),
    2. Automatically drop the oldest tokens before continuing, or
    3. Smart auto-compact mechanism
    4. At minimum, emit a clear warning instead of an unhandled server error.

Actual Behavior

Once the API rejects due to context overflow, the CLI surfaces a raw server error stack trace and exits.

Environment

  • macOS 13.5
  • Node.js v20.18.3 (via nvm)
  • No custom configuration

alokwhitewolf avatar Apr 16 '25 18:04 alokwhitewolf

Ty for the detailed issue report and suggested fixes! I think a good start would be to implement

  1. Prompt the user to trim or clear history (e.g. suggest /clear) and
  2. At minimum, emit a clear warning instead of an unhandled server error

from your suggestions.

Would you like to put up a PR?

tibo-openai avatar Apr 16 '25 19:04 tibo-openai

Thank you for the feedback! Please try again on the latest version of Codex CLI. If the issue persists, let us know.

codex-maintainers avatar Aug 07 '25 17:08 codex-maintainers