Skip to main content
Commands

Context compression

Shrink tool output and older turns before they reach the model, keep every original retrievable on your machine, and see what it saved. No new verbs: vg serve --compress, vg install <agent> --compress, and vg savings. Works with Claude Code, Codex, Cursor, Copilot, Aider, and more.

Overview

Every turn, an AI coding agent re-sends its whole conversation to the model — the test log it already read, the JSON payload it already parsed, the grep output it already acted on. You pay for that context again on every step.

Vibgrate CLI compresses that context before it reaches the model and keeps every original retrievable on your machine, so the model can pull back exactly the lines it needs instead of re-paying for all of them.

Compression is not a second product with its own commands. It is a mode of the local runtime you already start (vg serve), a flag on the installer you already use (vg install), and a section of the report you already read (vg savings). Inside vg code it is on by default.

vg serve --compress          # serve the map and compress context, one process
vg install claude --compress # point Claude Code at it (undo: vg uninstall claude)
vg savings                   # what it saved: today / 7 days / 30 days

Savings depend on the workload and are reported, not promised: vg savings --benchmark gives you a reproducible number on your own machine.


How a request is compressed

The pipeline walks each message and each content block and decides per block:

  1. Exclusions first. System prompts, user text, assistant text, blocks already carrying a marker, retrieval results, error outputs, file reads and edits are left alone by default. In cache mode only the newest turn is eligible, so your provider's prompt cache keeps hitting; token mode makes every eligible block a candidate. The most recent messages keep their code intact.
  2. Route by shape. A detector classifies the block — JSON, source code, search results, build output, diff, HTML, tabular, structured config, plain text — and hands it to the compressor built for that shape.
  3. Lossless first. Repeated lines, grep and directory headings, path headings, diff index lines and config boilerplate fold into byte-reversible markers. Lossy compression runs on top only when it beats the fold by a clear margin.
  4. Lossy, shape-aware. JSON arrays keep a head / middle / tail sample plus every error-looking item and the rows that match your question. Logs collapse near-duplicates and keep errors, stack traces, first and last lines. Grep output is capped per file. Diffs keep every changed line and cap context. Source code keeps signatures, imports, exports and docstrings and collapses long bodies. Prose is compressed extractively — a deterministic sentence score, no model.
  5. Guarded. Errors, ids, hashes, URLs and test names may never be dropped; a result that is not smaller, or that fails the recoverability check, is rejected and the original forwarded. A per-request deadline forwards the original if compression runs long.
  6. Retrievable. Each lossy block stores its original (secrets redacted) in a local store with a short TTL and adds a marker. When a request carries markers, the listener offers the model a vg_retrieve tool and answers it from the store without a client round-trip. You can do the same with vg serve retrieve <hash>.

Across turns the pipeline also replaces verbatim repeats of earlier tool output with a pointer, marks file reads that a later edit made stale, and holds fresh reads byte-exact for a few turns before they become eligible.

Profiles. coding (default) is cache-mode with reads and edits byte-exact; balanced lets older reads compress; aggressive is token-mode over everything eligible, for long autonomous runs; general is for non-coding chat. Pick one with --profile or VG_COMPRESS_PROFILE.


Turning it on

You wantRun
Route an agent durably and start the listener — the one-command setupvg install <agent> --compress
Route an agent's home config insteadvg install <agent> --compress --compress-scope user
One session, nothing written (the listener is started for you)vg serve --compress <agent> [its flags]
The map and compression, one foreground processvg serve --compress
Compression only — no code map, nothing to buildvg serve --compress-only
Start (or reuse) the listener in the background and returnvg serve --compress --background
Stop the background listenervg serve stop
Put an agent's config backvg uninstall <agent>
See what is routed right nowvg serve status

vg install <agent> --compress writes the agent's own base-URL config, then makes sure the listener it points at is running — it starts vg serve --compress --background or reuses the healthy one already on the port. For Claude Code it also adds a SessionStart hook to the same .claude/settings.json that restarts the listener when a new session finds it gone, so the routing survives a reboot. The hook needs vg on your PATH; with npx it is skipped and the install says so.

Agents --compress can route: claude, codex, cursor, aider, copilot-cli, opencode, cline, continue, goose, openhands, gemini, qwen, kimi, grok, crush, amp, factory, kiro, vibe, zcode, vscode. Agents that read a config file (Codex, Claude Code, Droid, OpenCode, Crush, Continue, Goose) get an atomic edit with a backup beside it, a marker recording exactly which fields changed, and an owner file so two concurrent sessions never undo each other. Agents that only read an environment variable have nothing to write durably — run them for one session with vg serve --compress <agent>; the install reports that rather than silently skipping.

The listener speaks the Anthropic Messages API, OpenAI Chat Completions and Responses, streaming and non-streaming. Every response carries x-vg-tokens-before, x-vg-tokens-after, x-vg-tokens-saved, x-vg-usd-saved and x-vg-transforms headers so a client can see what happened to its request. If a healthy listener is already on the port, a second vg serve --compress attaches to it instead of failing.


Seeing what it saved

vg savings                  # both sections: code-map queries and compression
vg savings --compression    # only compression: requests, tokens, dollars, by model / client / project
vg savings --benchmark      # measure the compressors offline on built-in fixtures
vg show savings --open      # the live page in your browser

The ledger holds numbers only — never message content — and nothing leaves your machine. See vg savings.


Memory and learned guardrails

Two smaller capabilities live on the same verbs, because they write the same things:

  • vg serve memory — project-scoped memory shared across your agents: facts, preferences, rules, decisions, gotchas, commands and snippets, with an evidence count so a rule is promoted only after it has been seen enough times. vg serve --memory injects the top matches into each request and exposes memory_search / memory_save to the model. Scopes are project, user and global; storage is a local file per scope, searched with a lexical ranker — no model, works offline.
  • vg install <agent> --learn — scans the assistant's past session logs for this project, detects loops, repeated errors and missing-context patterns, and writes a short guardrails block into its instructions file (preview by default; --apply writes). See vg install.

Settings

Everything beyond the flags above is a setting rather than a flag — around 130 VG_* variables covering upstream URLs, spend caps, rate limits, output shaping, logging and per-subsystem tuning. Read them with vg serve config and change one with vg serve config set KEY VALUE; hot knobs take effect on the next request. Precedence: flag → environment → settings.json → profile default. The most-used ones:

VariableDefaultWhat it does
VG_COMPRESStrueMaster switch (off = passthrough)
VG_COMPRESS_MODEcachecache or token
VG_COMPRESS_PROFILEcodingSavings profile
VG_COMPRESS_PROTECT_RECENT3Keep code intact in the last N messages
VG_COMPRESS_PROTECT_READStrueFile reads stay byte-exact
VG_COMPRESS_EXCLUDE_TOOLSTool names skipped entirely
VG_COMPRESS_DEADLINE_MS2000Per-request budget; over it, the original is forwarded
VG_CCR_TTL_SECONDS1800How long an original stays retrievable
VG_PROXY_HOST / VG_PROXY_PORT127.0.0.1 / 8787Where the listener binds
VG_PROXY_BUDGET / VG_PROXY_RPM / VG_PROXY_TPM0Spend and rate limits (0 = off)
VG_MEMORYfalseMemory injection

Privacy

Everything runs on your machine. The listener binds to loopback unless you pass a token, forwards your provider credentials untouched, strips its own headers before forwarding, never logs message bodies unless you ask, and redacts secret shapes before anything — stored originals, ledgers, logs, memory — is written to disk. Files are created 0600. There is no beacon and no update check unless you opt in, and DO_NOT_TRACK / VIBGRATE_TELEMETRY=0 win over any opt-in.


In your own code

The same pipeline is available programmatically from @vibgrate/cli, offline, with no listener running: compress() over a message array, withCompression() for the Anthropic and OpenAI SDK client shapes, and compressionMiddleware() for the Vercel AI SDK. See the programmatic API.


Related

  • vg serve — the runtime the listener lives in, and the serve compress / retrieve / memory / config subcommands
  • vg install — routing an agent, and --learn
  • vg savings — the report, the benchmark, and the live page
  • vg doctor — listener state, store size, routed agents, invalid settings
  • Token-savings benchmarks — the measured code-map comparison

Example use cases

Expand any use case to watch a live replay of the real @vibgrate/cli running against one of our test repositories. Nothing executes in your browser — these are recordings of actual runs, with the scan time shown as of now.

vg savings --benchmark

Related Documentation

Vibgrate CLI

See a real scan run

A replay of the actual CLI running against our test repositories — live progress, real findings, a genuine DriftScore. Nothing executes in your browser.

Replay
demo@vibgrate — bash
npx @vibgrate/cli scan
 
╭──────────────────────────────────────────╮
Vibgrate Drift Report
╰──────────────────────────────────────────╯
 
── node-turborepo (node) .
Runtime: >=18.0.0 (6 majors behind)
Frameworks:
Turbo: 1.13.4 → 2.10.12 (1 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Dependencies:
1 current 1 1-behind 3 2+ behind 1 unknown
 
── @repo/admin (node) apps/admin
Frameworks:
TanStack Query: 5.102.8 → 5.102.8 (current)
React: 18.3.1 → 19.3.0 (1 behind)
React DOM: 18.3.1 → 19.3.0 (1 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Vite: 5.4.21 → 8.3.0 (3 behind)
Dependencies:
3 current 9 1-behind 3 2+ behind 4 unknown
 
── @repo/api (node) apps/api
Frameworks:
Express: 4.22.2 → 5.2.1 (1 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Vitest: 1.6.1 → 5.0.0 (4 behind)
Dependencies:
7 current 5 1-behind 3 2+ behind 4 unknown
 
── @repo/web (node) apps/web
Frameworks:
Next.js: 14.2.35 → 16.3.4 (2 behind)
React: 18.3.1 → 19.3.0 (1 behind)
React DOM: 18.3.1 → 19.3.0 (1 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Dependencies:
2 current 6 1-behind 3 2+ behind 5 unknown
 
── @repo/config (node) packages/config
Frameworks:
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Dependencies:
2 current 2 1-behind 5 2+ behind 0 unknown
 
── @repo/database (node) packages/database
Frameworks:
Prisma: 5.22.0 → 7.10.0 (2 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Dependencies:
1 current 0 1-behind 3 2+ behind 1 unknown
 
── @repo/types (node) packages/types
Frameworks:
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Dependencies:
0 current 0 1-behind 1 2+ behind 1 unknown
 
── @repo/ui (node) packages/ui
Frameworks:
React: 18.3.1 → 19.3.0 (1 behind)
TypeScript: 5.9.3 → 7.0.2 (2 behind)
React: 18.3.1 → 19.3.0 (1 behind)
Dependencies:
1 current 4 1-behind 1 2+ behind 1 unknown
 
── @repo/utils (node) packages/utils
Frameworks:
TypeScript: 5.9.3 → 7.0.2 (2 behind)
Vitest: 1.6.1 → 5.0.0 (4 behind)
Dependencies:
0 current 1 1-behind 2 2+ behind 1 unknown
 
Tech Stack
Frontend: React, React DOM
Meta-frameworks: Next.js
Bundlers: tsx, Turbo, Vite
CSS / UI: Autoprefixer, PostCSS, Tailwind CSS
Backend: Express
ORM / Database: Prisma, Prisma Client
Testing: Vitest
Lint & Format: ESLint, ESLint Prettier, ESLint React, Prettier, typescript-eslint
 
TypeScript
v5.3.3 · strict ✔ · MIXED · target: ES2022
 
Build & Deploy
Package Managers: pnpm
Monorepo: npm-workspaces, pnpm-workspaces, turbo
 
Product Purpose Signals
Frameworks: react, nextjs
Evidence: 177
Top Signals:
- [heading] Dashboard (apps/admin/src/pages/Dashboard.tsx)
- [title] Revenue Overview (apps/admin/src/pages/Dashboard.tsx)
- [copy] workspace:* (packages/ui/package.json)
- [copy] ./dist (packages/ui/tsconfig.json)
- [copy] ./src/index.ts (packages/ui/package.json)
- [copy] @repo/config/tsconfig-base.json (packages/ui/tsconfig.json)
- [copy] @repo/ui (packages/ui/package.json)
- [copy] #3b82f6 (apps/admin/src/pages/Dashboard.tsx)
Unknowns:
- No pricing or billing evidence found.
- No integrations/connectors evidence found.
- No route structure evidence found.
 
Security Posture
Lockfile ✖ · .env ✔ · node_modules ✔
 
Platform
Native modules: turbo
 
Code Quality
Files: 36 · Functions: 183 · Avg complexity: 2.62 · Avg length: 21.13 lines
Max nesting: 2 · Circular deps: 0 · Dead code: 0%
God files: apps/admin/src/pages/Products (448 lines)
 
Database Schema
postgresql · 8 models · 1 enum
Models: Address, CartItem, Category, Order, OrderItem (+3 more)
 
Findings (16 errors, 11 warnings)
Node.js runtime ">=18.0.0" reached end-of-life on 2025-04-30 (latest: 24.0.0).
vibgrate/runtime-eol in .
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in .
60% of dependencies are 2+ major versions behind in node-turborepo.
vibgrate/dependency-rot in .
@types/node is 6 major versions behind (spec: ^20.11.0, latest: 26.5.1).
vibgrate/dependency-major-lag in .
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in apps/admin
Vite is 3 major versions behind (current: 5.4.21, latest: 8.3.0).
vibgrate/framework-major-lag in apps/admin
vite is 3 major versions behind (spec: ^5.0.12, latest: 8.3.0).
vibgrate/dependency-major-lag in apps/admin
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in apps/api
Vitest is 4 major versions behind (current: 1.6.1, latest: 5.0.0).
vibgrate/framework-major-lag in apps/api
@types/node is 6 major versions behind (spec: ^20.11.0, latest: 26.5.1).
vibgrate/dependency-major-lag in apps/api
vitest is 4 major versions behind (spec: ^1.2.1, latest: 5.0.0).
vibgrate/dependency-major-lag in apps/api
Next.js is 2 major versions behind (current: 14.2.35, latest: 16.3.4).
vibgrate/framework-major-lag in apps/web
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in apps/web
@types/node is 6 major versions behind (spec: ^20.11.0, latest: 26.5.1).
vibgrate/dependency-major-lag in apps/web
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in packages/config
56% of dependencies are 2+ major versions behind in @repo/config.
vibgrate/dependency-rot in packages/config
eslint-plugin-react-hooks is 3 major versions behind (spec: ^4.6.0, latest: 7.1.1).
vibgrate/dependency-major-lag in packages/config
Prisma is 2 major versions behind (current: 5.22.0, latest: 7.10.0).
vibgrate/framework-major-lag in packages/database
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in packages/database
75% of dependencies are 2+ major versions behind in @repo/database.
vibgrate/dependency-rot in packages/database
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in packages/types
100% of dependencies are 2+ major versions behind in @repo/types.
vibgrate/dependency-rot in packages/types
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in packages/ui
TypeScript is 2 major versions behind (current: 5.9.3, latest: 7.0.2).
vibgrate/framework-major-lag in packages/utils
Vitest is 4 major versions behind (current: 1.6.1, latest: 5.0.0).
vibgrate/framework-major-lag in packages/utils
67% of dependencies are 2+ major versions behind in @repo/utils.
vibgrate/dependency-rot in packages/utils
vitest is 4 major versions behind (spec: ^1.2.1, latest: 5.0.0).
vibgrate/dependency-major-lag in packages/utils
 
╭──────────────────────────────────────────╮
Top Priority Actions
╰──────────────────────────────────────────╯
 
1. Upgrade EOL runtime in node-turborepo
End-of-life runtimes no longer receive security patches and block ecosystem upgrades.
./.
>=18.0.0 → 24.0.0 (6 majors behind)
Impact: −10 drift points (runtime & EOL)
 
2. Fix security posture: no lockfile found
Without a lockfile, installs are non-deterministic. Run the install command to generate one and commit it.
./
Missing: package-lock.json, pnpm-lock.yaml, or yarn.lock
 
3. Upgrade Vitest 1.6.1 → 5.0.0 in @repo/api (+2 more)
4 major versions behind. Major framework drift increases breaking change risk and blocks access to security fixes and performance improvements.
./apps/api
Vitest: 1.6.1 → 5.0.0 (4 majors behind)
./packages/utils
Vitest: 1.6.1 → 5.0.0 (4 majors behind)
./apps/admin
Vite: 5.4.21 → 8.3.0 (3 majors behind)
Impact: −5–15 drift points
 
4. Reduce dependency rot in @repo/types (100% severely outdated)
1 of 1 dependencies are 2+ majors behind. Run `npm outdated` and prioritise packages with known CVEs or breaking API changes.
./packages/types
typescript: 5.9.3 → 7.0.2 (2 majors behind)
Impact: −5–10 drift points
 
5. Reduce dependency rot in @repo/database (75% severely outdated)
3 of 4 dependencies are 2+ majors behind. Run `npm outdated` and prioritise packages with known CVEs or breaking API changes.
./packages/database
@prisma/client: 5.22.0 → 7.10.0 (2 majors behind)
prisma: 5.22.0 → 7.10.0 (2 majors behind)
typescript: 5.9.3 → 7.0.2 (2 majors behind)
Impact: −5–10 drift points
 
╭──────────────────────────────────────────╮
Architecture Layers
╰──────────────────────────────────────────╯
 
Archetype: nextjs (80% confidence)
Files classified: 24 (11 unclassified)
Folders classified: 8
apps/admin/src presentation 100% 4 files
apps/admin/src/pages presentation 100% 2 files
apps/api/src/middleware middleware 100% 2 files
apps/api/src/routes routing 100% 2 files
apps/web/src/app presentation 100% 4 files
apps/web/src/app/products presentation 100% 2 files
apps/web/src/app/products/[id] presentation 100% 1 file
packages/ui/src presentation 100% 6 files
Unclassified source (sample): 11
 
presentation 15 files drift ████████████████████ 100 risk high
routing 4 files drift ████████████████████ 100 risk high
middleware 2 files drift ███████▍░░░░░░░░░░░░ 37 risk moderate
config 2 files drift ░░░░░░░░░░░░░░░░░░░░ 0 risk none
shared 1 file drift ████████████████████ 100 risk high
 
╭──────────────────────────────────────────╮
DriftScore Summary
╰──────────────────────────────────────────╯
 
DriftScore: 70/100
Risk Level: HIGH
Projects: 9
Classified: 8 nano · 1 micro · 0 small · 0 standard
Billable: 0.42 · 9 detected → 0.42 billable projects (micro-project pricing)
0.1 micro · 0.32 nano
These fractions add up across repositories, then round down to whole billable projects.
 
Score Breakdown
Runtime: ████████████████████ 100
Frameworks: ███████████▊░░░░░░░░ 59
Dependencies: ██████▌░░░░░░░░░░░░░ 33
EOL Risk: ████████████████████ 100
 
Scanned at 2026-09-11T17:01:34.639Z · 7.9s · 286 files scanned · 56 workspace files · 27 dirs
Press Run to start.