Aident AI

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 compactedrepeats 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 diffand 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:
Record the current branch and commit too:
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:
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:
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:
Then run only the targeted test or validation named in the handoff. Success means:
the expected file or diagnostic changed;
the targeted verification completed;
the agent did not repeat the previous investigation;
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:
Then verify the account and Vault from a trusted shell:
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 | 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
Unrolling the Codex agent loop, OpenAI, January 23, 2026
Compaction guide, OpenAI, accessed July 28, 2026
Compaction leaves a long-running thread about 80% full, OpenAI Codex issue, opened July 23, 2026
Auto-compaction loop rereads files and loses progress, OpenAI Codex issue, opened July 24, 2026
Infinite context-compaction loop consumed usage, OpenAI Developer Community, posted July 20, 2026
Codex is compacting context too often, r/codex, posted July 21, 2026
Six compactions in 30 minutes, r/codex, posted July 26, 2026
How to preserve current state after compaction, r/codex, posted July 26, 2026
Aident setup instructions, accessed July 28, 2026
Refresh this guide when OpenAI changes Codex compaction behavior, thread recovery controls, context telemetry, or the supported configuration surface.


