Magic Compact: Replacing OpenCode's Terrible Compaction Algorithm
Back in February, I was reverse engineering Claude Code's source code from the minified JS bundles shipped on NPM. Over a series of turns, we had progressively built up a map of the codebase: what symbols meant, what different functions did under the hood, the main conversation loop, reverse-engineered Ink TUI modules, permission system, etc.
Then the context got filled up, OpenCode's compaction kicks in, and the entire conversation was compacted. All tool calls, all file reads and writes, all the decisions we made and secrets we uncovered gets replaced with a single summary blob generated from OpenCode's compaction prompt.
Of course, I had my agent constantly document its findings and recreate source files whenever possible, but that wasn't enough. After compaction, the agent woke up with basically no memory of the codebase at all. The quality fell off a cliff. All symbol mappings, structural inferences, module separations, hours of untangling minified garbage, all flattened into a generic template that captured maybe 10% of what mattered. It started re-reading the same minified files, re-deriving the same mappings we'd already worked out, undoing everything.
Thus, I decided to build a better replacement for OpenCode's built-in compaction system.
The core idea was simple: standard compaction destroys information because it tries to summarize the entire conversation at once into a single Markdown summary. But what if you kept the conversation structure intact and only compressed the parts that actually needed it?
That's what Magic Compact does. It preserves every user message, replaces old assistant turns with lean summaries, and strips out large tool I/O into a cache while keeping the tool calls themselves visible. The conversation reads like the original, just condensed. Same flow, same decisions, same memory.
The difference in agent quality post-compaction is night and day. Per-turn summaries preserve the thought process and decisions for each turn, so the agent retains its working memory instead of waking up blind. It knows what files it explored, why it made certain choices, what the user actually asked for. Pruned tool calls means maximum savings, and past results can be reread from the cache at any time.
And since compaction is basically lossless in quality, you can run it way more aggressively than built-in compaction. I run /magic-compact constantly. Whenever I'm implementing adjacent features, I would compress after each feature and work on the next. If I'm working on a multi-phase plan, I would compact after each major phase. If I'm mid-implementation, I can pass an argument to keep the last few turns and only prune earlier ones.
In addition to lossless compression for long conversations, Magic Compact also helps me make my coding plan last at least 2-3x as long. For anyone on coding plans, this is a big deal. I've probably saved hundreds of billions of cached read tokens and thousands of dollars in billed token costs, letting me code 2 or 3x as much than before using the cheapest plans from Z.ai and OpenAI.
I've been using Magic Compact daily since March and it's become an integral part of my workflow, and today I've decided to open source it to share with the community.
Install it now with:
opencode plugin magic-compact --global
Run /magic-compact [N] to compact, where N is the number of recent assistant turns to keep. If not provided, by default N = 0. Run /magic-stats to see token and accumulated cache read savings for the current conversation.
Magic Compact is also open source, fully open to contributions and feedback: https://github.com/aerovato/magic-compact
PS: You may have also heard of another plugin called OpenCode DCP; while DCP asks the model to periodically summarize conversations via distracting prompt injections which results in constant cache churn, Magic Compact takes a different approach: comprehensive compacting and pruning of the entire conversation on your command.
Compared to DCP, Magic Compact is much more cache and token friendly with its aggressive summarization while being better at preserving quality. Magic Compact also doesn't periodically inject notices into the conversation, forcing the agent to compact and invalidate caches, so your agent stays 100% on task. You can use Magic Compact as a superior replacement over DCP.