475 2026 02 02 Remove Heuristic Token Counting 9c1ea9ff

Remove heuristic token counting from agent flow

tools-only Updated 7 repo stars

File contents

Remove heuristic token counting from agent flow

Summary

The agent run flow no longer recomputes token totals by iterating over message history. This removes the slow heuristic update_token_count path in favor of usage totals as the canonical source for token display.

Context

  • SessionState.update_token_count() and adjust_token_count() were used after message mutations (cleanup, abort handling, persistence).
  • These methods depended on estimate_messages_tokens, which scales poorly with large histories and does not match provider usage totals.

Changes

  • Removed update_token_count calls from core/agents/main.py cleanup and persistence paths.
  • Deleted SessionState.update_token_count() and adjust_token_count() along with the heuristic import in core/state.py.
  • Removed update_token_count from SessionStateProtocol.

Behavioral Impact

  • Message cleanup no longer recalculates conversation.total_tokens.
  • UI token display will rely on usage totals rather than heuristic counts.

Related Cards

  • [[usage-total-token-source]]

tools-only/X-Skills/tree/main/development/475-2026-02-02-remove-heuristic-token-counting_9c1ea9ff commit 5d9f91f23b

Frequently asked questions

npx skillmds@latest add tools-only/475-2026-02-02-remove-heuristic-token-counting-9c1ea9ff