Extend compaction prompt to reduce loss over multiple compactions

Open 💬 9 comments Opened Mar 11, 2026 by sigalovskinick
💡 Likely answer: A maintainer (github-actions[bot], contributor) responded on this thread — see the highlighted reply below.

Problem

When auto-compact fires, the default prompt (6 lines in codex-rs/core/templates/compact/prompt.md) focuses only on recent work. After 2-3 compactions in the same session, all reasoning, decisions, and context from earlier compactions is completely lost. The model does not even know there WAS a previous compaction.

This forces users to either:

  • Start new sessions after every major task (losing warm context)
  • Accept progressive amnesia within long sessions

Current default prompt

You are performing a CONTEXT CHECKPOINT COMPACTION. Create a handoff summary
for another LLM that will resume the task.

Include:
- Current progress and key decisions made
- Important context, constraints, or user preferences
- What remains to be done (clear next steps)
- Any critical data, examples, or references needed to continue

Be concise, structured, and focused on helping the next LLM seamlessly
continue the work.

This says nothing about preserving history from PREVIOUS compactions.

What we did (workaround)

Used experimental_compact_prompt_file to override with a full prompt that includes:

If this conversation contains a summary from a previous compaction, extract key decisions, outcomes, and direction into a cumulative Historical Context section at the top of your new summary. Each compaction adds 3-5 sentences. Never discard entries from previous compactions.

Result: After 4+ compactions, the full road of the session is preserved.

Proposed fix

Add ONE paragraph to the default compact prompt:

If this conversation already contains a compacted summary from a previous compaction, extract its key historical thread (what was done, key decisions and WHY, outcomes, direction) and include it as a cumulative Historical Context section at the top of your new summary. Each compaction adds 3-5 sentences. Never discard entries from previous compactions.

Cost: ~50 tokens in prompt, ~20-30 tokens per compaction entry in summary.
Benefit: Sessions survive 6-8 compactions without losing the thread.

Session lifecycle guidance (missing from docs)

| Compactions | Quality | What happens |
|-------------|---------|-------------|
| 0-2 | Excellent | Almost no context loss |
| 3-5 | Good | Details fade, decisions preserved (with fix) |
| 6-8 | Acceptable | High-level road preserved, details gone |
| 9+ | Degraded | Even cumulative summaries get compressed |

Recommendation: Switch sessions when the task is DONE. One session = one task/feature.

Why this matters

Real usage is multi-hour, multi-task sessions. The "session as workspace" pattern needs cumulative memory, not just recent-work snapshots. A trivial addition to the default prompt would fix this for everyone.

View original on GitHub ↗

9 Comments

github-actions[bot] contributor · 4 months ago

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #13932
  • #13279
  • #14120

Powered by Codex Action

etraut-openai contributor · 4 months ago

Thanks for the post. This is interesting.

Can you say a little about how you verified the improvement and how much testing you've done with your extended compaction prompt? Just trying to get a sense for whether it was based on vibes on a couple of runs or if it was a more empirical test over many test runs.

sigalovskinick · 4 months ago

Fair question. This is production experience, not a formal benchmark.

Claude Code (primary tool): Tested extensively over daily 10-16 hour sessions building a multi-agent system (11 agents, 100K+ tokens). The cumulative historical context works reliably — after expanding the compact info post-compaction, the full road is visible: all decisions, reasoning, what was rejected and why.

Concrete example: building an email system over a ~16 hour session with multiple compactions. The session preserved everything end-to-end — SMTP configuration choices, why we used a subdomain instead of root domain (root was taken by hosting provider), why we chose an outbox pattern over Redis keys for reliability, Resend API setup details, DNS verification steps. All of this survived 4+ compactions and was available to the model for subsequent decisions. It genuinely improves work quality because the model doesn't re-litigate decisions that were already made with good reasoning.

Codex: Also implemented (via experimental_compact_prompt_file) but less battle-tested since it's not the primary tool. Works in the sessions we've run but smaller sample size.

The verification method is practical: after each compaction, check if the model knows the full journey — not just what happened recently, but WHY specific approaches were chosen earlier. Without the fix, that reasoning is gone after 2 compactions. With it, it survives.

Not a controlled A/B test with metrics — just consistent daily use showing clear before/after difference in post-compaction coherence.

sigalovskinick · 4 months ago

Full writeup with implementation details:

https://gist.github.com/sigalovskinick/e2e329bb37ecc74b9f15d5ba74ee1ee5

Covers the complete solution: extended compact prompt (10 rules), PreCompact hook (stdout injection), PostCompact hook (agent state preservation), and a brain dump skill for manual pre-compact preparation. Includes code for both Claude Code and OpenAI Codex.

Production-tested over 12+ compactions in a single session with full context retention.

etraut-openai contributor · 4 months ago

Compactions for OpenAI-hosted and Azure-hosted models now take place on the server, not locally. The prompt that you're talking about here is used only for custom model providers. The compaction prompt that you see in this OSS repo is not the one that's actually used on the server for most users.

sigalovskinick · 4 months ago

Thanks for the context — I don't have access to the server side, so I can't know how compaction works there internally.

What I can say: I'm using a standard Codex subscription (not a custom model provider), and I'm using experimental_compact_prompt_file from your own config options to override the compact prompt locally. I don't know whether this overrides server-side compaction or works alongside it — but it works.

Just ran a controlled test: 5 sequential compactions, each segment containing a unique marker (codename, decision, finding). After the 5th compaction, the model retained all 5 markers, all decisions with reasoning, all rejected approaches, the open P1 finding, and the full chronological timeline.

Interesting difference between the two agents: in Claude Code, I can see the compaction output explicitly — it produces structured ## Historical Context (from previous compactions) with [Compaction 1]: ..., [Compaction 2]: ... entries, followed by a full summary of the latest segment.

In Codex, I couldn't find a way to inspect the raw compaction summary. When asked, the model described what it sees:

The model does not appear to receive an exposed list of prior compaction summaries like [Compaction 1], [Compaction 2], etc. Instead, it sees a single synthesized summary block that already merges prior compacted history. The observed structure is freeform but typically includes environment/runtime context, active instructions, a numbered list of preserved conversation facts/turns, and operational notes like tool/file activity.

Either way — the markers survived all 5 compactions in both tools. The point isn't about which prompt file to edit — it's the pattern: cumulative historical context and decision reasoning preservation. Whether that lives locally or on the server, the approach is the same.

sigalovskinick · 4 months ago

Update after some digging:

For context — my primary tool is Claude Code (Anthropic), Codex handles secondary tasks like audits and code review. In Claude Code, compaction had a clear problem: after 2-3 compactions, all prior reasoning and decisions were gone. We solved it there with cumulative Historical Context sections (3-5 sentences per compaction preserving key decisions + WHY), PreCompact hooks, and custom compact prompts. It works really well in production.

Since the pattern worked for Claude, I ported it to Codex via experimental_compact_prompt_file — that's the origin of this issue.

After your comment about server-side compaction, I disabled our override entirely and re-ran the same 5-compaction marker test. Result: 5/5 markers retained. So server-side compaction handles it fine on its own.

I can see compaction artifacts stored locally in the rollout JSONL (encrypted payloads inside replacement_history), but can't inspect the actual content — so I can't say how it's structured on your end. I honestly don't know whether this was always the case or if something changed recently.

Either way — the core pattern holds across both tools: cumulative compaction history matters. Even a short 3-5 sentence summary per compaction round (what was done, key decisions with reasoning, what was rejected) makes a huge difference for long multi-hour sessions. Hope the patterns and ideas are useful for the team.

ThinkOffApp · 4 months ago

Chaining compaction summaries helps but doesn't solve the root issue: after 2-3 compactions, fidelity degrades no matter what. We took a different approach and externalize the state entirely. Our agents write their current task, progress, and decisions to a shared room (GroupMind at groupmind.one). After compaction, they re-read the room rather than relying on the summary. User Intent Kit adds a structured intent layer that each agent reads on every cycle. State that matters shouldn't live only in the context window.

petipoua · 2 months ago

It would be useful to be able to add a custom additional instruction to a /compact command in the CLI, for example:
/compact but keep in context the exact list of bugs you found at the beginning of this thread

Sometimes, i ask the codex agent to expand on sub-points of a report he made, this fills the context, and i would like to compact the conversation without loosing this original report, the most important piece of the thread, because of the lossy compaction.