Skip to content
Hiep Lam
Go back

Shrinking the Agent Loop, Part 1: How RTK and Caveman Attack Opposite Ends of the Pipe

Edit page 🇻🇳 Tiếng Việt

“Shrinking the Agent Loop: RTK vs Caveman” - Part 1 of 3. A source-level tour of how two token-reduction tools work - one filtering command output on the way in, the other making the model talk less on the way out. This series audits both tools from source, cross-checked against adversarially verified public research.


Two open-source tools set out to solve the same problem - an agent loop burning through its token budget - by attacking it from opposite ends. rtk-ai/rtk (“Rust Token Killer”) is a single Rust binary that intercepts your shell commands and filters their output before it ever reaches the model. JuliusBrussee/caveman (“why use many token when few do trick”) takes the opposite approach: it coaches the model itself into writing tersely, plus a small deterministic compressor on the way in. This post is a source-level look at how each one actually works.

RTK shrinks what goes into the model (noisy command output). Caveman shrinks what comes out of the model (verbose prose), plus a little on the way in. They attack different parts of the agent loop, so they’re complementary - you can run both.

An agentic LLM loop spends tokens in three places: the context it reads (files, tool schemas), the tool results piped back in, and the response it writes. RTK owns the tool-results pipe; Caveman owns the response pipe and dabbles in the context/schemas pipe.

1. RTK (rtk-ai/rtk) - “Rust Token Killer”

What it is: a single compiled Rust binary (no LLM, no network in the hot path, <10ms overhead). ~100 supported commands. It’s a transparent CLI proxy.

End-to-end flow:

  1. Transparent rewrite via a hook. When your agent (Claude Code, Cursor, Copilot, Gemini, OpenCode… 9+ agents) is about to run cargo test, a PreToolUse hook fires. The hook is a thin delegate - it reads the tool-call JSON and calls rtk rewrite "cargo test".
  2. Registry match. src/discover/registry.rs runs a compiled RegexSet over a static RULES table (70+ patterns) and returns rtk cargo test. The hook hands that back as updatedInput, so the agent silently runs the wrapped version. You never see this.
    • Security: the rewriter passes through unchanged (refuses to rewrite) any command with $(...), backticks, heredocs, or process-substitution - a quote-aware lexer.
  3. Run + capture. The rtk binary spawns the real cargo test as a child process and captures stdout/stderr (capped at 10 MiB, ANSI stripped, exit-code aware). RunMode enum: Filtered (capture whole then filter), Streamed (filter line-by-line for huge outputs), Passthrough.
  4. Per-tool deterministic filter. The heart. Each tool has a dedicated Rust parser (cargo_cmd.rs, pytest_cmd.rs, mvn_cmd.rs, gradlew_cmd.rs, tsc_cmd.rs, git/*.rs, aws_cmd.rs, golangci_cmd.rs…). It understands the format: keeps failures + error context + summary counts, drops passing tests, progress spinners, banners. Unknown commands fall back to a generic 8-stage TOML pipeline (.rtk/filters.toml: strip ANSI → regex sub → success short-circuit → strip/keep lines → truncate → head/tail → max_lines → on_empty) or pass through untouched.
  5. Compact result reaches context. cargo test ≈ 25k tokens → RTK returns ≈ 2.5k ("3 failed, 142 passed" + the 3 failures).

It’s lossy-but-structured: throws away the boring 90%, keeps the signal. Savings: 60–90% on command output, measured per-command into a local SQLite DB (~/.local/share/rtk/) via a chars÷4 token estimate, surfaced by rtk gain.

2. Caveman (JuliusBrussee/caveman) - “why use many token when few do trick”

What it is: not a binary - a multi-platform skill/plugin (Node + Python + hooks) for Claude Code, Codex, Gemini, Cursor, OpenCode… 30+ agents. It’s two mechanisms in a trenchcoat:

① Behavioral mode (output side - the real win)

It injects a rule into the system context: “Respond terse like smart caveman. All technical substance stay. Only fluff die.” - delivered via a SessionStart hook (loads SKILL.md) and reinforced every turn via UserPromptSubmit. The model itself then writes shorter answers:

“Sure! I’d be happy to help. The issue is likely your auth middleware…” (69 tok) → “Bug in auth middleware. Token expiry check use < not <=. Fix:” (19 tok)

② Deterministic compressor (input side)

Pure-Node/Python regex (compress.js), no model call:

Savings - read with skepticism: marketplace says ~75%; the live /caveman-stats applies a hardcoded 0.65 constant (only “full” mode is actually benchmarked); and Caveman’s own 10-prompt eval vs an “Answer concisely.” control shows ~50–53% with huge variance (−2% to +87%). Because the win comes from the model choosing to write less, it’s not measurable without a counterfactual - unlike RTK, which sees exactly what it discarded.

3. Point-by-point differences

RTKCaveman
ReducesINPUT tokens (tool/command output)OUTPUT tokens (model’s prose) + some input (schemas/files)
MechanismDeterministic per-tool Rust parsers① LLM behavior change (prompt) · ② regex
LLM in the loop?No① Yes (the model itself) · ② No
RuntimeOne Rust binary, <10ms, offlineNode+Python hooks, MCP proxy
Lossy?Lossy but structured① lossy summarization · ② near-lossless
Coverage~100 known tools (else fallback)Any prose, any task (generic)
Savings basisMeasured raw-vs-filtered (rtk gain)Mostly estimated/behavioral
Best forBuild/test/git/cloud-heavy sessionsExplanation-heavy chat; bloated schemas/CLAUDE.md

4. They’re complementary, not competitors

RTK cuts what the agent reads from tools; Caveman cuts what the agent writes (and the schemas/files it reads). Run both and they stack - with the only seam being MCP/CLAUDE.md compression, and even there they target different content.

Key idea: two different chokepoints, one shared goal. RTK is a deterministic filter on the input pipe; Caveman is a behavioral nudge on the output pipe plus a small regex on the side. Neither replaces the other.

The next post turns to the question that matters once you’ve decided to run either of these inside your agent loop: what does it mean for your security posture to hand a hook this much trust?


Sources

Source-level read of both repositories - rtk-ai/rtk (Rust) and JuliusBrussee/caveman (Node + Python). Benchmark figures are vendor-reported.


Next: Part 2 - Living Inside the Trust Boundary →

The series - Shrinking the Agent Loop: RTK vs Caveman: 1 · How They Work (you are here) · 2 · Living Inside the Trust Boundary · 3 · A NUL Sentinel, a ReDoS, and a Secret Left in Plaintext


Edit page
Share this post:

Previous Post
Beyond Coverage, Part 6: The Agentic Merge-Gate Readiness Rubric
Next Post
Shrinking the Agent Loop, Part 2: Living Inside the Trust Boundary

Keep reading

A few more pieces in the same vein.

Rather read on your own time? Subscribe via RSS