Skip to content

fix: count daily quota against the org that owns the limit - #378

Merged
Priyanshu-u07 merged 1 commit into
mainfrom
fix/org-scoped-quota
Oct 3, 2026
Merged

Priyanshu-u07 merged 1 commit into
mainfrom
fix/org-scoped-quota

Conversation

@Priyanshu-u07

Copy link
Copy Markdown
Collaborator

The daily limit comes from an org-wide policy. The query says so:

    (DBPolicy.org_id == org_id) & (DBPolicy.deployment_id.is_(None))

but the Redis counters were keyed on user_id, which is apikey:<id> for an API-key request. Each key got the whole org's allowance to itself, so an org with five keys could spend five times its limit.

Both sides now resolve the same scope through one helper, and the key is built in one place because a limit counted against a different key than it is enforced on is not a limit.

increment_redis_only gained a db parameter. It had no way to turn an API key into an org on its own, and router.py already had a session. The alternative carrying the org from the resolved context would have meant a wire-format change across the inference boundary for a bug that lives inside the policy engine.

Identities that are not API keys fall back to their own id. That is narrower than an org, so a sandbox token cannot spend someone else's quota.

On deploy

Counters under the old key shape are orphaned rather than migrated. They are daily keys and age out on their own, but every org starts that day from zero regardless of what it has already spent. Deliberate: migrating the would mean summing each org's keys at an arbitrary moment, and the result would be wrong in a different way.

Closes #347

Signed-off-by: Priyanshu-u07 <connect.priyanshu8271@gmail.com>
@Priyanshu-u07
Priyanshu-u07 merged commit d9c18b3 into main Oct 3, 2026
1 check passed
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.

Org quota is enforced per API key, not per org

1 participant