Skip to content

fix(ui): persist budget window deletion on virtual keys - #31107

Merged
ryan-crabbe-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_fix_budget_window_delete
Jun 24, 2026
Merged

ryan-crabbe-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_fix_budget_window_delete

Conversation

@ryan-crabbe-berri

@ryan-crabbe-berri ryan-crabbe-berri commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Resolves LIT-3742

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review (5/5)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.

Screenshots / Proof of Fix

Root cause: when every budget window is removed in the virtual key edit form, the form set budget_limits to undefined. JSON.stringify drops undefined keys, so /key/update never received a budget_limits field, model_dump(exclude_unset=True) excluded it, and the backend branch that clears the stored windows on an empty value never ran. Editing a window kept working because a non-empty list is always sent. The one-line fix sends [] instead of undefined, which the backend already maps to "clear the stored windows"; see key_management_endpoints.py where budget_limits == [] is stored as JSON null.

The contrast below was captured against a live proxy backed by Postgres. The first update reproduces what today's UI sends on a delete (no budget_limits field); the second is what the patched UI sends.
Screenshot 2026-06-23 at 1 33 25 PM
Screenshot 2026-06-23 at 1 33 17 PM
Screenshot 2026-06-23 at 1 33 06 PM

########## 1. CREATE key with one budget window ##########
  created sk-V4h3fBj...  sent budget_limits=[{30d, $100}]
  budget_limits = [{"budget_duration": "30d", "max_budget": 100.0, "reset_at": "2026-07-01T00:00:00Z"}]

########## 2. BUG REPRO: POST /key/update WITHOUT budget_limits ##########
  (today's UI deletes the window by sending budget_limits=undefined; JSON.stringify omits the field)
  re-read /key/info -> budget_limits = [{"reset_at": "2026-07-01T00:00:00+00:00", "max_budget": 100.0, "budget_duration": "30d"}]
  window is STILL there: the deletion did not persist (the bug)

########## 3. FIX: POST /key/update WITH budget_limits: [] ##########
  (patched UI sends budget_limits=[] when all windows are deleted)
  re-read /key/info -> budget_limits = null
  window cleared: deletion now persists (fixed)

Edit and partial-delete still behave correctly (this is the "editing persists" half of the report):

Create key with TWO windows: [{30d,$100},{24h,$10}]
  budget_limits = [{"budget_duration": "30d", "max_budget": 100.0}, {"budget_duration": "24h", "max_budget": 10.0}]

A) EDIT a window (30d $100 -> $250); UI resends the full non-empty list
  budget_limits = [{"budget_duration": "30d", "max_budget": 250.0}, {"budget_duration": "24h", "max_budget": 10.0}]  (edit persisted)

B) DELETE one of two windows; UI sends the remaining one
  budget_limits = [{"budget_duration": "24h", "max_budget": 10.0}]  (partial delete persisted)

C) DELETE the last window; patched UI sends []
  budget_limits = null  (all windows cleared)

To reproduce the end-user flow in the UI on this branch: open http://localhost:4000/ui/?page=api-keys, edit a key that has a budget window under "Budget Windows", click the red "✕" next to the only window, click "Save Changes", reopen the key, and the window stays gone. Before this change it reappears.

Type

🐛 Bug Fix

Changes

ui/litellm-dashboard/src/components/templates/key_edit_view.tsx now submits budget_limits: [] when the user has removed every window, so a delete is sent to the backend as an explicit clear. When valid windows remain it submits those, and when entries remain but are still incomplete (a window whose max_budget has not been filled in yet) it omits the field so the saved windows are left untouched rather than wiped; that last case preserves the prior behavior and avoids a half-typed window clearing existing budgets on save. Added three regression tests in key_edit_view.test.tsx: deleting the last window submits [] (fails on the old code), leaving existing windows untouched still resubmits them so editing is not regressed, and an incomplete window omits budget_limits rather than clearing.

@ryan-crabbe-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where deleting the last budget window on a virtual key did not persist because the UI sent budget_limits: undefined, which JSON.stringify drops, leaving the backend unaware that the field changed. The one-line logic change now sends [] in that case, which the backend already treats as "clear all windows".

  • key_edit_view.tsx: Replaces the ternary validWindows.length > 0 ? validWindows : undefined with a three-branch block — send the valid windows when any exist, send [] when the list was explicitly emptied, and omit the field when entries exist but are all incomplete (preserving existing saved windows rather than clearing them with a half-typed entry).
  • key_edit_view.test.tsx: Adds three regression tests covering the delete-all case (must send []), the leave-untouched case (must resend existing windows), and the incomplete-entry case (must omit the field).

Confidence Score: 5/5

Safe to merge — the change is minimal, isolated to the budget-window submission path, and all three edge cases are covered by new regression tests.

The fix touches a single code path in the form submit handler. The before/after behavior is clearly documented in the PR, proven against a live proxy, and covered by three targeted tests that would catch regressions in any of the three submission scenarios (clear, preserve, incomplete). No unrelated code is changed.

No files require special attention.

Important Files Changed

Filename Overview
ui/litellm-dashboard/src/components/templates/key_edit_view.tsx One-line logic fix: sends [] instead of undefined when all budget windows are removed, so the backend receives an explicit clear signal. All three cases (valid windows, empty list, incomplete entries) are handled correctly.
ui/litellm-dashboard/src/components/templates/key_edit_view.test.tsx Three new regression tests added: delete-last-window sends [], untouched windows are resent, and an incomplete entry omits budget_limits. Tests are well-structured and cover the key scenarios.

Reviews (3): Last reviewed commit: "fix(ui): persist budget window deletion ..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a budget window deletion bug in the virtual key edit form where clearing all windows had no effect on the backend. When budget_limits was set to undefined, JSON.stringify silently dropped the field, so the backend's model_dump(exclude_unset=True) path never triggered the clear logic.

  • Core fix (key_edit_view.tsx, line 300): Changes validWindows.length > 0 ? validWindows : undefined to validWindows.length > 0 ? validWindows : [], guaranteeing the budget_limits key is always present in the payload — an empty array is already the backend's signal to clear stored windows.
  • Tests (key_edit_view.test.tsx): Adds two mock-only regression tests — one that fails on the old code (delete-last-window → []) and one that guards the non-regression path (untouched windows are resent).

Confidence Score: 4/5

Safe to merge; the change is a one-line frontend fix with no backend modifications and no changes to existing tests.

The fix is minimal and directly addresses the root cause: sending [] instead of undefined keeps budget_limits in the JSON payload so the backend clears the windows. Both new tests are mock-only, cover the main regression and the non-regression path, and no existing tests were weakened. There is a subtle secondary behavior change — a partially-filled window (valid budget_duration but empty max_budget) will now cause [] to be sent, clearing all backend windows, whereas before undefined was sent and the backend state was preserved. This is arguably the more correct behavior but is not explicitly tested.

No files require special attention; both changed files are straightforward and well-scoped.

Important Files Changed

Filename Overview
ui/litellm-dashboard/src/components/templates/key_edit_view.tsx One-line fix: sends budget_limits: [] instead of undefined when all windows are deleted, ensuring the backend receives an explicit clear signal rather than omitting the field entirely.
ui/litellm-dashboard/src/components/templates/key_edit_view.test.tsx Adds two new mock tests: one verifying that deleting the last budget window submits [], and one ensuring existing untouched windows are still resent correctly. Both are mock-only and satisfy the no-real-network-calls policy.

Reviews (2): Last reviewed commit: "fix(ui): persist budget window deletion ..." | Re-trigger Greptile

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Deleting every budget window from a virtual key looked like it saved but
reverted on reload, while editing a window persisted. The key edit form set
budget_limits to undefined once the window list was emptied, and
JSON.stringify drops undefined keys, so /key/update received no budget_limits
field at all and model_dump(exclude_unset=True) skipped the existing
clear-on-empty branch. Sending [] instead lets the backend store JSON null and
clear the stored windows, matching how it already treats an explicit empty list

Resolves LIT-3742
@ryan-crabbe-berri
ryan-crabbe-berri force-pushed the litellm_fix_budget_window_delete branch from cc90dc0 to e57b5a7 Compare June 23, 2026 17:29
@ryan-crabbe-berri

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up: only send budget_limits: [] when the user removed every window. When entries remain but are still incomplete (a window whose max_budget is not filled in yet) the field is now omitted so existing windows are preserved instead of being cleared by a half-typed window on save. Added a regression test for that case. @greptileai

@ryan-crabbe-berri
ryan-crabbe-berri merged commit 8f43892 into litellm_internal_staging Jun 24, 2026
76 checks passed
@ryan-crabbe-berri
ryan-crabbe-berri deleted the litellm_fix_budget_window_delete branch June 24, 2026 16:19
@ghost ghost mentioned this pull request Jun 25, 2026
9 of 13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants