Skip to content

About

Claude Code plugin: verbatim context compaction scored through the Vercel AI Gateway. Prunes stale tool calls instead of summarizing.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

vercel-compaction

Claude Code plugin that replaces the compaction summary with pruning. Every old tool call and tool result is scored in one fast request through the Vercel AI Gateway; stale ones are dropped or truncated, and everything kept stays verbatim. Also usable as a library.

You need one thing: a Vercel AI Gateway key. No other account, no proxy process, no SDK dependency.

Why

Most compaction asks an LLM to summarize old turns. A summary is lossy: a file path, an exact error, a constraint or a command can vanish even when it matters later. This plugin never rewrites anything. It only deletes tool calls and tool results that an evaluation model judges no longer needed. User and assistant text stays verbatim and in order.

The judge is Jev (typesafe-ai/jev in the gateway catalog), an evaluation model that answers typed questions about a shared state with calibrated probabilities. It is priced per input token with no output cost, so scoring a whole session is cheap.

How it works

  1. Every tool_use is paired with its tool_result. Calls in the first message or in the newest preserveRecentMessages messages are pinned and never touched.
  2. The state sent to the model is the whole conversation, oldest first, with every tool result replaced by a short note (ok, 4213 chars (omitted)).
  3. The state is fitted into maxStateTokens in stages, each applied only if the previous one was not enough: tool inputs truncated, long texts abridged to head and tail, old messages collapsed to a note, old tool calls reduced to one line each. If it still does not fit, compaction throws.
  4. For every non-pinned call the model gets two yes/no questions: should the call stay, and should its result stay verbatim.
  5. Questions are split into as many requests as needed to stay under maxRequestTokens. Requests run concurrently and their answers are merged.
  6. Per call, against keepThreshold: keep both; or keep the call and truncate the result to truncateHeadChars plus a one-line note; or remove both.
  7. The message list is rebuilt. No result is ever left without its call.

A failed request, a malformed or incomplete answer, a missing key, or a history that cannot be fitted all throw. The plugin then falls back to Claude Code's built-in summary, so a failure never costs you the session.

Install in Claude Code

Function hooks are an early-access Claude Code feature (2.1.274 or later). Enable them and provide the key wherever Claude Code runs, for example in .claude/settings.local.json:

{ "env": { "CLAUDE_CODE_ENABLE_FUNCTION_HOOKS": "1", "AI_GATEWAY_API_KEY": "<your key>" } }

Keep the key out of any settings file you commit. Then:

claude plugin marketplace add gshost1/vercel-compaction
claude plugin install vercel-compaction@vercel-compaction --scope project

Restart Claude Code. From then on /compact and auto-compaction go through the gateway. The toast reads kept N/M messages, no summary (…) when the pruned history replaced the summary, or fallback to built-in summary (…) when not enough could be removed or the request failed.

Keeping the raw key out of Claude Code

If you use a local key broker that mints short-lived tokens and proxies the gateway, launch Claude Code with two environment variables and no stored key: AI_GATEWAY_API_KEY set to the temporary token and AI_GATEWAY_BASE_URL set to the broker's URL for /v4/ai/evaluation-model. The URL must be https, or http on localhost. When the token expires the plugin falls back to the built-in summary.

Project scope is recommended. See What leaves your machine below before installing it for every project.

What leaves your machine

At each compaction the plugin sends the conversation state to the Vercel AI Gateway: your prompts, the assistant's text, and tool inputs (file paths, commands, search patterns). Tool results are not sent; each is replaced by a one-line size note. The gateway lists this model as zero data retention and no training, but the text still transits Vercel and the model provider. Do not enable the plugin in projects whose contents may not leave your machine.

Library

import { compactMessages, reductionRatio, type Message } from 'vercel-compaction';

const result = await compactMessages(transcript, { preserveRecentMessages: 4 });
if (reductionRatio(result) < 0.25) {
  // not worth it: keep the original transcript, or summarize instead
}

apiKey defaults to process.env.AI_GATEWAY_API_KEY. To bring your own transport, implement JevAsker (one ask(state, questions) method) and call compact(messages, asker, options).

Options

Option Default Description
apiKey AI_GATEWAY_API_KEY Vercel AI Gateway key
model typesafe-ai/jev Gateway id of the evaluation model
baseUrl AI_GATEWAY_BASE_URL, else the gateway evaluation endpoint https only; http allowed for localhost
keepThreshold 0.5 Minimum keep probability for a call or result to stay
preserveRecentMessages 6 Newest messages never touched (the first is always kept)
maxStateTokens 25000 Estimated token ceiling for the state
maxRequestTokens 30000 Estimated ceiling for state plus one batch of questions
truncateHeadChars 300 Characters of a dropped tool result kept before its note
compactAtPercent 60 Plugin only: context percentage that triggers compaction
minReductionRatio 0.25 Plugin only: below this, fall back to the built-in summary

Status and limits

  • The request format follows the gateway's evaluation-model protocol as spoken by the AI SDK (experimental_evaluate). That protocol is experimental and undocumented as plain HTTP, so an SDK or gateway release may change it. The unit tests use a fake gateway; run a real /compact to confirm your setup.
  • Only tool calls and results are candidates. Text is never removed.
  • Token sizes are estimates from character counts, not a tokenizer.
  • A probability is not a proof that a result is safe to delete. The assistant can always re-run the tool.

Development

npm install
npm run typecheck        # library
npm run typecheck:hooks  # hook; first run /plugin-types types inside Claude Code
npm test
npm run build
npm run validate:plugin

Credits

Adapted from fast-jev-compaction (MIT), which does the same pruning against TypeSafe's own API. This project changes the transport to the Vercel AI Gateway. MIT licensed; see LICENSE.

About

Claude Code plugin: verbatim context compaction scored through the Vercel AI Gateway. Prunes stale tool calls instead of summarizing.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages