token-saver
A Claude Code plugin that minimizes token consumption with deterministic, LLM-free machinery:
guard hooks, output rewriting, codegraph-style structure queries, context-pressure warnings,
and delegation to cheap subagents. Written in Rust — hooks run on every tool call, so startup
cost is the budget that matters.
Install
claude plugin marketplace add kmizu/token-saver-plugin
claude plugin install token-saver@token-saver-plugin
Or interactively: /plugin marketplace add kmizu/token-saver-plugin → /plugin install.
Update later with claude plugin update token-saver@token-saver-plugin.
Requirements: a Rust toolchain (cargo on PATH — rustup.rs). The first
hook invocation builds the binary once (a few seconds, zero third-party crates — std only);
every call after that is the built binary. No Python, no Node, no runtime dependencies.
Native Windows: supported. Hooks run under Git Bash when Git for Windows is installed,
otherwise PowerShell. (WSL needs nothing special.)
Design
Token leaks in agentic sessions come from a handful of predictable places:
| Leak source | Countermeasure | Mechanism |
|---|
| Huge tool results (cat of lockfiles, full-file reads, unbounded grep) | Block upfront, suggest a cheaper alternative | PreToolUse (guard) |
| Long command output (build/test logs) | ANSI-strip, drop git hint boilerplate, collapse repeated lines, then truncate before it reaches context; full text saved to a file | PostToolUse updatedToolOutput (post-tool) |
| Green test runs listing every PASSED test | Per-item progress lines fold into one counted marker; the full list stays in the stashed log | PostToolUse progress collapse |
| Failing test runs re-printing the source around each failure | Source-echo lines and the repeated short-summary section fold; > expressions, E lines, pointers, captured output and tallies all survive | PostToolUse traceback folding |
| Re-running a command whose output didn't change (pytest, git diff …) | Identical repeats become a one-line "nothing changed" (durations/addresses normalized; counts never are) | PostToolUse dedup |
| Re-running a command whose output barely changed | Only the drifted lines enter context; removals are anchored by their first line, never re-printed | PostToolUse delta |
| Huge diffs (blind truncation loses middle hunks) | Leading hunks verbatim, every remaining hunk kept as a file:@line pointer + full diff stashed | PostToolUse cap_diff |
| Grep/Glob result floods | First matches keep their text; the rest collapse to file:line pointers (ranges) | PostToolUse |
| Re-reading unchanged files | mtime tracking, deny-once | PreToolUse + PostToolUse |
| Re-reading a file right after editing it | "Edit already applied; Read a slice if you must" nudge, deny-once | PreToolUse + PostToolUse |
codemap show of a symbol you just edited | "The new body is already in your context" nudge, deny-once | PreToolUse + codemap cache |
| Write re-emitting whole files (output tokens cost ~5x input) | Steer to Edit | PreToolUse |
| Exploration polluting the main context | Delegate to disposable cheap subagents | scout / digest agents (haiku) |
| The explore loop itself (read → grep → read again) | Replace with code-graph queries | token-saver codemap + /token-saver:code-map skill |
| Unnoticed context growth | One-shot nudges when crossing 100k/140k/170k tokens | UserPromptSubmit (context-probe) |
| Model non-cooperation | Tiny behavioral creed (<200 tokens) injected once | SessionStart |
Principles:
- Hooks run outside the LLM. An allow costs 0 tokens; only deny reasons enter the context,
so they are short and always include the next move.
- Soft guards are deny-once. If the model really needs the call, repeating it once goes
through. No fights with the model.
- Fail-open, at every layer. A harness bug must never break the session: the binary
swallows panics and exits 0, and the hook registration itself is guarded by a
test -x … || true so that even a missing binary degrades to "allow, unchanged".
- Output rewriting is lossless or pointer-preserving. De-noising removes nothing the model
needs; dedup fires only on repeats identical up to timing noise (counts are never
normalized); progress collapse and diff caps stash the full text first; diff caps keep every
hunk location; truncation always links the stashed full text. Savings that cost correctness
are not savings.
- The harness itself is light. Skills use
disable-model-invocation: true, so the standing
cost is just the creed plus two agent descriptions.