Skip to content

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

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

fix(tracking): cap token estimates at the shell output truncation ceiling#3635
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.

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.

2 participants