Skip to content

rtk gain reports unreproducible savings — read/grep/tail show 0% compression #2762

Closed as duplicate

Description

@Co8epp

RTK v0.42.4 — Bug Report

Summary: rtk gain reports token-savings figures that are not reproducible via measured output size. The largest reported contributor (rtk read, credited with ~90M tokens saved) produces byte-identical output to the raw command (0% reduction) in controlled tests, including on 1.2 MB and 10.7 MB files. Separately, the highest-token commands (read/cat, grep, head, tail) receive 0% compression, while only small structured-output commands (ls, find, ps, env, wc) are actually compressed.


Environment

  • rtk 0.42.4 (/home/<user>/.local/bin/rtk)
  • Linux (WSL2), commands run via the Claude Code Bash hook (auto-prefixing rtk)
  • rtk gain reported (global scope): 116 commands, "Tokens saved: 90.0M (100.0%)", top row rtk read 16 calls / 90.0M saved

Methodology

For each command we compared:

  • Raw output: rtk proxy <cmd> | wc -c
  • Filtered output: rtk <cmd> | wc -c

Savings % = (raw - filtered) / raw. Correctness verified by grepping for a unique marker in filtered output (it survived → no corruption). Command counter (rtk gain → "Total commands") was checked before/after and did increment, confirming the hook is active.


Results (measured output bytes)

Command Raw (bytes) Filtered (bytes) Savings
wc -l <file> 30 4 86% ✅
env 3,785 749 80% ✅
ls -la <dir> 1,760 568 67% ✅
find <dir> -name '*.py' 251 101 59% ✅
ps aux 4,794 2,742 42% ✅
cat <file> / read 6,025 6,025 0% ❌
read amrts.log (1.2 MB) 1,215,833 1,215,833 0% ❌
read yields.json (10.7 MB) 10,777,373 10,777,373 0% ❌
grep -rn def <dir> 2,871 2,871 0% ❌
head -100 <log> 9,822 9,822 0% ❌
tail -100 <log> 11,516 11,516 0% ❌
du -ah <dir> 623 623 0% ❌
df -h 870 862 ~0% ❌

Issue 1 — rtk gain savings appear overstated / not reproducible

rtk gain credits ~90.0M tokens saved (100%), dominated by rtk read (16 calls, 90.0M). But in controlled tests, rtk read returns output identical to the raw file at the byte level for every size tested (24 KB, 1.2 MB, 10.7 MB) → 0% actual reduction. The reported savings cannot be reproduced from output size and appear inconsistent with observed behavior. Recommend auditing how read savings are computed and what baseline gain compares against.

Issue 2 — Highest-token commands are not compressed

The commands that produce the most output — and therefore where token savings would matter most — are exactly the ones with 0% compression: read/cat, grep, head, tail. Meanwhile the commands that are compressed (ls, find, ps, env, wc) already emit small outputs, so the absolute savings are minor (e.g., 80% of 3.8 KB). Net practical token savings on real workloads (reading files, grepping code, tailing logs) appear negligible.


What works well (credit where due)

  • No output corruption — a unique marker survived filtering; byte-for-byte passthrough on reads.
  • Real compression on structured commands: ls (67%), find (59%), ps (42%), env (80%), wc (86%).
  • Hook/tracking is active — command counter increments correctly.

Reproduction

# Compression per command (raw vs filtered)
t() { raw=$(rtk proxy "$@" 2>/dev/null | wc -c); filt=$(rtk "$@" 2>/dev/null | wc -c);
      echo "$1: raw=$raw filt=$filt saved=$(( raw>0 ? (raw-filt)*100/raw : 0 ))%"; }

t cat  <some_file>
t read <some_large_file>     # 0% even on multi-MB files
t grep -rn def <some_dir>    # 0%
t head -100 <some_log>       # 0%
t ls -la <some_dir>          # ~67%
t env                        # ~80%
t wc -l <some_file>          # ~86%

# gain metric vs reality
rtk gain    # note "rtk read ... saved" — compare to the 0% measured above

Suggested priorities

  1. Fix/validate rtk gain accounting (esp. the read savings attribution) so reported savings match measured output reduction.
  2. If read/grep compression is intended, verify it's actually engaging (it returns raw output in this build).
  3. Consider that compressing read/grep/tail output (the large ones) is where the real token wins are.

Findings gathered via controlled raw-vs-filtered byte measurements, 2026-06-30.

Activity

  1. pszymkowiak commented on Jul 1, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 92%

    Summary

    rtk gain overstates savings with unreproducible accounting (90M tokens credited to read with 0% measured compression); read/grep/head/tail produce byte-identical output to raw, while only small structured-output commands are actually compressed


    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. thangtran7314 commented on Jul 1, 2026

    @thangtran7314

    Hi, I can help with this and keep the change focused.

    I looked at "rtk gain reports unreproducible savings — read/grep/tail show 0% compression" in rtk-ai/rtk and the scope looks suitable for a small, reviewable fix.
    What stood out to me: Summary: rtk gain reports token-savings figures that are not reproducible via measured output size. The largest reported contributor (rtk read, cre... / rtk 0.42.4 (/home/<user/.local/bin/rtk)

    How I would approach it:

    1. confirm the impact and scope first so the fix stays targeted.
    2. add the guard/validation at the sensitive boundary with a small diff.
    3. add a negative regression test and document the verification path.

    I will keep the first PR small; if the fix needs to touch more than expected, I will call that out before widening the scope.
    HARUWE: Small, testable fixes with clear progress and reliable handoff.
    Profile: HARUWE | @thangtran7314 (https://github.com/thangtran7314)
    Estimated turnaround: same day to 24 hours.
    For payment, I can follow the project bounty/payment flow after acceptance.

    If you are comfortable with this plan, I can start with reproduction and a narrow PR.

    Payment details can follow after assignment through the project-approved flow.

  3. aeppling commented on Jul 2, 2026

    @aeppling
    Contributor

    Hey @Co8epp most of this should be fixed since 0.43.0

    Thus being said some tools like grep can sometimes produce 0 savings, because outputs are already enough compressed.
    Could you please upgrade to 0.43.0 and give me your results ?

    Also i'm working on #2781 which should help for more coverage as 0.44.0 lands

  4. self-assigned this
    on Jul 2, 2026
  5. RichelynScott commented on Jul 21, 2026

    @RichelynScott

    Independent corroboration at ~100x the sample size, plus one detail that may explain why this is hard to debug post-hoc.

    Sample: 11,932 tracked commands over 5 weeks, 15+ projects, rtk 0.42.2, Linux/WSL2, Claude Code Bash hook.

    1. Same pattern, same magnitude

    rtk gain reports 187.8M saved (96.6%). A single row accounts for 176.1M of it: one cat of a large CSV. Excluding just that one command drops the global figure to 63.6%; excluding all >1M-token single commands drops it to 56.6%. As in your report, rtk read is credited with the overwhelming majority of all savings (184.3M of 187.8M).

    2. Not reproducible here either

    $ wc -c FYI.md
    71863
    $ rtk read FYI.md | wc -c
    71863                      # 0%
    
    $ wc -c <large .log>
    3114114
    $ rtk read <large .log> | wc -c
    3114114                    # 0%

    Yet the DB has rows for that same cat FYI.md recording input_tokens=71827, output_tokens=588 (99.2% saved).

    The rtk read savings distribution across 1,092 calls is strikingly bimodal:

    Savings bucket Calls Input Output
    0% 471 461.9K 461.9K
    0–50% 19 44.1K 30.1K
    50–90% 292 536.7K 102.7K
    90–100% 310 183,918.5K 91.1K

    That top bucket — 310 calls claiming ~184M input compressed to 91K output — is where essentially all reported savings live, and it's the bucket that does not reproduce.

    3. Why post-hoc diagnosis is blocked: rtk_cmd is a constant for read

    src/cmds/system/read.rs tracks with a hardcoded label:

    timer.track(
        &format!("cat {}", file.display()),
        "rtk read",           // <-- literal constant, not the actual invocation
        &content,
        &rtk_output,
    );

    So every rtk read row stores rtk_cmd = "rtk read" regardless of the flags actually used (--max-lines, --tail, filter level, etc.). The recorded invocation cannot be replayed, which makes it impossible to tell from history whether a 99% row used a filter level/flag that the bare reproduction command doesn't.

    Suggest, independent of the root cause: record the real argv in rtk_cmd for read (as other handlers appear to). That alone would make this class of discrepancy diagnosable from the DB instead of requiring live re-runs against files that have since changed.

    4. Consequence for rtk gain

    Because savings are this concentrated (one row = 94% of the global total; one bucket = ~99% of read's total), a mean-only report is very fragile. Worth considering for gain: outlier flagging/exclusion, a distribution view, and a "0% savings" bucket count — currently 55.3% of all 11,932 commands here saved exactly 0% and that's invisible in the headline.

  6. Yohjisakamoto commented on Jul 22, 2026

    @Yohjisakamoto

    This unreproducible savings report reinforces why end-to-end agent outcomes matter more than compressed text alone: DeepSWE found Tura cuts coding-agent rounds by 89% and total token cost by 80%; would you evaluate the methodology? https://turaai.net/docs#benchmark-current-test-set-record

  7. negativetime commented on Aug 10, 2026

    @negativetime

    Reproduced on v0.45.0 (macOS, Apple Silicon), three minor versions after the v0.42.4 report. read is unchanged. grep is not — the claim there needs narrowing, which I think makes this report stronger rather than weaker.

    read — still 0%, confirmed

    Byte-identical to cat at every size tried:

    file cat rtk read saved
    PluginProcessor.cpp 80,138 80,138 0.0%
    ZoneDsp.h 23,878 23,878 0.0%
    StepCanvas.h 5,685 5,685 0.0%

    Meanwhile rtk gain on this machine credits rtk read with 4.2M tokens saved over 1,092 calls (18.0% avg). Measured compression for that command is zero, so that entire line is unearned — about 19% of the reported global total.

    grep — the 0% claim doesn't hold on v0.45.0

    This is where I'd refine the report. rtk grep compresses substantially on large results and no-ops on small ones:

    command raw filtered saved
    grep -rn juce <dir> 192,143 19,773 89.7%
    grep -rn ZoneEqParams <dir> 2,450 2,450 0.0%

    So "grep receives 0% compression" is true only below some size threshold. On big outputs — the ones that actually matter for context — it's doing real work. Worth splitting that out from the read finding, since they have different causes and only one of them is a no-op across the board.

    Controls behave as this issue describes

    command raw filtered saved
    ps aux 199,488 3,544 98.2%
    wc -l <file> 87 5 94.3%
    ls -la <dir> 2,209 575 74.0%
    env 4,033 1,478 63.4%

    Methodology note for anyone reproducing

    rtk proxy read <file> is not a valid raw baseline — read is an rtk subcommand, not a shell command, so proxy runs the shell builtin and yields 0 bytes. It looks like infinite savings. Compare against cat <file> instead. (I got this wrong first time round.)

    Everything above was written to files and measured with stat -f%z, so no wc in a pipeline could itself be rewritten and skew the count.

    Suggestion

    The gap seems to be that gain records an expected saving per command class rather than the measured delta for that invocation. If it recorded actual pre/post byte counts per call — and attributed 0 when a filter passes through unchanged — the headline would be smaller but defensible, and read-style no-ops would surface immediately instead of being the single largest reported contributor.

    Related: for anyone auditing their own numbers, note that Claude Code's native Read/Grep tools never reach the Bash hook (#538), so gain describes one lane of token usage rather than the total, independently of this bug.

  8. randomparity commented on Aug 24, 2026

    @randomparity

    Independent reproduction on rtk 0.45.0 (macOS arm64), against a different codebase and a different harness — this reproduces the read/grep half of the report. Adding it since the issue carries needs-reproduction.

    Method: same as the original. Raw output bytes vs rtk <cmd> output bytes, on a pinned Rust repository (457 .rs files, ~92k LOC).

    command raw rtk change
    git status 305 53 −82%
    wc -l src/main.rs 21 4 −80%
    ls -la src 1,488 461 −69%
    git log -20 10,196 4,795 −52%
    git diff HEAD~3 47,893 24,666 −48%
    grep -rn TODO . 834 690 −17%
    rg "impl" src --type rust 14,716 13,089 −11%
    rg -n "pub fn" src 18,686 17,136 −8%
    grep -rn unsafe src 5,910 5,910 0%
    rg -l test src 13,051 13,051 0%
    cat Cargo.toml 6,415 6,415 0%
    cat README.md 22,917 22,917 0%
    cat src/main.rs 7,168 7,168 0%
    git diff HEAD~3 --stat 1,197 1,196 0%
    ls -R src 6,926 8,956 +29%
    ls src 251 341 +35%

    Consistent with the original report: read (from cat) is 0% on every file tested, and grep/rg are 0–17%. The structured-output commands compress as advertised.

    Two things I can add beyond the original:

    1. Flags matter as much as the command. The same command compresses well or not at all depending on form — git log is −52% bare and 0% with --oneline; git diff is −48% bare and 0% with --stat. Agents overwhelmingly use the compact forms, since they are already minimising their own context. Mining 188,376 real Bash tool calls from local Claude Code transcripts, only 13.3% were bare commands; 59.8% contained a pipe.

    2. Weighting by real-world usage, the weak handlers dominate. Asking rtk rewrite about a random sample of 5,000 of those real calls, RTK offers a rewrite on 42.2% of them. Of those rewrites, grep (18.2%), rg (12.7%) and read (9.8%) together take 40.7% — i.e. the handlers measured at 0–17% above account for the plurality of RTK's real-world reach, while the handlers that genuinely compress (git status, git log, git diff, git show) total about 19%.

    So the "unreproducible savings" in the title has, I think, two distinct causes worth separating:

    Neither fully explains the ~90M figure in the original report, but both push the aggregate upward and would need fixing before rtk gain could be reconciled against measured output.

    Full method and data: happy to share the harness if useful.

  9. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    Closing as a duplicate of #2805, which explains the numbers measured here: rtk read emits the file unchanged but records the whole file as the baseline, labelled as a cat the caller never ran, so the byte-identical output and the 100%-saved rows are two faces of the same accounting defect. #2805 carries the root cause and the fix discussion, and the aggregate tables from this thread are directly useful there - please continue on #2805.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions