aerovato /
magic-compact
Compress long Claude Code and OpenCode sessions without flattening them into one summary
- 174 stars
- 1 stars since 3 Oct
- TypeScript
- Updated 35 days ago
- Beginner friendly
- Token & cost savings
Coding agents get slow and expensive as a session grows. The built-in compaction in Claude Code and OpenCode replaces the whole conversation with one summary in a fixed template. Your messages, the agent's reasoning and its design decisions get flattened, and the agent has to rebuild its working state from that.
Magic Compact keeps the shape of the conversation instead. Your messages stay word for word, and tool calls stay in place. Each old assistant turn is replaced by its own short summary. Long tool inputs and outputs are swapped for an omission notice with an ID, and the agent can call a read_omitted_content tool to fetch the original when it needs it.
Compaction runs once, when you ask for it, so there is no background process and no prompt injection during the agent loop. The README contrasts this with the OpenCode plugins DCP and Magic Context, which manage context continuously and cost more tokens and cache invalidations. On Claude Code the plugin can't edit the current session, so it creates a new compacted session that you resume. The README says development is paused and points to the successor, Operator Memory, but the plugin still works.
What it does
Per-turn summaries
Each old assistant turn gets its own summary. User messages and tool call structure stay as they were.
Pruned tool output, still retrievable
Completed tool I/O over 128 words or 1024 characters is replaced by an omission notice. The agent can fetch the original with
read_omitted_content. File reads are always omitted, since they can be reloaded.Recompactable with a backup
Run
/magic-compact [N]again later to compact new turns while keeping earlier summaries. N keeps that many recent turns untouched. A backup session is created first.Trim and stats on OpenCode
/magic-trimprunes tool I/O without calling an LLM or summarizing./magic-statsshows tokens pruned and estimated money saved. Both are OpenCode only.
What you could build
- Shrink a long Claude Code session so you can keep working on the same task without starting over.
- Keep exact requirements and past instructions visible to the agent across a multi-hour coding session.
- Cut token use in OpenCode by trimming old tool output while leaving the conversation intact.
- Check how many tokens and how much money compaction saved in an OpenCode session.
- Compare one-shot compaction against continuous context managers like DCP or Magic Context.
Get started
The first two commands install it in Claude Code (run /reload-plugins if it's already open); the third installs it in OpenCode. Then run /magic-compact in a long session, and on Claude Code resume the new session it names.
/plugin marketplace add aerovato/magic-compact/plugin install claude-magic-compact@magic-compactopencode plugin magic-compact --globalCommands are copied from the project's README. Read it on GitHub for the full setup.
More in Token & cost savings
caveman
Makes coding agents answer tersely and shrinks the logs and output they read
billion-context
A proxy that compresses long coding-agent chats so one session can run for weeks