Toknife Lite — 輸入端 Token 壓縮工具(開源版)
Toknife Lite — Input-side Token Compression (Open Source)
> 中文 | English
🇹🇼 中文
這是什麼
Toknife Lite 在請求送達 LLM 前壓縮輸入(messages),降低 token 用量。本機運行、資料不外送,核心零第三方依賴。
能力:
1. JSON 數值保真壓縮:JSON→CSV 去重複 key,數值精度保留。
2. Code-aware 代碼壓縮:識別代碼邊界,只精簡註釋/空白/重複行,不破壞語法。
3. 對話歷史與工具輸出聚合:多輪歷史溢出才收斂、工具 schema/列表結構化瘦身、重複機器行摺疊。
4. 通用 OpenAI 相容透明代理:可指向 OpenAI/DeepSeek/本機 vLLM、Ollama 等,透傳客戶端 model,支援 SSE 串流原樣穿透(不修改輸出)。
> 邊界:Lite 只做輸入端、固定規則、長文會截斷。輸出端最佳化、長文 100% 可逆、自學習閉環、硬預算與視覺儀表板、一鍵 GUI 為 Pro 功能。
快速開始(代理)
# 上游用環境變數指定(也可用 --upstream/--api-key/--model)
export OPENAI_BASE_URL=https://api.openai.com/v1
export OPENAI_API_KEY=sk-...
python scripts/ts_proxy_server.py --port 8001
# 相容舊式:python scripts/ts_proxy_server.py 8001
把客戶端 base_url 指向 http://127.0.0.1:8001/v1 即可。
- GET /health:就緒與版本(不打上游)
- GET /stats:本次啟動以來的輸入端累計節省(JSON)
實測怎麼看(誠實口徑)
python scripts/benchmark.py # 離線、只計輸入端、逐場景、不取總平均
成效隨內容類型差異很大:代碼、密集日誌、重複資料列省幅高;短文本與一般散文會自動跳過。這是離線輸入端對比,不是帳單承諾,真實節省取決於內容、模型與 prompt cache。
環境要求
- Python 3.8+,跨平台;核心純標準庫,
tiktoken可選(不裝自動降級估算)。
許可證與致謝
MIT,可商用、需保留版權聲明。Toknife 受開源社群專案啟發(Caveman、Ponytail、RTK),特此致謝。
作者 momogigi123 — https://github.com/momogigi123/toknife
🇬🇧 English
What it is
Toknife Lite compresses the input (messages) before they reach an LLM, cutting token use. It runs locally, sends nothing to third parties, and its core uses the standard library only.
Capabilities:
1. Lossless-numeric JSON compression (JSON→CSV, repeated keys removed, numbers preserved).
2. Code-aware compression (comments/whitespace/duplicate lines trimmed, syntax intact).
3. Conversation-history and tool-output aggregation (overflow-only history compaction, tool schema slimming, repeated machine-line folding).
4. Generic OpenAI-compatible transparent proxy for OpenAI / DeepSeek / local vLLM, Ollama, etc.; passes the client model through and relays SSE streaming untouched (it never edits output).
> Boundary: Lite is input-only, fixed-rule, and truncates very long text. Output-side optimization, 100%-reversible long context, a self-learning loop, hard budget caps, a visual dashboard and the one-click GUI are Pro features.
Quick start (proxy)
export OPENAI_BASE_URL=https://api.openai.com/v1
export OPENAI_API_KEY=sk-...
python scripts/ts_proxy_server.py --port 8001
Point your client's base_url to http://127.0.0.1:8001/v1. Endpoints: GET /health, GET /stats (cumulative input savings).
Benchmarks (honest scope)
python scripts/benchmark.py — offline, input-only, per-scene, no cross-scene average. Gains vary a lot by content: code, dense logs and repeated rows compress well; short text and prose are skipped. It is an offline input comparison, not a billing promise.
Requirements
Python 3.8+, cross-platform; standard-library core, optional tiktoken.
License & acknowledgments
MIT (commercial use allowed, keep the notice). Inspired by open-source projects Caveman, Ponytail and RTK — thank you.
By momogigi123 — https://github.com/momogigi123/toknife
取得 Pro(付費訂閱)
商業 Pro 版為僅訂閱制(無終身買斷),於 Lemon Squeezy 銷售:
- Pro:每月 US$5 或每年 US$39
- Team:每年 US$149(最多 5 裝置)
- 付費方案皆附 7 天免費試用
開賣商店:https://toknife.lemonsqueezy.com |定價:https://momogigi123.github.io/toknife/#pricing
Get Pro (paid subscription)
The commercial Pro edition is subscription-only (no lifetime plan), sold on Lemon Squeezy:
- Pro: US$5/month or US$39/year
- Team: US$149/year for up to 5 devices
- Every paid plan includes a 7-day free trial
Store: https://toknife.lemonsqueezy.com · Pricing: https://momogigi123.github.io/toknife/#pricing