From 18a4447aa2e2fd08a1140ba77da2b4e7a69c6458 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Fri, 27 Nov 2026 21:58:34 +0000 Subject: [PATCH] usage: handle an uncapped team in /broker/usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An operator account has team max_budget NULL (no cap). broker_get_usage only assigned balance when max_budget was not None, so the DB path left 'balance' unbound and the HTTP fallback read the same name before assignment — a 500 for exactly the accounts the uncapped policy created. Initialise balance from the member's allowance + credits still available, mark the response uncapped when the team row has no cap, and keep the dashboard arithmetic defined. --- app/main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index b4e3335..814040d 100644 --- a/app/main.py +++ b/app/main.py @@ -7681,13 +7681,20 @@ async def broker_get_usage(email: str, days: int = 0) -> dict: credits_drawn = get_credits_drawn(email) spend = 0.0 reset_at = None + # Default: the member's own allowance + credits still available. An + # uncapped operator account has no team max_budget (NULL); keep this figure + # for the dashboard's arithmetic and say plainly that there is no cap. + balance = allowance + max(0.0, credits - credits_drawn) + uncapped = False # Fast path: direct Postgres reads. if litellm_db_ready(): try: team = get_team_budget_from_db(team_id) if team: - if team["max_budget"] is not None: + if team["max_budget"] is None: + uncapped = True + else: balance = float(team["max_budget"]) spend = float(team["spend"] or 0.0) reset_at = team["budget_reset_at"] @@ -7732,6 +7739,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict: return { "email": email, "guest": is_guest_account(email), + "uncapped": uncapped, "balance": balance, "allowance": allowance, "credits": credits, @@ -7761,7 +7769,11 @@ async def broker_get_usage(email: str, days: int = 0) -> dict: r.raise_for_status() for t in r.json(): if t.get("team_id") == team_id: - balance = float(t.get("max_budget") or balance) + mb = t.get("max_budget") + if mb is None: + uncapped = True + else: + balance = float(mb) spend = float(t.get("spend") or 0.0) reset_at = t.get("budget_reset_at") break @@ -7815,6 +7827,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict: return { "email": email, "guest": is_guest_account(email), + "uncapped": uncapped, "balance": balance, "allowance": allowance, "credits": credits,