Auto-run a skill when context is near exhaustion before compaction
What variant of Codex are you using?
cli
What feature would you like to see?
I would like Codex CLI to support context-threshold automations that can automatically run a configured skill before automatic compaction or context exhaustion.
Use case:
When a long Codex session gets close to the context limit, I want Codex to automatically create a clean handoff file before the conversation gets compacted or polluted by stale context. This would reduce hallucinations and make long-running coding sessions much safer.
Example behavior:
when context_remaining_percent <= 15:
invoke: $context-handoff
require_file_update: docs/CODEX_HANDOFF.md
after_handoff: compact # or clear/new
require_user_confirmation: false
The skill could be responsible for writing a verified handoff containing:
- current goal
- repo facts verified from files
- files changed and why
- commands/tests run and exact results
- known failures
- unknowns explicitly marked as UNKNOWN
- next recommended steps
Then Codex could automatically continue with the configured policy:
[context_handoff]
enabled = true
trigger_remaining_percent = 15
skill = "context-handoff"
handoff_file = "docs/CODEX_HANDOFF.md"
after_handoff = "compact" # or "clear" / "new"
require_repo_verification = true
require_user_confirmation = false
This should be opt-in, because some users may not want automatic file writes or automatic context resets. But for users who explicitly enable it, it would remove the need to manually watch the context meter and manually type $context-handoff before clearing/compacting.
Additional information
Currently, the closest workaround seems to be:
- Set
model_auto_compact_token_limit. - Use a
PreCompacthook. - Have the hook run a command that warns or launches a separate
codex execprocess to update a handoff file.
But this is awkward because hooks currently run command handlers, not full agent/skill handlers. So a hook cannot cleanly say: “run this skill as the current Codex agent, update the handoff file, then proceed with compact/clear.”
The missing feature is not just a warning. The desired behavior is:
context threshold reached
→ automatically invoke configured skill
→ skill updates handoff file
→ Codex compacts/clears/starts new session according to config
→ no manual intervention required
This would be especially useful for long autonomous coding sessions, where the user does not want to interrupt the agent just to manually run a context handoff workflow.