fix(insights): persist token usage for non-CLI sessions - #4547
Closed
kshitijk4poor wants to merge 1 commit into
Closed
fix(insights): persist token usage for non-CLI sessions#4547kshitijk4poor wants to merge 1 commit into
kshitijk4poor wants to merge 1 commit into
Conversation
kshitijk4poor
force-pushed
the
fix/gateway-token-persistence
branch
from
April 2, 2026 06:07
ed55d52 to
ebfbe7d
Compare
kshitijk4poor
force-pushed
the
fix/gateway-token-persistence
branch
from
April 2, 2026 06:10
ebfbe7d to
65697c9
Compare
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.
Problem
/insightscan show 0 tokens for non-CLI sessions even when those sessions clearly consumed model usage.I confirmed the split-brain persistence path described in the repro notes:
run_agent.pyaccumulated token usage on every API call, but only wrote it to SQLite whenplatform == 'cli'run_conversation()returnedThat left
/insightsvulnerable to missing token/accounting data for Telegram/cron rows.Root Cause
Token persistence was split across two places:
run_agent.pygateway/run.pyFix
run_agent.pygateway/run.pygateway.session.SessionStore.update_session()so it now only updates lightweight session metadata (updated_at+last_prompt_tokens) used for context-window tracking / compression decisionsTesting
source /Users/kshitij/Projects/hermes-agent/.venv/bin/activate && python -m pytest tests/test_token_persistence_non_cli.py -qsource /Users/kshitij/Projects/hermes-agent/.venv/bin/activate && python -m pytest tests/test_token_persistence_non_cli.py tests/gateway/test_status_command.py tests/gateway/test_session.py tests/test_insights.py -qNotes
This now fully follows the hybrid approach from the repro notes:
last_prompt_tokensonly