Українська версія

Author: abavyka

The foundation is in place: what fills the context, how MCP and hooks work. Now for how to save tokens on top of it.

Why

I think every one of you has hit the limits and sat waiting for them to reset. The point of the previous posts was to figure out where the fuck the tokens go: what lands in the context, what the fundamental concepts are and how you can optimize with them, why the start of a session can burn a shitload of tokens on the very first message (hello cache and long sessions, a pile of messages and chatting with the model).

The problem is that an agent writing code is token-hungry by nature: searching the repo, reading files, tool output. All of it lands in the context and then rides along with every request (see Context and cache and Tokens and limits).

Complex tasks on Pro hit the limits really fast. On Max the savings are a plus too: more headroom for parallel tasks.

I’ve got three tools, each on its own level:

  • rtk — output of shell commands
  • code graph — searching the code instead of grep and reading files
  • headroom — the whole request to the API

rtk

A CLI in Rust that squeezes command output before Claude sees it: throws out noise, groups, trims, collapses repeats. Tests show only failures, git log — hash, author and subject.

You set it up with rtk init -g. It’s that PreToolUse hook on Bash from the hooks post: through updatedInput it rewrites git status into rtk git status, and Claude doesn’t have to call rtk itself.

My numbers from rtk gain:

  • overall — 39,874 commands, 24.9M tokens saved (27.2%)
  • pet project — 6,092 commands, 14.1M (79.2%)
  • the biggest wins are tests (vitest, jest — 82–95%) and git diff (~94%)

Sometimes it cuts something Claude needs. The model gets that and re-runs the command without rtk if it matters. That’s one more tool call, but in practice it happened rarely.

Worth knowing:

  • the hook only catches Bash. Claude Code’s built-in Read, Grep and Glob go right past it
  • it counts tokens roughly (bytes / 4), so the percentages are fine, but the absolute numbers are ballpark
  • it only saves on command output, so the effect on the whole limit is smaller
  • rtk discover — shows where else you could have saved

Code graph

code-review-graph builds a local graph of your code: functions, classes, imports, who calls whom, which tests cover what. It lives in SQLite in .code-review-graph/ and gets updated by hooks. Claude hits it as an MCP server with a few dozen tools (semantic_search_nodes, query_graph, get_impact_radius…).

I set it up to cut the cost of finding files: instead of grep and reading a dozen files — one query to the graph.

pipx install code-review-graph
code-review-graph install
code-review-graph build

The authors promise a median of ~63x fewer tokens per question. But they themselves write that it’s an upper bound, that on small edits the graph can cost more than just reading the file, and that for small repos and one-off questions you don’t need it.

Honestly, my numbers from a month of transcripts. Pet project, graph built (442 files), an instruction in CLAUDE.md “graph first, then grep”. Claude called it 3 times in 2 sessions out of 18. Over the same time — 1,930 Reads. Plus the graph goes stale: right now it’s built on a different branch.

I’m keeping an eye on it and optimizing further, it definitely doesn’t make things worse.

headroom

A local proxy between Claude Code and the API. It squeezes tool output, JSON, logs and code, and leaves the old prefix of the conversation alone, so the cache doesn’t break. The originals sit locally, and the model can pull them back through headroom_retrieve.

I installed it with headroom install apply: it runs the proxy as a service on 127.0.0.1:8787 and writes ANTHROPIC_BASE_URL pointing at it into env in ~/.claude/settings.json by itself. So headroom works in every session by default.

Your subscription stays. If only ANTHROPIC_BASE_URL is set, without a key, requests go through the proxy but with your claude.ai login and Pro limits.

My numbers from headroom savings: 14.2% over 30 days (49M out of 346M tokens). headroom itself says the real figure is probably lower.

A proxy is a different approach, and because of it some Claude Code features disappear:

  • Remote Control — gets switched off when ANTHROPIC_BASE_URL points anywhere other than api.anthropic.com
  • MCP tool search — off by default behind a proxy, meaning the descriptions of all MCP tools are back hanging in the context from the first request (that was in the MCP post). You bring it back with "ENABLE_TOOL_SEARCH": "true" in env, headroom install sets it by itself

So I’ve got two ways to launch:

  • claude — with headroom
  • claude-remote — without it, so I can keep working on pet projects from my phone
claude-remote() {
  claude --settings '{"env":{"ANTHROPIC_BASE_URL":""}}' --remote-control "$@"
}

A function in ~/.zshrc: --settings overrides ANTHROPIC_BASE_URL with an empty value, and the session goes straight to Anthropic.

Also, headroom has anonymous telemetry on by default (no prompts or code). You turn it off with HEADROOM_TELEMETRY=off.

Read more