fix(router): enforce deployment budgets for dynamically added models#29273
Conversation
Register deployment max_budget/budget_duration when models are added via upsert_deployment (e.g. /model/new) so RouterBudgetLimiting matches startup model_list behavior. Co-authored-by: Cursor <[email protected]>
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Remove unused RouterBudgetLimiting import and add router unit tests for deployment budget helper methods required by router_code_coverage. Co-authored-by: Cursor <[email protected]>
Greptile SummaryThis PR fixes a gap where deployments added at runtime via
Confidence Score: 5/5Safe to merge — the change is additive, all new code paths are covered by unit tests, and no existing behaviour is altered for routers that don't use runtime deployment additions. The fix is well-scoped: No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/router.py | Adds _sync_deployment_budget_config, _deployment_has_budget_limits, and _get_router_deployment_budget_limiter to wire budget registration into add_deployment and delete_deployment. Lazy init of RouterBudgetLimiting when the first budgeted deployment arrives at runtime is correctly handled. Upsert path also cleans up stale budget entries via _sync_deployment_budget_config. |
| litellm/router_strategy/budget_limiter.py | Adds register_deployment_budget (delegates to _init_deployment_budgets) and unregister_deployment_budget (removes entry, nulls map when empty). Minor type-hint cleanup of model_list signature. Logic is sound and consistent with existing _init_deployment_budgets behavior. |
| litellm/proxy/spend_tracking/spend_management_endpoints.py | Hoists _get_router_deployment_budget_limiter() lookup outside the provider loop and removes the redundant RouterBudgetLimiting import. Cleaner than the previous per-iteration isinstance scan. |
| litellm/proxy/hooks/model_max_budget_limiter.py | Adds explicit self.deployment_budget_config = None in _PROXY_VirtualKeyModelMaxBudgetLimiter.__init__ so the attribute exists when accessed, preventing AttributeError since the subclass never calls super().__init__(). |
| tests/router_unit_tests/test_router_helper_utils.py | Adds unit tests for _deployment_has_budget_limits, _sync_deployment_budget_config (add and clear paths), and the upsert stale-budget scenario. One existing assertion reformatted (no semantic change). Tests are well-structured and cover the new code paths. |
| tests/test_litellm/router_strategy/test_budget_limiter_hotpath.py | Adds two tests directly exercising register_deployment_budget/unregister_deployment_budget on RouterBudgetLimiting and the full Router.add_deployment → budget config path. Both tests use the disable_budget_sync fixture and mock asyncio.create_task, matching the file's existing test style. |
Reviews (3): Last reviewed commit: "fix black" | Re-trigger Greptile
Unregister deployment budget config when max_budget/budget_duration are removed, including upsert replace paths. Hoist provider budget logger lookup outside the provider loop. Co-authored-by: Cursor <[email protected]>
0ffab87
into
litellm_internal_staging
…erriAI#29273) * fix(router): enforce deployment budgets for dynamically added models Register deployment max_budget/budget_duration when models are added via upsert_deployment (e.g. /model/new) so RouterBudgetLimiting matches startup model_list behavior. Co-authored-by: Cursor <[email protected]> * fix(router): address CI lint and router coverage for budget sync helpers Remove unused RouterBudgetLimiting import and add router unit tests for deployment budget helper methods required by router_code_coverage. Co-authored-by: Cursor <[email protected]> * fix(router): clear stale deployment budget on upsert without limits Unregister deployment budget config when max_budget/budget_duration are removed, including upsert replace paths. Hoist provider budget logger lookup outside the provider loop. Co-authored-by: Cursor <[email protected]> * Fix mypy * fix black --------- Co-authored-by: Cursor <[email protected]>
Summary
max_budget/budget_durationonRouterBudgetLimitingwhen deployments are added viaadd_deployment/upsert_deployment(e.g./model/new), not only at router startup frommodel_list.router_budget_limitingwhen the first budgeted deployment is added at runtime._PROXY_VirtualKeyModelMaxBudgetLimiteris not mistaken for deployment budget enforcement.Test plan
poetry run pytest tests/test_litellm/router_strategy/test_budget_limiter_hotpath.py::test_register_deployment_budget_for_runtime_added_deployment -vpoetry run pytest tests/test_litellm/router_strategy/test_budget_limiter_hotpath.py::test_router_add_deployment_registers_deployment_budget -v/model/newwith tinymax_budget; verify second request fails with budget errormodel_listbudget behavior unchangedFixes LIT-3043


Fixes #25799