Token Optimizer

io.github.omkar9854由 GitHub 帳號 omkar9854 發布v0.1.0更新於 Oct 24, 2026

Reversible context compression for AI agents: cut token usage up to 94%, originals retrievable

已驗證STDIO僅桌面開發者工具AI 與機器學習

概覽

AI 產生的概覽

一個本機 MCP 伺服器,在上下文(工具輸出、日誌、JSON、程式碼、文字)送進大型語言模型前先行壓縮,並可取回逐位元組一致的原文。

可以做什麼
在上下文送進模型前壓縮,依 JSON、日誌、程式碼與文字分別採用無損或可逆轉換以降低 token 用量。透過引用標記按需取回任何壓縮內容的逐位元組原文。錯誤與警告內容原樣保留,系統提示與最近訊息不會被修改。原文可存放於記憶體、SQLite 或自備的加密儲存。
適用情境
適合代理程式讀取大量工具輸出、日誌、JSON 資料或原始碼,且 token 成本或上下文長度成為問題時使用,尤其適用於禁止對外連線或需交付客戶環境的情境。
執行需求
透過 uvx 從 PyPI 套件 slimctx 以本機程序執行(Python,僅用標準函式庫)。README 的 MCP 範例需指定一個 SQLite 儲存路徑。未宣告帳號、API 金鑰、環境變數或請求標頭。
安裝前請注意
部分內容類型的壓縮是有損的,回答品質取決於模型是否按需取回原文。原文保存在本機儲存中,除非提供加密器,否則為明文 SQLite。README 說明其功能少於所比較的 Headroom 專案。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 Token Optimizer,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

slimctx — the token optimizer for AI agents

[CI] [PyPI] [MCP Registry] [License: Apache 2.0] [Python 3.9+] [Dependencies: zero]

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.

[slimctx demo: 61,700 tokens compressed to 298 in 29ms, FATAL lines preserved, byte-exact retrieval]
Live output of python3 benchmarks/demo.py — run it yourself, nothing is staged.

python
from slimctx import Pipeline, Config
pipe = Pipeline(Config(target_tokens=32_000))result = pipe.compress(messages)        # OpenAI/Anthropic-style dictsprint(result.savings_ratio)             # e.g. 0.82
original = pipe.retrieve("a1b2c3d4...")  # byte-exact original, any time

Results (synthetic workloads modeled on real agent traffic)

WorkloadBeforeAfterSavingsKey facts kept
Code search (100 results)5,55791684%✓
SRE incident debugging61,699298100%✓
GitHub issue triage12,83697592%✓
Codebase exploration5,7342,76052%✓

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

  1. 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.
  2. Errors are never dropped. Every compressor pins error/warning content: log errors pass verbatim, salient JSON rows are kept, salient sentences outrank filler.
  3. Deterministic output. Same input → byte-identical output, across runs and processes. Compressed prefixes stay stable, so provider prompt-caches (Anthropic/OpenAI) keep hitting.
  4. 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:

Headroomslimctx
ReversibilityJSON only (CCR); dropped text is goneevery lossy transform
Log handlinggeneric text scoringtemplate mining ([x1432] collapse)
Code handlingAST skeletonAST skeleton + query-relevant bodies kept
DependenciesRust core, ONNX runtime, 261MB HF modelstdlib only
Network egressHuggingFace pull on first runnone, ever
Store encryptionnone (plaintext SQLite)cipher hook (bring your own)
Determinismcache-aligner componentby 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

bash
pip install slimctx           # from PyPI — or vendor the slimctx/ directorypython -m pytest tests/ -q    # 26 tests: invariants, not examplespython3 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:

bash
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:

python
from cryptography.fernet import Fernet          # optional, your choicef = 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.

來源:README.md,提交 f2cc938

工具

0
SourceWeft 尚未在沙箱中啟動過該軟體包,因此其工具未知。

版本歷史

1
  1. v0.1.0最新Sep 16, 2026

評分

已滿足適用檢查項的 50%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分

    請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。

  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具15 分

    請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。

  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分