Skip to content

Add usage history per API key and model - #441

Merged
Priyanshu-u07 merged 2 commits into
mainfrom
feat/usage-history
Oct 26, 2026
Merged

Priyanshu-u07 merged 2 commits into
mainfrom
feat/usage-history

Conversation

@Priyanshu-u07

Copy link
Copy Markdown
Collaborator

/config/quota/usage returns only today's totals per key, summed across models, so there's no way to see usage per model or for past days.

Add GET /management/config/quota/usage/history?start=&end=: daily requests and prompt/completion/total tokens per API key and model, for the caller's org.

  • One query joining usage_stats to api_keys (usage is stored as apikey:<id>), instead of a query per key.
  • Revoked keys are included and flagged key_active: false, since their usage still counts.
  • Requires ORG_VIEW, like the existing endpoint. The range defaults to the last 30 days and is limited to 366.
  • Days are the dates usage was recorded under (the server's local date, as persist_usage_db writes them).

Tests: 4 run in CI (permission, no org, invalid ranges). One checks the query against Postgres and skips without TEST_DATABASE_URL. I ran it against a Postgres with the repo schema: it passes, and fails if the org filter is removed. Full suite with the CI ignore list: 0 failures.

The usage endpoint returns only today's totals per key, summed across models. Add /config/quota/usage/history: daily requests and tokens per API key and model over a date range, for the caller's org, with revoked keys included. One join through api_keys replaces a query per key.

Signed-off-by: Priyanshu-u07 <connect.priyanshu8271@gmail.com>
So the history can be read for an org in-process, not only through the endpoint. The endpoint keeps its checks and calls it; behaviour is unchanged.

Signed-off-by: Priyanshu-u07 <connect.priyanshu8271@gmail.com>
@Priyanshu-u07
Priyanshu-u07 merged commit 84ced1f into main Oct 26, 2026
2 checks passed
@Priyanshu-u07
Priyanshu-u07 deleted the feat/usage-history branch October 26, 2026 11:38
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.

1 participant