fix: gateway token double-counting — use absolute set instead of increment#3317
Merged
fix: gateway token double-counting — use absolute set instead of increment#3317
Conversation
…ement The gateway's update_session() used += for token counts, but the cached agent's session_prompt_tokens / session_completion_tokens are cumulative totals that grow across messages. Each update_session call re-added the running total, inflating usage stats with every message (1.7x after 3 messages, worse over longer conversations). Fix: change += to = for in-memory entry fields, add set_token_counts() to SessionDB that uses direct assignment instead of SQL increment, and switch the gateway to call it. CLI mode continues using update_token_counts() (increment) since it tracks per-API-call deltas — that path is unchanged. Based on analysis from PR #3222 by @zaycruz (closed). Co-authored-by: zaycruz <zay@users.noreply.github.com>
# Conflicts: # gateway/session.py
StreamOfRon
pushed a commit
to StreamOfRon/hermes-agent
that referenced
this pull request
Mar 29, 2026
…ement (NousResearch#3317) The gateway's update_session() used += for token counts, but the cached agent's session_prompt_tokens / session_completion_tokens are cumulative totals that grow across messages. Each update_session call re-added the running total, inflating usage stats with every message (1.7x after 3 messages, worse over longer conversations). Fix: change += to = for in-memory entry fields, add set_token_counts() to SessionDB that uses direct assignment instead of SQL increment, and switch the gateway to call it. CLI mode continues using update_token_counts() (increment) since it tracks per-API-call deltas — that path is unchanged. Based on analysis from PR NousResearch#3222 by @zaycruz (closed). Co-authored-by: zaycruz <zay@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The gateway's token usage stats inflate with every message because
update_session()uses+=for cumulative values the agent already tracks.Root cause
The cached agent's
session_prompt_tokens/session_completion_tokensare running totals that accumulate acrossrun_conversation()calls. The gateway reads these cumulative values and passes them toupdate_session(), which doesentry.input_tokens += cumulative. Each message re-adds the full running total.Live test result (before fix):
Fix
+=to=for input_tokens, output_tokens, cache_read/write_tokens, estimated_cost_usdset_token_counts()— uses SQL direct assignment (input_tokens = ?) instead of increment (input_tokens = input_tokens + ?)update_token_countstoset_token_countsCLI mode continues using
update_token_counts()(increment) since it tracks per-API-call deltas.Live test result (after fix):
Validation
python -m pytest tests/test_hermes_state.py tests/gateway/test_session.py -n0 -q→ 186 passedBased on analysis from PR #3222 by @zaycruz.
Co-authored-by: zaycruz zay@users.noreply.github.com