askimo icon indicating copy to clipboard operation
askimo copied to clipboard

Display session-level token usage in the chat header

Open haiphucnguyen opened this issue 4 months ago • 4 comments

Display session-level token usage in the chat header

Summary

Show a running token usage summary in the chat session header so users can see at a glance how much context a session has consumed. Token totals are aggregated on demand from the messages table — keeping the schema clean with a single source of truth.

Why not sum from loaded messages

ChatView lazy-loads messages (hasMoreMessages / loadPrevious). Summing token counts from currently loaded messages only reflects a partial view. Opening an old session would show an incorrect total until all pages are loaded.

Approach — aggregate query on session open

When a session is opened, run a single lightweight aggregation query against the messages table — separate from and independent of message pagination:

SELECT SUM(total_tokens), SUM(input_tokens), SUM(output_tokens)
FROM chat_messages
WHERE session_id = ? AND deleted_at IS NULL

This gives the true total immediately, before any messages are loaded. No schema changes needed. No denormalization risk. On new AI response — append the new message's token counts to the in-memory total (optimistic delta update). No re-query needed. On message delete / retry — re-run the aggregation query to get an accurate count (infrequent operation, acceptable cost).

UI

A compact token pill in the existing session header card in ChatView.kt, sitting between the session title and the dropdown menu (the directive dropdown is going to move to the ChatInputField in this ticket https://github.com/askimo-ai/askimo/issues/535): Shows ~12K tokens when total > 0 Hidden for new/empty sessions Hover tooltip shows full breakdown: 12,450 total · 8,230 in / 4,220 out

What to figure out

Where to run the aggregation query — repository method, service layer, or ViewModel on session load? How to expose the result to ChatState — a dedicated sessionTokenSummary field or inline on the session object? Optimistic delta update on new response — where does this live cleanly without leaking into ChatView?

Files likely affected

  • ChatMessageRepository (local) — add sumTokensBySessionId(sessionId) query method
  • ChatSessionService / SessionManager — call aggregation on session open, expose result
  • ChatView.kt — render the token pill in the header

haiphucnguyen avatar Jul 23 '26 01:07 haiphucnguyen

I think I can take this up now 😄

edit: I have started this.

MrChatterjee98 avatar Aug 09 '26 12:08 MrChatterjee98

I can pick this up. I would run the single aggregation query on chat_messages (summing total, input and output tokens where deleted_at IS NULL) when a session opens, independent of ChatView pagination, show the totals in the chat header and add each new response's counts to the in-memory total, with a test that deleted messages are excluded and a screenshot of the header. Should the header show only the total, or the input/output split as well?

conka8 avatar Oct 21 '26 23:10 conka8

it is the good approach @conka8 . I think the total tokens is fine and input/output could be displayed as tooltip. Currently we do not have the delete message (though I need we have and I am going to create the ticket for deleting message later).

For the total tokens, it should not be impacted by deleting / outdated messages because if user made requests to AI, and AI consume tokens then the total tokens consumed is not impacted by deleted messages. So you just simply query the messages in the session. The query does not impact the deleting messages is supported in the future because we are going to do the soft delete approach.

I assigned this ticket to you, feel free to raise question if you have any

haiphucnguyen avatar Oct 22 '26 06:10 haiphucnguyen

Create a ticket for deleting chat message https://github.com/askimo-ai/askimo/issues/778

haiphucnguyen avatar Oct 22 '26 06:10 haiphucnguyen