Skip to content

Commit a29c040

Browse files
committed
Redesign RTK integration to be net-positive on tokens and quality
Shift from "always prefix every command with rtk, use ultra-compact for maximum savings" to a selective, lossless-by-default policy so RTK cuts tokens without hiding detail the model needs — detail loss otherwise forces re-runs that cost more than they save and degrade output quality. - AGENTS.md: lean, always-on policy. Compress noisy/low-stakes output; keep full fidelity for diffs/patches, structured/JSON output, secrets, small output, and files to edit; never default to lossy modes; escalate fidelity (not blind re-runs) when a compressed view falls short. - SKILL.md: recommend RTK's auto-rewrite PreToolUse hook (rtk init -g) over manual prefixing; add a fidelity ladder, when-NOT-to-use, and net-savings guidance; bump to 0.2.0. - references/commands.md: reorganize into green/yellow/red tiers (compress / plain mode only / run raw); fix rtk-read vs native Read; drop aggressive-as-default. Leaner, so it costs less always-on context. - references/analytics.md: optimize NET savings — watch `rtk gain --failures` and `rtk discover`; stop wrapping poor fits. - README.md: correct the "no auto-rewrite hook" framing, add a "when RTK helps vs keep full fidelity" rationale grounded in context-rot research, reframe the teaching table by tier. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VvpBXNCbbg4eibmZQZAbb6
1 parent a3f0db8 commit a29c040

5 files changed

Lines changed: 249 additions & 220 deletions

File tree

AGENTS.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
# RTK Token Optimization for Command Code
22

3-
RTK (Rust Token Killer) is installed on this system. It reduces LLM token
4-
consumption by 60-90% by filtering and compressing shell command output.
3+
RTK (Rust Token Killer) compresses **shell command output** before it enters the
4+
context window. Used well it cuts tokens on noisy commands *and* sharpens context
5+
(less noise → better reasoning). Used badly — compressing output you actually
6+
needed — it hides detail and forces re-runs that cost *more* than they save.
7+
These rules keep it net-positive.
58

6-
## Prerequisite
9+
## Setup
710

8-
RTK must be installed and in PATH. Verify with: `rtk --version`
11+
RTK is optional. If `rtk --version` fails, run commands normally — never block
12+
work to install it. Best setup: install RTK's auto-rewrite hook once with
13+
`rtk init -g` (then restart Command Code) so Bash commands are rewritten to `rtk`
14+
automatically and you never prefix by hand. Without a hook, prefix manually per
15+
the tiers in @references/commands.md.
916

10-
If not installed, direct the user to https://github.com/rtk-ai/rtk#installation.
17+
## The one rule: compress noise, preserve signal
1118

12-
## Core Rule
19+
Use `rtk <cmd>` when output is **large, repetitive, low-stakes** — skim, not
20+
study: `rtk ls`, `rtk tree`, `rtk git status`, `rtk git log`, `rtk docker ps`,
21+
`rtk pip list`, and big test/build runs (`rtk cargo test`, or `rtk err <cmd>`
22+
RTK keeps the failures and drops the green).
1323

14-
Always prefix supported shell commands with `rtk`. Instead of running
15-
`git status`, run `rtk git status`. Instead of `ls -la`, run `rtk ls`.
24+
Run the command **raw (no `rtk`)** when you need every character:
1625

17-
@references/commands.md
18-
@references/analytics.md
26+
- a **diff/patch you'll apply**, or a hunk you'll edit — exact bytes and line numbers matter
27+
- **structured output you'll parse** — JSON, `--format=…`, anything piped into another tool
28+
- **small output** (≲30 lines) — nothing to save, and real risk of dropping the one line you need
29+
- **secrets / exact config** — never reason about a lossy view of these
30+
- a file you intend to **edit** — use the native Read tool (lossless; it bypasses RTK anyway)
31+
32+
When unsure, start raw. Lean context comes from cutting *noise*, not *signal*.
1933

20-
## Ultra-Compact Mode
34+
## Never default to lossy modes
2135

22-
Add `-u` or `--ultra-compact` for maximum savings:
23-
```bash
24-
rtk git status -u
25-
```
36+
Plain `rtk <cmd>` is near-lossless (keeps errors, diffs, stack traces, exit
37+
codes). `-u` / `--ultra-compact`, `rtk read … -l aggressive`, and `rtk smart`
38+
(2-line summary) are **lossy** — opt-in only for skimming something huge and
39+
unimportant, never your default.
2640

27-
## Exclusions
41+
## If a compressed view isn't enough
2842

29-
- Agent built-in file tools (non-Bash) do not pass through RTK
30-
- Use shell equivalents: `rtk read <file>`, `rtk grep <pattern>`, `rtk find <pattern>`
31-
- Piped commands (`|`) and heredocs (`<<`) bypass RTK rewrite
32-
- Commands already prefixed with `rtk` are left as-is
43+
RTK retains the full unfiltered output, so you don't have to guess and you
44+
shouldn't thrash: re-run that one command raw (or `rtk proxy <cmd>`) to see
45+
everything, then move on. One deliberate re-run is fine; blind repeated re-runs
46+
erase the savings.
47+
48+
@references/commands.md

README.md

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ learns your coding style.
99
## How It Works
1010

1111
Command Code CLI reads `~/.commandcode/AGENTS.md` and injects it into the system
12-
prompt for every session. This repository provides that AGENTS.md file, teaching
13-
Command Code to prefix shell commands with `rtk` for automatic output compression.
12+
prompt for every session. This repository provides that AGENTS.md file (plus a
13+
loadable skill), teaching Command Code to route **noisy** shell output through
14+
`rtk` while keeping **full fidelity** for output it needs exactly. The fastest
15+
setup is RTK's auto-rewrite hook (`rtk init -g`), which rewrites commands for you
16+
so nothing has to be prefixed by hand.
1417

1518
```
1619
Without RTK: With RTK:
@@ -20,6 +23,37 @@ cargo test (25,000 tokens) rtk cargo test (2,500 tokens)
2023
ls -la (800 tokens) rtk ls (150 tokens)
2124
```
2225

26+
## When RTK helps — and when to keep full fidelity
27+
28+
RTK only pays off when it removes **noise**. This integration is deliberately
29+
selective:
30+
31+
**Compress (🟢/🟡)** — large, repetitive, low-stakes output you skim: listings,
32+
`git status` / `log`, dependency installs, container/cluster status, and big
33+
test/build runs (plain `rtk` keeps the failures and drops the green).
34+
35+
**Keep full fidelity (🔴)** — run the bare command for diffs/patches you'll apply,
36+
JSON or `--format` output you'll parse, secrets, small outputs, and files you'll
37+
edit (use the agent's native file tools). Compressing these is exactly what makes
38+
a tool like this *cost* tokens — the model loses the detail and re-runs the
39+
command.
40+
41+
Two design choices keep it net-positive:
42+
43+
- **Lossless by default.** Plain `rtk <cmd>` preserves errors, stack traces, diff
44+
hunks, and exit codes; the lossy modes (`-u` / `--ultra-compact`, `-l
45+
aggressive`, `rtk smart`) are opt-in for skimming only — never the default. On
46+
output it can't parse, RTK falls back to the full raw text.
47+
- **Measure net, not gross.** `rtk gain` reports gross savings; the goal is *net*
48+
— savings minus any re-runs and minus the standing cost of these instructions.
49+
`rtk gain --failures` and `rtk discover` show where RTK fits and where it
50+
doesn't (see [references/analytics.md](references/analytics.md)).
51+
52+
This matters for quality too: every frontier model degrades as irrelevant context
53+
grows ("context rot" / "lost in the middle"), so cutting genuine noise can *help*
54+
reasoning — while over-compressing real signal hurts it. The rules above aim for
55+
the first and avoid the second.
56+
2357
## Installation
2458

2559
### Prerequisites
@@ -36,6 +70,11 @@ ls -la (800 tokens) rtk ls (150 tokens)
3670
> enforcement, install the memory (Method 2); the skill (Method 1) is the
3771
> quickest install and is enough when you mainly want it during shell-heavy work.
3872
73+
> **Even better — the auto-rewrite hook.** RTK can install a `PreToolUse` hook
74+
> (`rtk init -g`) that rewrites Bash commands to `rtk` automatically, so the agent
75+
> never prefixes anything by hand. Pair it with the memory or skill below, which
76+
> carry the *when-to-compress* rules. See `rtk init --help`.
77+
3978
### Method 1: Skill (quick install, on-demand)
4079

4180
```bash
@@ -81,22 +120,24 @@ cp references/analytics.md ~/.commandcode/references/analytics.md
81120

82121
## What It Teaches the Agent
83122

84-
Once installed, Command Code will automatically use RTK-optimized commands:
123+
Once installed, Command Code routes **noisy, low-stakes** commands through RTK and
124+
leaves precise output alone:
85125

86-
| Category | Without RTK | With RTK | Savings |
126+
| Category | Command | Through RTK? | Est. savings |
87127
|---|---|---|---|
88-
| Git | `git status` | `rtk git status` | ~80% |
89-
| Files | `cat file.rs` | `rtk read file.rs` | ~70% |
90-
| Search | `grep -r "pattern" .` | `rtk grep "pattern" .` | ~80% |
91-
| Tests | `cargo test` | `rtk cargo test` | ~90% |
92-
| Build | `cargo build` | `rtk cargo build` | ~80% |
93-
| Docker | `docker ps` | `rtk docker ps` | ~80% |
94-
| GitHub | `gh pr list` | `rtk gh pr list` | ~80% |
128+
| Status / listings | `rtk git status`, `rtk ls`, `rtk tree` | 🟢 yes | ~80% |
129+
| Logs / containers | `rtk docker ps`, `rtk log app.log` | 🟢 yes | ~80% |
130+
| Dependencies | `rtk pip list`, `rtk pnpm list` | 🟢 yes | ~70% |
131+
| Tests / build | `rtk cargo test`, `rtk err <cmd>` | 🟡 plain mode (keeps failures) | ~90% |
132+
| Diffs you'll apply | `git diff`, `git show` | 🔴 run raw | |
133+
| JSON / parsed output | `… --format json` | 🔴 run raw | |
134+
| Files you'll edit | native Read tool | 🔴 not RTK | |
95135

96-
_Savings are illustrative estimates; actual numbers vary by command and output
97-
size. Run `rtk gain` to measure your own._
136+
_Savings are illustrative; actual numbers vary by command and output size. Run
137+
`rtk gain` to measure your own — and `rtk gain --failures` to spot poor fits._
98138

99-
See [references/commands.md](references/commands.md) for the full list.
139+
See [references/commands.md](references/commands.md) for the full tiered list and
140+
[references/analytics.md](references/analytics.md) for measuring net savings.
100141

101142
## Verify
102143

@@ -106,7 +147,10 @@ rtk gain --graph # Visual savings chart
106147
```
107148

108149
After running a few commands through Command Code, `rtk gain` will show the
109-
accumulated savings.
150+
accumulated savings. Track **net** savings, not just the headline number:
151+
`rtk gain --failures` lists commands RTK had to pass through raw (poor fits), and
152+
`rtk discover` finds new high-value targets. See
153+
[references/analytics.md](references/analytics.md).
110154

111155
## Files
112156

SKILL.md

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
---
22
name: rtk-token-optimizer
33
description: >-
4-
Compress shell command output to cut LLM token usage 60-90% by prefixing
5-
commands with `rtk`. Use whenever running shell commands in Command Code —
6-
git, cargo, npm/pnpm, pytest/jest, docker, kubectl, gh, grep, find, ls, cat,
7-
build and lint tools — so large outputs are filtered before entering context.
4+
Cut LLM token usage on noisy shell output by routing large, low-stakes
5+
commands (listings, status, logs, dependency installs, test/build runs)
6+
through RTK — while keeping full fidelity for diffs, structured/JSON output,
7+
errors you're debugging, and anything you'll parse or edit. Use when running
8+
shell commands in Command Code that produce big, repetitive output.
89
license: Apache-2.0
910
compatibility: >-
10-
Requires the RTK binary (>= 0.42.0) installed and on PATH. Verify with
11-
`rtk --version`. Install from https://github.com/rtk-ai/rtk
11+
Requires the RTK binary (>= 0.42.0) on PATH. Verify with `rtk --version`.
12+
Install from https://github.com/rtk-ai/rtk. RTK is optional — run commands
13+
normally if it isn't installed.
1214
metadata:
1315
author: Coding-Dev-Tools
14-
version: "0.1.0"
16+
version: "0.2.0"
1517
homepage: https://github.com/rtk-ai/rtk
1618
allowed-tools: Bash(rtk:*) Bash(git:*) Bash(cargo:*) Bash(ls:*) Bash(cat:*) Bash(grep:*) Bash(find:*) Bash(diff:*) Bash(docker:*) Bash(kubectl:*) Bash(gh:*) Bash(glab:*) Bash(pnpm:*) Bash(npm:*) Bash(pip:*) Bash(bundle:*) Bash(ruff:*) Bash(tsc:*) Bash(eslint:*) Bash(pytest:*) Bash(go:*) Bash(jest:*) Bash(vitest:*) Bash(dotnet:*) Bash(aws:*) Bash(psql:*) Bash(prisma:*) Bash(wget:*)
1719
---
@@ -20,67 +22,91 @@ allowed-tools: Bash(rtk:*) Bash(git:*) Bash(cargo:*) Bash(ls:*) Bash(cat:*) Bash
2022

2123
## What it is
2224

23-
[RTK](https://github.com/rtk-ai/rtk) (Rust Token Killer) is a CLI proxy that
24-
filters and compresses shell command output before it reaches the context
25-
window — removing noise, grouping similar items, truncating redundancy, and
26-
deduplicating repeated lines. Typical savings are 60-90% on common dev
27-
commands. RTK ships as a single self-contained Rust binary that adds minimal
28-
per-command overhead.
25+
[RTK](https://github.com/rtk-ai/rtk) (Rust Token Killer) is a single Rust binary
26+
that filters **shell command output** before it reaches the context window —
27+
dropping noise (progress bars, passing tests, decorative formatting, repeated log
28+
lines) while keeping signal (errors, stack traces, diff hunks, exit codes). When
29+
it can't parse a command's output it falls back to the full raw text, so it never
30+
silently eats data. Typical savings are 60–90% on noisy commands, at <10 ms
31+
overhead.
2932

30-
## How it works
33+
Done right this is a **double win**: fewer tokens *and* a leaner context, which
34+
measurably improves model reasoning — every frontier model degrades as irrelevant
35+
context grows ("context rot" / "lost in the middle"). Done wrong —
36+
over-compressing output you actually needed — it hides detail and triggers
37+
re-runs that cost more than they saved. The rest of this skill is how to stay on
38+
the winning side.
3139

32-
```
33-
You run: git status
34-
RTK rewrites: rtk git status
35-
RTK filters: smart filtering · grouping · truncation · deduplication
36-
Result: ~200 tokens enter context instead of ~2,000
40+
## Install — two ways
41+
42+
### 1. Auto-rewrite hook (recommended)
43+
44+
Let a `PreToolUse` hook rewrite Bash commands to their `rtk` equivalents
45+
automatically, so you never prefix by hand and nothing gets forgotten. RTK ships
46+
this for natively-supported agents:
47+
48+
```bash
49+
rtk init -g # installs the PreToolUse rewrite hook, then restart the agent
3750
```
3851

39-
## Core rule
52+
RTK natively targets Claude Code, Copilot, Cursor, Gemini, Cline, and more.
53+
Command Code also supports `PreToolUse` hooks — register one that pipes the Bash
54+
command through RTK's rewrite (see the Command Code hooks docs and `rtk init
55+
--help`). With a hook installed you **run normal commands**, and only need the
56+
fidelity rules below for the cases where you want to *bypass* compression.
4057

41-
Prefix supported shell commands with `rtk`. Run `rtk git status` instead of
42-
`git status`, `rtk cargo test` instead of `cargo test`, `rtk read file.rs`
43-
instead of `cat file.rs`.
58+
### 2. Manual prefixing (no hook)
4459

45-
Most-used commands:
60+
Without a hook, prefix commands yourself per the tiers in
61+
[references/commands.md](references/commands.md). Reliable, but you have to
62+
remember it — and `|` pipes and `<<` heredocs bypass the rewrite.
4663

47-
| Instead of | Use |
48-
|---|---|
49-
| `git status` / `git diff` | `rtk git status` / `rtk git diff` |
50-
| `ls -la` | `rtk ls` |
51-
| `cat file` | `rtk read file` |
52-
| `grep -r "x" .` | `rtk grep "x" .` |
53-
| `cargo test` / `pytest` / `npm test` | `rtk cargo test` / `rtk pytest` / `rtk jest` |
54-
| `docker ps` | `rtk docker ps` |
64+
## Decision rule: compress noise, preserve signal
5565

56-
Full command list: [references/commands.md](references/commands.md).
57-
Savings analytics: [references/analytics.md](references/analytics.md).
66+
- 🟢 **Compress freely** — large, noisy, low-stakes output you skim:
67+
`rtk ls`, `rtk git status`, `rtk git log`, `rtk docker ps`, `rtk pip list`.
68+
- 🟡 **Default mode only** — big runs where you need the failures: `rtk cargo
69+
test`, `rtk err <cmd>`. Plain `rtk` keeps errors/diffs — don't add `-u`.
70+
- 🔴 **Keep full fidelity (run raw)** — diffs/patches you'll apply, JSON or
71+
`--format` output you'll parse, secrets, small outputs, and files you'll edit
72+
(use the native Read tool).
5873

59-
## Prerequisite
74+
Full tiered table: [references/commands.md](references/commands.md).
6075

61-
RTK must be on PATH. If `rtk --version` fails, install it:
76+
## Fidelity ladder
6277

63-
- **macOS:** `brew install rtk`
64-
- **Linux/macOS:** `curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | sh`
65-
- **Windows:** download `rtk-x86_64-pc-windows-msvc.zip` from the [releases page](https://github.com/rtk-ai/rtk/releases) and put `rtk.exe` on PATH.
78+
Use the *least* compression that still answers the question:
6679

67-
## Ultra-compact mode
80+
```
81+
raw / native Read → rtk <cmd> (near-lossless, default) → -u / -l aggressive / rtk smart (lossy, skim-only)
82+
```
6883

69-
Add `-u` / `--ultra-compact` for maximum savings: `rtk git status -u`.
84+
Start as far left as the task needs. Escalate compression only for big, boring
85+
output; escalate *fidelity* (drop back to raw or `rtk proxy <cmd>`) the moment a
86+
compressed view is missing something — once, deliberately, not by re-running
87+
blindly.
7088

7189
## When NOT to use RTK
7290

73-
- RTK filters **command output**, not conversation messages.
74-
- Piped commands (`|`) and heredocs (`<<`) bypass the rewrite.
75-
- No auto-rewrite hook in Command Code — prefix each command with `rtk` explicitly.
91+
- Diffs/patches you'll apply, JSON/`--format` you'll parse, secrets, small
92+
outputs — run raw.
93+
- Files you'll edit — use the native Read tool (lossless; bypasses RTK anyway).
94+
- It filters command output, not conversation messages; `|` and `<<` bypass the
95+
hook.
7696

77-
## Verify savings
97+
## Measure net savings
7898

79-
```bash
80-
rtk gain # session summary
81-
rtk gain --graph # 30-day savings chart
82-
```
99+
`rtk gain` shows gross savings; `rtk gain --failures` shows what passed through
100+
raw; `rtk discover` finds good new targets. Optimize **net** tokens (savings
101+
minus re-runs), not the headline number. Full reference:
102+
[references/analytics.md](references/analytics.md).
103+
104+
## Prerequisite
105+
106+
RTK on PATH (`rtk --version`). If missing:
107+
108+
- **macOS:** `brew install rtk`
109+
- **Linux/macOS:** `curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | sh`
110+
- **Windows:** download `rtk-x86_64-pc-windows-msvc.zip` from the [releases page](https://github.com/rtk-ai/rtk/releases) and put `rtk.exe` on PATH.
83111

84-
Savings vary by command and output size; `rtk gain` reports your actual
85-
numbers. See [references/analytics.md](references/analytics.md) for the full
86-
analytics reference.
112+
RTK is optional; never block work to install it.

references/analytics.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
# RTK Analytics
1+
# RTK Analytics — measure *net* savings, not just gross
22

3-
Quantify token savings with `rtk gain`, and find more opportunities with
4-
`rtk discover`. This is the canonical analytics reference (command rewrites
5-
live in [commands.md](commands.md)).
3+
`rtk gain` reports **gross** tokens saved. The number that actually matters is
4+
**net**: gross savings minus (a) tokens spent re-running commands when a
5+
compressed view hid something, and (b) the standing cost of these instructions in
6+
context. Optimize for net.
67

7-
| Command | Description |
8+
| Command | Use it to |
89
|---|---|
9-
| `rtk gain` | Session summary: tokens saved, efficiency meter |
10-
| `rtk gain --graph` | ASCII bar chart of daily savings (last 30 days) |
11-
| `rtk gain --history` | Recent command history with per-command savings |
12-
| `rtk gain --daily` | Day-by-day breakdown |
13-
| `rtk gain --weekly` | Weekly breakdown |
14-
| `rtk gain --monthly` | Monthly breakdown |
15-
| `rtk gain --all --format json` | JSON export for dashboards |
16-
| `rtk gain --quota` | Monthly quota savings estimate |
17-
| `rtk gain --failures` | Commands that bypassed RTK (parse-failure log) |
18-
| `rtk gain --reset --yes` | Reset all saved-token counters to zero |
19-
| `rtk discover` | Find commands that could benefit from RTK |
20-
| `rtk discover --all --since 7` | All projects, last 7 days |
10+
| `rtk gain` | session summary: tokens saved, efficiency |
11+
| `rtk gain --graph` | 30-day savings trend |
12+
| `rtk gain --history` | per-command savings — see where RTK actually pays off |
13+
| `rtk gain --failures` | commands RTK **couldn't parse** and passed through raw |
14+
| `rtk discover` | find *good* new opportunities (don't blanket-apply) |
2115
| `rtk session` | RTK adoption across recent sessions |
16+
| `rtk gain --all --format json` | export for dashboards (run raw if you'll parse it) |
2217

23-
Run `rtk gain` periodically to quantify actual savings; numbers vary by
24-
command and output size.
18+
## Reading the signal
19+
20+
- **High `--history` savings on noisy commands** → working as intended; keep going.
21+
- **Entries in `--failures`** → RTK fell back to raw output for those, so they
22+
aren't saving — and may be a poor fit (structured or edge-case commands). Run
23+
them raw and stop wrapping them.
24+
- **You re-ran a command raw right after its `rtk` version** → that pair was a net
25+
*loss*. Note the command type and stop compressing it.
26+
- **`rtk discover`** surfaces high-volume, noisy commands worth wrapping — a far
27+
better guide than wrapping everything by reflex.
28+
29+
Savings vary by command and output size; let `rtk gain` show your real numbers
30+
rather than assuming the headline 60–90%.

0 commit comments

Comments
 (0)