slimctx-token-optimizer
Provides reversible context compression for AI agents, reducing token usage while preserving the ability to retrieve original content, and serves as an MCP server for integration with tools like GitHub Copilot and Claude Code.
README
slimctx — the token optimizer for AI agents
<!-- mcp-name: io.github.omkar9854/token_optimizer -->
Zero-dependency, fully-reversible context compression for AI agents.
slimctx compresses what your agent reads — tool outputs, logs, JSON, source files, prose — before it reaches the LLM. Same answers, fraction of the tokens. Pure Python stdlib: no ML models, no downloads, no network calls, ever. Auditable end to end in ~1,600 lines.
<p align="center"> <img src="demo.gif" alt="slimctx demo: 61,700 tokens compressed to 298 in 29ms, FATAL lines preserved, byte-exact retrieval" width="820"> <br><sub>Live output of <code>python3 benchmarks/demo.py</code> — run it yourself, nothing is staged.</sub> </p>
from slimctx import Pipeline, Config
pipe = Pipeline(Config(target_tokens=32_000))
result = pipe.compress(messages) # OpenAI/Anthropic-style dicts
print(result.savings_ratio) # e.g. 0.82
original = pipe.retrieve("a1b2c3d4...") # byte-exact original, any time
Results (synthetic workloads modeled on real agent traffic)
| Workload | Before | After | Savings | Key facts kept |
|---|---|---|---|---|
| Code search (100 results) | 5,557 | 916 | 84% | ✓ |
| SRE incident debugging | 61,699 | 298 | 100% | ✓ |
| GitHub issue triage | 12,836 | 975 | 92% | ✓ |
| Codebase exploration | 5,734 | 2,760 | 52% | ✓ |
Every run also verifies that each planted "needle" (the FIXME, the OOMKill,
the outlier) survives compression, and that every lossy transform is
byte-exact reversible. Reproduce with python3 benchmarks/bench.py.
How it works
messages ──► ContentRouter ──► one of:
├─ JSON : lossless tabularization (repeated keys → header,
│ constant columns → legend), then relevance-ranked
│ row selection only if still over budget
├─ LOG : Drain-style template mining — repeated lines
│ collapse to `pattern [x1432]`; errors verbatim
├─ CODE : AST skeleton — signatures + docstrings kept,
│ bodies elided EXCEPT those relevant to the query
└─ TEXT : extractive sentence selection (BM25 + salience
+ position), verbatim, never paraphrased
The four guarantees
- Universal reversibility. Before any lossy transform, the original
goes into a content-addressed store (memory / SQLite / bring-your-own
cipher) and the output carries a
[slimctx-ref <hash> ...]marker. The model — or you — can always get the byte-exact original back. - Errors are never dropped. Every compressor pins error/warning content: log errors pass verbatim, salient JSON rows are kept, salient sentences outrank filler.
- Deterministic output. Same input → byte-identical output, across runs and processes. Compressed prefixes stay stable, so provider prompt-caches (Anthropic/OpenAI) keep hitting.
- Net gain or no-op. If a transform doesn't save enough tokens to pay for its marker, the original is kept untouched. The live zone (system prompt + last N messages) is never modified at all.
Why not just use Headroom?
Headroom is the established project in this space and is more featureful today (provider proxy with SSE streaming, agent wrappers, cross-agent memory, an ML compression model). slimctx makes a different set of trade-offs, aimed at locked-down / client-site deployments:
| Headroom | slimctx | |
|---|---|---|
| Reversibility | JSON only (CCR); dropped text is gone | every lossy transform |
| Log handling | generic text scoring | template mining ([x1432] collapse) |
| Code handling | AST skeleton | AST skeleton + query-relevant bodies kept |
| Dependencies | Rust core, ONNX runtime, 261MB HF model | stdlib only |
| Network egress | HuggingFace pull on first run | none, ever |
| Store encryption | none (plaintext SQLite) | cipher hook (bring your own) |
| Determinism | cache-aligner component | by construction (pure functions + memo) |
| Audit surface | ~10s of KLOC across 3 languages | ~1,200 lines of Python |
If you need the proxy/wrap ecosystem, use Headroom. If you need something you can read in an afternoon, run air-gapped, and certify for a client environment, use slimctx.
Install / test
pip install slimctx # from PyPI — or vendor the slimctx/ directory
python -m pytest tests/ -q # 26 tests: invariants, not examples
python3 benchmarks/bench.py # reproduce the numbers above
Integration sketches
As a library (any framework): call pipe.compress(messages) right
before your provider SDK call; expose pipe.retrieve as a tool named
retrieve so the model can pull originals.
As an MCP server (GitHub Copilot, Claude Code, Cursor, ...): ships built in, stdlib-only:
python3 -m slimctx.mcp_server --db ~/.slimctx/store.db
See USAGE.md for the GitHub Copilot (.vscode/mcp.json) setup
and a security deployment checklist.
Encrypted store:
from cryptography.fernet import Fernet # optional, your choice
f = Fernet(key)
store = SqliteStore("ccr.db", cipher=(f.encrypt, f.decrypt))
pipe = Pipeline(store=store)
License
Apache-2.0. Original implementation — no code derived from Headroom.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。