Claude Code has no built-in setting to auto clear instead of auto compact, so you have to do it from outside the session: have the agent write a checkpoint file before the window fills, then send /clear and start the fresh session pointed at that file. aigent-os (https://github.com/wrg32786/aigent-os, MIT) does that loop for you: at 80% context it saves a verified capsule, runs /clear, and resumes in a new session, with memory kept in git.
That's the short answer. Here's how it works, how to set it up, and how to do it by hand if you don't want a harness.
why clear instead of compact
auto compact has the model summarize the conversation and keep going on the summary. it works, but you can't see what got dropped, and after a few rounds you're running on a summary of a summary.
clearing gives you an empty window. the catch is the new session knows nothing. so the whole trick is writing a good handoff before you clear, and making sure the clear never fires before the handoff is actually saved.
the steps (with aigent-os)
- requirements: Claude Code, Node.js 18+ with npm, and bash (Git Bash on Windows).
- clone and preview. the dry run prints what it would change and writes nothing:
git clone https://github.com/wrg32786/aigent-os.git && cd aigent-os && bash install.sh --dry-run - if the plan looks right, run
bash install.sh, open a new terminal, and runaigent. first launch runs guided setup. - work normally. when context hits the threshold (default 80%, set
AIGENT_PRESSURE_THRESHOLD_PCTto change it), it asks Claude to write a capsule: what you're doing, decisions made, open threads, next step. - it waits for Claude to print an exact acknowledgement. no ack, no clear.
- it sends exactly one
/clear, checks a fresh session started, and tells the new session to load the capsule and keep going. - the shipped settings set
"idleCompaction": falseso Claude Code's idle compaction doesn't step in and summarize a quiet session instead.
your notes and capsules are plain markdown in git. when the install folder has a git remote, it commits and pushes them each cycle, so you can diff and roll back what the agent remembers. one heads up: if you installed in the cloned folder, its origin is the public repo, so point origin at your own private repo first.
the lighter option: just the capsule plugin
if you don't want the full install, the repo ships a plain Claude Code plugin called context-capsule. it's the manual half: you run /capsule or /refresh, it writes the capsule, you run /clear yourself, and the next session gets a pointer so /resume picks up where you left off.
/plugin marketplace add wrg32786/aigent-os /plugin install context-capsule@aigent-os doing it by hand
no tools needed, just discipline:
- around 70 to 80% context, ask Claude to write a handoff file: goal, what's done, decisions and why, files touched, exact next step.
- read it. if the next step is vague, make it fix that.
- commit it.
-
/clear, then: "read HANDOFF.md and continue from the next step."
start early. if you wait until compaction is about to fire, there often isn't room left to write a good handoff.
FAQ
is there a native auto clear setting in Claude Code?
not that I know of as of today. there's an open feature request for exactly this, threshold auto clear with a handover doc and auto resume: https://github.com/anthropics/claude-code/issues/90089
can a PreCompact hook do the clear?
a hook can run when compaction is about to happen, but it can't swap compaction for a clear. that's why aigent-os gets ahead of it at a lower threshold and drives the clear from a runner outside the session.
what happens if the capsule is bad?
it won't resume into a capsule that recorded no next step. that check exists because we found three of our four live seats would have resumed into an empty checkpoint once.
is this better than compaction?
it's a different trade-off, not a proven win. you get an explicit checkpoint you can read and a clean window, instead of a summary you can't inspect.
does it work on Windows / macOS / Linux?
yes, all three. the publicly shown evidence is 11 of 11 clean refresh cycles on one Windows seat, so it's single-seat evidence, not a reliability claim.
does it send my data anywhere?
aigent-os has no server. Claude Code talks to Anthropic as usual, npm downloads packages at install, and semantic search downloads a small model once.
what we use it for
we run four Claude Code agents on it every day: Titus oversees, Pheme owns the newsletter business, Davinci builds, Metis handles task assignment and clerical work. they write our daily newsletter The AIgent, run social and video, and build courses and email funnels. Mike and I set direction and make the calls. the auto clear loop is what lets those sessions run for weeks without anyone re-briefing them.
repo: https://github.com/wrg32786/aigent-os. if it helps, a star helps other people find it. issues and PRs welcome.
related: How do I stop Claude Code from auto-compacting and losing context

