Skip to content

Support asynchronous/background compaction in Codex CLI #18007

@sebthom

Description

@sebthom

What variant of Codex are you using?

cli

What feature would you like to see?

Problem

In long Codex CLI sessions, compaction can interrupt the flow once the remaining context gets low. This is especially noticeable in fast-moving chats where the user wants to keep working instead of pausing for context management.

Proposal

Support asynchronous/background compaction that starts before the context window is nearly exhausted.

For example, when remaining context drops below a configurable threshold such as 15%, Codex CLI could:

  • start compacting the current conversation state in the background
  • allow the chat to continue while compaction is running
  • switch to the compacted history once it is ready

Expected behavior

  1. Session is approaching context pressure.
  2. Codex CLI detects that remaining context is below a threshold.
  3. It starts compaction asynchronously on a snapshot of the current history.
  4. New turns continue to be added while compaction runs.
  5. Once compaction finishes, Codex CLI merges or replays the interim turns and atomically switches to the compacted state.

Why this helps

  • avoids workflow interruptions in long sessions
  • makes context management feel seamless
  • improves UX for iterative or agentic workflows
  • reduces the need to stop and wait at the worst possible moment

Implementation sketch

A possible approach:

  • trigger compaction when remaining context falls below a threshold, for example 15%
  • compact a snapshot of the conversation state
  • keep a delta log of turns added while compaction is in progress
  • once compaction completes, replay or merge the delta onto the compacted result
  • atomically replace the active history with the updated compacted history

Open questions

  • Should the threshold be configurable?
  • Should this be opt-in, opt-out, or default behavior?
  • How should the CLI surface compaction status?
  • What is the fallback if context runs out before background compaction finishes?
  • How should merge/replay conflicts be handled for deterministic behavior?

Potential acceptance criteria

  • background compaction can start automatically before context exhaustion
  • user can continue chatting while compaction is running
  • interim turns are preserved correctly
  • switch to compacted history happens without losing conversation state
  • behavior is deterministic and debuggable

Example

If only 15% of context remains, Codex CLI starts compaction in the background instead of waiting until the session is nearly blocked. The user continues chatting normally. When compaction is ready, the CLI transparently switches to the compacted history.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    contextIssues related to context management (including compaction)enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions