Skip to content

Token usage tracking for LLM, STT, and TTS calls and Usage Page. - #114

Merged
TEJASNARAYANS merged 35 commits into
mainfrom
token-usage
Aug 18, 2026
Merged

Token usage tracking for LLM, STT, and TTS calls and Usage Page.#114
TEJASNARAYANS merged 35 commits into
mainfrom
token-usage

Conversation

@MSami625

@MSami625 MSami625 commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

What Changed?

Added usage tracking for LLM, STT, and TTS calls, including voice agent usage.

  • Track LLM input/output, cache, reasoning tokens, and call count.
  • Track STT and TTS usage from voice agent calls.
  • Added Redis-based usage counters with batched Postgres rollups.
  • Added usage context for organization, workspace, product, and resource.
  • Added Usage API for summary and breakdown.
  • Added a single /usage page with filters and grouping.
  • Added scheduled flushing of usage data from Redis to Postgres.

Why?

We need a centralized way to track AI usage across different products and voice agents without adding significant overhead to individual calls.

Usage is aggregated in Redis first and periodically flushed to Postgres, instead of creating a database record for every request. This keeps the system scalable while allowing usage to be viewed from a single Usage page.

How to Test?

  1. Run an LLM request/evaluation.
  2. Run a voice agent call using STT, LLM, and TTS.
  3. Open the /usage page.
  4. Verify that the usage data is recorded correctly.
  5. Test filters for workspace, product, model, and resource.
  6. Verify that usage data is updated after the Redis flush.

Release Label

  • major
  • [✅] minor
  • fix
  • No label

Checklist

  • [ ✅] I have read the CONTRIBUTING.md guide.
  • [ ✅] My code follows the project's style guidelines.
  • [ ✅] I have added tests that prove my fix is effective or my feature works.
  • [ ✅] I have updated documentation where needed.

@greptile-apps

greptile-apps Bot commented Aug 12, 2026

Copy link
Copy Markdown

Greptile Summary

The PR introduces centralized LLM, STT, and TTS usage accounting with Redis aggregation, durable PostgreSQL rollups, usage-context propagation, reporting APIs, and a frontend usage page.

  • Adds usage instrumentation across AI, evaluation, optimization, and voice-agent paths.
  • Adds migrations and scheduled workers for aggregate persistence and committed-claim recovery.
  • Adds organization usage summary, breakdown, filtering, and drill-down interfaces.

Confidence Score: 3/5

The PR is not yet safe to merge because restricted organization members can still retrieve usage data and labels from workspaces they are not authorized to access.

The usage endpoints remain scoped only by organization identity, while workspace filtering is optional and caller-controlled, leaving the previously reported workspace authorization bypass reachable.

Files Needing Attention: app/api/v1/routes/org_usage.py

Important Files Changed

Filename Overview
app/api/v1/routes/org_usage.py Adds the usage reporting API, but its organization-only authorization still exposes data from workspaces inaccessible to restricted members.
app/services/usage/llm_usage.py Implements Redis counters, PostgreSQL aggregation, durable committed-claim tracking, and orphan recovery; the current transaction structure addresses the previously reported replay paths.
app/services/ai/llm_gateway.py Adds normalized usage recording around LiteLLM completion and batch-completion paths.
frontend/src/pages/usage/Usage.tsx Implements the usage dashboard with date, grouping, filtering, and drill-down behavior.
app/migrations/068_usage_committed_claims.py Adds the durable committed-claim table used to prevent already-applied Redis claims from being restored.

Reviews (6): Last reviewed commit: "refactor: usage commited claims" | Re-trigger Greptile

Comment thread app/api/v1/routes/org_usage.py Outdated
Comment thread app/services/usage/llm_usage.py Outdated
Comment thread app/services/usage/llm_usage.py Outdated
@MSami625 MSami625 added the minor Minor version update label Aug 13, 2026
Comment thread app/services/usage/llm_usage.py Outdated
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown

Too many files changed for review (162 files, 100 file limit).

Bypass the limit by tagging @greptile-apps to review.

@MSami625
MSami625 requested a review from TEJASNARAYANS August 17, 2026 05:36
…on to clear cache before seeding pricing rates
…sage pricing routes; enhance usage snapshot billability checks
…eamline cost calculations for pending usage deltas
@TEJASNARAYANS TEJASNARAYANS added fix Fix version release and removed minor Minor version update labels Aug 18, 2026
@TEJASNARAYANS
TEJASNARAYANS merged commit a63f0f2 into main Aug 18, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Fix version release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants