
Densely
io.github.alibaizhanov由 GitHub 帳號 alibaizhanov 發布v0.2.1更新於 Oct 24, 2026
Lossless context compression: 2-8x fewer tokens, byte-exact recovery, search inside payloads
概覽
本機 MCP 伺服器,把大量文字、日誌與 JSON 無損壓縮成更少 token,可逐位元組還原並依行範圍展開。
- 可以做什麼
- 把磁碟上的檔案壓縮成密集載荷並提供預覽,取代整份讀入上下文,同時在原檔旁寫入 .dense 附屬檔。壓縮手邊既有的文字,例如體積很大的工具輸出。把載荷或載荷檔還原成完全一致的原文,可只取指定行範圍,並以 sha256 驗證。統計檔案的 token 節省量。選用掛鉤可自動壓縮大型工具輸出並記錄節省帳本。
- 適用情境
- 適合長日誌、JSON 傾印、相依鎖檔或參考程式碼不斷佔滿上下文視窗、且必須能精確還原的情境。對重複度高的資料效果最好;對密集散文、已壓縮資料或助理正在編輯的程式碼幾乎沒有助益。
- 執行需求
- 以 Python 套件透過 pip 安裝(含 MCP 附加元件),在本機以 stdio 執行。未宣告需要帳號、API 金鑰或環境變數。選用的神經後端需要 torch 與約 1 GB 的模型下載。選用的自動掛鉤需在用戶端設定檔中以絕對路徑設定。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 Densely,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
densely
Lossless context compression for LLMs. Pack any text into 2x–8x fewer tokens with guaranteed byte-exact reconstruction — verified by sha256 on every decompress.
MIT, free forever. (A hosted tier is coming for teams — waitlist.)
An o200k token can carry up to ~17.6 bits of information, but typical code occupies tokens at only ~5–6 bits each. densely reclaims the difference:
Each carrier word (" the", " of", …) costs exactly 1 token — the
o200k pre-tokenizer never merges across word boundaries — so every token
in the payload carries 2 bytes of compressed data (16 of the ~17.6
theoretically available bits, 91% of channel capacity).
Benchmarks
Reproduce with python3 bench.py (fixed seeds, stdlib code sample):
The neural backend (--backend neural, python3 bench.py --neural) drives
an integer arithmetic coder with next-token probabilities from
Qwen2.5-Coder-0.5B, NNCP-style batched across segments. The 88.3% figure
benefits from the model having seen Python's stdlib during training; the
86.3% row is this repo's own sources — code that did not exist before
2026-08-08 — and is the honest number for novel code (~0.56 bit/byte).
Measured on real-world data
Same pipeline pointed at data we didn't curate: this repo's own sources, an
installed pip module, a slice of a real macOS /var/log/install.log, a real
package-lock.json, and the live npm registry response for express.
o200k counted locally with tiktoken; Claude counted via Anthropic's own
count_tokens endpoint (Sonnet 5, request overhead calibrated out), using
the claude1 alphabet. Every row round-tripped byte-exact.
For comparison, Headroom reports 15–20% savings for coding agents and 60–95% on JSON — achieved by dropping content from context, with originals kept in a local cache with a TTL. densely keeps the full data in the context itself, restorable byte-for-byte with no external storage and no expiry.
Lossless compression below the entropy of the data is mathematically impossible (Shannon; see also Fundamental Limits of Prompt Compression) — within that bound, densely sits near the practical ceiling for a deterministic, CPU-only method.
Usage
Library:
compress via the CLI self-verifies the round trip before writing output;
decompress raises ValueError on any corruption or hash mismatch.
Use it in Claude Code / Cursor (MCP)
Three tools:
- compress_file(path) — agent calls this instead of reading a large log/JSON/dump: gets a preview + dense payload at 2x-8x fewer tokens.
- compress_text(text) — same for a big tool output already in hand.
- expand(payload | payload_file, start_line, end_line) — exact original back, sha256-verified; line ranges let the agent pay only for the slice it needs.
Every compression also writes a .dense sidecar file next to the
original — a plain text file you can commit, ship, or archive; no cache,
no TTL, nothing to expire. Small payloads are additionally returned
inline. This matters for context compaction: a compaction summary
replaces old conversation content, so an inline payload alone could be
summarized away — but the sidecar on disk (and its path, which carries
into summaries) cannot. If you use the Anthropic compaction API beta
directly, add this to your instructions so payload references survive
verbatim:
Preserve any densely payload references (paths ending in .dense, or lines starting with DENSE1/DENSE2) verbatim in the summary. Do not call any tools while writing this summary.
Automatic mode (hook)
Zero-effort savings: a PostToolUse hook compresses every large tool
output (Bash output, Read of non-code files, >= 5,000 tokens) on the
fly — the agent sees a preview + stats, exact content stays available
via search/expand, and every replacement is recorded in a savings
ledger (stats tool, or the running total shown in each replacement).
Add to ~/.claude/settings.json (absolute paths — hooks run outside
your shell PATH):
(which densely-hook after install gives the absolute path — hooks run
outside your shell PATH, so absolute paths only.)
Tune with DENSELY_HOOK_MIN_TOKENS (default 5000). Code files the agent
is editing are never touched.
When it saves tokens (and when it doesn't)
Agent sessions resend the whole history to the API on every turn, so anything sitting in context is paid for again and again. What densely does to each kind of content:
Note on prompt caching: cached history is cheaper, but the context window stays the same size — densely primarily buys you room, then money.
vs. the field
The main players make different trade-offs (numbers from each project's own published benchmarks):
Different philosophies: maximum savings with silent degradation vs. exactness or nothing. Readable-lossy tools win on content the model must keep reading; densely wins when the data must never be wrong and must survive compaction, export, and machine moves. Use both.
The honest caveats
- The payload is not readable — by humans or by the model. It looks like a stream of random English words. Use it as a dense carrier for exact data (chat history, tool outputs, source files) alongside a readable summary; expand it with a tool call when exact content is needed.
- Savings depend on redundancy: highly repetitive data (JSON, logs)
compresses 7x+, dense prose ~1.5–2x, already-compressed or random data
~0% (payload is never larger than a few header tokens worse than raw
input — check
statsbefore shipping). - Density is per-tokenizer; reconstruction is always byte-exact. Two
alphabets ship today:
o200k(65,536 words, exactly 16 bits/token on OpenAI o200k) andclaude1(1,024 words, exactly 10 bits/token on Claude Sonnet 5 / Opus 5 / Fable 5 — harvested empirically through the count_tokens API, since Anthropic's vocabulary isn't public). The MCP server and hook default toclaude1; the CLI defaults too200k(--alphabet/DENSELY_ALPHABETto override). Measured on a 172KB log with count_tokens on Sonnet 5: raw 93,117 tokens -> claude1 payload 25,904 (72.2% saved; the o200k alphabet managed only 47.8% there). Verify any alphabet against any target withpython3 tools/calibrate.py; harvest new ones withtools/build_alphabet.py. - When you don't need this (fair point from a redditor): if you control your agent harness end-to-end, you can build the same pattern yourself — zstd/lzma the big outputs into files, give the agent a search tool, keep payloads out of the model's input. densely is that architecture packaged (plus sha256 verification, line-level retrieval, and per-tokenizer carrier alphabets for the subset that must live inside the prompt — content that has to survive compaction verbatim or travel with an exported session). It earns its keep inside closed harnesses (Claude Code, Cursor), ephemeral sandboxes, and anywhere "reliable disk + grep" doesn't reach.
- Neural backend caveats: slow (~85 KB of code takes minutes on Apple
Silicon vs milliseconds for lzma) and requires torch + a ~1 GB model
download on first use. Reconstruction is bit-exact only when
decompression runs the same model/software stack as compression — which
is why
compress(backend="neural")verifies the full round trip before returning and silently falls back to lzma on any mismatch. The 100% guarantee never rests on the neural path.
Densely Pro (coming)
The library is MIT and stays free. We're building a managed tier for teams running agents in production:
- Cloud neural compression — 86% on code without a local GPU
- Managed proxy — savings with zero code changes
- Team dashboard — token savings per agent, per day, in dollars
- Cross-machine payloads — compress in CI, expand anywhere
Join the waitlist → (early access order + founding-user pricing, no rug pull — the library stays MIT)
Roadmap
- Cross-machine determinism for the neural backend (integer/fixed-point inference or a mismatch-tolerant coder), so payloads compressed on one machine decompress on another.
- Larger/faster models via llama.cpp for better ratios at higher speed.
Tests
Fast suite: byte-exact round-trips (unicode, CJK, emoji, random bytes, payload-lookalike inputs), tamper detection, carrier-alphabet density. Neural suite: single- and multi-segment round trips, no-silent-fallback, beats-lzma check.
來源:README.md,提交 55c501a
工具
0版本歷史
1- v0.2.1最新Sep 16, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分