Tutorial 9 min read | November 20, 2026

Codex CLI compaction lost your context? What survives /compact and resume, and how to get the task back

After Codex compacts a long chat it can forget the task, redo finished work or ask what to do next. Here is what compaction and codex resume actually keep, one feature flag worth checking, a free file-and-hook fix, and where a local task store helps.

Keep your Codex task list outside the compact summary

If Codex just compacted and lost the thread, leave your email. We'll send the setup tutorial, and you can tell us you use Codex, before any price is shown.

Step 1 of 4

This needs to be a real email — we'll email you in a few minutes.

No spam. Unsubscribe anytime.

You are deep in a long Codex CLI session. A line says Context compacted, and the next reply is something like "Understood. I'll follow these repository guidelines. What would you like me to work on?" Or worse, Codex carries on confidently with a different plan, rewrites code it already finished, or edits files you told it to leave alone.

You are not imagining it. Several open issues on the openai/codex GitHub repo describe exactly this. This guide explains what compaction keeps, what codex resume does and does not bring back, one feature flag to check first, and how to keep the task somewhere compaction cannot reach.

What /compact does in Codex

Codex's official command reference describes /compact plainly: after a long exchange, Codex "replaces earlier turns with a concise summary, freeing context while keeping critical details." The same thing happens automatically when the chat crosses the auto-compaction threshold, and that can happen in the middle of a turn, so it is easy to miss.

A summary is not a transcript. What the summary leaves out is gone from what the model sees next. User reports point to a consistent pattern:

  • Assistant-side confirmations ("bug X is fixed", "step 3 done") are easy to lose, so Codex may redo finished work. A long-running GitHub discussion describes this.
  • In bad cases the new context holds the standing instructions (AGENTS.md and system/developer context) but not the actual task, so Codex asks what you want.
  • Repeated compactions on one long task compound the problem: each summary is a summary of a summary.

Treat these as reports from real sessions, not guaranteed behavior. Compaction quality varies by version, model and task. What is reliable is the conclusion: anything you care about should not exist only in the chat.

First check: is token_budget turned on?

If /compact finishes almost instantly and Codex then knows nothing about your task, check this before anything else. In openai/codex issue #31822, a user found that on 0.143 and 0.144, with the under-development token_budget feature enabled, both manual and automatic compaction started a fresh context window without a summary. The prior task, progress and tool output were all gone. The change that introduced this (PR #29743) says it makes compaction "behave like new_context" when that feature is on.

codex features list
codex features disable token_budget

codex features list shows each flag, its stage and whether it is on. codex features disable writes the change to your config.toml. In the current source (rust-v0.171.0, Nov 2026), token_budget is still marked under development and off by default. Most people will find it off, so if yours is off, the rest of this guide is the fix. The issue was still open when we wrote this.

What codex resume does and does not bring back

codex resume opens a picker of saved chats. codex resume --last skips the picker and reopens the most recent chat from the current directory, and --all includes chats from other directories. You can also pass a session ID or name. It is the right tool after a crash, a closed terminal or a reboot.

It is not an undo for compaction. Resume reopens the chat as it was saved. If that chat already compacted, you get the compacted chat back, not the turns from before the summary. In the rollout one user inspected for issue #31822, the saved replacement history after compaction held only fresh developer and AGENTS.md instructions.

Related commands worth knowing (all from the official command reference):

CommandWhat it doesUse it when
/export session.mdSaves the complete conversation as MarkdownBefore a manual /compact on a chat you care about
/recapSummarizes the chat without freeing contextYou want to see what Codex thinks is going on
/compactReplaces earlier turns with a summaryYou need room and have saved the plan elsewhere
codex resume --lastReopens the most recent saved chat hereCrash, closed terminal, next morning
codex fork --lastCopies a saved chat into a new chatYou want to branch without changing the original
/planPlan mode; can then "clear context and implement" with the full planStarting a big task with a clean window
/goalKeeps a persistent goal attached to the chatOne clear objective for a long run

Get the task back in the next five minutes

  1. Stop the current turn before Codex rewrites anything else. Run git status and git diff (or /diff) so you can see what is already done.
  2. Write the missing state down while you still remember it. Keep a short NOTES.md at the repo root: the goal, what is done (with file names), what is still open, decisions you made and what you ruled out, and the single next step.
  3. Tell Codex: "Read NOTES.md and the current git diff. The changes in the worktree are already done. Continue with the next step in NOTES.md only."
  4. If the chat has compacted several times or mixes tasks, start a fresh chat instead of compacting again, and give it the same instruction. A fresh chat plus a good file is often cheaper than another summary.

Stop it happening again: AGENTS.md plus a compact hook

Codex reads AGENTS.md for standing instructions, and reports show it comes back after compaction while the conversation does not. Put one short rule there:

## Long tasks
- Keep NOTES.md current: goal, done, open, decisions, next step.
- Update NOTES.md before any compaction-sized milestone.
- At the start of a session and after any compaction, read NOTES.md
  and the git diff before continuing. Do not redo finished items.

Then make it automatic with a hook. Codex hooks are on by default. A SessionStart hook's matcher is applied to the start source, which can be startup, resume, clear, compact or fork, and plain text the hook prints to stdout is added as developer context. Put this in .codex/hooks.json in a trusted project, or in ~/.codex/hooks.json for all projects:

{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "^compact$",
        "hooks": [
          {
            "type": "command",
            "command": "cat \"$(git rev-parse --show-toplevel)/NOTES.md\" 2>/dev/null || true"
          }
        ]
      }
    ]
  }
}

Notes from the official hooks guide: non-managed hooks must be reviewed and trusted before they run; resolving the path from the git root keeps the hook working when Codex starts in a subdirectory; output above about 2,500 tokens is saved to disk and the model sees a shorter preview, so keep NOTES.md short. You can widen the matcher to ^(startup|clear|compact)$ if you want the same brief in every fresh window. There are also PreCompact and PostCompact events (matcher manual or auto). Plain text from PreCompact is ignored, so use SessionStart on compact to put context back.

Two config options also exist. compact_prompt (or experimental_compact_prompt_file) in config.toml overrides the compaction prompt, so you can tell the summary to keep the task, finished steps and next action. It replaces the default prompt, so test it on a throwaway chat first. model_auto_compact_token_limit sets the token threshold that triggers automatic compaction. Changing it moves the point where compaction happens. It does not make the summary any better.

How does this compare with Claude Code and Cursor? See which AI agent task lists survive compaction, with a 2-minute test you can run on any agent.

Where a file runs out

NOTES.md plus a hook is free, and for many people it is enough. Its limits:

  • Nobody enforces the update. If Codex forgets to update NOTES.md before a compaction, the hook faithfully re-reads stale notes.
  • A flat file has no task status or dependencies. "Which item is in progress, and what does it block?" is whatever the last edit says.
  • The file grows. Past the hook preview limit, Codex sees a truncated version.
  • If you also use Claude Code or Cursor on the same repo, each tool needs its own wiring to the same file. How to add memory to Claude Code, Cursor and Codex covers sharing one plan across all three, and the Claude Code version of this problem is in Claude Code /compact dropped your plan. What Codex carries between separate chats, including Codex memories, is in Does OpenAI Codex remember across sessions?

Where Todo-MCP fits, honestly

Todo-MCP is a local MCP server that Codex runs over stdio. Tasks, their status and dependencies, and named context keys live in a SQLite file on your machine. It is a paid download, and the data stays yours. To see the options, leave your email in the form on this page first. It sends the setup tutorial and then shows plans.

After a compaction, a resume or a fresh chat, the usual first call is context_resume. It returns the current task, stored context keys and a suggested next step, so Codex rebuilds from the store instead of from a summary. Codex calls it through MCP like any other tool. The Todo-MCP setup guide for Codex CLI shows the [mcp_servers.todo-mcp] entry in ~/.codex/config.toml.

What it will not do:

  • It does not hook into Codex's compaction by itself. Add the AGENTS.md line "after any compaction, call context_resume before continuing", or a compact hook that prints that instruction.
  • It cannot recover anything that was never saved. Only tasks and context written to the store come back.
  • It does not replace AGENTS.md for standing rules, or /export if you want a full transcript.
  • It is a different tool from codex resume. Resume reopens a chat. Todo-MCP keeps the task list outside any chat, so a new chat, or a different MCP client, can read the same state.

Whichever route you choose, the rule is the same: the context window will compact again, so the task list should already live somewhere else.

Keep your Codex task list outside the compact summary

If Codex just compacted and lost the thread, leave your email. We'll send the setup tutorial, and you can tell us you use Codex, before any price is shown.

Step 1 of 4

This needs to be a real email — we'll email you in a few minutes.

No spam. Unsubscribe anytime.

Frequently asked questions

Why did Codex forget my task right after "Context compacted"?
Compaction replaces earlier turns with a summary so the chat can keep going. Anything the summary leaves out is gone from what the model sees next. Users have reported cases on GitHub where the post-compaction context held little more than fresh instructions and AGENTS.md, so Codex asked what to work on. In one reported case (openai/codex issue #31822, versions 0.143 and 0.144) the cause was the under-development token_budget feature, which starts a fresh context window without a summary.
Does codex resume bring back what compaction dropped?
No. codex resume (or codex resume --last) reopens a saved chat so you can keep working in it. If that chat already compacted, you are resuming the compacted chat, not the turns from before the summary. To keep a full record, run /export session.md before you compact, and keep the plan in a file in the repo.
How do I check whether token_budget is turned on?
Run codex features list. It shows each feature flag, its maturity stage and whether it is enabled. token_budget is listed as under development and off by default. If you turned it on and compaction now drops your task, codex features disable token_budget turns it off persistently in config.toml.
How do I make Codex re-read my plan after compaction?
Codex supports a SessionStart hook whose matcher can target the compact source. Plain text the hook prints to stdout is added to the context as developer context. Put a hook in .codex/hooks.json (trusted projects) or ~/.codex/hooks.json that prints your NOTES.md, and add a line to AGENTS.md telling Codex to re-read the plan before continuing. Hooks must be reviewed and trusted before they run.
Does Todo-MCP run automatically after Codex compacts?
No. Todo-MCP is a local MCP server. Tasks and context keys sit in a SQLite file on your machine, and Codex reads them only when it calls the tools, usually starting with context_resume. An AGENTS.md line or a compact hook can tell Codex to call it after compaction, but the agent still has to make the call.

Continue Reading

Back to all articles