Skip to content

fix(insights): persist token usage for non-CLI sessions - #4547

Closed
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/gateway-token-persistence
Closed

fix(insights): persist token usage for non-CLI sessions#4547
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/gateway-token-persistence

Conversation

@kshitijk4poor

@kshitijk4poor kshitijk4poor commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

/insights can 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.py accumulated token usage on every API call, but only wrote it to SQLite when platform == 'cli'
  • gateway sessions also attempted a second higher-level token/model write after run_conversation() returned
  • cron and other non-CLI/non-gateway sessions could miss persistence entirely, while gateway accounting stayed split across two code paths

That left /insights vulnerable to missing token/accounting data for Telegram/cron rows.

Root Cause

Token persistence was split across two places:

  1. run_agent.py

    • had the real per-API-call usage data
    • but skipped non-CLI sessions because of a CLI-only guard
  2. gateway/run.py

    • did a second session-store write after the run finished
    • duplicated token/model persistence responsibilities instead of keeping them in one place

Fix

  • removed the CLI-only guard in run_agent.py
  • now any session with a session DB + session ID persists token usage directly from the agent loop
  • removed the gateway's redundant token/model/cost write in gateway/run.py
  • simplified gateway.session.SessionStore.update_session() so it now only updates lightweight session metadata (updated_at + last_prompt_tokens) used for context-window tracking / compression decisions
  • removed stale gateway-session test coverage for the deleted write-through path
  • added regression coverage for Telegram and cron session persistence
  • updated gateway message-handling assertions to match the consolidated write path

Testing

  • source /Users/kshitij/Projects/hermes-agent/.venv/bin/activate && python -m pytest tests/test_token_persistence_non_cli.py -q
  • source /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 -q

Notes

This now fully follows the hybrid approach from the repro notes:

  • agent-level persistence for all platforms
  • gateway-level last_prompt_tokens only
  • no dead gateway token/model persistence path left behind

@kshitijk4poor
kshitijk4poor force-pushed the fix/gateway-token-persistence branch from ebfbe7d to 65697c9 Compare April 2, 2026 06:10
@kshitijk4poor
kshitijk4poor deleted the fix/gateway-token-persistence branch August 5, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant