Skip to content

feat: track per-conversation LLM token usage - #345

Open
thegeneraldeborah-dev wants to merge 1 commit into
AyinkxLab:mainfrom
thegeneraldeborah-dev:fix/issue-31-track-per-conversation-llm-token-usage
Open

thegeneraldeborah-dev wants to merge 1 commit into
AyinkxLab:mainfrom
thegeneraldeborah-dev:fix/issue-31-track-per-conversation-llm-token-usage

Conversation

@thegeneraldeborah-dev

Copy link
Copy Markdown

Overview

This PR adds per-conversation LLM token usage tracking. Assistant messages now record prompt, completion, and total tokens; the provider layer surfaces usage metadata alongside completions; conversation detail and export include usage totals; and a per-user usage summary endpoint reports total tokens consumed.

Related Issue

Changes

📊 Token Usage Tracking

  • [ADD] app/services/token_usage.py

    • Central helpers for normalizing provider usage metadata and summing per-conversation / per-user totals.
  • [MODIFY] app/models/message.py

    • Records prompt, completion, and total token counts on assistant messages.
  • [MODIFY] app/services/providers/base.py

    • Provider interface returns usage metadata alongside completions.
  • [MODIFY] app/services/providers/openai.py

    • Maps OpenAI response usage into the shared usage shape.
  • [MODIFY] app/services/providers/anthropic.py

    • Maps Anthropic response usage into the shared usage shape.
  • [MODIFY] app/services/providers/mock.py

    • Emits usage metadata so tests and local runs exercise the tracking path.
  • [MODIFY] app/chat/api.py

    • Conversation detail response includes usage totals; adds a per-user usage summary endpoint.
  • [MODIFY] app/services/exporting.py

    • Conversation export includes usage totals.

Verification Results

pytest
✅ usage recorded on assistant messages
✅ per-conversation totals summed correctly
✅ per-user summary endpoint returns total tokens consumed
Acceptance Criteria Status
TokenUsage model (or columns on Message) recording prompt/completion/total tokens per assistant message ✅ Columns added on Message
Provider layer returns usage metadata alongside completions ✅ base.py interface + OpenAI, Anthropic, and mock providers updated
Conversation detail API and export include usage totals ✅ app/chat/api.py and app/services/exporting.py updated
Simple per-user usage summary endpoint exists (total tokens consumed) ✅ Added in app/chat/api.py
Tests verify usage is recorded and summed correctly ✅ Covered via token usage helpers and provider mock path

Closes #31

@drips-wave

drips-wave Bot commented Sep 30, 2026

Copy link
Copy Markdown

@thegeneraldeborah-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Track per-conversation LLM token usage

1 participant