Repository navigation
auto compaction - expose compaction to agent #21777
Description
What variant of Codex are you using?
cli
What feature would you like to see?
Problem description
During goal runs, a new iteration is often started when most of the context window is already filled. The agent then proceeds to read additional files anyway, triggering compaction during the process. This can waste the work done in that iteration, because the agent may effectively have to start over after compaction.
Proposition 1
The simplest solution would be to check available context before starting the next goal slice and trigger compaction when context usage exceeds a configured threshold.
Ideally, this threshold should be configurable, including an option equivalent to “always compact before starting a new slice.”
When a goal is paused or finished, this action should not be attempted automatically, because the user may want to continue iterating on the knowledge already accumulated in the current context.
Proposition 2
This is not exclusive with the previous option, but it is definitely more complex and may require model training for optimal behavior.
Assuming the agent is not currently aware of its own context usage, the system could provide hints after the model crosses configured context-usage marks. For example:
context left: plenty
context left: moderate
context left: low
context left: critical
The agent could also be given a special tool call to trigger compaction, with safeguards such as requiring some minimum context usage before the tool is available.
This would be a more powerful version of Proposition 1, allowing the agent to decide when compaction is appropriate based on its current progress. I would expect the agent to trigger compaction early after reaching an implementation milestone, rather than waiting until the context is almost full.
Proposition 3 (bonus one)
This is an extension of Proposition 2 and would likely require significant RL or other training.
Consider partial compaction, where the agent can decide which parts of the context should be preserved as-is and which parts are no longer needed.
For example, the agent could decide to drop outdated file reads from the context while keeping the latest versions. It could also choose to preserve specific reasoning, summarize other reasoning, or discard irrelevant intermediate steps.
This would require a sophisticated API for the agent and would likely need to be performed server-side. I am deliberately leaving this as a vague idea for consideration.
Summary
I believe Proposition 1 is the easiest to implement and would have the largest immediate impact.
The remaining suggestions could be used as experiment ideas. If such experiments are performed, I would be very interested in any follow-up, even in a heavily redacted or limited form, if anything can be shared. I would be happy to know whether the idea was useful in practice.
Additional information
No response
Activity
github-actions commentedon May 8, 2026
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
mahdikayvan commentedon May 11, 2026
@Szpadel Hey — I felt this issue immediately, and it is very close to the exact pain I’m trying to solve.
I’m building AgaveCore, and I’d like to let a few early people use it for free while I shape it into something stronger.
It helps when a coding agent starts to drift, loses important context, or gives you an output you do not fully trust.
In practice, it works through two habits:
remember-progressorremember-thiswhen something important should not be lostconsult-brainwhen the current task needs more grounded feedback on what to do nextOver time, as useful journals, decisions, and failures pile up, the product should help the agent make sharper decisions and take better actions with less drift.
If this sounds relevant, reply here and I’ll send the exact steps. You can also use the email or Telegram on my profile.
Cheers,
Mads.
kyboi commentedon May 26, 2026
I believe we just need a programmatic way to trigger compaction, something like a compaction tool or even make it available through hooks. Currently the only way seems to use JSON-RPC over the client-server
migsferro commentedon Jun 5, 2026
Request: expose compaction as a first-class agent-callable tool/RPC, with prompt-guided state preservation.
Building on the earlier comment asking for a programmatic compaction trigger: agents often know they are at a clean compaction boundary after a handoff, review, synthesis, checkpoint, or completed implementation slice.
Suggested shape:
compact_current_thread(summaryInstructions?: string)compact_thread(threadId, summaryInstructions?), guarded by permissions and idle-state checksPriority: medium-high. This should be a tool/RPC, not slash-command injection; sending
/compactas a normal message is too ambiguous for reliable orchestration.Footnote: drafted by ChatGPT/Codex, directed and reviewed by Miguel before posting.
[-]auto compaction in goal / expose compaction to agent[/-][+]auto compaction - expose compaction to agent[/+]MeGaNeKoS commentedon Jul 4, 2026
I have been experimenting with this in my own fork because this is a real workflow issue for me:
https://github.com/MeGaNeKoS/codex/tree/agent-compact-context-v0.142.5
My use case is long-running Codex sessions where I leave the agent working in the background across multiple turns. In those sessions, the agent often reaches a natural cleanup point before the user does: after a build/debug loop finishes, after an implementation slice is complete, after old logs become irrelevant, or before starting a new phase.
Today, compaction is mostly user-triggered or automatic. That means the agent can recognize ?this context is now stale/noisy,? but cannot act on it directly. I think an agent-callable compaction request would help reduce drift, stale-context carryover, and token usage in these longer workflows.
The shape I tested is:
compact_contexttoolContext compaction request accepted.rather than claiming compaction already completedThe agent path is intentionally not the same as manual
/compact:While testing this, I also added optional guidance to
/compact, because it seemed like the same preservation-instruction concept should apply to both user-triggered and agent-triggered compaction. That overlaps with #21468 and #29816, but the main reason I am commenting here is the agent-callable compaction part.The fork currently includes the core/tool wiring, config gating, direct-model-only tool exposure,
/compact <instruction>support, local/remote compaction instruction propagation, protocol/schema updates, and tests around config parsing, tool exposure, slash-command args, app-server handling, and local/remote compaction behavior.Demo video recorded on 2026-07-04:
2026-07-04.19-15-47.mp4
z0rgoyok commentedon Aug 8, 2026
I have been testing a downstream implementation of the narrow model-managed compaction slice proposed here.
The prototype exposes two model-callable tools behind the existing token-budget feature:
The runtime behavior is deliberately small and host-controlled:
get_context_remainingbefore a context-intensive phase.compact_contextwith the reason, the phase that should continue afterward, and a bounded preservation hint.The first slice does not need selective deletion, arbitrary history rewriting, or a parallel compaction implementation. It can reuse the existing local/remote summarizing paths, remain feature-gated, and keep every new model-visible fragment bounded.
The policy used during testing was the current compaction protocol from my personal
~/.codex/AGENTS.md, translated into English without changing its thresholds or decision conditions. The compact-decision section is:The complete current English translation, including the checkpoint schema and post-compact resume protocol, is here:
https://github.com/z0rgoyok/codex-luna-kit/blob/main/docs/AGENTS.compaction.example.md
Version used for this comment:
https://github.com/z0rgoyok/codex-luna-kit/blob/aa0a07fd4d11d30f8260de8973898641722f4c3a/docs/AGENTS.compaction.example.md
My experience after using this setup for a full day:
A focused PR could cover:
I have a working downstream prototype based on an earlier
mainrevision and can rebase it with unrelated model/auth changes removed.Would this narrow two-tool slice align with the intended direction of the token-budget/context-window work? If so, would a maintainer be willing to invite a focused PR, and should #21777 remain the canonical issue rather than #36669 or #33310?
Prototype and comment prepared with Codex and reviewed by Denis Zabozhanov (@z0rgoyok).
EateralSpirial commentedon Aug 22, 2026
One concrete way to make Proposition 2 more deterministic would be to combine a soft context threshold with Codex's existing structured plan state.
Suggested policy:
compact_pending = true.update_planmarks the currentin_progressstep ascompleted, treat that transition as a natural compaction boundary and compact before starting the next step.update_plan.In short:
compact = (context >= soft_threshold && plan_step_completed) || context >= hard_thresholdThis seems useful because context pressure answers whether cleanup is needed, while a completed plan step gives the runtime a structured signal for when compaction is least disruptive. It also avoids requiring the model to infer an implementation milestone from scratch.
Capture1999 commentedon Aug 24, 2026
I have a concrete multi-repository, cross-platform use case that reinforces the model-managed compaction slice proposed here.
On Codex CLI 0.149.1, I verified that:
The practical use case is a single logical leader switching implementation scope across repositories or platforms. The task goal, contracts, decisions, changed files, and verification state should survive, while stale implementation details from the previous scope should be de-emphasized by native summarizing compaction.
The narrow missing intersection is therefore:
This does not require a custom compactor, structured handoff schema, selective deletion, or a new thread.
Related: #36057, #29816, #34428.
famulare commentedon Aug 24, 2026
I need to be able to invoke /compact from a park-this skill to manage cache miss rates. I don't understand why compaction isn't model call-able like most other skills or plugins.