Skip to content

rtk read: truncation via --max-lines incorrectly counted as token savings, producing 250M+ saved tokens in one command #1045

@kotofos

Description

@kotofos

Bug

When rtk read truncates output via --max-lines the dropped content is recorded as "saved tokens" in the
tracking database.
This inflates rtk gain numbers. In my case, a handful of large bioinformatics files (FASTA, SAM) pushed reported savings to 278M tokens — almost entirely fake.
Real data from my history — a few bioinformatics file reads dominate the entire dashboard:

rtk gain --tier pro -q
RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════

Total commands:    92
Input tokens:      278.1M
Output tokens:     6.0K
Tokens saved:      278.1M (100.0%)
Total exec time:   25.6s (avg 277ms)
Efficiency meter: ████████████████████████ 100.0%

By Command
────────────────────────────────────────────────────────────────────────
  #  Command                   Count   Saved    Avg%    Time  Impact    
────────────────────────────────────────────────────────────────────────
 1.  rtk read                     17  269.2M   88.1%   150ms  ██████████
 2.  rtk curl -sL https://...      1    8.8M  100.0%    1.8s  ░░░░░░░░░░
 3.  rtk ls                       27    2.8K   65.5%     1ms  ░░░░░░░░░░
...

Reproduction

Environment

  • rtk 0.34.2
  • Linux 6.12.0
  • Claude Code with PreToolUse hook (rtk-rewrite.sh v3)
# 1. Create test file: 1000 lines, ~90KB                                                                                                     
python3 -c "for i in range(1000): print(f'line {i}: ' + 'x'*80)" > /tmp/test.txt                                                             
wc -c /tmp/test.txt   # 90890 bytes                                                                                                          
                                                                                                                                             
# 2. Read with --max-lines                                                                                        
rtk read --max-lines 1 /tmp/test.txt > /dev/null                                                                                             
                                                                                                                                             
# 3. Check tracking DB                                         
sqlite3 ~/.local/share/rtk/history.db \
  "SELECT input_tokens, output_tokens, saved_tokens, savings_pct
   FROM commands ORDER BY id DESC LIMIT 1;"                                                                                                  

# Result:                                                                                                                                    
#   input_tokens  = 22723    ← ceil(file_size / 4), from stat()
#   output_tokens = 9        ← actual output after truncation                                                                                
#   saved_tokens  = 22714    ← WRONG: this is discarded data, not savings                                                                    
#   savings_pct   = 99.96    ← meaningless    

Expected behavior

Does not count dropped data with --max-lines as saved, input_tokens should be derived from the content actually read

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingeffort-smallQuelques heures, 1 fichierfilter-qualityFilter produces incorrect/truncated signalgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions