credits: defer a clawback cap that would fall under recorded spend
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
Applying the draw mid-cycle used to push max_budget below the member's already-recorded spend, so LiteLLM refused every call until their next reset (weeks for some anchors). The draw is still recorded immediately — books are correct — but the cap is only pushed once the member's spend has room for it, which happens right after LiteLLM's reset. The tick retries every 10 minutes, so the cap lands at the first tick after the boundary; deferred members are reported in close_credit_cycles().deferred. tests/test_credit_cycle_close.py covers draw-recorded/cap-deferred and the cap landing after spend resets, with no second draw.
This commit is contained in:
1 parent
1070fc5dd0
commit
3e01f727c2
2 files changed
+68
-6
No files matched your search
+26
-6
@@ -4424,6 +4424,7 @@ async def close_credit_cycles() -> dict:
|
||||
skipped = 0
|
||||
errors: list[str] = []
|
||||
details: list[dict] = []
|
||||
deferred: list[dict] = []
|
||||
|
||||
for email, anchor_day, balance, credit_balance, credits_drawn in rows:
|
||||
if not anchor_day:
|
||||
@@ -4477,14 +4478,32 @@ async def close_credit_cycles() -> dict:
|
||||
drawn_total += drawn
|
||||
details.append({"email": email, "cycle": cycle_key,
|
||||
"spend": round(spend, 2), "drawn": round(drawn, 2)})
|
||||
try:
|
||||
applied = await sync_team_budget_from_balance(email)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"{email}: cap sync failed: {str(exc)[:120]}")
|
||||
else:
|
||||
details[-1]["team_budget"] = applied
|
||||
cycle_end = cycle_start
|
||||
|
||||
# Re-sync the cap only when it will not put the member under their
|
||||
# ALREADY-RECORDED spend. A clawback applied mid-cycle would otherwise
|
||||
# leave max_budget < spend and LiteLLM refuses every call until the
|
||||
# next reset — weeks for some anchors. The draw is still recorded now;
|
||||
# the cap falls at the first tick after their spend resets.
|
||||
if (email or "").strip().lower() in UNCAPPED_EMAILS:
|
||||
continue
|
||||
try:
|
||||
team = get_team_budget_from_db(team_id) or {}
|
||||
spend_now = team.get("spend")
|
||||
cur_cap = team.get("max_budget")
|
||||
eff = compute_max_budget(allowance, purchased, spend_now,
|
||||
get_credits_drawn(email))
|
||||
if cur_cap is not None and abs(cur_cap - eff["max_budget"]) <= 1e-9:
|
||||
continue
|
||||
if spend_now is None or spend_now <= eff["max_budget"] + 1e-9:
|
||||
await litellm_update_team_budget(team_id, eff["max_budget"])
|
||||
details.append({"email": email, "cap_applied": eff["max_budget"]})
|
||||
else:
|
||||
deferred.append({"email": email, "cap": eff["max_budget"],
|
||||
"spend_now": round(float(spend_now), 2)})
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"{email}: cap sync failed: {str(exc)[:120]}")
|
||||
|
||||
if errors:
|
||||
logger.warning("close_credit_cycles: %d error(s): %s", len(errors), "; ".join(errors[:5]))
|
||||
return {
|
||||
@@ -4493,6 +4512,7 @@ async def close_credit_cycles() -> dict:
|
||||
"closed": closed,
|
||||
"credits_drawn": round(drawn_total, 2),
|
||||
"skipped": skipped,
|
||||
"deferred": deferred,
|
||||
"errors": errors,
|
||||
"details": details,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user