cd /news/ai-agents/make-claude-code-compact-itself-a-cl… · home › topics › ai-agents › article
[ARTICLE · art-163192] src=gist.github.com ↗ pub= topic=ai-agents verified=true sentiment=· neutral

Make Claude Code compact itself: a CLAUDE.md rule plus a script that types /compact into its own tmux pane once the turn ends

A developer published a workaround that lets Claude Code trigger its own /compact command, since the tool cannot invoke it internally. A rule added to CLAUDE.md has Claude run a compact-self script as its final action, and the script waits for the turn to end before typing /compact plus the context to keep into Claude's own tmux pane and pressing Enter. The approach requires tmux, and the author notes it will not type into a pane that has been scrolled back and can collide with user input if typing begins just as a turn ends.

by read1 min views4 publishedNov 3, 2026

Claude Code can't run /compact on itself, so this does it from outside. A rule in CLAUDE.md has Claude run compact-self "<what to keep>" as the last thing it does when it finishes a task. The script waits in the background until the turn is over, then types /compact <what to keep> into Claude's own tmux pane and presses Enter.

Needs tmux.

  1. Save compact-self as~/.claude/bin/compact-self and make it executable (chmod +x ).
  2. Merge settings.json into~/.claude/settings.json . The Stop hook records when a turn ends; the allow rule lets Claude run the script without asking.
  3. Add the rule in rule-for-CLAUDE.md to~/.claude/CLAUDE.md .

It won't type into a pane you've scrolled back in. It doesn't check the input line, so if you start typing in that pane just as the turn ends, the two get mixed.

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/make-claude-code-com…] indexed:0 read:1min 2026-11-03 · —