Skip to content

Display session-level token usage in the chat header #538

Open

Description

@haiphucnguyen

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

Activity

  1. added
    enhancement
    help wanted
    on Jul 23, 2026
  2. MrChatterjee98 commented on Aug 9, 2026

    @MrChatterjee98
    Contributor

    I think I can take this up now 😄

    edit: I have started this.

  3. conka8 commented on Oct 21, 2026

    @conka8

    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?

  4. haiphucnguyen commented on Oct 22, 2026

    @haiphucnguyen
    CollaboratorAuthor

    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

  5. haiphucnguyen commented on Oct 22, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Create a ticket for deleting chat message #778

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementhelp wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions