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 分