# Remove heuristic token counting from agent flow

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

- Skill: `tools-only/remove-heuristic-token-counting-from-agent-flow` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add tools-only/remove-heuristic-token-counting-from-agent-flow`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/remove-heuristic-token-counting-from-agent-flow/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-29
- Page: https://skillmd.com/skills/tools-only/remove-heuristic-token-counting-from-agent-flow

---


# 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]]

