Architecture Overview
RTK uses a proxy pattern with three core layers:Command Interception
When you run
rtk git status, RTK receives the command and routes it to the appropriate module (git.rs, lint_cmd.rs, etc.)Execution & Filtering
RTK executes the real command (
git status), captures the raw output, and applies command-specific filtering strategiesProxy Pattern Flow
Hook-Based Command Rewriting
The recommended deployment mode uses a Claude Code PreToolUse hook for 100% transparent command rewriting. Claude never sees the rewrite—it just gets optimized output.
Hook Installation
The hook is installed automatically withrtk init -g:
What gets installed?
What gets installed?
~/.claude/hooks/rtk-rewrite.sh- Shell script with command detection~/.claude/RTK.md- Minimal context hint (10 lines, 99.5% token savings vs full injection)~/.claude/settings.json- Hook registry (with backup to settings.json.bak)@RTK.mdreference in~/.claude/CLAUDE.md
Auto-Rewrite (default)
- Intercepts and modifies commands before execution
- 100% adoption (forced)
- Zero context overhead
- Best for: production workflows
Suggest (non-intrusive)
- Emits system reminder hints
- ~70-85% adoption (Claude decides)
- Minimal context overhead
- Best for: learning, auditing
Six-Phase Execution Lifecycle
Every RTK command follows this flow:Phase 1: PARSE
- Command:
Commands::Git - Args:
["log", "--oneline", "-5"] - Flags:
verbose = 1
Phase 2: ROUTE
main.rs matchesCommands::Git and calls git::run(args, verbose)
Phase 3: EXECUTE
Phase 4: FILTER
git::format_git_output(stdout, "log", verbose) applies:
- Strategy: Stats Extraction
- Count commits: 5
- Extract stats: +142/-89
- Compress: “5 commits, +142/-89” (20 chars, 96% reduction)
Phase 5: PRINT
Phase 6: TRACK
input_tokens: 125 (500 / 4)output_tokens: 5 (20 / 4)savings_pct: 96.0
Exit Code Preservation
Critical for CI/CD: RTK always preserves exit codes from underlying tools. If
git push fails with exit code 1, RTK exits with code 1.- CI/CD pipelines rely on exit codes for build success/failure
- Pre-commit hooks need accurate failure signals
- Git workflows require proper exit code propagation
Fallback Behavior on Errors
RTK follows a fail-safe design: if filtering fails, it falls back to raw command execution.Design Principles
Single Responsibility
Each module handles one command type (git.rs for Git, lint_cmd.rs for ESLint, etc.)
Minimal Overhead
~5-15ms proxy overhead per command (measured with hyperfine)
Exit Code Preservation
CI/CD reliability through proper exit code propagation
Fail-Safe
If filtering fails, fall back to original output
Transparent
Users can always see raw output with
-v flagsZero Config
Works out of the box, no setup required (hook optional)
Performance Characteristics
| Metric | Target | Verification |
|---|---|---|
| Startup time | <10ms | hyperfine 'rtk git status' |
| Memory overhead | <5MB resident | /usr/bin/time -l rtk git status |
| Token savings | 60-90% | Measured on real workflows |
| Binary size | <5MB stripped | Release build |
Verbosity Levels
RTK supports three verbosity levels for debugging:| Flag | Behavior |
|---|---|
| (none) | Compact output only |
-v | + Debug messages (eprintln! statements) |
-vv | + Command being executed |
-vvv | + Raw output before filtering |
