0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

トークン節約術: RTK (Rust Token Killer) を導入してみた

0
Posted at

TL;DR

  • RTK(Rust Token Killer) は、git statuscat、テスト実行 (vitest) などのコマンド出力を、LLM に届く前に、不要な文章が削ってくれる CLI ツール
  • LLM が RTK経由のコマンドを使用するようになるだけで (例: git status の代わりに、rtk git status)、トークンを約80%ほど節約できる
  • Claude Code では Hook (PreToolUse) として使用するので、自動的に rtk 経由に書き換えてくれる
  • Rust でできていて高速

Website: https://www.rtk-ai.app/
GitHub: https://github.com/rtk-ai/rtk

背景

以前お話しした方に、たまたま教えていただいて、使ってみようかなと思いました。

私は Claude Code の USD 100 / 月のプランを利用しています。

Anthropic は Rate Limit を制限したり、緩和していたりしていて、最近 (5 月) は大丈夫なのですが、4 月くらい(?)は毎日夕方にトークン制限がかかっていて辛かったです。

RTKとは

ざっくり言うと、AI との間に挟まる CLI です。

AI に RTK コマンド経由でコマンドを実行させることで、文字数を抑えて AI に渡すことができます。

公式の解説

以下公式の GitHub からですが、

RTKなし:                              RTKあり:

Claude → git status → shell → git    Claude → git status → RTK → git
  ↑                          |          ↑                |       |
  | ~2,000トークン(生)      |          | ~200トークン    | 圧縮  |
  +--------------------------+          +----- (圧縮済) --+-------+
  1. フィルターリング : コメント・空白・定型文などのノイズを除去
  2. グルーピング — 似たもの同士をまとめる(ファイルをディレクトリ別、エラーを種別ごと)
  3. トランケーション (切り取り) — 関連する文脈は残しつつ冗長な部分をカット
  4. 重複排除 — 繰り返されるログ行を件数表示に折りたたむ

をしてくれるそうです。

実際のコマンド例

図を見てもいまいちピンと来なかったのですが、以下の例を見るとよくわかります。

本来は LLM が使用するコマンドを選んで実行してくれるのですが、今はわかりやすいように、実際のコマンドを自分で実行してみます。

↓ RTK あり

% rtk git status
* temp/rtk...origin/temp/rtk
?? docs/plans/test-rtk.md

↓ RTK なし

% git status
On branch temp/rtk
Your branch is up to date with 'origin/temp/rtk'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	docs/plans/test-rtk.md

nothing added to commit but untracked files present (use "git add" to track)

rtk git status は通常の git status よりも 75-93% 短い文章にしてくれるみたいです。

見てみると、確かに文字が短くなって、情報が圧縮されています。

git status 以外にも、たくさんのコマンドが用意されています。

公式サイトにコマンド一覧がありますが、テスト関連を見てみても 90% 近く節約してくれるみたいです。

Screenshot 2026-06-02 at 21.02.51.png

セットアップ

インストール

公式によると、 Homebrew が Recommended だそうです。

brew install rtk

Claude Code に設定する

rtk init -g # PreToolUseが設定されます

私はグローバルに設定したので、ルートの設定ファイル (~/.claude/settings.json) がこのようになります。

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "rtk hook claude"
          }
        ]
      }
    ]
  }
}

ただし、この方法だと、Hook を使っているので、これらが効くのは、Bash ツールの呼び出しだけです。

ほかにも、このコマンドで、以下のような LLM 向けにRTKを説明するファイル ~/.claude/RTK.md が生成されます。


# RTK - Rust Token Killer

**Usage**: Token-optimized CLI proxy (60-90% savings on dev operations)

## Meta Commands (always use rtk directly)

rtk gain              # Show token savings analytics
rtk gain --history    # Show command usage history with savings
rtk discover          # Analyze Claude Code history for missed opportunities
rtk proxy <cmd>       # Execute raw command without filtering (for debugging)

## Installation Verification

rtk --version         # Should show: rtk X.Y.Z
rtk gain              # Should work (not "command not found")
which rtk             # Verify correct binary

⚠️ **Name collision**: If `rtk gain` fails, you may have reachingforthejack/rtk (Rust Type Kit) installed instead.

## Hook-Based Usage

All other commands are automatically rewritten by the Claude Code hook.
Example: `git status``rtk git status` (transparent, 0 tokens overhead)

Refer to CLAUDE.md for full command reference.

その上で、~/.claude/CLAUDE.mdもそのファイルを参照するように変更されています。

...

@RTK.md

使ってみた

実際にどれくらい節約されたのか、統計を見ることができます。

rtk gain

試しに、いくつか実行してみました。

grep しかしていないのですが、34%も節約されていると出てきてちょっと嬉しくなります。

% rtk gain
RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════

Total commands:    2
Input tokens:      55
Output tokens:     36
Tokens saved:      19 (34.5%)
Total exec time:   14ms (avg 7ms)
Efficiency meter: ████████░░░░░░░░░░░░░░░░ 34.5%

By Command
───────────────────────────────────────────────────────────────────────
  #  Command                   Count  Saved    Avg%    Time  Impact
───────────────────────────────────────────────────────────────────────
 1.  rtk grep                      1     19   34.5%     5ms  ██████████
 2.  rtk fallback: grep -A...      1      0    0.0%     9ms  ░░░░░░░░░░
───────────────────────────────────────────────────────────────────────

この解析コマンド、色々あるみたいです。
しばらく使ってみてみてみると良さそうです。

rtk gain            # サマリ
rtk gain --graph    # 直近30日のASCIIグラフ
rtk gain --daily    # 日別の内訳
rtk discover        # 最適化を逃しているコマンドを分析
rtk gain --history  # コマンドごとの履歴と削減を表示
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?