Token Compressor

io.github.base76-research-lab由 GitHub 帳號 base76-research-lab 發布v0.1.1更新於 Oct 24, 2026

Compress prompts 40-60% using local LLM + embedding validation. Preserves all conditionals.

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

概覽

AI 產生的概覽

一個本機 MCP 伺服器,在提示詞送進大型語言模型前先用本機 Ollama 模型壓縮,並以向量相似度驗證,減少 token 用量。

可以做什麼
用本機 Ollama 模型把提示詞改寫成語意最精簡的形式,保留條件語句與否定表達。以餘弦相似度比對原文與壓縮後文字,低於門檻時原樣回傳原文。回傳壓縮後文字與統計資訊,包含模式、覆蓋度以及輸入、輸出與節省的估算 token 數。對低於可設定 token 下限的短輸入略過壓縮。
適用情境
適合反覆把長提示詞送給大型語言模型、在意 token 成本或上下文長度的情況,且希望壓縮在本機完成並有防止語意流失的驗證。短提示詞會被略過,效益有限。
執行需求
以本機 Python 套件執行(需 Python 3.10 以上),可透過 uvx 或 python3 啟動。需要在本機執行 Ollama,並已拉取 llama3.2:1b 與 nomic-embed-text 兩個模型,另需 ollama 與 numpy 相依套件。未宣告帳號、API 金鑰或標頭。
安裝前請注意
提示詞會交給本機 Ollama 模型改寫與向量化,因此會離開 MCP 用戶端行程並由這些模型處理。壓縮仍可能改變措辭,相似度門檻是唯一保障,調低門檻會讓壓縮更激進。README 也建議設定 Claude Code 掛鉤,在每次送出提示詞時執行並寫入暫存檔案。

安裝

在 SourceWeft 中

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

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

其他 MCP 客戶端

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

README

token-compressor

Reduce LLM prompt tokens by 30–70% while preserving semantic meaning.

mcp-name: io.github.base76-research-lab/token-compressor

Semantic prompt compression for LLM workflows. Reduce token usage by 40–60% without losing meaning.

[License: MIT] [Requires: Ollama] [MCP Compatible]

Built by Base76 Research Lab — research into epistemic AI architecture.


Live demo

Intent Compiler MVP is now live and uses this project as part of the idea -> spec -> compressed output flow:


What it does

token-compressor is a two-stage pipeline that compresses prompts before they reach an LLM:

  1. LLM compression — a local model (llama3.2:1b via Ollama) rewrites the prompt to its semantic minimum, preserving all conditionals and negations
  2. Embedding validation — cosine similarity between original and compressed embeddings must exceed a threshold (default: 0.85) — if not, the original is sent unchanged

The result: shorter prompts, lower costs, same intent.

Input prompt (300 tokens)        ↓  LLM compresses        ↓  Embedding validates (cosine ≥ 0.85?)        ↓  Pass → compressed (120 tokens)   Fail → original (300 tokens)

Key design principle: conditionality is never sacrificed. If your prompt says "only do X if Y", that constraint survives compression.


Requirements

  • Python 3.10+
  • Ollama running locally
  • Two models pulled:
bash
ollama pull llama3.2:1bollama pull nomic-embed-text
  • Python dependencies:
bash
pip install ollama numpy

Quick start

python
from compressor import LLMCompressEmbedValidate
pipeline = LLMCompressEmbedValidate()result = pipeline.process("Your prompt text here...")
print(result.output_text)   # compressed (or original if validation failed)print(result.report())      # MODE / COVERAGE / TOKENS saved

Result object:

FieldDescription
output_textText to send to your LLM
modecompressed / raw_fallback / skipped
coverageCosine similarity (0.0–1.0)
tokens_inEstimated input tokens
tokens_outEstimated output tokens
tokens_savedDifference

CLI usage

bash
echo "Your long prompt here..." | python3 cli.py

Output: compressed text on stdout, stats on stderr.


Claude Code hook (recommended setup)

Add to your ~/.claude/settings.json under hooks → UserPromptSubmit:

json
{  "type": "command",  "command": "echo \"${CLAUDE_USER_PROMPT:-}\" | python3 /path/to/token-compressor/cli.py > /tmp/compressed_prompt.txt 2>/tmp/compress.log || true"}

This runs on every prompt submission and writes the compressed version to a temp file, which can be injected back into context via a second hook or MCP server.


MCP server

The MCP server exposes compression as a tool callable from Claude Code and any MCP-compatible client.

Install:

bash
pip install token-compressor-mcp

Tool: compress_prompt

  • Input: text (string)
  • Output: compressed text + stats footer

Claude Code MCP config (~/.claude/settings.json):

json
{  "mcpServers": {    "token-compressor": {      "command": "uvx",      "args": ["token-compressor-mcp"]    }  }}

Or from source:

json
{  "mcpServers": {    "token-compressor": {      "command": "python3",      "args": ["-m", "token_compressor_mcp"],      "cwd": "/path/to/token-compressor"    }  }}

Configuration

python
pipeline = LLMCompressEmbedValidate(    threshold=0.85,          # cosine similarity floor (lower = more aggressive)    min_tokens=80,           # skip pipeline below this (not worth compressing)    compress_model="llama3.2:1b",    embed_model="nomic-embed-text",)

How it works

Stage 1 — LLM compression

The compression prompt instructs the model to:

  • Preserve all conditionals (if, only if, unless, when, but only)
  • Preserve all negations
  • Remove filler, hedging, redundancy
  • Target 40–60% of original length

Stage 2 — Embedding validation

Computes cosine similarity between the original and compressed text using nomic-embed-text. If similarity falls below threshold, the original is returned unchanged. This prevents silent meaning loss.


Results

Tested across Swedish and English prompts, technical and natural language:

InputTokens inTokens outSaved
Research abstract (EN)893857%
Session intent (SV)321844%
Technical instruction472253%
Short command (<80t)——skipped

Research background

This tool implements the architecture from:

Wikström, B. (2026). When Alignment Reduces Uncertainty: Epistemic Variance Collapse and Its Implications for Metacognitive AI. DOI: 10.5281/zenodo.18731535

Part of the Base76 Research Lab toolchain for epistemic AI infrastructure.


License

MIT — Base76 Research Lab, Sweden

來源:README.md,提交 1f5e39e

工具

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

版本歷史

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

評分

已滿足適用檢查項的 50%

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

  • 近 30 天內可連通25 分

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

  • 已宣告認證方式10 分

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

  • 已列出工具15 分

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

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