Repository navigation
fix: Custom Model pricing / Allow zero‑cost models to stay unblocked when BudgetExceeded is true (#14004) #25645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c324364
6ae499c
23a78f1
9b27d10
fe79bbd
3082325
78845a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,6 +318,8 @@ def update_valid_token_with_end_user_params( | |
| valid_token.end_user_model_max_budget = end_user_params[ | ||
| "end_user_model_max_budget" | ||
| ] | ||
| if end_user_params.get("end_user_max_budget") is not None: | ||
| valid_token.end_user_max_budget = end_user_params["end_user_max_budget"] | ||
| return valid_token | ||
|
|
||
|
|
||
|
|
@@ -1428,6 +1430,20 @@ async def _user_api_key_auth_builder( # noqa: PLR0915 | |
| model=current_model, | ||
| ) | ||
|
|
||
| # Check 5c. End user max budget | ||
| end_user_mb = valid_token.end_user_max_budget | ||
| if ( | ||
| end_user_mb is not None | ||
| and _end_user_object is not None | ||
| and valid_token.end_user_id is not None | ||
| ): | ||
| await proxy_logging_obj.max_budget_limiter.is_end_user_within_budget( | ||
| end_user_id=valid_token.end_user_id, | ||
| end_user_max_budget=end_user_mb, | ||
| end_user_spend=_end_user_object.spend, | ||
| route=route, | ||
| ) | ||
|
Comment on lines
+1483
to
+1499
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @greptile There were already duplication in Team Budget Checks and Global Proxy Budget Check between common_checks and the logic inside user_api_key_auth.py. This is a job for another refactor.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point — if Team and Global Proxy budget checks already follow the same "duplicated in both places" pattern, then this PR is consistent with the existing style rather than introducing a new problem. The broader deduplication is a separate concern. Tip: You can give me more context with .greptile/rules so I get this right next time. |
||
|
|
||
| # Check 6: Additional Common Checks across jwt + key auth | ||
| if valid_token.team_id is not None: | ||
| try: | ||
|
|
@@ -1935,6 +1951,20 @@ async def _run_post_custom_auth_checks( | |
| model=current_model, | ||
| ) | ||
|
|
||
| # 4b. Check end-user max_budget | ||
| end_user_mb = valid_token.end_user_max_budget | ||
| if ( | ||
| end_user_mb is not None | ||
| and end_user_object is not None | ||
| and valid_token.end_user_id is not None | ||
| ): | ||
| await proxy_logging_obj.max_budget_limiter.is_end_user_within_budget( | ||
| end_user_id=valid_token.end_user_id, | ||
| end_user_max_budget=end_user_mb, | ||
| end_user_spend=end_user_object.spend, | ||
| route=route, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In the standard key-auth and JWT auth flows, "Check 5c" and the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @greptile Is the problem now fixed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the issue is now fixed. The PR adds exactly the guard I described: # 2a. Check if model has zero cost - if so, skip all budget checks
model = get_model_from_request(request_data, route)
skip_budget_checks = False
if model is not None and llm_router is not None:
skip_budget_checks = _is_model_cost_zero(model=model, llm_router=llm_router)All three budget checks in |
||
|
|
||
| # 5. Look up user object if user_id is set | ||
| user_object = None | ||
| if valid_token.user_id is not None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this a regression for jwt auth?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greptile can you confirm this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my analysis of the diff, yes — this is a regression for JWT auth.
Here's why:
Before:
_check_end_user_budgetwas called directly insideget_end_user_object, which is invoked from both the key auth path AND the JWT auth path (handle_jwt.pyalso callsget_end_user_object). Budget enforcement was guaranteed regardless of auth method.After: The budget check was removed from
get_end_user_objectand moved to two places inuser_api_key_auth.py:_user_api_key_auth_builder(key auth path)_run_post_custom_auth_checks(custom auth)The new Check 5c only fires if
valid_token.end_user_max_budget is not None. This field is populated viaupdate_valid_token_with_end_user_params. For the JWT auth path, theend_user_paramsdict is constructed from the JWT builder result — and there's no evidence in this PR thatend_user_max_budgetis being populated in that flow. If it'sNoneon the token (the default), the check is silently skipped.The net result: a JWT-authenticated end user who has exceeded their
max_budgetwill no longer be blocked, because:get_end_user_objectno longer raisesBudgetExceededErrorvalid_token.end_user_max_budgetwas never set from the JWT pathCan you confirm whether
end_user_max_budgetis reliably populated onvalid_tokenduring JWT auth? If not, you'll need to either ensure the JWT builder populates it, or add a fallback in Check 5c that reads_end_user_object.litellm_budget_table.max_budgetdirectly whenvalid_token.end_user_max_budgetisNone.Tip: You can customize Greptile's behavior for this repo with
.greptile/rulesand.greptile/config.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greptile does adding a simple fallback like:
if end_user_mb is None and end_user_object is not None:
end_user_mb = getattr(end_user_object.litellm_budget_table, 'max_budget', None)
in 5c and 4b solves the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the fallback is the right approach, but your proposed code has a subtle bug:
getattr(end_user_object.litellm_budget_table, 'max_budget', None)will raiseAttributeErroriflitellm_budget_tableitself isNone— thegetattrdefault only catches missing attributes, notNone.attraccess.The safe version:
Apply this before the existing
if end_user_mb is not Nonecheck in both 5c and 4b (usingend_user_objectinstead of_end_user_objectin 4b, since that's the variable name there).This ensures:
valid_token.end_user_max_budgetwas never populated fromend_user_params, the budget is read directly from the fetchedend_user_objectvalid_token.end_user_max_budgetis already populated viaupdate_valid_token_with_end_user_paramsNonepointer risk from a missinglitellm_budget_tableTip: You can customize Greptile's behavior for this repo with
.greptile/rulesand.greptile/config.