Chuyển tới nội dung chính

Auto-allocate default_credit on user create / update

Credit behavior on POST /api/v1/auth/users and PUT /api/v1/auth/users/{user_id}.

User create/update still succeeds when auth checks pass. Credit allocation is soft-fail: missing or insufficient budget does not fail the user API.

Helpers live in app/features/rate_limit/service.py:

  • delete_user_credit_config — removes UserLlmCreditConfig (no used guard)
  • try_allocate_default_user_credit — allocates org default_credit when possible; notifies the requester on budget / usage shortfall

Available budget:

remaining = org.limit_credits - SUM(user_llm_credit_configs.limit_credits)

Allocate only when all of:

  • target user is not system_admin (quota bypass)
  • org has OrgLlmCreditConfig and default_credit > 0
  • user has no existing UserLlmCreditConfig
  • remaining >= default_credit
  • current-period used <= default_credit

Notify type: llm_credit_org_budget_insufficient (requester only).


Create user (POST /auth/users)​

#ConditionCredit behaviorNotify requester?
C1New user, has org_id, default_credit > 0, remaining OK, used OKCreate UserLlmCreditConfig with limit = default_creditNo
C2Has org_id, default_credit > 0, remaining < default_creditNo configYes
C3Has org_id, default_credit > 0, used > default_creditNo configYes
C4default_credit = 0 or no OrgLlmCreditConfigNo configNo
C5org_id is NoneSkip allocateNo
C6Target is system_adminSkip (quota bypass)No

Reactivate user (same POST, soft-deleted email)​

When create finds an existing user with deleted=True, it reactivates that row (same user_id) instead of inserting a new user.

#ConditionCredit behaviorNotify?
R1Org changed + old config existsDelete old config → try allocate for new org (C1–C6)Only if allocate fails (budget / used)
R2Same org + config already existsNo delete; allocate no-opNo
R3Same org + no config yetTry allocate like createSame as C1–C3

Update user (PUT /auth/users/{id}, admin path)​

Self-edit by a non–system_admin returns early and does not touch credit (U7).

#ConditionCredit behaviorNotify?
U1Org changed + old config existsDelete old config → allocate for new orgOnly if allocate fails
U2Org changed + no configAllocate for new orgSame as above
U3Org changed → org_id = null (sysadmin)Delete old config; no allocateNo
U4Same org + no config + default_credit > 0 + budget OKBackfill: create configNo
U5Same org + no config + insufficient budget / usedNo configYes
U6Same org + config already existsAllocate no-op (keep old limit)No
U7Self-edit (non–sysadmin edits self)Early return — no credit logicNo

Quick summary​

ActionWhen config is createdWhen config is deletedWhen requester is notified
CreateBudget + default OK—Allocate attempted but remaining/used blocks it
Reactivate org changeAfter delete, if new org OKAlways if old config existsSame as create shortfall
Update org changeAfter delete, if new org OKAlways if old config existsSame
Update same org, no quotaBackfill if OK—Shortfall only
Update same org, has quota———