Skip to content

Add per-user AI usage quota tracking #515

Description

@knoxiboy

Description

Track AI usage per user to control costs and support future pricing or institution-level limits.

Problem

Without AI usage tracking, project admins cannot understand or control Groq API cost drivers. The /api/ask-ai and /api/doubts/check-similarity routes make Groq calls with no per-user accounting.

Proposed Solution

  1. Add an ai_usage table with: userEmail, route, model, tokensUsed (from Groq response), timestamp. 2. Log usage after each successful Groq call. 3. Add a daily/monthly quota check before making Groq calls. 4. Expose usage stats to admin dashboard.

Files Affected

  • src/configs/schema.ts -- add aiUsageTable
  • [NEW] drizzle/0009_ai_usage.sql -- migration
  • src/app/api/ask-ai/route.ts -- log usage after line 466
  • src/app/api/doubts/check-similarity/route.ts -- log usage after line 95
  • [NEW] src/lib/ai/usage-tracker.ts -- track and check quota

Implementation Checklist

  • ai_usage table exists with user, route, model, and token fields
  • AI usage is recorded after each successful Groq call
  • Daily/monthly quota checks can be performed before calling Groq
  • Quota exceeded returns 429 without calling the model provider
  • Admin or debug queries can inspect per-user usage

Additional Context

This is a GSSoC'26 contributor issue created from the DoubtDesk market-readiness roadmap.

Field Value
Level intermediate
Area backend
Stack Next.js, TypeScript, Drizzle, Neon Postgres, Clerk, Groq, Inngest

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions