Skip to content

fix: add AI usage quotas and cost tracking#962

Open
Divv1524 wants to merge 1 commit into
imDarshanGK:mainfrom
Divv1524:fix/issue-667-usage-quotas
Open

fix: add AI usage quotas and cost tracking#962
Divv1524 wants to merge 1 commit into
imDarshanGK:mainfrom
Divv1524:fix/issue-667-usage-quotas

Conversation

@Divv1524

@Divv1524 Divv1524 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Description

Adds backend usage tracking, estimated AI provider cost reporting, and quota enforcement for AI analysis requests.

This PR includes:

  • UsageLog model to store request usage, token estimates, provider/model, user/team scope, and estimated cost
  • QuotaConfig model to configure user/team quotas by requests, tokens, and cost
  • Reusable usage service for token estimation, cost estimation, aggregation, alerts, and quota enforcement
  • New authenticated endpoints:
    • GET /usage/summary
    • GET /usage/costs
    • POST /quotas
    • GET /quotas
  • /analyze/ quota enforcement with 429 response when limits are exceeded
  • Per-user quota support via auth and per-team support via X-Team-Id
  • README documentation for the new usage/quota APIs
  • Comprehensive tests for happy paths, auth, validation, quota precedence, cached requests, daily/monthly aggregation, provider cost grouping, and quota edge cases

Related Issue

Fixes #667

Type of change

  • Bug fix
  • New feature / enhancement
  • Documentation update
  • Test addition
  • Refactor

Checklist

  • I have read CONTRIBUTING.md
  • My branch is up to date with main
  • I have run pytest -v and all tests pass
  • I have not introduced duplicate issues or features
  • My PR title follows the format: feat/fix/docs/test: short description
  • I have added tests for new features (Level 2 and 3 issues)
  • No hardcoded secrets or API keys in my code
  • This PR is linked to a GSSoC 2026 issue

Screenshots (if frontend change)

Not applicable. Backend-only change.

Test evidence

.\.venv\Scripts\python.exe -m ruff check backend\app
All checks passed!

.\.venv\Scripts\python.exe -m compileall backend\app
# passed

.\.venv\Scripts\python.exe -m pytest -v
432 passed, 1 warning in 38.85s

@Divv1524 Divv1524 requested a review from imDarshanGK as a code owner June 8, 2026 13:37
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.

Add cost estimation and quota management for AI provider usage

1 participant