Repository navigation
Conversation
…ling estimate_tokens() had no ceiling, so a single large read (e.g. a 349KB file) could log tens of thousands of input tokens that no model context ever received — coding-agent shells commonly truncate captured Bash output well below that. Those inflated rows drown real savings in `rtk gain` aggregates. Clamp input/output token estimates at track/record time to tracking.estimate_cap_chars / 4 (default 30_000 chars, 0 disables the cap), using the same ceiling rounding as estimate_tokens() so the two never disagree at small remainders. RTK_TRACK_CAP_CHARS overrides the config value and is checked on every call; the config-derived fallback is resolved once per process. estimate_tokens() itself is unchanged; other callers (guard.rs, git.rs) are unaffected.
Open
5 tasks
KuSh
pushed a commit
to KuSh/rtk
that referenced
this pull request
Oct 29, 2026
`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.
Collaborator
|
Thank you for working on this. #4550 (merged 2026-10-13) addresses the same problem: rows over 30,000 chars of input are now left out of the rtk gain headline and the period tables, while still showing in the per-command listing and being counted as excluded, so the raw sizes stay available. That ceiling is a fixed constant with no config key or environment override, and an issue or a narrow PR adding one on the current model would be welcome. The docs do not describe the exclusion yet, and an issue or a narrow docs PR for that would be welcome too. If I missed part of this change, please comment or reopen. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
estimate_tokensis uncappedbytes/4: a singlertk readof a 349KB file records 87,224 input tokens. Coding-agent shells truncate captured command output (~30,000 chars is a common ceiling), so recorded savings past that point never existed in any model context, and one large row drowns thertk gainaggregate (a 0.0% efficiency meter over an otherwise honest run). CLAUDE.md already documents absolute counts as approximate — this makes the approximation honest.Fix
Clamp input and output token estimates at the ceiling inside
TimedExecution::track. Configurable:tracking.estimate_cap_charsin config.toml (default 30000, 0 disables) withRTK_TRACK_CAP_CHARSenv override taking precedence.estimate_tokensitself and its other callers are unchanged.Tests
7 new: input/output clamping, saved consistency, env override, 0-disables, config default and missing-field default. Full suite green in a clean environment.