Skip to content

fix(tracking): cap token estimates at the shell output truncation ceiling - #3635

Closed
xantorres wants to merge 1 commit into
rtk-ai:developfrom
xantorres:fix/tracking-honest-token-cap
Closed

xantorres wants to merge 1 commit into
rtk-ai:developfrom
xantorres:fix/tracking-honest-token-cap

Conversation

@xantorres

Copy link
Copy Markdown

Problem

estimate_tokens is uncapped bytes/4: a single rtk read of 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 the rtk gain aggregate (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_chars in config.toml (default 30000, 0 disables) with RTK_TRACK_CAP_CHARS env override taking precedence. estimate_tokens itself 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.

…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.
@CLAassistant

CLAassistant commented Aug 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.
@KuSh

KuSh commented Nov 16, 2026

Copy link
Copy Markdown
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.

@KuSh KuSh closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants