diff --git a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py index fd160636d46..94d539f94af 100644 --- a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py +++ b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py @@ -349,11 +349,21 @@ ALLOWED_UI_SETTINGS_FIELDS: Final = { } ENABLE_PTU_COST_ATTRIBUTION_UI_SETTING: Final = "enable_ptu_cost_attribution" +APPLY_USER_BUDGET_TO_TEAM_KEYS_UI_SETTING: Final = "apply_user_budget_to_team_keys" # UI settings derived from the deployment environment. Deliberately kept out of # ALLOWED_UI_SETTINGS_FIELDS: they are read-only, never persisted, and PATCH # rejects them so an admin cannot flip an env-gated feature at runtime. -_DERIVED_UI_SETTINGS_FIELDS: Final[frozenset[str]] = frozenset({ENABLE_PTU_COST_ATTRIBUTION_UI_SETTING}) +_DERIVED_UI_SETTINGS_FIELDS: Final[frozenset[str]] = frozenset( + {ENABLE_PTU_COST_ATTRIBUTION_UI_SETTING, APPLY_USER_BUDGET_TO_TEAM_KEYS_UI_SETTING} +) + + +def _apply_user_budget_to_team_keys_enabled() -> bool: + from litellm.proxy.proxy_server import general_settings + + settings: Final = _UI_SETTINGS_OBJECT.validate_python(general_settings) + return settings.get("apply_user_budget_to_team_keys") is True def _derived_ui_setting_value(key: str) -> object: @@ -366,6 +376,8 @@ def _derived_ui_setting_value(key: str) -> object: """ if key == ENABLE_PTU_COST_ATTRIBUTION_UI_SETTING: return is_ptu_cost_attribution_enabled() + if key == APPLY_USER_BUDGET_TO_TEAM_KEYS_UI_SETTING: + return _apply_user_budget_to_team_keys_enabled() return None @@ -1564,6 +1576,7 @@ async def get_ui_settings(): values={ **settings["values"], ENABLE_PTU_COST_ATTRIBUTION_UI_SETTING: is_ptu_cost_attribution_enabled(), + APPLY_USER_BUDGET_TO_TEAM_KEYS_UI_SETTING: _apply_user_budget_to_team_keys_enabled(), }, field_schema=settings["field_schema"], ) diff --git a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py index fc5733b1a82..2fc2a7d0de9 100644 --- a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py +++ b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py @@ -3268,6 +3268,68 @@ class TestPtuCostAttributionUISetting: assert not mock_prisma.db.litellm_uisettings.upsert.called +class TestApplyUserBudgetToTeamKeysUISetting: + """``apply_user_budget_to_team_keys`` mirrors general_settings on every GET. + + The proxy enforces the key owner's user budget on team keys only when + ``general_settings.apply_user_budget_to_team_keys`` is on, so the dashboard + shows the owner's budget gate on a team key iff this derived value is true. + Like the other derived settings it is read-only and never persisted. + """ + + @staticmethod + def _mock_prisma(monkeypatch, stored=None): + from unittest.mock import AsyncMock, MagicMock + + mock_prisma = MagicMock() + mock_record = None + if stored is not None: + mock_record = MagicMock() + mock_record.ui_settings = stored + mock_prisma.db.litellm_uisettings.find_unique = AsyncMock(return_value=mock_record) + mock_prisma.db.litellm_uisettings.upsert = AsyncMock() + monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma) + return mock_prisma + + def test_reported_false_when_general_settings_lacks_the_flag(self, mock_auth, monkeypatch): + monkeypatch.setattr("litellm.proxy.proxy_server.general_settings", {}) + self._mock_prisma(monkeypatch) + + response = client.get("/get/ui_settings") + + assert response.status_code == 200 + assert response.json()["values"]["apply_user_budget_to_team_keys"] is False + + def test_reported_true_when_general_settings_enables_the_flag(self, mock_auth, monkeypatch): + monkeypatch.setattr( + "litellm.proxy.proxy_server.general_settings", + {"apply_user_budget_to_team_keys": True}, + ) + self._mock_prisma(monkeypatch) + + response = client.get("/get/ui_settings") + + assert response.status_code == 200 + assert response.json()["values"]["apply_user_budget_to_team_keys"] is True + + def test_a_persisted_true_cannot_forge_the_derived_value(self, mock_auth, monkeypatch): + """A row written before the allowlist existed must not be able to turn the feature on.""" + monkeypatch.setattr("litellm.proxy.proxy_server.general_settings", {}) + self._mock_prisma(monkeypatch, stored={"apply_user_budget_to_team_keys": True}) + + response = client.get("/get/ui_settings") + + assert response.status_code == 200 + assert response.json()["values"]["apply_user_budget_to_team_keys"] is False + + def test_is_not_an_allowlisted_persisted_setting(self): + from litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints import ( + ALLOWED_UI_SETTINGS_FIELDS, + ) + + assert "apply_user_budget_to_team_keys" not in ALLOWED_UI_SETTINGS_FIELDS + + class TestTeamAdminEditableTeamFieldsSetting: """team_admin_editable_team_fields: the proxy-wide allow-list update_team applies to team admins.""" diff --git a/ui/litellm-dashboard/src/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys.ts b/ui/litellm-dashboard/src/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys.ts new file mode 100644 index 00000000000..3aeed5e4338 --- /dev/null +++ b/ui/litellm-dashboard/src/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys.ts @@ -0,0 +1,6 @@ +import { useUISettings } from "./useUISettings"; + +export const APPLY_USER_BUDGET_TO_TEAM_KEYS_SETTING_KEY = "apply_user_budget_to_team_keys"; + +export const useApplyUserBudgetToTeamKeys = (): boolean => + useUISettings().data?.values?.[APPLY_USER_BUDGET_TO_TEAM_KEYS_SETTING_KEY] === true; diff --git a/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.test.tsx b/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.test.tsx index 4d963a2f603..7ef7f1fcb09 100644 --- a/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.test.tsx +++ b/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.test.tsx @@ -58,6 +58,10 @@ vi.mock("@/app/(dashboard)/hooks/keys/useKeyInfo", () => ({ useKeyInfo: vi.fn(), })); +vi.mock("@/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys", () => ({ + useApplyUserBudgetToTeamKeys: vi.fn(() => false), +})); + vi.mock("@/app/(dashboard)/hooks/useTeams", () => ({ default: vi.fn(), })); diff --git a/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.tsx b/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.tsx index 39dd2cc5ab2..ec405ee9cad 100644 --- a/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.tsx +++ b/ui/litellm-dashboard/src/components/VirtualKeysPage/VirtualKeysTable.tsx @@ -3,6 +3,7 @@ import { useKeyInfo } from "@/app/(dashboard)/hooks/keys/useKeyInfo"; import { useKeys } from "@/app/(dashboard)/hooks/keys/useKeys"; import { useOrganizations } from "@/app/(dashboard)/hooks/organizations/useOrganizations"; +import { useApplyUserBudgetToTeamKeys } from "@/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys"; import { useAllTeams } from "@/app/(dashboard)/hooks/teams/useTeams"; import { DEBOUNCE_WAIT_MS } from "@/utils/debounceConstants"; import { @@ -139,10 +140,18 @@ export function VirtualKeysTable({ headerActions }: VirtualKeysTableProps) { const keyList = useMemo(() => keys?.keys ?? [], [keys]); const rowCount = keys?.total_count ?? 0; - const columns = useMemo( - () => getKeyTableColumns({ allTeams, organizations, onSelectKey: (key) => void setSelectedKeyId(key.token) }), - [allTeams, organizations, setSelectedKeyId], + const applyUserBudgetToTeamKeys = useApplyUserBudgetToTeamKeys(); + + const columnDeps = useMemo( + () => ({ + allTeams, + organizations, + onSelectKey: (key: KeyResponse) => void setSelectedKeyId(key.token), + applyUserBudgetToTeamKeys, + }), + [allTeams, organizations, setSelectedKeyId, applyUserBudgetToTeamKeys], ); + const columns = useMemo(() => getKeyTableColumns(columnDeps), [columnDeps]); const selectedKeyFromList = useMemo( () => keyList.find((key) => key.token === selectedKeyId), diff --git a/ui/litellm-dashboard/src/components/VirtualKeysPage/keyTableColumns.tsx b/ui/litellm-dashboard/src/components/VirtualKeysPage/keyTableColumns.tsx index 36a0f079204..33625c0e1eb 100644 --- a/ui/litellm-dashboard/src/components/VirtualKeysPage/keyTableColumns.tsx +++ b/ui/litellm-dashboard/src/components/VirtualKeysPage/keyTableColumns.tsx @@ -4,7 +4,7 @@ import { Info } from "lucide-react"; import { ColumnDef } from "@tanstack/react-table"; import { DataTableMultiSortHeader, DataTableSortHeader, type DataTableSortField } from "@/components/shared/DataTable"; -import { inheritedBudgetGates } from "@/components/shared/InheritedBudgetHint"; +import { inheritedBudgetGates, keyOwnerBudgetSource } from "@/components/shared/InheritedBudgetHint"; import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card"; import { Skeleton } from "@/components/ui/skeleton"; import { @@ -86,12 +86,14 @@ interface KeyTableColumnsDeps { allTeams: Team[]; organizations: Organization[]; onSelectKey: (key: KeyResponse) => void; + applyUserBudgetToTeamKeys: boolean; } export const getKeyTableColumns = ({ allTeams, organizations, onSelectKey, + applyUserBudgetToTeamKeys, }: KeyTableColumnsDeps): ColumnDef[] => [ { id: "key_alias", @@ -279,7 +281,7 @@ export const getKeyTableColumns = ({ maxBudget={row.original.max_budget} inheritedGates={ row.original.max_budget == null - ? inheritedBudgetGates(team, organization, row.original.team_id ? null : row.original.user) + ? inheritedBudgetGates(team, organization, keyOwnerBudgetSource(row.original, applyUserBudgetToTeamKeys)) : [] } /> diff --git a/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.test.tsx b/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.test.tsx index a2c44c79e96..8faae9c9660 100644 --- a/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.test.tsx +++ b/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.test.tsx @@ -2,7 +2,7 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it } from "vitest"; -import { InheritedBudgetHint, inheritedBudgetGates } from "./InheritedBudgetHint"; +import { InheritedBudgetHint, inheritedBudgetGates, keyOwnerBudgetSource } from "./InheritedBudgetHint"; const team = { team_id: "team-1", team_alias: "Platform", max_budget: 1200, budget_duration: "30d" }; const organization = { @@ -75,6 +75,21 @@ describe("inheritedBudgetGates", () => { }); }); +describe("keyOwnerBudgetSource", () => { + it("returns the owner on a personal key regardless of the flag", () => { + expect(keyOwnerBudgetSource({ team_id: null, user }, false)).toBe(user); + expect(keyOwnerBudgetSource({ team_id: null, user }, true)).toBe(user); + }); + + it("hides the owner on a team key when the flag is off", () => { + expect(keyOwnerBudgetSource({ team_id: "team-1", user }, false)).toBeNull(); + }); + + it("returns the owner on a team key when the flag is on", () => { + expect(keyOwnerBudgetSource({ team_id: "team-1", user }, true)).toBe(user); + }); +}); + describe("InheritedBudgetHint", () => { it("renders nothing without gates", () => { const { container } = render(); diff --git a/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.tsx b/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.tsx index 85129f65948..4c02efa66ed 100644 --- a/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.tsx +++ b/ui/litellm-dashboard/src/components/shared/InheritedBudgetHint.tsx @@ -63,6 +63,11 @@ export const inheritedBudgetGates = ( ): readonly InheritedBudgetGate[] => [teamGate(team), organizationGate(organization), userGate(user)].filter((gate) => gate !== null); +export const keyOwnerBudgetSource = ( + key: { team_id?: string | null; user?: UserBudgetSource | null }, + applyUserBudgetToTeamKeys: boolean, +): UserBudgetSource | null => (!key.team_id || applyUserBudgetToTeamKeys ? key.user ?? null : null); + const formatGate = (gate: InheritedBudgetGate): string => `${gate.scope} ${gate.alias}: $${formatNumberWithCommas(gate.maxBudget, 2)}${gate.budgetDuration ? ` / ${gate.budgetDuration}` : ""}`; diff --git a/ui/litellm-dashboard/src/components/templates/key_info_view.budget_display.test.tsx b/ui/litellm-dashboard/src/components/templates/key_info_view.budget_display.test.tsx index 0f4354159df..965fb3e8054 100644 --- a/ui/litellm-dashboard/src/components/templates/key_info_view.budget_display.test.tsx +++ b/ui/litellm-dashboard/src/components/templates/key_info_view.budget_display.test.tsx @@ -7,6 +7,7 @@ import KeyInfoView from "./key_info_view"; import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized"; import useTeams from "@/app/(dashboard)/hooks/useTeams"; import { useOrganizations } from "@/app/(dashboard)/hooks/organizations/useOrganizations"; +import { useApplyUserBudgetToTeamKeys } from "@/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys"; import type { Organization } from "../networking"; // IMPORTANT: do not mock `@/utils/dataUtils` here. We want to exercise the @@ -26,6 +27,9 @@ vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: vi.fn() })); vi.mock("@/app/(dashboard)/hooks/projects/useProjects", () => ({ useProjects: vi.fn().mockReturnValue({ data: [], isLoading: false }), })); +vi.mock("@/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys", () => ({ + useApplyUserBudgetToTeamKeys: vi.fn(() => false), +})); vi.mock("@/app/(dashboard)/hooks/keys/useResetKeySpend", () => ({ useResetKeySpend: vi.fn(() => ({ mutate: vi.fn(), isPending: false })), })); @@ -114,6 +118,26 @@ const baseAuthorized = { isAuthorized: true, }; +const TEST_BUDGET_TEAM_FIELDS = { + team_id: "team-123", + team_alias: "Test Budget", + max_budget: 1200, + budget_duration: "30d", +}; + +const TEAM_KEY_WITH_BUDGETED_OWNER = { + ...MOCK_KEY_DATA, + max_budget: null, + team_id: "team-123", + user: { + user_id: "user-1", + user_email: "owner@example.com", + user_alias: "Budget Owner", + max_budget: 1500, + budget_duration: "1mo", + }, +} as unknown as KeyResponse; + const makeTeam = (overrides: Partial): Team => ({ team_id: "team-default", team_alias: "Default Team", @@ -158,6 +182,7 @@ describe("KeyInfoView overview budget display (LIT-2845)", () => { vi.mocked(useTeams).mockReturnValue({ teams: [], setTeams: vi.fn() }); vi.mocked(useAuthorized).mockReturnValue(baseAuthorized); mockOrganizations([]); + vi.mocked(useApplyUserBudgetToTeamKeys).mockReturnValue(false); }); it("renders a sub-dollar max_budget ($0.10) with 2-decimal precision in the overview Spend card", async () => { @@ -214,7 +239,7 @@ describe("KeyInfoView overview budget display (LIT-2845)", () => { it("never pairs key spend with the team budget: shows Unlimited plus an inherited-budget hint", async () => { vi.mocked(useTeams).mockReturnValue({ - teams: [makeTeam({ team_id: "team-123", team_alias: "Test Budget", max_budget: 1200, budget_duration: "30d" })], + teams: [makeTeam(TEST_BUDGET_TEAM_FIELDS)], setTeams: vi.fn(), }); renderWithProviders( @@ -288,27 +313,14 @@ describe("KeyInfoView overview budget display (LIT-2845)", () => { expect(screen.getByTestId("inherited-budget-hint")).toHaveTextContent("User Budget Owner: $1,500.00 / 1mo"); }); - it("omits the owner's user budget from the hint for a team key", async () => { + it("omits the owner's user budget from the hint for a team key when apply_user_budget_to_team_keys is off", async () => { vi.mocked(useTeams).mockReturnValue({ - teams: [makeTeam({ team_id: "team-123", team_alias: "Test Budget", max_budget: 1200, budget_duration: "30d" })], + teams: [makeTeam(TEST_BUDGET_TEAM_FIELDS)], setTeams: vi.fn(), }); renderWithProviders( {}} keyId={"test-key-id"} onKeyDataUpdate={() => {}} @@ -323,6 +335,29 @@ describe("KeyInfoView overview budget display (LIT-2845)", () => { expect(screen.getByTestId("inherited-budget-hint")).not.toHaveTextContent("User Budget Owner"); }); + it("lists the owner's user budget in the hint for a team key when apply_user_budget_to_team_keys is on", async () => { + vi.mocked(useApplyUserBudgetToTeamKeys).mockReturnValue(true); + vi.mocked(useTeams).mockReturnValue({ + teams: [makeTeam(TEST_BUDGET_TEAM_FIELDS)], + setTeams: vi.fn(), + }); + renderWithProviders( + {}} + keyId={"test-key-id"} + onKeyDataUpdate={() => {}} + teams={[]} + />, + ); + await waitFor(() => { + expect(screen.getByText(/of Unlimited/)).toBeInTheDocument(); + }); + await userEvent.setup().hover(screen.getByLabelText("question-circle")); + expect(screen.getByTestId("inherited-budget-hint")).toHaveTextContent("Team Test Budget: $1,200.00 / 30d"); + expect(screen.getByTestId("inherited-budget-hint")).toHaveTextContent("User Budget Owner: $1,500.00 / 1mo"); + }); + it("renders 'Unlimited' with no hint when neither key, team, nor org has a budget", async () => { vi.mocked(useTeams).mockReturnValue({ teams: [makeTeam({ team_id: "team-789", team_alias: "Free Team" })], diff --git a/ui/litellm-dashboard/src/components/templates/key_info_view.tsx b/ui/litellm-dashboard/src/components/templates/key_info_view.tsx index 2e4c380e1a5..3c2c9bb352d 100644 --- a/ui/litellm-dashboard/src/components/templates/key_info_view.tsx +++ b/ui/litellm-dashboard/src/components/templates/key_info_view.tsx @@ -1,6 +1,7 @@ import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized"; import { useProjects } from "@/app/(dashboard)/hooks/projects/useProjects"; import { useUISettings } from "@/app/(dashboard)/hooks/uiSettings/useUISettings"; +import { useApplyUserBudgetToTeamKeys } from "@/app/(dashboard)/hooks/uiSettings/useApplyUserBudgetToTeamKeys"; import useTeams from "@/app/(dashboard)/hooks/useTeams"; import { useOrganizations } from "@/app/(dashboard)/hooks/organizations/useOrganizations"; import { formatNumberWithCommas } from "@/utils/dataUtils"; @@ -45,7 +46,7 @@ import { extractMcpEntitlement } from "../mcp_server_management/mcpEntitlement"; import ObjectPermissionsView from "../object_permissions_view"; import { RegenerateKeyModal } from "../organisms/RegenerateKeyModal"; import { parseErrorMessage } from "../shared/errorUtils"; -import { InheritedBudgetHint, inheritedBudgetGates } from "../shared/InheritedBudgetHint"; +import { InheritedBudgetHint, inheritedBudgetGates, keyOwnerBudgetSource } from "../shared/InheritedBudgetHint"; import { KeyEditView } from "./key_edit_view"; interface KeyInfoViewProps { @@ -95,6 +96,7 @@ export default function KeyInfoView({ const { data: organizations } = useOrganizations(); const { data: projects } = useProjects(); const { data: uiSettingsData } = useUISettings(); + const applyUserBudgetToTeamKeys = useApplyUserBudgetToTeamKeys(); const { data: allMcpServers } = useMCPServers(); const { data: allMcpToolsets } = useMCPToolsets(); const enableProjectsUI = Boolean(uiSettingsData?.values?.enable_projects_ui); @@ -507,7 +509,7 @@ export default function KeyInfoView({ const hasOwnBudget = currentKeyData.max_budget !== null; const budgetDisplay = hasOwnBudget ? `$${formatNumberWithCommas(currentKeyData.max_budget, 2)}` : "Unlimited"; - const ownerUser = currentKeyData.team_id ? null : currentKeyData.user; + const ownerUser = keyOwnerBudgetSource(currentKeyData, applyUserBudgetToTeamKeys); const inheritedGates = hasOwnBudget ? [] : inheritedBudgetGates(parentTeam, parentOrg, ownerUser); return (