Infra15 min read
How to save tokens in Claude Code with rtk and graphify
How I cut token usage in Claude Code with rtk, which compresses terminal output, and graphify, which answers from a code graph. With my real numbers.

Every AI agent that works in the terminal has the same problem: everything the terminal returns ends up in the model's context. Tests running, logs being read, files being searched: every output takes up space and costs tokens.
A test suite returns one PASS line per file, framework warnings and logs nobody will read. A git log brings the full hash, email and date of every commit. A question like "where is this used?" turns into ten files opened one after another.
A few months ago I installed two tools to cut that waste: rtk, which compresses command output before it reaches the model, and graphify, which turns the code into a graph so I can ask the graph before opening files. Both are trending: on 2026-11-16, rtk had over 86 thousand stars on GitHub and graphify was close to 149 thousand, for a repository created in April.
This post is how I use both, with the real numbers from my machine, and where they don't help.
TL;DR#
- rtk is a Rust binary that plugs into Claude Code as a hook: before every terminal command, it swaps
git logforrtk git logand hands the model only what matters. - On my machine, since August: 148 thousand commands and 228.1 million tokens saved, according to
rtk gain. But when I measured by hand, I found the test number is inflated. - graphify reads code with tree-sitter, with no LLM and nothing leaving your machine, and builds a graph of functions, imports and calls. Then
graphify explain "articlesPath()"answers in 15 lines what would take ten open files. - Both have limits: the rtk hook only catches terminal commands (the native Read and Grep tools go straight through), a filter can fail, and the graph misses references.
- Measure before you believe. On a real test suite,
rtk gainrecorded 99.6% savings. Compared with plain jest, it was 73%.
Where the tokens go#
Everything a tool returns goes into the conversation context: a command's output, a file's contents, a search result. And it stays there, sent again every turn, until the conversation ends or gets summarized.
In practice, I see two kinds of waste:
- Command output full of noise. Passing tests, progress bars, 40-character hashes, repeated warnings. The model needs the failure, not a hundred lines of
PASS. - Opening files to find out where something is. To answer "who calls this function?", the agent runs a
grepand then opens file after file to understand each use.
rtk goes after the first one. graphify goes after the second.
rtk: the terminal, summarized#
rtk (Rust Token Killer) is a single Rust binary, licensed under Apache-2.0. It has filters for more than 100 commands: git, gh, npm, jest, vitest, tsc, eslint, pytest, cargo, docker, ls, grep and so on. Each filter knows what matters in that output. For tests, only the failures with their assertion. For git log, one line per commit. For grep, results grouped by file.
How it plugs into Claude Code#
Install it with brew install rtk and then run rtk init -g, which registers a hook in ~/.claude/settings.json. Mine looks like this:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": "rtk hook claude" }]
}
]
}
}A PreToolUse hook runs before each tool. This one only looks at Bash: Claude Code says it's about to run a command, rtk returns the rewritten command, and that version is what runs. You can see the swap with rtk rewrite. These are real outputs:
| The agent asks for | What runs |
|---|---|
git log -10 | rtk git log -10 |
npx jest | rtk jest |
cat src/root.tsx | rtk read src/root.tsx |
cd src && git status | cd src && rtk git status |
git log | head -5 | rtk git log | head -5 |
python3 build.py | (nothing: no filter, runs as is) |
Compound commands work too: it rewrites each piece it knows. Anything it doesn't know goes straight through.
Before and after: a real git log#
I ran git log -10 on this blog's repository, without and with rtk. Without it, each commit looks like this:
commit 0785527405c98135c27f735d944f7a560279cb31
Author: Yasmin Lopes <...>
Date: Mon Nov 16 19:12:27 2026 -0300
feat(artigos): publica o post do rscoveryWith rtk, each commit becomes one line, with a short hash, relative date and author. A long commit message gets cut, with a note saying how many lines are gone:
0785527 feat(artigos): publica o post do rscovery (4 minutes ago) <Yasmin Lopes>
87c3543 feat(artigos): botao de copiar nos blocos de codigo (5 minutes ago) <Yasmin Lopes>
5abcd1b feat(artigos): rascunho do post arquivo apagado nao some (rscovery) (10 minutes ago) <Yasmin Lopes>
Post sobre por que da pra recuperar arquivo apagado de HD, SSD e pendrive,
como o rscovery acha esses dados por assinatura e como apagar de verdade
antes de vender. Quatro ilustracoes, animacao da varredura, capa #20 e
[+1 lines omitted]That's 2,244 characters without and 1,243 with: 45% less. For a git log, that's not much. The big difference shows up in tests.
Before and after: the app's test suite#
This is where I expected the nice number. I ran the 103 test files of the largest module in one of my backends, without and with rtk:
| Characters | Tokens (estimated) | |
|---|---|---|
npx jest src/modules/game, without rtk | 8,603 | 2.1k |
| the same command, with rtk | 2,288 | 570 |
About 73% less. Good, but far from the 94.5% that rtk gain shows for jest. And when I looked at rtk's history (it keeps everything in a SQLite file on your machine), that same run was recorded like this:
original_cmd rtk_cmd input_tokens output_tokens savings_pct
jest run rtk jest run 142508 539 99.6142 thousand input tokens, against the 2.1 thousand that plain jest returned. The difference is that rtk doesn't run jest the normal way: it asks for the JSON report, which is much bigger (558,065 characters in this case), filters that JSON and measures the savings against it. In other words, the baseline is an output you'd never see without rtk.
And the filter didn't even work this time. What reached the model was this, followed by the start of the JSON cut at 2,000 characters:
[RTK:PASSTHROUGH] jest parser: All parsing tiers failed
[RTK:PASSTHROUGH] Output truncated (558065 chars → 2000 chars)
[full output: ~/Library/Application Support/rtk/tee/1794868468_jest_run.log]The summary comes at the start of the JSON (everything passed, 1,419 tests), so the model got the result. But if a test had failed somewhere in those 103 files, the reason could have been past the cut, and the agent would have had to open the full log.
One more data point: the app's whole suite, with 3,088 passing tests, returns only 1,926 characters with plain jest. When everything passes, jest is already short. rtk really makes a difference when the output is noisy: console.log in the middle of a test, framework warnings, a failure with a stack trace.
My numbers#
With that in mind, here's my rtk gain on 2026-11-16:
RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════
Total commands: 148053
Input tokens: 405.1M
Output tokens: 177.1M
Tokens saved: 228.1M (56.3%)
Total exec time: 4661m25s (avg 1.9s)
Efficiency meter: ██████████████░░░░░░░░░░ 56.3%"Input" is the size of the commands' original output. "Output" is what the model read after the filter. In three and a half months, my agents ran 148 thousand commands in the terminal and, by its own count, rtk kept 228.1 million tokens out of the context.
Now, what it says per command:
- jest is 61% of the total, but it's the least reliable bar. That's 139.9 million tokens over 2,422 runs, counted against the JSON, as I showed above. The real savings are lower, and I have no way to recalculate the history.
- Search saves a little each time, but runs a lot.
grepran 45 thousand times with a 24.9% average. - Reading files barely changes anything.
catbecamertk read24 thousand times, with a 4.7% average. Makes sense: in a code file, almost every line matters. - It varies a lot with the month's work. In September, a month with lots of testing, the recorded savings were 83.7%. In October, 29.7%.
One caveat the README itself makes: rtk estimates tokens as bytes divided by 4, and it reduces command output, which is only part of what you send to the model. Cutting 90% of the output is not cutting 90% of the bill.
graphify: ask the graph before opening a file#
graphify reads a folder and builds a knowledge graph: every function, type, file and import becomes a node, and every call or import becomes an edge. With the graph built, the agent asks it instead of opening files.
What I like about it:
- Code is read locally with tree-sitter, with no LLM and nothing leaving your machine. Documents, PDFs and images need a model to extract meaning, but code doesn't.
- Every edge says where it came from:
EXTRACTEDwhen it's written in the code,INFERREDwhen graphify deduced it,AMBIGUOUSwhen it isn't sure. - It's not embedding search. It's an actual graph you can walk: a node's neighbors, the path between two nodes, communities of files that talk to each other.
Building this blog's graph#
I installed it with pipx install graphifyy (yes, with two "y"s) and ran it on this blog's repository:
$ graphify update .
Re-extracting code files in . (no LLM needed)...
AST extraction: 170/170 files (100%) [10 workers]
[graphify watch] Rebuilt: 1049 nodes, 1410 edges, 83 communities
[graphify watch] graph.json, graph.html and GRAPH_REPORT.md updated in graphify-outIt took 1.5 seconds. The graphify-out/ folder gets graph.json (the whole graph), a graph.html to explore by clicking, and GRAPH_REPORT.md, a summary with the most connected nodes:
## God Nodes (most connected - your core abstractions)
1. `cn()` - 31 edges
2. `useI18n()` - 23 edges
3. `compilerOptions` - 20 edges
4. `compilerOptions` - 18 edges
5. `articlesPath()` - 15 edges
6. `ArticleMeta` - 14 edges
7. `absoluteUrl()` - 12 edgesIt matches what I know about the project: cn() merges Tailwind classes in every component, useI18n() translates the site into four languages and articlesPath() builds the URL of the article list in each language.
A real example: who uses articlesPath()?#
This is the typical question before touching a function. Without the graph, the agent's path is a grep (23 lines across 10 files) and then opening those files to understand each use: 30,622 characters, around 7.6 thousand tokens.
With the graph:
$ graphify explain "articlesPath()"
Node: articlesPath()
ID: articles_paths_articlespath
Source: src/features/articles/paths.ts L18
Type: code
Community: 0
Degree: 15
Connections (15):
<-- paths.ts [contains] [EXTRACTED]
<-- static-build.ts [imports] [EXTRACTED]
<-- article-page.tsx [imports] [EXTRACTED]
<-- site-pages.ts [imports] [EXTRACTED]
<-- article-seo.ts [imports] [EXTRACTED]
<-- articles-page.tsx [imports] [EXTRACTED]
<-- articlesListMeta() [calls] [EXTRACTED]
<-- articleMeta() [calls] [EXTRACTED]
<-- renderRss() [calls] [EXTRACTED]
<-- ArticlesPage() [calls] [EXTRACTED]
<-- trail-filter.tsx [imports] [EXTRACTED]
<-- ArticlePage() [calls] [EXTRACTED]
<-- TrailFilter() [calls] [EXTRACTED]
<-- routes.ts [imports] [EXTRACTED]
<-- navHref() [calls] [INFERRED]That's 798 characters, around 200 tokens. And it already says more than grep: which functions call it (the RSS feed, the SEO, the page), not just which files mention the name. Then the agent opens only the file it needs.
The rule I left for Claude#
graphify has a graphify claude install that installs a hook to nudge the assistant toward the graph. I preferred to write the rule in my ~/.claude/CLAUDE.md, which applies to every project. The original is in Portuguese; here it is translated:
# Token saving (default in EVERY session, EVERY project)
2. **graphify (graph-first)**: before opening a file to answer "where/how/what connects",
use the graph. At the start of every task on a codebase, silently:
- `which graphify`: if installed AND `graphify-out/graph.json` exists in the project,
navigate the graph (`GRAPH_REPORT.md`, `graphify query/path/explain`) BEFORE Read.
- Installed but no graph, in a code project I'll change: run
`graphify update .` once (AST-only, cheap) and then use the graph.
- Not installed: carry on with Grep/Read silently (don't warn, don't block).
- Never read more than 3 source files per question; if you need to, the question is
too broad, ask me to narrow it. Trust EXTRACTED/INFERRED edges; only verify AMBIGUOUS.Plus an /econ command that turns both back on in a session that started without them.
The graph gets things wrong too#
When I checked the answer above against grep, I found three things you should know:
- It missed a file.
src/routes/articles.tsxcallsarticlesPath()inside an object (handle.alternates), and that call didn't become an edge. The graph found 9 of the 10 files. To rename a function, I still double-check withgrep. INFERREDis sometimes just a similar name. In the report's "surprising connections" section, it linked adescribe()from the image plugin to aresolve()from the preview server. They have nothing to do with each other: it's a guess based on the name. My rule trustsINFERRED, and this case made me rethink that.- Natural-language questions work poorly on code.
graphify querydoes a keyword search and walks the graph from there. I asked how each language's cover is chosen and it started from the headings of an article in English (the graph also indexes the.mdxfiles). With the symbol name, like"ArticleMeta cover", it gets it right.
And the graph goes stale: the report says which commit it came from. Changed the code? Run graphify update . again.
Limits and when not to use them#
The rtk hook only catches the terminal. Claude Code's native tools, like Read, Grep and Glob, don't go through Bash, so they arrive in full. The rtk README says a pipeline can compress those too, which I don't use. That's why my rule says: for big output (diffs, test logs, builds), prefer running it through the terminal.
A filter can break the command. This happened while I was writing this post. In one of my apps, npx jest became rtk jest, and rtk decided to run jest through pnpm, because the project has a forgotten pnpm-lock.yaml next to package-lock.json. Corepack's pnpm was broken on my machine. Result: the tests didn't even run, the command exited with an error and the model got only this:
[RTK:PASSTHROUGH] jest parser: All parsing tiers failed
[full output: ~/Library/Application Support/rtk/tee/1794867612_jest_run.log]Without rtk, the same tests passed. And rtk gain still recorded that run as 90% savings. An agent that reads "failed" without seeing why may go off fixing something that isn't broken. The clue was in the full log, which rtk saves and points to.
When you need the whole output, ask for it. When the filter cut too much from an error, run the command unfiltered: in version 0.43, which I use, that's rtk proxy git log, and in the current README it's called rtk bypass. It still counts in rtk gain.
Don't use the graph for a small project or a question about a single file. If you already know which file to open, open it. The report itself checks the project's size first and tells you when the graph isn't worth it.
Watch what leaves your machine.
- rtk has optional telemetry. Check it with
rtk telemetry status(mine is off). - graphify has no telemetry, but it logs every question to
~/.cache/graphify-queries.log. And if you ask it to read documents, PDFs or images, that content goes to whatever model you configure. - The
graphify-out/folder holds the map of your code. I keep it out of git.
Make sure it's the right rtk. There's another project called rtk on crates.io. If rtk gain doesn't show the savings dashboard, you installed the other one.
How to set up yours#
brew install rtkrtk init -gpipx install graphifyygraphify update .Restart Claude Code after rtk init -g. After a few days, run rtk gain to see what actually changed in your case.
What I learned#
The counter measures against its own baseline. rtk gain said 99.6% on a suite where the real gain, compared with plain jest, was 73%. Still good. But if I went by the dashboard, I'd think I save a lot more than I do. To know for real, run the same command with rtk proxy and compare.
The savings show up where there's noise. Tests with logs, lint with hundreds of warnings, ps aux, git log. Passing tests without logs are already short, and reading files barely changes anything.
Every new layer is a new place to break. The hook is invisible on purpose, and that's exactly why, when it gets something wrong, it takes a while to notice. Today, when a test fails in a strange way, the first thing I check is whether the command actually ran.
A graph is a map, not the truth. It answers "who uses this?" fast and cheap, and it's right almost every time. For changes that can't leave anything behind, like a rename, I confirm in the code's text.
References#
Read next

Infra13 min read
How to wipe a hard drive or SSD before selling it

Back-end18 min read
What is RAG? Indexing, hybrid search and a real case

Front-end16 min read
Multilingual SEO in React: hreflang, prerendering, sitemap