Inspiration

Running an autonomous coding agent through a long task gets expensive fast because of how tool loops work.

On every turn, the agent resends its entire conversation history: file reads, compiler errors, test logs, and reasoning blocks. By turn 20 or 30, a single-line bug fix ends up sending 80,000 tokens over the wire. You hit rate limits, context windows fill up, latency spikes, and API bills pile up.

The common workarounds are bad. Clearing context wipes the agent's working memory, which makes it forget what it just built. Blind truncation chops off relevant file paths and instructions. Remote proxy services ask you to send private code and API keys to third-party servers.

I built CMX to act like an operating system page cache for agent context. It runs on 127.0.0.1, cuts out redundant data the model no longer needs, protects active edits, and keeps keys and code on your local disk.

What it does

CMX is a local HTTP proxy (127.0.0.1:17322) that sits between your coding agent and model providers like OpenAI, Anthropic, Gemini, DeepSeek, Groq, and Ollama. It catches outgoing requests, compresses the context history, and forwards the cleaned payload.

Here is how it works:

Zero-Cost Mode by default: It runs mechanical compression passes without spending money on secondary model calls: Superseded tool pruning: If an agent reads a file, edits it, and reads it again, CMX swaps the older read with a short reference pointer and stores the original in a local vault. Aging tool compaction: Outputs older than three turns are compacted down to structural symbols (functions, structs, classes) in Go, Python, TypeScript, and Rust, instead of keeping full raw files. Algorithmic checkpointing: Builds a chronological timeline of user requests and tool outcomes once context passes threshold limits. Deduplication and cleanup: Drops duplicate tool calls, removes old resolved compiler errors, clamps large terminal outputs, and strips historical reasoning blocks. Optional model-directed summarization: For deep context over 8,000 tokens, you can turn on summarization using cheap flash models like Gemini Flash or GPT-4o-mini. These runs have a 15-second timeout, single-flight locking, and spend caps to ensure the summarizer never costs more than it saves. Protected tools: Tools that change files (write_to_file, replace_file_content, apply_patch), subagent tasks, and active goals are never compressed. Prompt cache protection: Upstream providers offer up to 90% discounts for cached prompt prefixes. If your prompt cache hit rate is above 60%, CMX locks the prefix and root message so it does not invalidate your KV cache. Local content vault: Every compressed snippet is saved to a local SQLite database. You can inspect any original payload with cmx vault show . Harness configuration: Configures Codex, OpenCode, Claude Code, Aider, Antigravity CLI, Cline, and Grok Build CLI with one command. Running cmx stop restores their original configuration files with zero leftover edits. Local privacy: Binds only to 127.0.0.1. API keys stay in memory and are never saved to disk. The optional dashboard syncs only daily token counters; prompts and code never leave your machine. How I built it Core proxy: Written in Go with CGO disabled. Compiles into a single static binary for Linux, macOS, and Windows. Storage: Embedded SQLite using modernc.org/sqlite with WAL mode and &_txlock=immediate to prevent database locks during parallel agent streaming. Protocol adapters: Built an internal message representation (internal/xmsg) with translation layers for OpenAI Chat Completions, OpenAI Responses API (with previous_response_id), Anthropic Messages, and Gemini. Unrecognized JSON fields pass through untouched via json.RawMessage. Terminal UI: Built with Bubble Tea and Lip Gloss to display real-time throughput, active harness connections, and token savings in the console. Dashboard: React, Vite, TypeScript, and Tailwind CSS running on Cloudflare Workers, Cloudflare Assets, and Cloudflare D1. Uses Ed25519 keypairs for local device pairing. Challenges I ran into The prompt caching trap: Compressing everything sounds smart until you factor in prompt caching. Anthropic and OpenAI heavily discount cached prefixes. If a proxy edits early messages on every turn, it breaks the cache and increases the total bill. CMX had to track cache hit rates and anchor early messages when caching is active. Agents without conversation headers: Several coding agents do not send conversation IDs in their HTTP headers. String matching on prompt text caused unrelated sessions to mix together. I wrote an assistant-history fingerprinting routine that isolates sessions by provider, client, and credentials. Compressing without breaking task logic: If an agent forgets which file it just modified or what error it just resolved, it starts looping. I had to draw a hard boundary: file modifications, active goals, and recent turns are protected. Only exploratory reads and resolved errors get compacted. Safe config management: Editing files like ~/.claude/settings.json or ~/.codex/config.toml can break a developer's setup if an agent crashes. CMX backs up original configs atomically and rolls them back cleanly when shut down. Accomplishments that I'm proud of Cuts context tokens by 60% to 80% on long multi-turn sessions without degrading task completion. Zero-Cost Mode saves tokens purely through deterministic AST extraction, deduplication, and checkpointing, with no secondary LLM costs. Native configuration support across seven major coding harnesses (Codex, OpenCode, Claude Code, Aider, Antigravity CLI, Cline, Grok Build CLI). Zero data collection: binds to loopback, keeps keys in memory, and never writes code or prompt text to an external server. Uses under 30 MB of RAM and boots in milliseconds. What I learned Most tokens in coding agent sessions are waste: repeated reads of unchanged files, long compiler stack traces that were already resolved, and stale thinking blocks. Stripping that waste preserves the model's focus. Secondary LLM summarization is often an economic trap. If you spend 1,500 tokens on a flash model to save 2,000 tokens on a frontier model, the latency and cost margin is too thin. Mechanical rules have to do the heavy lifting first. Local developer tools have to fail open. If a proxy hangs or corrupts an IDE configuration, developers remove it. Timeouts must be tight, fallbacks immediate, and client settings untouched if an error occurs. What's next for Compressius Maximus CMX Diff compaction: Fold multiple rounds of intermediate file edits into a single clean patch. Vault recall tool: Give agents an optional tool to search and pull raw logs from the local vault if they need details from an earlier turn. More agent support: Add setup adapters for Cursor, Windsurf, Continue, and VS Code Copilot. Team analytics: Build opt-in, aggregate-only dashboards for engineering teams to track token efficiency and cost avoidance across local machines. Supplementary Directory Assets Tagline / One-Liner Options Short (under 60 characters): Local context-compression proxy for AI coding agents. Extended (under 120 characters): Cut coding agent token costs by up to 80% with a local, privacy-first context compression proxy. Category and Tags Category: Developer Tools Tags: coding-agents, context-compression, llm-proxy, token-savings, claude-code, codex, opencode, aider, go, privacy Quick Specs Binary: Pure Go, single executable, zero CGO (amd64, arm64) Platforms: Linux, macOS, Windows Local Endpoint: http://127.0.0.1:17322 Supported Clients: Codex, OpenCode, Claude Code, Aider, Antigravity CLI, Cline, Grok Build CLI Supported Providers: OpenAI, Anthropic, Google Gemini, DeepSeek, Groq, Mistral, xAI, Ollama What changed Removed banned words, marketing buzzwords, and promotional verbs (fundamentally changed, delve, robust, streamline, leverage). Removed throat-clearing openings and decorative abstractions. Replaced them with concrete descriptions of tool loops and token counts. Eliminated all em dashes (—) and decorative bolding. Replaced vague claims with concrete mechanisms: AST symbol extraction, prompt cache KV discounts, loopback binding, and atomic configuration rollbacks. Simplified sentence structures from passive academic explanations into direct first-person engineering prose.

Built With

Share this project:

Updates