· 9 min Automate

RTK and tokensave: reducing token costs in AI coding

Claude Code MCP Rust Developer Tools AI RTK tokensave

In a steady-state AI coding workflow, most tokens are not consumed by prompts. Two structural sources operate silently: verbose CLI output that the model reads raw, and the codebase exploration that agents perform before every task.

Two open source tools, both written in Rust, attack these two problems orthogonally: RTK and tokensave. Installed together, they cover the two main sources of structural token waste.

The problem: two invisible token sources

Unfiltered CLI output

Every time Claude Code runs a shell command, the raw output enters the model’s context. A standard git push produces around 200 tokens of boilerplate (“Enumerating objects”, “Counting objects”, “Delta compression…”). The model needs only the outcome: ok main. The other 190 tokens are noise that fills context without adding information.

Codebase exploration before every task

Before every complex task, Claude Code spawns Explore agents that use grep, glob, and Read to navigate the project. On a medium-sized codebase, this means dozens of tool calls and thousands of tokens - just to find the relevant files.

The two problems have different natures and require different solutions.

RTK: compressing CLI output

RTK is a CLI proxy that intercepts shell commands before their output reaches the model and applies four compression strategies:

  • Smart Filtering - removes boilerplate, comments, whitespace
  • Grouping - aggregates similar elements (files by directory, errors by type)
  • Truncation - keeps relevant context, cuts redundancy
  • Deduplication - collapses repeated log entries with a counter

Estimated savings for a 30-minute Claude Code session on a medium TypeScript/Rust project:

OperationStandardRTKSavings
ls / tree (10x)2,000400-80%
cat / read (20x)40,00012,000-70%
grep / rg (8x)16,0003,200-80%
git status (10x)3,000600-80%
git diff (5x)10,0002,500-75%
git add/commit/push (8x)1,600120-92%
cargo test / npm test (5x)25,0002,500-90%
pytest (4x)8,000800-90%
Total~118,000~23,900-80%

Installing RTK

Linux

curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | sh
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

macOS

brew install rtk

Cargo

cargo install --git https://github.com/rtk-ai/rtk

Verify

rtk --version
rtk gain

Claude Code integration: a hook that rewrites commands

rtk init -g
# restart Claude Code

This installs a PreToolUse hook in ~/.claude/settings.json that automatically rewrites Bash commands (git statusrtk git status) before execution. From that point on, RTK is active on every session without manual intervention.

Also supports other tools:

rtk init -g --gemini         # Gemini CLI
rtk init -g --codex          # Codex (OpenAI)
rtk init -g --agent cursor   # Cursor
rtk init --agent windsurf    # Windsurf
rtk init --agent cline       # Cline / Roo Code

The hook intercepts only Bash tool calls. Claude Code’s built-in tools (Read, Grep, Glob) don’t go through the hook. For those flows, shell equivalents can be used (cat, rg, find) or rtk read, rtk grep, rtk find can be called explicitly.

Main RTK commands

Files

rtk ls .                          # optimized directory tree
rtk read file.rs                  # smart read
rtk read file.rs -l aggressive    # signatures only (no body)
rtk grep "pattern" .              # grouped results
rtk diff file1 file2              # condensed diff

Git

rtk git status    # compact output
rtk git push      # "ok main"
rtk git commit    # "ok abc1234"
rtk git log -n 10 # one commit per line

Tests and build

rtk cargo test    # failures only (-90%)
rtk pytest        # failures only (-90%)
rtk jest          # failures only (-90%)
rtk cargo build
rtk tsc           # TypeScript errors grouped by file

RTK savings monitoring

rtk gain                       # aggregate statistics
rtk gain --graph               # ASCII chart (last 30 days)
rtk gain --history             # recent command history
rtk gain --daily               # daily breakdown
rtk gain --all --format json   # JSON export
rtk discover                   # unused savings opportunities

Advanced RTK configuration

~/.config/rtk/config.toml:

[hooks]
exclude_commands = ["curl", "playwright"]  # exclude from rewrite

[tee]
enabled = true       # save raw output on failure
mode = "failures"    # "failures" | "always" | "never"

When a command fails, RTK saves the full unfiltered output to ~/.local/share/rtk/tee/ so the model can read it without re-running the command.

Telemetry is disabled by default and requires explicit consent during rtk init.


tokensave: codebase knowledge graph

tokensave is an MCP server that builds a semantic knowledge graph of the project using Tree-sitter, indexed locally. Instead of letting Claude Code spawn Explore agents that scan files with grep and glob, the model queries the precomputed graph.

The system operates on three layers:

LayerFunction
MCP serverExposes tokensave_* tools to Claude Code
CLAUDE.md rulesInstructs the model to prefer tokensave over direct reads
PreToolUse hookBlocks Explore agents at the system level

Installing tokensave

Linux (x86_64)

curl -LO https://github.com/aovestdipaperino/tokensave/releases/download/v6.1.1/tokensave-v6.1.1-x86_64-linux.tar.gz
tar xzf tokensave-v6.1.1-x86_64-linux.tar.gz
sudo mv tokensave /usr/local/bin/

For ARM64, replace x86_64 with aarch64.

macOS

brew install aovestdipaperino/tap/tokensave

Windows

scoop bucket add tokensave https://github.com/aovestdipaperino/scoop-bucket
scoop install tokensave

Cargo

cargo install tokensave

Verify

tokensave --version
tokensave doctor

doctor checks the binary, local database, configuration, and Claude Code integration. On anomalies, it reports instructions for resolution.

Claude Code integration: a single command

tokensave install
# restart Claude Code

Writes to ~/.claude/settings.json: the MCP server, the PreToolUse hook, permissions for MCP tools, rules in ~/.claude/CLAUDE.md. The command is idempotent.

Per-project initialization

cd /path/to/your/project
tokensave init

Creates .tokensave/ with the knowledge graph. Run once per project. To force a full re-index:

tokensave sync --force

Three graph update strategies

Manual sync - for full control:

tokensave sync   # incremental: only re-indexes modified files

Daemon - automatic update on every file change:

tokensave daemon --enable-autostart

Git hook - automatic update on every commit:

tokensave install --git-hook

The hook is a no-op in repos not initialized with tokensave.

With branch-aware mode (daemon active), each branch maintains its own index. Switching branches gives a graph that reflects the current code without stale results.

MCP tools exposed to the model

The current version exposes 48 tools in total. The main ones:

ToolFunction
tokensave_searchFind symbols by name (functions, classes, types)
tokensave_contextRelevant context for a specific task
tokensave_callersWho calls a function
tokensave_calleesWhat a function calls
tokensave_impactWhat breaks when modifying a symbol
tokensave_nodeDetails and source of a symbol
tokensave_filesList indexed files with filters
tokensave_affectedTest files impacted by changes
tokensave_dead_codeUnreachable symbols
tokensave_diff_contextSemantic context for modified files
tokensave_module_apiPublic API of a file or directory
tokensave_circularCircular dependencies between files
tokensave_hotspotsMost connected symbols (high regression risk)
tokensave_similarSymbols with similar names
tokensave_rename_previewPreview impact of a rename
tokensave_unused_importsNever-referenced imports
tokensave_changelogSemantic diff between two git refs
tokensave_statusIndex state, statistics, tokens saved

Some can also be used directly from the CLI:

tokensave query <symbol>
tokensave affected src/main.rs
git diff --name-only HEAD~1 | tokensave affected --stdin
tokensave status --show-flags

Supported languages

34 languages as of v6.1.1, organized in three tiers: Lite (11 languages), Medium (20), Full (34). Includes: Rust, Go, Java, Scala, TypeScript, JavaScript, Python, C, C++, Kotlin, Dart, C#, Pascal, PHP, Ruby, and others.

Privacy

tokensave is 100% local: code never leaves the machine. It makes two optional network calls:

  • Global counter - sends only the number of tokens saved (no code, no filenames) to an anonymous Cloudflare worker. Disable with tokensave disable-upload-counter.
  • Version check - checks for new releases on GitHub. 1-second timeout, fails silently.

Troubleshooting tokensave

“tokensave not initialized”

tokensave init

MCP server not connected

which tokensave           # verify it's in PATH
tokensave install         # re-run if permissions are missing
# then restart Claude Code

Symbols missing from the graph

tokensave sync

Verify the language is supported and the file is not excluded by .gitignore.


Side by side

RTKtokensave
ProblemVerbose CLI outputExpensive codebase exploration
LayerShell - modelCodebase - model
MechanismProxy + compressionKnowledge graph + hook
Global setuprtk init -gtokensave install
Per-project setupNot neededtokensave init
UpdatesAutomatic via hooktokensave sync or daemon
Claimed savings-80% on CLI output93% avg on exploratory tool calls
LanguagesAgnostic34 (3 tiers)
LicenseApache 2.0MIT

RTK acts on the output of commands the model runs. tokensave acts on how the model navigates code. The two problems are orthogonal: one tool does not replace the other.

Full setup on Linux

# RTK
curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | sh
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
rtk init -g

# tokensave
curl -LO https://github.com/aovestdipaperino/tokensave/releases/download/v6.1.1/tokensave-v6.1.1-x86_64-linux.tar.gz
tar xzf tokensave-v6.1.1-x86_64-linux.tar.gz
sudo mv tokensave /usr/local/bin/
tokensave install

# Per project
cd /path/to/your/project
tokensave init

# Restart Claude Code

Resources

Related articles

Edit on GitHub