跳到正文
DEV Community· Deduped·· 4 小時前AI 評分63

如何用 RTK 在 10 分鐘內將 Claude Code 的 token 用量最多減少 90%

How to cut Claude Code token use by up to 90% with RTK (setup in 10 minutes)

AI 導讀

RTK 透過過濾及壓縮 shell 命令輸出,減少 Claude Code 接收的內容;文章示範以 `rtk init -g` 設定 hook。README 聲稱,RTK 可令 100+ 個支援命令的輸出最多縮小 90%;文章亦列出 `rtk gain` 等命令查看節省量。

正文

If you use Claude Code on a real project, a big share of your tokens never comes from your prompts. It comes from command output: a long git log, a test run that prints every passing case, a lint pass that repeats the same warning over and over. The agent reads all of it, every time, and you pay for it in context and in limits.

RTK is a single Rust binary that sits between your agent and the shell. When Claude Code runs git status, pytest or npm test, RTK filters and compresses the output before the model sees it. The README claims up to 90% smaller output across 100+ supported commands. You do not change how you prompt: the agent keeps typing normal commands.

Here is the setup in about ten minutes, how to check it is really saving tokens, and the one gotcha that makes people think it is broken.

Step 1: install it (and make sure it is the right rtk)

brew install rtk                 # macOS / Linux
winget install rtk-ai.rtk        # Windows

There is also an install script on the repo if you do not use a package manager.

Then check you got the right package, because another project named "rtk" exists on crates.io:

rtk --version
rtk gain        # must show the savings dashboard, not "command not found"

If rtk gain prints a dashboard, you are good.

Step 2: hook it into Claude Code

rtk init -g

That adds a PreToolUse hook to Claude Code (the same command covers Copilot in VS Code with --copilot). Restart Claude Code, then ask it to run git status. Behind the scenes the call is rewritten to rtk git status and the agent receives the compact version. It never has to call rtk itself.

Using something else? The same idea works across 18 tools:

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

Step 3: check the savings are real

After a few sessions:

rtk gain              # summary
rtk gain --graph      # last 30 days as an ASCII graph
rtk gain --daily      # day by day
rtk discover          # commands you ran that RTK could have compressed but did not

rtk discover is the useful one. It tells you where you are still leaking tokens.

The gotcha: the hook only sees shell commands

This trips up most people. The hook only fires on Bash tool calls. Claude Code's built-in Read, Grep and Glob tools do not pass through the shell, so RTK never sees them.

Two fixes:

  • Tell the agent to prefer shell commands (cat, rg, find) for reading and searching, or
  • Have it call rtk read, rtk grep and rtk find directly.

A line in your CLAUDE.md like "use rg and cat through Bash for search and reading" is enough for most projects.

What it does to the output

RTK applies four strategies depending on the command:

  1. Filtering: strips comments, whitespace and boilerplate
  2. Grouping: aggregates similar items, such as files by directory or errors by type
  3. Truncation: keeps the relevant part, cuts the repetition
  4. Deduplication: collapses repeated log lines into one line with a count

Need the raw output once? rtk bypass <command> runs it unfiltered and still tracks it.

Does it break Claude's prompt cache? No, according to the README: output is filtered once per command, stored in history, and cached normally on later calls. Smaller outputs also make cache writes and reads cheaper.

Honest caveats

  • The default rtk build mixes Apache-2.0 and BUSL-1.1 code. Production use is free. If your team needs Apache-only, install rtk-oss instead (brew install rtk-ai/tap/rtk-oss). Both install a binary named rtk, so uninstall one before the other.
  • There is an optional rtk-proxy that compresses the API traffic itself (rtk claude launches Claude through it). It is a separate binary under BUSL-1.1. Try the plain hook first.
  • Compression means the agent sees less. If it starts missing details in a specific command's output, use rtk bypass for that command.

Use cases with copy-paste prompts

Each page has who it is for, the steps and a prompt you can paste into your coding agent.

Use case: Cut your coding agent's token use from command output

Use case: Set up RTK for Codex, Gemini CLI, Cursor and other agents


Get one email every Monday with the week's hottest AI repos and exactly how to use them: subscribe free at dedupedai.com

來源:DEV Community · dev.to