Skip to content

Count the conversation reloaded after a pause apart from a turn's tokens - #85

Merged
httpsphl merged 2 commits into
mainfrom
feat/tokens-reload
Oct 1, 2026
Merged

httpsphl merged 2 commits into
mainfrom
feat/tokens-reload

Conversation

@httpsphl

@httpsphl httpsphl commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Why

A short turn after a pause showed a big token count: one real turn took 11 s and read "73.6k tokens", although the bot wrote under 1k. The numbers were right. The prompt cache had expired (the bot had been idle for 7 h and Botloft had just restarted), so the whole conversation was sent again and counted as cache_creation_input_tokens. Claude Code reports that next to what is really new, because it sends new content (the message, tool results) to the cache too. So input alone stays near zero and can't tell the two apart.

What

  • Daemon (context/reload.rs): for each main-conversation model request (counted once per message.id), it adds min(cacheWrite, before - cacheRead), where before is the previous request's prompt size, or the size Claude Code reported when no request has been seen yet. A new conversation, or one that was just compacted, counts nothing. The turn item gets tokens.reloaded, capped to the result's cacheWrite. Nothing new is stored beyond the field, and older items read 0.
  • Store: usage.tokens sums reloaded, and bots are ordered by the new headline number.
  • App: the headline is input + cacheWrite - reloaded + output. From 1k reloaded tokens, the turn line adds "reloaded the conversation (72k)", the line's tooltip explains it, and the usage dialog shows "· 72k reloaded". New text is in all three languages.
  • Spec: 8.7 describes the count. The section 19 row records the turn seen on a real install.

Tests

  • cargo fmt --check, clippy -D warnings, cargo test --workspace and pnpm check pass.
  • Unit tests for the tracker cover a warm cache, an expired cache, a request printed on several lines, a conversation not yet measured or just compacted, and a size reported after a request.
  • tests/token_reload.rs runs a cold turn then a warm one end to end.
  • App tests cover the turn line, the tooltip, the 1k threshold and the dialog row.

Manual test with real Claude Code

  1. On the dev daemon, send a bot two messages a few seconds apart. The second turn line shows no "reloaded".
  2. Leave the bot idle long enough for the cache to expire (or restart Botloft), then send another message. The turn line shows "reloaded the conversation (N)", with N close to the conversation size in the context meter, and the headline shows only the new tokens.
  3. Check the usage dialog shows the reloaded total for that bot.

A turn after a pause, or right after a restart, sends the whole
conversation again because the prompt cache expired. Claude Code reports
it as cache_creation_input_tokens, next to what is really new, so a
short turn showed tens of thousands of tokens. The daemon now follows
each model request and keeps, as `reloaded`, what the conversation
already held and did not come from the cache (spec 8.7).
The turn line and the usage dialog count new input and replies, and
tell the conversation sent again after a pause on its own, from 1k
tokens on.
@httpsphl
httpsphl enabled auto-merge October 1, 2026 02:19
@httpsphl
httpsphl merged commit a78a1cd into main Oct 1, 2026
3 checks passed
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.

1 participant