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()andadjust_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_countcalls fromcore/agents/main.pycleanup and persistence paths. - Deleted
SessionState.update_token_count()andadjust_token_count()along with the heuristic import incore/state.py. - Removed
update_token_countfromSessionStateProtocol.
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]]