distil

io.github.munhq由 GitHub 帳號 munhq 發布v0.3.4更新於 Oct 28, 2026

Context optimization for LLM agents: tool registry, result masking, budgeting, compaction.

已驗證STDIO僅桌面資料與分析AI 與機器學習

概覽

AI 產生的概覽

本機 MCP 伺服器,用來最佳化 LLM 代理的上下文:工具登錄表、結果遮蔽、預算與壓縮層,並可量測 token 與快取成本。

可以做什麼
  • 透過快取對齊、暫存板、遮蔽、摘要、壓縮與預算等層最佳化代理上下文。
  • 以登錄表層與程式碼模式層註冊並路由工具。
  • 逐層回報最佳化前後的 token 數與明細。
  • 在本機 Claude Code 工作階段紀錄上量測上下文成本,包含清除工具結果的損益兩平表。
  • 匯出工作階段並對本機模型執行保留度探測。
適用情境
適合需要量測或降低長工作階段中上下文與提示快取成本,或想實驗上下文分層與工具登錄表的情況。README 本身指出多數改寫歷史的層得不償失,並建議優先採用供應商原生功能,因此它較適合評估與處理上下文溢出,而非日常使用。
執行需求
以本機 stdio 程序執行,通常透過 npx 從 npm 套件啟動,因此需要 Node.js;沒有 Node 的使用者可用預先建置的執行檔與 bash 安裝腳本。不需帳號、API 金鑰或設定。Windows 上執行腳本需要 Git Bash、MSYS2 或 WSL。選用的基準測試與探測功能需要本機工作階段紀錄,探測另需模型端點。
安裝前請注意
改寫歷史的層(遮蔽、摘要、壓縮)會使快取前綴失效,可能反而增加成本,改動上下文並非沒有代價。基準測試會讀取本機 Claude Code 工作階段紀錄,其中包含你的工作階段資料;匯出工作階段會把它們寫入磁碟。探測評分會把工作階段內容傳送到你設定的模型端點。

安裝

在 SourceWeft 中

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

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

其他 MCP 客戶端

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

README

[distil]

[npm] [MCP Registry] [Smithery] [license]

[Install in Cursor] [Install in VS Code]

claude mcp add distil -- npx -y @munhq/distil

No account, no API key, nothing to configure. The package is a small wrapper that fetches the binary for your platform and verifies it against the published checksums; install.sh and a prebuilt binary remain for anyone without Node.


Cache writes are 2% of your tokens and 28% of your bill

That is not a claim, it is a measurement: 13,814 real agent sessions, 410,742 assistant turns, re-run on 2026-08-29. Every unique token in them was billed 550 times, because a request resends the whole history.

Which means the obvious move — compress the history — is usually the wrong one. Editing history invalidates the cached prefix from the edit onwards and converts reads at 0.1x into writes at 1.25x or 2.0x. You cannot compress your way out of context cost. You can only decline to admit tokens.

[Where an agent session's tokens go: tool results 59.9%, tool calls 17.9%, user text 14.0%, assistant text 7.2%, thinking 1.0%] [How much a rewrite must delete just to break even: 8% with one turn left, 46% with ten, 90% with a hundred]

Every tool in this space publishes a savings percentage measured on its own fixtures. What none publishes is the denominator: what share of a real session it is allowed to touch, and what that share costs once prompt-cache pricing is applied. distil measures both on transcripts an agent actually wrote.

What is prior art, and what is not. The cache arithmetic below is not a discovery. Anthropic's context editing docs state that clearing tool results invalidates the cached prefix, and ship clear_at_least so a clear only fires when it is large enough to pay for that. The break-even rule is published too: on a 5-minute cache, cleared tokens times requests-before-the-next-clear must exceed 11.5 times the tokens you keep. The table in this README reproduces that rule exactly — it was derived independently, which is a check on the arithmetic, not a contribution.

The gap is empirical. Every source says to calibrate against your own workload, and none ships a way to do it or publishes what the values turn out to be. That is what this crate is for: measuring the numbers you need in order to choose clear_at_least, or to decide not to clear at all.

The full measurement

Measured 2026-08-29 over 13,814 local Claude Code transcripts — 410,742 assistant turns, 212.7M tokens of unique text. Reproduce it on your own corpus with distil-bench ~/.claude/projects; a corpus grows, so the date matters more than the decimals.

tokensshare
tool results127,371,43059.9%
tool calls38,065,12117.9%
user text29,818,53614.0%
assistant text15,402,0777.2%
thinking2,076,7161.0%

Those 212.7M unique tokens were billed as 116.9 billion input tokens — every token paid for 550 times, because a request resends the whole history.

Price that at real cache multipliers (read 0.1x, write 1.25x for the 5-minute TTL and 2.0x for the 1-hour):

share of tokensshare of cost
cache read97.9%71.8%
cache writes2.0%27.6%

Cache writes are 2% of the tokens and 28% of the bill. Editing history invalidates the cached prefix from the edit onwards, converting reads at 0.1x into writes at 1.25x or 2.0x. So a rewrite must shrink what it invalidates below:

turns remaining5m TTL1h TTL
18.0%5.0%
1046.5%34.5%
2063.5%51.3%
10089.7%84.0%

That table is the published break-even rule in another form: at every row, cleared x turns / kept equals 11.5 for the 5-minute tier. Use it to pick a clear_at_least value, and use distil-bench to find the turn count and tail size to put into it — those are workload properties, and they are the part nobody publishes.

Per unit of history, at 10 remaining turns: keeping it costs 1.00, compressing it costs 2.15, and never admitting it costs 0. You cannot compress your way out of context cost. You can only decline to admit tokens.

What that means for using this crate

Layers that do not touch history are on the right side of that arithmetic: CacheAlignLayer (orders content so the stable prefix stays cacheable) and ScratchpadLayer (keeps working state outside the window).

Layers that rewrite history — MaskingLayer, SummarizationLayer, CompactionLayer — cost more than they save in the common case. Reach for them at one boundary only: context overflow, where the alternative is a failed request and cache price stops being the comparison. BudgetLayer exists for exactly that moment.

RegistryLayer and CodeModeLayer predate Anthropic's Tool Search Tool and Programmatic Tool Calling, which do the same jobs natively and better. Prefer the native features.

For clearing old tool results, prefer the provider's clear_tool_uses context editing over MaskingLayer: it runs server-side, it takes clear_at_least, and it is one API parameter against a dependency. Reach for a layer here only when you need behaviour the API does not offer.

Measuring

bash
cargo build --features bench --release
# Where tokens are, what they cost, and the break-even table./target/release/distil-bench ~/.claude/projects --json baseline.json
# Sessions that called a given tool, against those that did not./target/release/distil-bench ~/.claude/projects --split-by-tool mcp__codeindex__
# Export real traffic so other compressors run on the same input./target/release/distil-bench ~/.claude/projects --export-sessions ./sessions --min-turns 40

See bench/README.md for the external-tool comparison, the fairness rules, and the two harness mistakes that produced wrong numbers first.

Retention

A saving is only a saving if the model can still answer what the original context could answer.

bash
# No LLM judge: file paths checked against ground truth from the transcriptpython bench/artifact_retention.py ./sessions 12
# LLM-graded probes (recall / artifact / continuation / decision)cargo build --features probe --release./target/release/distil-probe <session.jsonl> --probes 6 --model qwen2.5:3b

The probe taxonomy is Factory.ai's; their write-up defines it and ships no harness. The judge is a Completer, never a Summarizer — a summarizer may impose summarization framing, which rewrites both the probe format and the grading instruction.

Using it as a library

rust
use distil::{CacheAlignLayer, Ctx, EstimateCounter, Pipeline};
let pipeline = Pipeline::builder()    .counter(EstimateCounter)    .layer(CacheAlignLayer::generic())    .build();
let mut ctx = Ctx::new(messages, tools, turn);let result = pipeline.optimize(&mut ctx);println!("{result}");

This example is kept compilable as examples/readme_quickstart.rs — run it with cargo run --example readme_quickstart.

Every layer implements Layer and reports tokens_before, tokens_after and a detail line, so each one can be measured on its own.

Features

featurewhat it adds
corpustranscript loader (no extra dependencies)
benchdistil-bench, needs tiktoken
probedistil-probe, needs proxy for the HTTP judge
tiktokenaccurate BPE counts instead of the chars/3.5 estimate
proxydistil-proxy HTTP server
mcpdistil-mcp MCP server
metricsPrometheus /metrics

Install

./install.sh                       # binaries, the skill, and the MCP server/plugin marketplace add munhq/distil/plugin install distil             # Claude Code: skill and server in one step

install.sh installs both binaries, drops the skill into every Claude home it finds, and registers the MCP server at user scope. When the plugin is already installed it installs the binary only, since the plugin declares the server and ships the skill itself.

The plugin launches the server with npx -y @munhq/distil, so it needs Node. It cannot use a plugin-relative path: Claude Code expands ${CLAUDE_PLUGIN_ROOT} and nothing else does, so a plugin declaring one hands every other client a literal path that does not exist. install.sh and the prebuilt binaries remain for anyone without Node.

Platform support

platformbinariesscripts
Linux x86_64 / arm64released, testedyes
macOS x86_64 / arm64released, built in CIyes
Windows x86_64 / arm64released, built in CIneeds a shell: Git Bash, MSYS2 or WSL

The release publishes six targets and plugin/test_platform.sh holds both the installer and the plugin launcher to that matrix, so an asset name and the name asked for cannot drift apart. install.sh and the launcher are bash scripts, so on Windows they need a shell — cmd and PowerShell cannot run them. Linux binaries are static musl builds, so they do not need a matching glibc.

Caveats

The corpus is one developer's machine. The ratios are the finding; the absolute totals are personal. Counts use cl100k_base, which approximates Claude's tokenizer within a few percent. The break-even model assumes a single cache breakpoint, so a rewrite confined to the tail costs less than the table shows — that refines it, it does not reverse it.

Contributing

Build and test instructions, the rules a benchmark change has to follow, and what a pull request needs before review: CONTRIBUTING.md. Report a vulnerability privately — SECURITY.md.

License

MIT. See LICENSE.

來源:README.md,提交 3d3f228

工具

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

版本歷史

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

評分

已滿足適用檢查項的 50%

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

  • 近 30 天內可連通25 分

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

  • 已宣告認證方式10 分

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

  • 已列出工具15 分

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

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