Skip to main content
RTK achieves 60-90% token savings through four core strategies: filtering, grouping, truncation, and deduplication. Each command uses one or more strategies tailored to its output format.

The Four Strategies

1. Filtering

Remove noise (comments, whitespace, boilerplate) while preserving structure

2. Grouping

Aggregate similar items (files by directory, errors by type)

3. Truncation

Keep relevant context, cut redundancy (first/last lines, signatures only)

4. Deduplication

Collapse repeated patterns with counts (“[ERROR] … (×5)“)

Strategy Matrix

Different commands use different strategies:

Language-Aware Filtering

RTK’s filter.rs module provides language-aware code filtering with three levels:

Filter Levels

Keep everything—raw file content.
Strip comments and normalize whitespace. Keep structure and code.
Strip comments and function bodies. Keep only signatures.

Language Support

RTK detects languages by file extension:

Usage Examples

When to use aggressive? When LLMs need to understand code structure but not implementation details. Perfect for “what functions exist?” queries.

Command-Specific Strategies

Git Operations

Raw output (50 lines, ~800 tokens):
RTK output (1 line, ~20 tokens):
Strategy:
  1. Count modified files: 3
  2. Count untracked files: 1
  3. Aggregate: “3 modified, 1 untracked”
  4. Token savings: 97%
Raw output (200+ lines, ~3000 tokens):
RTK output (~30 lines, ~500 tokens):
Strategy:
  1. Extract stats: +142/-89
  2. Group by file
  3. Show summary per file
  4. Token savings: 83%
Raw output (50 lines, ~1000 tokens):
RTK output (5 lines, ~100 tokens):
Strategy:
  1. Count commits: 5
  2. Extract total stats: +142/-89
  3. Show first line of each commit message
  4. Token savings: 90%

Testing

Raw output (200+ lines, ~4000 tokens):
RTK output (~10 lines, ~200 tokens):
Strategy:
  1. Hide passing tests (17 passed → omitted)
  2. Show only failures (2 failed)
  3. Include error messages for debugging
  4. Token savings: 95%
Raw output (100+ lines, ~2000 tokens):
RTK output (~5 lines, ~100 tokens):
Strategy (Rust):
  1. Hide passing tests (14 passed → omitted)
  2. Extract failure details (panic message, file:line)
  3. Token savings: 95%
Strategy (Go - NDJSON):
  1. Parse line-by-line JSON events
  2. Track test state per package
  3. Aggregate failures only
  4. Token savings: 90%

Linting

Raw output (150 lines, ~2500 tokens):
RTK output (~15 lines, ~300 tokens):
Strategy:
  1. Parse error lines (regex: file:line:col rule)
  2. Group by rule (no-unused-vars: 23, semi: 45, …)
  3. Group by file (auth.ts: 8, db.ts: 15, …)
  4. Token savings: 88%

Logs & Data

Raw output (1000+ lines, ~20000 tokens):
RTK output (~5 lines, ~100 tokens):
Strategy:
  1. Identify repeated lines (exact match)
  2. Collapse with counts: ”(×127)”
  3. Keep first occurrence + count
  4. Token savings: 99%
Raw output (500 lines, ~10000 tokens):
RTK output (~10 lines, ~200 tokens):
Strategy:
  1. Parse JSON structure
  2. Extract keys + types
  3. Count array lengths
  4. Strip values (especially large strings/base64)
  5. Token savings: 98%

Advanced Patterns

State Machine Parsing (pytest)

Pytest output doesn’t have JSON mode—RTK uses a state machine to parse text:
Result: Only failed tests appear in output (90% reduction).

NDJSON Streaming (go test)

Go’s test runner outputs newline-delimited JSON with interleaved package events:
RTK parses line-by-line and tracks state per package:
Result: Aggregated failures per package (90% reduction).

Package Manager Detection (JS/TS)

Modern JS/TS commands auto-detect package managers:
Why this matters:
  • CWD preservation: pnpm/yarn exec preserve working directory
  • Monorepo support: Works in nested package.json structures
  • No global installs: Uses project-local dependencies only

Choosing the Right Strategy

1

Identify output format

Is it structured (JSON, NDJSON) or unstructured (text, logs)?
2

Determine information density

High density (code) → filtering. Low density (test results) → failure focus.
3

Check for repetition

Repeated patterns (logs, errors) → deduplication. Unique items → grouping.
4

Measure effectiveness

Aim for 60%+ reduction. If <60%, consider combining strategies.

Best Practices

Prioritize structure

Keep structure (function signatures, file paths) and drop details (implementations, values)

Focus on failures

LLMs need to see errors, not successes. Hide passing tests, show only failures.

Use JSON when available

Structured formats (JSON, NDJSON) are easier to parse and compress than text.

Preserve exit codes

Always propagate exit codes for CI/CD reliability. Filter output, not behavior.