Windows 上用 RTK 给你的 AI 编程助手省下 70% Token
Windows 上用 RTK 给你的 AI 编程助手省下 70% Token
一分钟装好,每次
git status、cargo test、npm run build都自动瘦身,AI 看得更少,你花得更少。
一、痛点:你的 Token 都花哪儿了?
用 Claude Code 或 Codex CLI 写代码时,AI 会不停地跑命令:
git status # 一堆未追踪文件
npm run build # 几千行 warning
pytest -v # 几百个 passing 的测试名
ls -la # 每个文件一行权限信息
这些东西 AI 一个字不落地全读进去了。你为这些噪音付了 Token 费。
RTK(Rust Token Killer)就是解决这个问题的——它夹在 AI 和你系统之间,命令输出经过它过滤再发给 AI。
| 场景 | 原始输出 | RTK 输出 | 节省 |
|---|---|---|---|
git status(50个文件) |
~400 tokens | ~50 tokens | 87% |
ls -la(30个文件) |
~180 tokens | ~40 tokens | 78% |
pytest(200个passing) |
~3000 tokens | ~600 tokens | 80% |
npm run build(大量warning) |
~2000 tokens | ~400 tokens | 80% |
二、原理:不是压缩,是过滤
RTK 是一个 Rust 写的单二进制 CLI 代理,不修改任何源文件,只拦截命令输出。
四种过滤策略:
| 策略 | 做什么 | 例子 |
|---|---|---|
| Smart Filtering | 去掉注释、空白行、boilerplate | tsc 输出只留 error,全删 info |
| Grouping | 同类信息聚合 | 按目录聚合文件列表 |
| Truncation | 保留关键上下文,裁掉冗余 | diff 只显示改动行±2行 |
| Deduplication | 重复日志合并计数 | [repeated 47x] Connection timeout |
工作流程:
你输入: git status
↓
Claude Code PreToolUse Hook 拦截
↓
RTK 执行 git status,输出过滤
↓
过滤后结果 → 发给 AI
对 AI 来说,它根本不知道 RTK 存在——它只看到干净的输出。
三、Windows 安装(PowerShell 一键脚本)
前置条件
- Windows 10/11
- PowerShell(推荐 PowerShell 7+)
- 已安装 Claude Code 或 Codex CLI
一键安装脚本
把下面脚本保存为 install-rtk.ps1,以管理员身份运行:
# install-rtk.ps1 — RTK 一键安装脚本 for Windows
# 用法: powershell -ExecutionPolicy Bypass -File .\install-rtk.ps1
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " RTK Windows 安装脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
# === [1/5] 检查依赖 ===
Write-Host "`n=== [1/5] 检查依赖 ===" -ForegroundColor Yellow
$NodeCheck = Get-Command node -ErrorAction SilentlyContinue
if (-not $NodeCheck) {
Write-Host "未找到 Node.js,RTK 本身不需要 Node,但 Claude Code/Codex CLI 需要。" -ForegroundColor DarkYellow
Write-Host "如果尚未安装,请访问: https://nodejs.org" -ForegroundColor DarkYellow
}
# === [2/5] 下载 RTK ===
Write-Host "`n=== [2/5] 下载 RTK ===" -ForegroundColor Yellow
$RtkDir = Join-Path $env:USERPROFILE "rtk"
$RtkZip = Join-Path $RtkDir "rtk.zip"
$RtkBinDir = Join-Path $env:USERPROFILE "bin"
# 创建目录
New-Item -ItemType Directory -Path $RtkDir -Force | Out-Null
New-Item -ItemType Directory -Path $RtkBinDir -Force | Out-Null
# 获取最新版本
try {
Write-Host "正在获取最新版本信息..."
$Release = Invoke-RestMethod -Uri "https://api.github.com/repos/rtk-ai/rtk/releases/latest" -TimeoutSec 15
$Tag = $Release.tag_name
Write-Host "最新版本: $Tag"
$AssetUrl = ($Release.assets | Where-Object { $_.name -match "x86_64-pc-windows-msvc" }).browser_download_url
if (-not $AssetUrl) {
Write-Host "未找到 Windows MSVC 二进制,尝试 fallback..." -ForegroundColor DarkYellow
$AssetUrl = ($Release.assets | Where-Object { $_.name -match "windows" }).browser_download_url
}
} catch {
Write-Host "GitHub API 请求失败,使用已知版本 v0.39.0" -ForegroundColor DarkYellow
$Tag = "v0.39.0"
$AssetUrl = "https://github.com/rtk-ai/rtk/releases/download/v0.39.0/rtk-x86_64-pc-windows-msvc.zip"
}
Write-Host "下载地址: $AssetUrl"
Invoke-WebRequest -Uri $AssetUrl -OutFile $RtkZip
# === [3/5] 解压并安装 ===
Write-Host "`n=== [3/5] 解压并安装 ===" -ForegroundColor Yellow
Expand-Archive -Path $RtkZip -DestinationPath $RtkDir -Force
Copy-Item -Path (Join-Path $RtkDir "rtk.exe") -Destination (Join-Path $RtkBinDir "rtk.exe") -Force
Remove-Item $RtkZip -Force
Write-Host "rtk.exe 已安装到: $RtkBinDir"
# === [4/5] 添加到 PATH ===
Write-Host "`n=== [4/5] 配置 PATH ===" -ForegroundColor Yellow
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($UserPath -notlike "*$RtkBinDir*") {
$NewPath = if ($UserPath) { "$UserPath;$RtkBinDir" } else { $RtkBinDir }
[Environment]::SetEnvironmentVariable("Path", $NewPath, "User")
$env:Path = "$env:Path;$RtkBinDir"
Write-Host "已将 $RtkBinDir 添加到用户 PATH"
} else {
Write-Host "$RtkBinDir 已在 PATH 中"
}
# 刷新当前会话
$env:Path = [Environment]::GetEnvironmentVariable("Path", "User") + ";" + [Environment]::GetEnvironmentVariable("Path", "Machine")
# === [5/5] 配置 AI 工具 ===
Write-Host "`n=== [5/5] 配置 AI 工具 Hook ===" -ForegroundColor Yellow
$RtkExe = Join-Path $RtkBinDir "rtk.exe"
# 验证 rtk 可用
try {
& $RtkExe --version 2>&1 | Out-Null
Write-Host "rtk 已验证: $( & $RtkExe --version )" -ForegroundColor Green
} catch {
Write-Error "rtk 执行失败,请检查安装"
exit 1
}
# Claude Code: 添加 PreToolUse Hook
$ClaudeSettings = Join-Path $env:USERPROFILE ".claude\settings.json"
if (Test-Path $ClaudeSettings) {
Write-Host "`n检测到 Claude Code 配置,正在添加 RTK Hook..." -ForegroundColor Cyan
$Settings = Get-Content $ClaudeSettings -Raw | ConvertFrom-Json
# 检查是否已有 hooks
if (-not $Settings.hooks) {
$Settings | Add-Member -MemberType NoteProperty -Name "hooks" -Value @{} -Force
}
if (-not $Settings.hooks.PreToolUse) {
$Settings.hooks | Add-Member -MemberType NoteProperty -Name "PreToolUse" -Value @() -Force
}
# 检查是否已有 rtk hook
$HasRtkHook = $false
foreach ($hook in $Settings.hooks.PreToolUse) {
if ($hook.matcher -eq "Bash") {
foreach ($h in $hook.hooks) {
if ($h.command -match "rtk hook claude") {
$HasRtkHook = $true
break
}
}
}
}
if (-not $HasRtkHook) {
$RtkHook = @{
matcher = "Bash"
hooks = @(
@{
type = "command"
command = "rtk hook claude"
}
)
}
$Settings.hooks.PreToolUse += $RtkHook
$Settings | ConvertTo-Json -Depth 10 | Set-Content $ClaudeSettings -Encoding UTF8
Write-Host " Claude Code RTK Hook 已添加" -ForegroundColor Green
} else {
Write-Host " Claude Code RTK Hook 已存在,跳过" -ForegroundColor DarkYellow
}
} else {
Write-Host "`n未检测到 Claude Code 配置文件,跳过。(如果已安装,请手动配置)" -ForegroundColor DarkYellow
}
# Codex CLI: 添加 RTK.md 引用
$CodexAgents = Join-Path $env:USERPROFILE ".codex\AGENTS.md"
if (Test-Path (Split-Path $CodexAgents -Parent)) {
Write-Host "`n检测到 Codex CLI,正在添加 RTK 配置..." -ForegroundColor Cyan
$RtkMdPath = Join-Path $env:USERPROFILE ".codex\RTK.md"
# 创建 RTK.md
$RtkMdContent = @"
# RTK - Rust Token Killer (Codex CLI)
**Usage**: Token-optimized CLI proxy for shell commands.
## Rule
Always prefix shell commands with `rtk`.
Examples:
rtk git status
rtk cargo test
rtk npm run build
rtk pytest -q
## Meta Commands
rtk gain # Token savings analytics
rtk gain --history # Recent command savings history
rtk proxy # Run raw command without filtering
"@
Set-Content -Path $RtkMdPath -Value $RtkMdContent -Encoding UTF8
# 确保 AGENTS.md 引用 RTK.md
$AgentsContent = "@C:\Users\$env:USERNAME\.codex\RTK.md"
if (Test-Path $CodexAgents) {
$Existing = Get-Content $CodexAgents -Raw
if ($Existing -notmatch "RTK.md") {
$AgentsContent = "$Existing`n$AgentsContent"
} else {
$AgentsContent = $Existing
Write-Host " Codex CLI AGENTS.md 已包含 RTK 引用" -ForegroundColor DarkYellow
$AgentsContent = $null
}
}
if ($AgentsContent) {
Set-Content -Path $CodexAgents -Value $AgentsContent -Encoding UTF8
Write-Host " Codex CLI RTK 配置已添加" -ForegroundColor Green
}
} else {
Write-Host "`n未检测到 Codex CLI 目录,跳过。" -ForegroundColor DarkYellow
}
# === 完成 ===
Write-Host "`n========================================" -ForegroundColor Green
Write-Host " RTK 安装完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
# 验证
Write-Host "验证安装:" -ForegroundColor Cyan
& $RtkExe --version
Write-Host ""
Write-Host "查看 token 节省数据:" -ForegroundColor Cyan
Write-Host " rtk gain"
Write-Host ""
Write-Host "查看历史记录:" -ForegroundColor Cyan
Write-Host " rtk gain --history"
Write-Host ""
Write-Host "重启 Claude Code / Codex CLI 后生效。" -ForegroundColor Yellow
运行安装
# 保存脚本后,在 PowerShell 中执行:
powershell -ExecutionPolicy Bypass -File .\install-rtk.ps1
安装完成后,Claude Code 重启自动生效;Codex CLI 的 AI 会根据 AGENTS.md 自动在命令前加 rtk 前缀。
四、验证效果
重启 AI 工具后跑几个命令,然后查看节省数据:
rtk gain
输出示例:
RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════
Total commands: 70
Input tokens: 31.7K
Output tokens: 30.7K
Tokens saved: 1.0K (3.3%)
By Command
───────────────────────────────────────────────────────
git status 每次节省 ~90%
ls -la 每次节省 ~90%
pytest 每次节省 ~80%
npm run build 每次节省 ~80%
如果想看详细的命令历史:
rtk gain --history
五、常用命令速查
| 命令 | 作用 |
|---|---|
rtk gain |
查看 token 节省统计 |
rtk gain --history |
查看每条命令的节省详情 |
rtk discover |
分析 Claude Code 历史,找出还没被 RTK 拦截的命令 |
rtk proxy <cmd> |
以原始模式运行命令(不做过滤,用于调试) |
rtk --version |
查看版本 |
rtk git diff |
超精简 diff(只显示改动行) |
rtk pytest -q |
运行 pytest 且只显示失败 |
rtk npm run build |
过滤构建输出的 warning 噪音 |
rtk ls -la |
紧凑的目录列表 |
六、支持的命令(部分)
RTK 内置了 100+ 个命令的过滤器:
版本控制: git, gh, glab, gt
包管理: npm, npx, pnpm, cargo, pip
构建工具: tsc, next, lint, prettier, ruff
测试框架: pytest, jest, vitest, playwright, rspec
数据库: psql, prisma
容器: docker, kubectl
云: aws
日志: log(通用日志去重)
完整列表见 RTK GitHub。
七、常见问题
Q: RTK 会影响命令执行结果吗?
不会。RTK 只在输出层面过滤,不修改命令行为也不修改源文件。如果 RTK 判断失败(比如命令 exit code 非 0),会自动退回完整输出。
Q: 怎么禁用某个命令的过滤?
rtk proxy git log --all # 以原始模式运行,不做过滤
Q: Codex CLI 上似乎没用?
Codex CLI 没有 Hook 机制,RTK 靠 AGENTS.md 中的规则让 AI 主动加 rtk 前缀。如果 AI 没遵守,可以用 codexu wrapper(见下一篇博客)。
Q: 和 GitHub Copilot 能一起用吗?
已支持。用 rtk init --copilot -g 配置。
八、总结
RTK 是那种"装上就忘了"的工具——你不需要学任何东西,它静默地帮你省 Token。
- 单二进制,零依赖,3MB
- 支持 100+ 个常用开发命令
- Claude Code / Codex / Copilot / Cursor 全兼容
- 开源 MIT 协议
GitHub: https://github.com/rtk-ai/rtk
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐
所有评论(0)