omp packages

context-rem0.3.0

Compaction summaries from a smaller model, for Claude Code and Pi.

3mrfouad Published 6 days ago MIT ~0 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

context-rem

Tests License: MIT

Alt text

A Claude Code plugin that writes compaction summaries with a smaller model (Haiku by default) instead of your main model. The recent turns and every message you typed are kept as they were. When a session has been idle long enough for its prompt cache to expire, it compacts on its own, so your next message re-caches a smaller context instead of the whole session. The name comes from REM sleep, when the brain sorts the day's memories.

Why

A long session costs in two ways: every request re-reads the whole context, and after a break the whole context is written to the prompt cache again. Compaction shrinks the context, but Claude Code's own compaction has your main model read all of it to write the summary. context-rem has a small model write the summary instead, from a transcript with file contents and long tool output left out, at a small fraction of the price per token.

Because the summary is cheap, the right time to compact an idle session is just after the cache has expired: nothing cached is lost, and your next message re-caches a small context instead of the whole session.

Early results

In early runs on sessions of 500K to 800K tokens, /compact through context-rem cost over 90% less than Claude Code's own compaction on Opus 5.5, at API list prices: about $0.03 against about $2.50 for a 500K-token session. Haiku read a pruned transcript, about half the session, in calls under 100K tokens each, and wrote the summary; Opus would have read the whole session at the cache-write price ($5 per million tokens) once its cache had expired, then written its own summary at $20 per million output tokens. With a warm cache Opus reads its context at $0.20 per million, so that gap narrows, but the summary itself still costs $0.50 per million output tokens on Haiku against $20 on Opus.

These are a handful of sessions, not a benchmark. To measure it on your own work:

  1. Take a long session, at least a few hundred thousand tokens, and fork it so both compactions start from the same context: claude --resume <session-id> --fork-session.
  2. In one copy, run /compact; context-rem writes the summary. In the other, run /ctx-rem replace off, then /compact, then /ctx-rem replace on; Claude Code writes it with your session's model.
  3. Compare the measured tokens: /ctx-rem report shows both compactions, Haiku's calls and Claude Code's own request, as the API reported them. /ctx-rem report json exports every compaction for analysis across many sessions; your API usage or gateway logs give the prices.
  4. Compare quality: open each summary with ctrl+o, then give both copies the same next task and see whether either has lost something it needs.

Install

From the GitHub marketplace:

/plugin marketplace add 3mrfouad/context-rem
/plugin install context-rem@context-rem

Or from your shell, in one step:

claude plugin install context-rem --marketplace 3mrfouad/context-rem

This installs context-rem and its companion context-rem-trigger. Start a new session, or run /reload-plugins. Requires Claude Code 2.1.287 or later; tested on 2.1.332. Works with a Claude subscription or an API key, and needs no extra key: summaries are written with your session's own login.

context-rem isn't in Anthropic's plugin directory: the directory accepts plugins that run their own code only from Verified organizations, so it's installed from this repository's marketplace as above.

Install with managed settings

On a machine with managed settings (any file under managed-settings.d) or a Team or Enterprise sign-in, Claude Code seats its guard, cc-plugin-sec-default, ahead of every plugin, and the guard passes compactions only to the organization's plugins. Managed settings can make context-rem one of them. Add a drop-in, for example 20-context-rem.json, to the managed settings folder: /etc/claude-code/managed-settings.d/ on Linux, /Library/Application Support/ClaudeCode/managed-settings.d/ on macOS, C:\Program Files\ClaudeCode\managed-settings.d\ on Windows.

{
  "extraKnownMarketplaces": {
    "context-rem": { "source": { "source": "github", "repo": "3mrfouad/context-rem" } }
  },
  "enabledPlugins": {
    "context-rem@context-rem": true,
    "context-rem-trigger@context-rem": true
  },
  "appendPlugins": ["context-rem@context-rem", "context-rem-trigger@context-rem"]
}
  • extraKnownMarketplaces and enabledPlugins together make the plugins the organization's. The marketplace must be declared with the same source it is installed from.
  • appendPlugins seats them in the organization's append tier, which is where the guard hands compactions. Without it they run as a person's plugins and are skipped.
  • Both plugins must be listed: an organization's plugin can't run a command a person's plugin registered, so with only context-rem listed, /ctx-rem now and idle compaction fail with that error.

Then, as each person on the machine:

  1. Install it once: claude plugin install context-rem --marketplace 3mrfouad/context-rem.
  2. Start Claude Code twice. Claude Code checks a plugin it treats as the organization's against a hash of its copy; the first start records the hash ("its install record holds no hash" in the debug log), and the plugins load from the next start. The same happens after each update.
  3. Check: run claude -p hi --debug-file /tmp/cc.log, then grep "@context-rem loaded" /tmp/cc.log. Both plugins should show tier append. Then /compact in a long session should log "compacted: … summary by haiku".

Installing clones the repository from GitHub, so the machine needs to reach github.com. With CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 the install and the hash check still work, but plugin auto-update is off: update with claude plugin marketplace update context-rem and claude plugin update context-rem@context-rem, then start Claude Code twice again. To try a local checkout instead of GitHub, declare the marketplace as { "source": "directory", "path": "/full/path/to/context-rem" } and add it with claude plugin marketplace add /full/path/to/context-rem.

Use

Nothing to do: /compact, Claude Code's automatic compaction and idle sessions all go through context-rem. To see and change what it does:

Command What it does
/ctx-rem Context size, prompt cache state and re-cache cost, settings
/ctx-rem report What this session's compactions cost and saved, and totals for all sessions on this machine
/ctx-rem report json Every stored compaction as JSON, for your own analysis
/ctx-rem now Compact now with the cheap model
/ctx-rem on / off Compact automatically once the prompt cache has expired (default on)
/ctx-rem keep 20k Recent turns kept word for word, in tokens (default 20k)
/ctx-rem min 100k Smallest context idle compaction acts on (default 100k)
/ctx-rem model haiku Model that writes summaries (default haiku). A list such as haiku,sonnet is tried in order, so if a gateway refuses one model the next writes the summary instead of your main model
/ctx-rem replace on / off Use the cheap model for /compact and automatic compaction too (default on)
/ctx-rem agents on / off Use it for subagents' compactions too (default on)

Settings are saved for all your sessions on this machine.

How it works

context-rem is a Claude Code mod: a plugin whose JavaScript hooks run inside Claude Code.

  • It answers the compaction itself. Its session.compact hook returns the new conversation, so Claude Code makes no main-model summary request at all. That covers /compact, the automatic compaction near the context limit, and Claude Code's own idle compaction.
  • Subagents too: when a subagent (or a fork) fills its context, Claude Code compacts that subagent's own transcript, and context-rem answers it the same way. The task its parent gave it is kept word for word, and the summarizer is told it is summarizing a subagent's work. Claude Code's own compaction stays the fallback, and /ctx-rem agents off leaves subagents to it.
  • What it keeps: the most recent turns (up to keep tokens) exactly as they were, cut at a user message so no tool call is separated from its result and no running call is dropped. Every message you typed in the summarized part is copied into the summary word for word, and carried forward through later compactions. Three more things are copied straight from the tool calls rather than left to the summarizer: the files that were changed, the to-do list as last written, and the last tool error.
  • What the summarizer sees: the older turns with file contents dropped (files can be re-read), long tool output shortened and harness reminders removed. Anything over about 70k tokens is summarized in parts and merged, so each call stays in Haiku 5.5's cheaper price band.
  • When the cheap model fails: /compact and automatic compaction fall back to Claude Code's own. Idle compaction and /ctx-rem now just skip, so they can never cost a main-model compaction.
  • Idle: after each turn it sets a timer for 15 seconds after the prompt cache expires: 5 minutes with an API key, 1 hour on a subscription. When it fires, if the session is idle, nothing has compacted it since the last turn, and the context is at least min tokens, it compacts.
  • Resume: resuming a session whose cache has expired (at least min tokens) compacts it before your first message re-caches all of it.
  • Prepared ahead: Claude Code starts preparing a summary before its automatic compaction is due. context-rem writes its own at that point, and when the compaction runs it only summarizes what came after and merges the two, so a very large session doesn't wait on the whole history. If the older part changed in between, it starts over.
  • Why two plugins: Claude Code never lets a mod answer a compaction that the same mod started. So /ctx-rem now and idle compaction are started by context-rem-trigger, and context-rem answers them. Before its first compaction, the trigger sends a probe that context-rem declines. If Claude Code's own compaction would answer instead, a PreCompact hook blocks the probe and the trigger starts nothing, rather than risk a main-model compaction.

What /ctx-rem report measures

The report counts tokens; it doesn't price them, because prices change and a subscription doesn't bill per token.

  • Context before and after each compaction: before is Claude Code's count for the last request, after is the API's count for the first request that follows.
  • Requests since, counted one by one, for the main conversation only.
  • Fewer tokens read (idle compaction and cold resume only): the difference between before and after, times the requests since. Without that compaction nothing else would have shrunk the session, so each of those requests would have carried the difference. The first one writes the cache, the rest read it.
  • Summaries your models didn't write (/compact, /ctx-rem now, automatic compaction, subagents): Claude Code would have compacted anyway, with the model of that conversation (your session's, or the subagent's) reading the whole context to write the summary. The report shows that context size and which model wrote the summary instead. A subagent's sizes and model come from its own requests.
  • Claude Code's own compactions (with replace off, as a fallback, or for subagents with agents off): the tokens its summary request read and wrote, as the API reported them. That is the measured baseline the other lines compare against.
  • Summarizer tokens, in and out, exactly as the API reported them. They're shown apart from your main model's tokens, since a summarizer token costs much less.

Totals for all sessions on this machine are kept in the plugin's own storage.

With Claude Code's own idle compaction

On a subscription, Claude Code itself compacts sessions of 200k tokens or more about 54 minutes into a break, while the one-hour cache is still warm. context-rem answers that compaction with the cheap model too. For smaller sessions, its own timer compacts just after the hour.

Pi

The same summarizer runs in Pi as an extension; tested with Pi 1.7.0.

pi install npm:context-rem

or straight from this repository: pi install git:github.com/3mrfouad/context-rem.

  • /compact and Pi's automatic compaction get their summary from anthropic/claude-haiku-5-5 by default. Pi keeps the recent messages from its own cut point; the messages you typed and the state pack are kept as in Claude Code, and Pi's previous summary is merged in.
  • If no summary model answers, Pi compacts the usual way. A compaction context-rem started itself is cancelled instead.
  • Idle: after a run settles, context-rem waits 5¼ minutes (/ctx-rem idle 60 with a one-hour cache), then compacts a session of at least 100k tokens. While Pi keeps the prompt cache warm it waits, and when Pi stops warming it, it compacts shortly after.
  • /ctx-rem shows the status; /ctx-rem report, on/off, min 100k, idle <minutes> and model provider/model (or a list, tried in order) work as in Claude Code. Settings are kept in ~/.pi/agent/context-rem.json.
  • Not in the Pi version yet: subagents, summaries prepared ahead, and report json.

Prior art

Project What it does Who writes the summary
Claude Code's idle compaction compacts ≥200k-token sessions ~54 min into a break, 1-hour cache only your main model
claude-idle-compact (Anthropic directory), claude-idle-compactor, claude-self-compact, cachekeeper, claude-code-idle-compact, idle-compact compact after 20 to 55 idle minutes, mostly while the cache is still warm your main model
haiku-compact Haiku summary for /compact haiku, offered when the cache is cold and run on a cold resume; plain /compact and auto-compaction are left alone Haiku
claude-contextmanagement PreCompact hook runs claude -p --model haiku for a cumulative summary that hooks inject; the main model still writes a short one both
claude-smart-compact Haiku picks the moment and lists what must survive your main model
morph-compact injects Morph's summary through PreCompact and SessionStart hooks Morph, with Claude Code's summarizer still running
context-rem answers every compaction, subagents' included, and compacts after the cache expires and on a cold resume a cheap model, in place of Claude Code's summarizer

What's different here: one hook replaces Claude Code's summarizer for every compaction, the main conversation's and its subagents', including Claude Code's own idle compaction and the ones other plugins start, and idle compaction waits until the cache has expired, which only makes sense when the summary is cheap. There is also an open request for a native setting, compactModel / CLAUDE_CODE_COMPACT_MODEL; earlier requests (#12660, #49816) were closed without it.

Limits

  • A Haiku summary is less nuanced than your main model's. Keeping recent turns and your own messages word for word covers most of what matters; /ctx-rem model sonnet trades some savings for a better summary, and /ctx-rem replace off leaves /compact to Claude Code.
  • Claude Code's own compaction also re-reads up to five recently edited files afterwards; context-rem keeps the recent turns instead, and Claude reads files again as needed.
  • On a machine with managed settings, or signed in with a Team or Enterprise plan, Claude Code's guard (cc-plugin-sec-default) lets only the organization's plugins answer compactions. Installed the usual way, context-rem is skipped there: /compact shows "its session.compact hook was skipped" and runs Claude Code's own compaction, and the trigger stays off rather than fall back to your main model. Managed settings can make both plugins the organization's: see Install with managed settings.
  • /ctx-rem now, idle compaction and cold-resume compaction need an interactive terminal session: Desktop's Code tab, SDK and headless sessions refuse a plugin's compaction call. /compact and automatic compaction use the cheap model everywhere.
  • On Windows without Git Bash the probe's PreCompact guard can't run.

Develop

claude --plugin-dir plugins          # load both plugins from this checkout; edits reload live
claude plugin test plugins/context-rem
claude plugin test plugins/context-rem-trigger
claude plugin validate . --strict

The tests run on Claude Code's own test host, with no sign-in or network. .github/workflows/test.yml runs validation and tests on every push. To release, bump version in both plugin.json files; users get the update when their marketplace refreshes.

plugins/context-rem/hooks/core.ts holds the logic as plain functions with no Claude Code calls, so it can be ported to other harnesses.

License

MIT