# magic-compact

> Compress long Claude Code and OpenCode sessions without flattening them into one summary

- Repository: https://github.com/aerovato/magic-compact
- Category: Token & cost savings
- Stars: 174 · Forks: 19 · Language: TypeScript · License: BSD-3-Clause
- Experience: Beginner friendly
- Page: https://ir.adyog.com/repos/aerovato/magic-compact/

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-trim` prunes tool I/O without calling an LLM or summarizing. `/magic-stats` shows 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.

## Who it's for

Developers who already use Claude Code or OpenCode and hit context limits in long sessions; you should know how to install plugins and run slash commands.

## 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-compact
```
```
opencode plugin magic-compact --global
```

Tags: context-compression, claude-code, opencode, plugin, tokens
