# headroom vs tokensave: compress or index the code?

> A Python compressor that rewrites every API call against a Rust index that changes what the agent asks for. Same goal, opposite strategies, and very different risk profiles.

Source: https://tokenade.net/en/articles/headroom-vs-tokensave · Updated: 2026-08-28

Part of the [reduce Claude Code token usage](https://tokenade.net/en/articles/reduce-claude-code-token-usage) pillar. This page is the head-to-head between the two most opposite designs in the category.

## Is headroom or tokensave the better choice?

**tokensave carries less risk; headroom carries more upside on the narrow workloads it fits.** They do not overlap at all. headroom rewrites the message array before every API call. tokensave gives the agent an index so it stops needing to read as much in the first place. One intervenes on what the provider sees, the other on what the agent asks for, and that difference decides almost everything else.

I run an [open long-session benchmark](https://tokenade.net/en/stats/token-optimizer-benchmark) and build a competing tool. On it, headroom finished **53% more expensive than running no tool at all**, last of twelve. tokensave has not been through it, and its own benchmark measures retrieval rather than session cost, so its effect on a bill is unquantified. Those are two different statements and neither is a verdict on the engineering.

## What does headroom actually do?

**It compresses the whole conversation window before each call.** Python SDK plus CLI proxy plus MCP server, Apache 2.0, roughly 18.7k stars, in three stages: `CacheAligner` moves timestamps and UUIDs out of the static prefix into a non-cached tail; `ContentRouter` routes each piece of content to a format-specific compressor via ML detection — JSON, code, text, logs, diffs, HTML; `IntelligentContext` scores the remainder on six relevance dimensions.

It is reversible. Originals stay in a local LRU cache and the model gets an injected `headroom_retrieve` tool, with optional BM25 sub-search, to pull one back. The per-workload compression numbers are genuinely strong: 92% on code-search results and SRE incident logs, 73% on issue triage. `SmartCrusher` samples large JSON arrays at 70–95% while always keeping error lines, which is the kind of detail that shows the project thought about failure modes.

The footprint is heavy: Python 3.10+, a 150M-parameter ModernBERT model, Magika content detection, 16 to 50 ms per call.

## What does tokensave actually do?

**It builds a symbol index and serves it over MCP.** A Rust server with a semantic knowledge graph in libSQL with FTS5, built by tree-sitter extraction across **50+ languages**, exposing 80+ MCP tools.

Its distinctive features are all about the shape of the tool rather than the compression rate:

- **Compiled.** Fast startup, low memory, no runtime to install. Homebrew on macOS, Scoop on Windows, prebuilt binaries elsewhere.
- **Multi-branch indexing.** Diff and search across branches without switching checkout — nothing else in this category does that, and it removes a class of context-burning `git checkout` round-trips.
- **Subprocess isolation.** One parser crashing on a malformed file does not take the service down, which matters with 50+ grammars in play.
- **Atomic edit primitives with AST rewriting.** The agent renames a symbol through the tree rather than through a regex, avoiding the shell-quoting and partial-match hazards that make agentic edits fail quietly.

Its gaps: no output filtering, so command output still arrives at full size; navigation only, with no semantic vector search and no structural compression; framework detection limited to fourteen hardcoded frameworks; and no published numbers.

## Why does the compressor carry more risk?

**Because it spends prompt caching to buy token count.** Caching pays only when the prefix is byte-identical between turns, and cached input is billed at a fraction of fresh input on every major provider. A tool that rewrites the window changes the prefix whenever its compression decisions change — which is whenever the content changes, meaning every turn of an agent loop. The cache misses and you re-pay full price for context you already bought.

`CacheAligner` exists to fight exactly this, which tells you the project understands the risk. On a long interactive loop it is not enough.

There is a second path. When the compressed version is not enough, the model calls `headroom_retrieve` and pulls the original back — so you paid for the compressed copy, the original, and the round-trip. Rare on a short task, compounding on a session.

An index has no equivalent failure. It does not touch the prefix, so caching keeps working. Its worst case is a stale index giving a confident wrong answer, which costs a correction, not a systematic multiplier. That asymmetry is the real content of this comparison.

## Where does tokensave's 80-tool surface cost you?

**Mostly on agents that still load tools upfront.** Claude Code defers MCP tool definitions by default through tool search (Claude 4.5 and later models), so an 80-tool server costs little more than its tool names until a tool is used. Behind a third-party API proxy, with `alwaysLoad`, or in an agent without tool search, all 80 definitions are sent on every turn, used or not.

Even with deferral, 80 tools is a lot for the model to choose between. That is not a reason to avoid it — the tools are the product — but it is a reason to disconnect it on sessions that are pure terminal work, and a reason not to run two indexes at once.

## What does neither of them do?

- **Command output.** tokensave never sees it. headroom compresses it inside the window, after it has already been produced and paid for once at the tool boundary.
- **Whole-file reads for editing.** An index says which file; it does not shrink it. A compressor shrinks it and risks the cache.
- **Other MCP servers' output.** Neither compacts what your GitHub, browser or database servers return. tokensave adds a large tool surface of its own.
- **Re-reads.** Same file, four times, four charges. An index lowers the odds; neither deduplicates.

## Which one should you pick?

**Pick tokensave if** you work across many languages, want a single compiled binary with no runtime, or review branches often enough that multi-branch search maps onto your real workflow. The atomic edit primitives are a genuine safety improvement on any agent that edits through shell commands.

**Pick headroom if** your workload is batch rather than interactive: long, stable payloads processed once, where the prefix is not invalidated every turn and 92% off a large JSON blob is the whole story. That is a real use case and it is not an agent coding loop.

**Pick neither yet if** your spend is mostly command output. Neither is positioned on that channel in a way that survives a session.

## How to apply this today

1. **Check your cache-hit rate before installing anything that rewrites the window.** If it is high, a compressor is spending your best asset.
2. **Count tool calls for one session** if you are evaluating the index. That is the metric it moves and the one you can verify yourself.
3. **Disconnect what you are not querying.** An 80-tool server on a terminal-only session adds nothing but choices, and on setups without tool search its full manifest on every turn.
4. **Measure for a week, not for a task.** These two look completely different at those scales.

## What goes wrong (anti-patterns)

**Reading a per-payload compression rate as a session saving.** 92% off one blob and 53% more expensive over a session are both true of headroom.

**Treating "not measured here" as "does not work".** tokensave has no session-cost figure, only a retrieval one. That is missing evidence, not evidence of failure.

**Running a compressor on top of an index.** The index makes the window small; the compressor then destabilises the prefix that just got small. You get both downsides.

**Assuming a large MCP surface is free because tool search is on.** It is only deferred if your setup supports it; behind a third-party proxy or with `alwaysLoad`, it is charged on every turn.

**Or shrink it before it ever enters the window** Tokenade intervenes at the tool boundary, ahead of the message array, so the prompt prefix stays stable and provider caching keeps paying: structure-first file reads and semantic search instead of whole-file reads, compressed command output, and MCP results compacted by its MCP proxy before they reach the model. It ranks first on the open benchmark cited above, which I publish in full.

---

**See also:**

- [Reduce Claude Code token usage](https://tokenade.net/en/articles/reduce-claude-code-token-usage) — the pillar, tool-agnostic
- [headroom alternatives](https://tokenade.net/en/comparisons/alternatives/headroom-alternatives) — the wider field around headroom
- [tokensave vs codegraph](https://tokenade.net/en/articles/tokensave-vs-codegraph) — tokensave against the other code index
- [Token optimizer benchmark](https://tokenade.net/en/stats/token-optimizer-benchmark) — the open measurement behind the headroom result
- [Semantic search vs grep](https://tokenade.net/en/articles/semantic-search-vs-grep) — why indexing beats grepping, and where it stops
