Codex Keeps Compacting Context? Break the Loop Safely

Codex Keeps Compacting Context? Break the Loop Safely

Aident AI

A cyan looping ribbon breaks through a coral barrier toward an amber checkpoint on a dark field.

Codex Keeps Compacting Context? Break the Loop Safely

If Codex shows Context automatically compacted several times, rereads the same files, repeats the same plan, and makes no diff or test progress, stop the active turn. Preserve the repository state in a short handoff, open a fresh thread in the same project, and resume from one exact next action. Do not keep prompting the affected thread, paste its full transcript into a new one, delete session files, or rely on unsupported settings that claim to disable compaction.

One normal compaction is expected in a long thread. A loop is different: the thread returns nearly full, repeats work, or compacts again after only a few operations.

Confirm the Compaction Loop

Use this guide when at least two of these signals appear together:

  • Context automatically compacted repeats within a short period;

  • the context meter stays near full immediately after compaction;

  • Codex rereads the same files, skill, or memory document;

  • it restates the same conclusion without making the next edit;

  • git diff and test results do not change between cycles;

  • usage continues rising while the task does not advance.

Recent reports described six compactions in 30 minutes, a thread returning about 80% full after compaction, and a loop that repeatedly loaded the same files without completing the planned edit. Those reports are evidence of a current failure pattern, not proof that every long thread is broken.

Prerequisites

Before recovery, make sure you can:

  • open a terminal in the repository;

  • inspect the working tree without changing it;

  • start a fresh Codex thread for the same project;

  • identify the original objective and the next verifiable action.

If the current turn is still running, press the surface's Stop or Cancel control. In the Codex CLI, press Ctrl-C once. Do not kill unrelated shell processes or delete %USERPROFILE%\.codex or ~/.codex.

Step 1: Capture the Repository State

Run these read-only checks from the repository root:

git status --short
git diff --name-status
git diff --stat
git diff --check

Record the current branch and commit too:

git branch --show-current
git rev-parse --short HEAD

Expected result: you know exactly which files changed, whether the diff has whitespace errors, and which commit the recovery starts from. Starting a new thread does not remove repository changes, but the new thread must inspect them before editing.

If the worktree contains changes you do not recognize, stop and review them. Do not reset, clean, or overwrite them to make the handoff simpler.

Step 2: Write a Small Handoff

Create a temporary task note in the repository or use the project's existing plan file. Keep it short enough to read in one pass:

# Objective

State one concrete outcome.

# Verified state

- Completed: facts proven or edits already made
- Files inspected: only the relevant paths
- Files changed: paths from git status
- Tests run: command and result
- Decision: the conclusion that should not be rediscovered

# Next action

Name one exact edit or diagnostic.

# Stop condition

Define the test, diff, or output that proves completion

Do not paste the full chat, raw logs, or entire file contents. OpenAI explains that Codex automatically compacts a growing conversation into a smaller replacement context after a threshold. Reintroducing the whole transcript recreates the pressure you are trying to escape.

Expected result: a developer who never saw the old thread can identify the current state and perform the next action without repeating discovery.

Step 3: Start a Fresh Thread

Open a new thread in the same project and working directory. Do not resume the looping thread for the recovery attempt.

Use a first prompt like this:

Read AGENTS.md and the task handoff. Inspect git status and the current diff.
Do not repeat completed investigation. Start with the exact next action in the
handoff. Run the named verification, then stop and report the changed files and
result

Keep the old thread available until the recovery succeeds, but do not continue feeding it prompts. The repository and handoff are the source of truth now, not the affected conversation's memory.

Expected result: the fresh thread begins at the named next action rather than rereading the same project surface.

Step 4: Verify Progress Before Expanding Scope

After the first recovered action, run the same checks again:

git status --short
git diff --stat
git diff --check

Then run only the targeted test or validation named in the handoff. Success means:

  1. the expected file or diagnostic changed;

  2. the targeted verification completed;

  3. the agent did not repeat the previous investigation;

  4. no new compaction loop appeared during the bounded task.

If no repository state or test result changed, stop again. Refine the next action rather than asking the agent to "keep trying."

Step 5: Keep the Replacement Thread Lean

Compaction exists because conversation history, tool calls, file contents, and command output consume the model's context window. Reduce avoidable history without hiding evidence:

  • ask for narrow file ranges instead of whole repositories;

  • return aggregates before raw logs;

  • cap search and issue results to the rows that answer the question;

  • split an independent task into a separate thread;

  • update the handoff after a milestone, decision, or passing test;

  • start a fresh thread before a new objective rather than carrying an unrelated transcript forward.

For integration-heavy work, reduce MCP and tool-result token usage instead of disconnecting required security or audit tools. If the account meter is the main problem, use the controlled comparisons in How to Diagnose Codex Usage Spikes.

Recover One Missing External Fact With Aident Loadout

If the old thread contained a large web, analytics, or SaaS result, rerun one narrow read-only query instead of copying the raw transcript. Aident Loadout lets the fresh thread discover and inspect a governed Action without putting provider credentials in the prompt.

Install or update it with this exact prompt:

Follow https://aident.ai/SETUP.md

Then verify the account and Vault from a trusted shell:

aident account auth status
aident vault vault --action status
aident capabilities search \
  --queries '["find one read-only Action for the missing external fact"]' \
  --types '["action"]'

Inspect the selected Action's current schema before execution and keep its date window and result limit small. Measure recovery as one missing fact returned, zero provider credentials copied into the thread, and no repeated compaction during the bounded query.

Common Failure Modes

What happens

Why it fails

Safe response

The new thread receives the whole transcript

It rebuilds a large, noisy context

Use the handoff fields and exact next action only

Codex repeats the same file reads

The handoff lacks a verified decision or next edit

Add the decision, evidence path, and stop condition

Someone suggests an undocumented --autocompact=off flag

The flag may not exist or may not affect the active surface

Use supported product controls and a fresh thread

The context meter remains high after one compact

The retained state may still be too large

Stop before another loop and hand off to a fresh thread

The diff contains unknown changes

Recovery could overwrite someone else's work

Pause and review ownership before editing

Usage rose but no file changed

The agent is reprocessing rather than advancing

Stop, record evidence, and reduce the next action

Why This Recovery Works

OpenAI describes compaction as replacing a long conversation with a smaller list of items that carries forward important state. The failure reports show that a problematic compacted thread can preserve the broad goal while losing execution progress, or retain so much state that it reaches the threshold again quickly.

A concise handoff makes the missing execution state explicit: verified facts, current diff, tests, next action, and stop condition. A fresh thread removes the repeated conversation and tool history. The repository remains the durable record, so progress can continue without trusting a loop that has already failed to advance.

This follows the useful pattern seen in Aident's Ollama networking guide: name the exact symptom, run a safe diagnostic, give reproducible recovery steps, show the expected result, and explain why the fix works. It does not assume that every compaction is defective or that a workaround fixes the underlying product bug.

Sources

Refresh this guide when OpenAI changes Codex compaction behavior, thread recovery controls, context telemetry, or the supported configuration surface.

Home

Home

Home

Integrations

Integrations

Integrations

Vault

Vault

Vault

Audit

Audit

Audit

Arana Grande

Arana Grande

Arana Grande

Free

Free

Free

30-day audit summary

30-day audit summary

30-day audit summary

Daily action-call volume and the latest receipts from the Loadout audit trail.

Daily action-call volume and the latest receipts from the Loadout audit trail.

Daily action-call volume and the latest receipts from the Loadout audit trail.

View Audit

View Audit

View Audit

Loadout usage

Loadout usage

Loadout usage

617 action calls in the last 30 days

617 action calls in the last 30 days

617 action calls in the last 30 days

May 19 - Jun 17

May 19 - Jun 17

May 19 - Jun 17

10 active days

10 active days

10 active days

Less

Less

Less

More

More

More

Recent activity

Recent activity

Recent activity

Latest action-call receipts from connected agents

Latest action-call receipts from connected agents

Latest action-call receipts from connected agents

Apr 23, 09:23 AM

Apr 23, 09:23 AM

Apr 23, 09:23 AM

Shopify

Shopify

Shopify

Creates Or Updates An Asset For A Theme

Creates Or Updates An Asset For A Theme

Creates Or Updates An Asset For A Theme

Success

Success

Success

Apr 23, 09:21 AM

Apr 23, 09:21 AM

Apr 23, 09:21 AM

Shopify

Shopify

Shopify

Update Products Param Product Id

Update Products Param Product Id

Update Products Param Product Id

Success

Success

Success

Apr 23, 08:53 AM

Apr 23, 08:53 AM

Apr 23, 08:53 AM

Shopify

Shopify

Shopify

Update Products Param Product Id

Update Products Param Product Id

Update Products Param Product Id

Failed

Failed

Failed

Apr 22, 22:13 PM

Apr 22, 22:13 PM

Apr 22, 22:13 PM

Shopify

Shopify

Shopify

Create Product Image

Create Product Image

Create Product Image

Success

Success

Success

Apr 22, 22:12 PM

Apr 22, 22:12 PM

Apr 22, 22:12 PM

Shopify

Shopify

Shopify

Create Product Image

Create Product Image

Create Product Image

Success

Success

Success

Connected integration coverage

Connected integration coverage

Connected integration coverage

162

162

162

of 753 accessible connected

of 753 accessible connected

of 753 accessible connected

Callable actions

Callable actions

Callable actions

1,126

1,126

1,126

Vault credentials

Vault credentials

Vault credentials

8

8

8

Explore what's possible

Explore what's possible

Explore what's possible

See all Integrations

See all Integrations

See all Integrations

Google Ads

Google Ads

Google Ads

All available Goolge Ads tools via...

All available Goolge Ads tools via...

All available Goolge Ads tools via...

X (twitter)

X (twitter)

X (twitter)

All available X tools via...

All available X tools via...

All available X tools via...

Github

Github

Github

All available Github tools via...

All available Github tools via...

All available Github tools via...

Notion

Notion

Notion

All available Notion tools via...

All available Notion tools via...

All available Notion tools via...

Slack

Slack

Slack

All available Slack tools via...

All available Slack tools via...

All available Slack tools via...

Firecrawl

Firecrawl

Firecrawl

All available Firecrawl tools via...

All available Firecrawl tools via...

All available Firecrawl tools via...

753 integrations are available for loadouts.

753 integrations are available for loadouts.

753 integrations are available for loadouts.

Plug your entire stack into your AI agents.

Plug your entire stack into your AI agents.

Plug your entire stack into your AI agents.

Skip the integration headache. Plug 750+ tools into Claude Code, Codex, and OpenClaw in one go, and let your agents execute today.

Skip the integration headache. Plug 750+ tools into Claude Code, Codex, and OpenClaw in one go, and let your agents execute today.