Repository navigation
Conversation
…ceiling Issue rtk-ai#1973 / rtk-ai#1935: 'rtk gain' was reporting impossible figures — e.g. ~1.6M saved tokens per command on average, with one day showing 1.4B total tokens used and saved. The root cause: when a command like 'rtk read 50MB.log' filters a multi-megabyte file, the naive 'saved = input_tokens - output_tokens' attribution recorded millions of 'saved' tokens. But Claude's tool-result surface is capped around 25K tokens; anything beyond that wouldn't have reached Claude under any scheme, so the attribution overstated RTK's contribution by orders of magnitude. This change caps the per-call saved_tokens attribution at CLAUDE_TOOL_RESULT_CAP (25_000, matching Claude Code's default MAX_OUTPUT_TOKENS for tool results), in two places: - write side (Tracker::record): new rows are capped on insert; the savings_pct denominator is also capped so a 25K / 12M ratio doesn't flatten the percentage to ~0% for legitimately-filtered large logs. - read side (Tracker::new migration): on first open, historical rows with saved_tokens > 25_000 are clamped to 25_000, and their savings_pct is recomputed using the capped denominator. Idempotent. Four new tests cover: huge-input cap, small savings unchanged, pct uses capped denominator, passthrough (input == output) yields 0 saved. Fixes rtk-ai#1973, rtk-ai#1935
|
recheck |
1 similar comment
|
recheck |
|
@CLAassistant recheck |
|
Reviewer guide to reduce review time: Expert lens: analytics correctness / token economics. This changes attribution, not command filtering. The issue is that huge local outputs can produce impossible saved-token totals even though the model-facing tool result is capped. The PR caps per-call Suggested review checks:
|
|
This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet. |
|
Closing this pull request after a prolonged period of inactivity. If this is still relevant, please ask for it to be reopened. Thank you! |
`rtk gain`'s "tokens saved" headline can be inflated by orders of magnitude by a single oversized command. Root cause: nothing bounds a per-call token estimate, and RTK's counterfactual is "the agent would have consumed the full raw output" -- it wouldn't have, since agent shells (Claude Code, etc.) truncate captured tool-result output well below what RTK's estimator counts. Confirmed against a real local history.db: a single `rtk read` call recorded 135,722,224 input tokens against a 21-token real output -- one row alone accounting for ~92% of the entire lifetime "saved" total (135.7M of 148.2M), invisible in the unweighted by-command average (5.3%) since 4,627 of 5,180 `rtk read` calls genuinely saved 0%, and 158 outlier rows (3% of calls) supplied 99.86% of the sum. This combines and adapts two stale/unmerged community PRs (rtk-ai#3635, rtk-ai#1978) that independently targeted the same bug from different angles -- neither was mergeable as-is (both conflicting with develop, zero maintainer review, two different unreconciled magic-number ceilings): - New `tracking.estimate_cap_chars` config (default 30,000 chars, 0 = uncapped), overridable via `RTK_TRACK_CAP_CHARS` (from rtk-ai#3635). `TimedExecution::track` clamps input_tokens/output_tokens to this ceiling before they ever reach the DB. - A version-gated retroactive migration (schema v1 -> v2) caps already-inflated historical rows to the same ceiling and recomputes saved_tokens/savings_pct from the capped values -- adapted from rtk-ai#1978's migration idea, but keyed to the same configurable cap instead of a second, hardcoded 25,000-token ceiling, so historical and new rows converge on one number. Floors at 0 rather than manufacturing a fake positive saving when capping turns two oversized values into an equal pair (or when the original row was already a regression pre-cap). Deliberately out of scope (separate, real issues, not band-aided here): rtk-ai#3702 (saturating_sub silently floors regressions to 0 instead of surfacing a negative delta -- needs a signed-delta schema change, not a quick fix) and the `rtk read`-specific measurement issues in rtk-ai#2805/Cybercommanders' OOM-stub-return finding (hardcoded "rtk read" rtk_cmd label blocks auditing which flag combinations are affected; worth its own PR). 9 new tests, including a direct regression test replaying the exact 135.7M-token row found in the wild and asserting it lands at the cap afterward, plus a test proving the migration can't turn a genuine regression into a fake positive saving.
Summary
`rtk gain` has been reporting impossible figures — issue #1973 shows ~1.6M saved tokens per command on average; issue #1935 shows one day with 1.4B tokens used and saved. Both are the same root cause: when a command like `rtk read 50MB.log` filters a multi-megabyte file, the naive `saved = input_tokens - output_tokens` attribution recorded millions of "saved" tokens, but Claude's tool-result surface is capped around 25K tokens — anything beyond that never reaches Claude under any scheme.
This PR caps the per-call saved attribution at the Claude tool-result ceiling so the dashboard reflects realistic LLM-side savings.
Reproduction
```bash
Before this PR
rtk read /tmp/huge.log # any file > ~100KB
rtk gain --history | head -3
Shows e.g. 'Saved: 12M tokens (100%)' for one call
After this PR (or after first
rtk gainre-open of an existing DB)Per-call saved_tokens ≤ 25_000.
```
Root cause
`src/core/tracking.rs::Tracker::record` computed `saved = input_tokens.saturating_sub(output_tokens)` and stored it verbatim. There was no realistic upper bound, so a 12M-token `input` (raw `stdout`) yielded a 12M-token "saved" figure even though Claude's tool-result cap would have truncated that input to ~25K before it reached the model.
Fix approach
Behavior changes
Test plan
Fixes #1973, #1935