TokenSlim

How much of your agent's token bill is duplicated tool output?

Agents re-read their own logs every turn — repeated headers, identical stack frames, the same JSON payload echoed back six times. TokenSlim is a zero-dependency Python library and MCP server that trims tool outputs and logs before they re-enter the context. No model calls, no dependencies, nothing leaves your machine.

Try the live demo GitHub repo pip install tokenslim-py
55/55
facts kept, redundant-log benchmark
−97.9%
tokens on log-style content (see limits)
0
dependencies · no compressor model
112
adversarial cases: 106 pass / 0 fail

Every number on this page comes from a runnable script in the repository, with a fact-recall check attached — compression only counts if the facts survive.

The benchmark

Four separate measurements, kept separate on purpose. All token counts use tiktoken (cl100k_base). Reproduce them with the scripts in the repository's tests/ directory (benchmark_headroom.py, benchmark_headroom_own_extended.py, accuracy_eval.py) and in benchmarks/compact-vs-trim/, after pip install tokenslim-py.

Benchmark 1 — Extended redundant-log corpus (55 embedded facts)

MethodTokensvs rawFacts kept
Raw context353,523—55/55
TokenSlim7,522−97.9%55/55
Headroom—−41.7%52/55

A large corpus of redundant tool output with 55 checkable facts buried in it. Headroom's token count is shown as published (a percentage); we do not back-compute counts we did not print.

Benchmark 2 — Headroom's own published inputs (18 facts)

MethodFacts kept
TokenSlim18/18
Headroom17/18

Same inputs the Headroom project publishes for itself — the away game, not the home game.

Benchmark 3 — Accuracy eval (24 questions)

ContextCorrect
Raw context23/24
TokenSlim23/24
Headroom23/24

Small eval, single run — treat it as a smoke test, not a study. The honest reading is parity: trimming matched raw context; it did not beat it, and we do not claim otherwise.

Adversarial suite

112 adversarial cases (contradictory values, no-signal rows, buried error lines, giant single-line dumps): 106 pass, 0 fail, and 6 marked observe by design — inputs where the correct behaviour is to keep the data and say so, rather than cut it.

Benchmark 4 — Facts at the same budget: trimming vs summarization-style compression

MethodTokensvs rawFacts kept
Raw context353,319—55/55
TokenSlim7,318−97.9%55/55
Extractive summary (same budget)7,274−97.9%1/55
Head+tail truncation (same budget)7,316−97.9%14/55

Same corpus as Benchmark 1, counted here as plain joined text (Benchmark 1 counts per-message framing instead: 353,523 raw, 7,522 TokenSlim — same runs). Each baseline was given exactly TokenSlim's output budget on each scenario, so all three compressed rows cost the same; what differs is how many of the 55 facts — SHAs, trace IDs, emails, file paths — survive. The extractive baseline is a small deterministic term-frequency summarizer written for this test: a stand-in for summarization-style compression, not any product's compaction feature — nothing here measures Claude Code's /compact or any other product. Method, script and full per-scenario results: benchmarks/compact-vs-trim/ in the repository.

Honest limits — read this before quoting the headline

  • The ~98% figure is for redundant log-style content — repeated headers, stack frames and echoed payloads. That is the case TokenSlim is built for, and it is not prose.
  • The pure compressor alone reaches ~64% on unique prose.
  • Across content types, the realistic range is 25–90%+. Your mileage is determined by how much of your context is tool output.
  • Most of the win on logs is deletion of duplication, not compression — exact duplicates and repeats are removed first; what remains is kept head + tail + the high-signal lines (errors, IDs, amounts).

Code Mode shrinks definitions. TokenSlim shrinks results.

An MCP-heavy agent pays for two different things, every turn. The first is tool definitions — the schemas of every tool on every connected server, re-sent with each request. The second is tool results — the logs, search hits, file reads and query dumps the tools hand back, which then ride along in the context for the rest of the session.

Claude Code's Code Mode (programmatic tool calling) attacks the first half: instead of loading every definition into the context, the model writes code against the tools it needs. In Cloudflare's published figures, describing ~2,594 tools the classic way costs ≈1.17M tokens of definitions; with Code Mode the same surface costs ~1,000 tokens. Those are Cloudflare's numbers, quoted as published — we did not re-measure them, and they are about definitions only.

TokenSlim is the other half of that equation. Results are the bigger half of the bill — one log dump gets re-sent on every later turn — and Code Mode does not trim them. The library trims tool output before it re-enters the context (the benchmark above), and the newest family member does it with no code changes at all:

TokenSlim Gateway — the results-side layer

An MCP proxy that sits between your client (Claude Code, Cursor, Cline, …) and the real upstream server, and trims every tool-call result with TokenSlim before it reaches the model. One line changes in your client config; the upstream server never knows. Error results and non-text blocks pass through untouched, and if trimming ever fails, the original result is forwarded as-is — the proxy never breaks the protocol.

8,062 → 244
tokens through the gateway, bundled demo (−97.0%)
4/4
embedded facts preserved (job id, request id, exit code, test total)
pip install tokenslim-gateway Gateway on GitHub

Read the demo number honestly: the fixture is redundant log output, where duplicate collapse does most of the work — the same honest limit as the library benchmark above. The two techniques compose: Code Mode shrinks what the tools are; TokenSlim shrinks what they said.

The family — four packages, one job

All MIT-licensed, all live on PyPI, all built on the same trimming core. Take the layer that matches how you work:

PackageWhat it isInstall
tokenslim-pyThe library + CLI: audit, trim, compact and compress agent tool outputs and logs in your own code.pip install tokenslim-py
tokenslim-mcpThe MCP server: four tools (audit, compress, trim, compact) that any MCP client can call.pip install tokenslim-mcp
tokenslim-langgraphThe LangGraph / LangChain integration: trims tool results inside your graph without breaking the tool-calling protocol.pip install tokenslim-langgraph
tokenslim-gatewayThe MCP proxy: sits in front of any upstream MCP server and trims every tool result — no code changes.pip install tokenslim-gateway

Live demo — run the real library on your own log

Paste a tool log or agent output below and run it. This page loads Pyodide (Python in WebAssembly), installs the real tokenslim-py from PyPI, wraps your text as a single tool-output message and calls smart_pipeline with library defaults (tool outputs capped at 400 tokens, history budget 4,000). Token counts are measured exactly in your browser with a cl100k_base tokenizer.

Runs 100% in your browser — your text never leaves this page. The only network requests are the runtime and the package itself, fetched from public CDNs/PyPI.

The in-browser demo could not start — the Python runtime or the package could not be fetched (offline, blocked CDN, or an older browser). Nothing is stuck; run the identical thing locally:
pip install tokenslim-py
python -c "from tokenslim.smart import smart_pipeline; print(smart_pipeline([{'role':'tool','content':open('your.log').read()}]))"
–
tokens before
–
tokens after
–
saved
–
est. saving per run
–
est. saving per 1,000 runs

Not yet measured — and we say so

A benchmark page that only lists the tools its author has beaten is an ad. These tools are in the same space and have not been run on this corpus yet:

ToolStatus
RTKNot yet measured — the corpus and harness are open; results PRs welcome.
PonytailNot yet measured — the corpus and harness are open; results PRs welcome.
Any other trimmer / compressorNot yet measured — the corpus and harness are open; results PRs welcome.

The corpus, the harness and the fact-recall checks are in the repository. A fair measurement needs three things: the same corpus, the same cl100k_base counter, and the fact-recall check attached — a token count without a fact check is just a smaller number. If you maintain one of these tools, a PR with your numbers, produced that way, will be linked here whether or not TokenSlim wins it.