Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Allow setting organization_id on key update endpoint

yuneng-jiang377c12b91765
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index d7ccd7d6f1b..06b3f20bcf3 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -1004,6 +1004,7 @@ class UpdateKeyRequest(KeyRequestBase):
temp_budget_expiry: Optional[datetime] = None
auto_rotate: Optional[bool] = None
rotation_interval: Optional[str] = None
+ organization_id: Optional[str] = None
@model_validator(mode="after")
def validate_temp_budget(self) -> "UpdateKeyRequest":
diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py
index 654205252b6..2c265c8066d 100644
--- a/litellm/proxy/management_endpoints/key_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py
@@ -1806,6 +1806,7 @@ async def update_key_fn(
- user_id: Optional[str] - User ID associated with key
- team_id: Optional[str] - Team ID associated with key
- agent_id: Optional[str] - The agent id associated with the key.
+ - organization_id: Optional[str] - The organization id of the key.
- budget_id: Optional[str] - The budget id associated with the key. Created by calling `/budget/new`.
- models: Optional[list] - Model_name's a user is allowed to call
- tags: Optional[List[str]] - Tags for organizing keys (Enterprise only)
@@ -1956,6 +1957,27 @@ async def update_key_fn(
user_api_key_cache=user_api_key_cache,
)
+ # Check org key limits if organization_id is being set or already exists on the key
+ _org_id_to_check = data.organization_id or getattr(
+ existing_key_row, "organization_id", None
+ )
+ if _org_id_to_check is not None:
+ org_table = await get_org_object(
+ org_id=_org_id_to_check,
+ user_api_key_cache=user_api_key_cache,
+ prisma_client=prisma_client,
+ )
+ if org_table is None:
+ raise HTTPException(
+ status_code=400,
+ detail=f"Organization not found for organization_id={_org_id_to_check}",
+ )
+ await _check_org_key_limits(
+ org_table=org_table,
+ data=data,
+ prisma_client=prisma_client,
+ )
+
# if team change - check if this is possible
if is_different_team(data=data, existing_key_row=existing_key_row):
if llm_router is None:
diff --git a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
index 09dfdb81cbb..fe1ba93699e 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
@@ -6765,3 +6765,112 @@ class TestValidateKeyAliasFormat:
_validate_key_alias_format(alias)
assert str(exc.value.code) == "400"
assert "Invalid key_alias format" in str(exc.value.message)
+
+
+async def test_check_org_key_limits_on_update_within_bounds():
+ """
+ Test that _check_org_key_limits works with UpdateKeyRequest when updating
+ a key's TPM/RPM limits within organization bounds.
+ """
+ mock_prisma_client = AsyncMock()
+ mock_prisma_client.db.litellm_verificationtoken.find_many = AsyncMock(
+ return_value=[]
+ )
+
+ org_table = LiteLLM_OrganizationTable(
+ organization_id="test-org-update-1",
+ organization_alias="test-org",
+ budget_id="budget-123",
+ models=["gpt-4"],
+ created_by="admin",
+ updated_by="admin",
+ litellm_budget_table=LiteLLM_BudgetTable(
+ budget_id="budget-123",
+ tpm_limit=20000,
+ rpm_limit=2000,
+ ),
+ )
+
+ data = UpdateKeyRequest(
+ key="sk-test-key",
+ tpm_limit=10000,
+ rpm_limit=1000,
+ tpm_limit_type="guaranteed_throughput",
+ rpm_limit_type="guaranteed_throughput",
+ organization_id="test-org-update-1",
+ )
+
+ # Should not raise any exception
+ await _check_org_key_limits(
+ org_table=org_table,
+ data=data,
+ prisma_client=mock_prisma_client,
+ )
+
+ mock_prisma_client.db.litellm_verificationtoken.find_many.assert_called_once_with(
+ where={"organization_id": "test-org-update-1"}
+ )
+
+
+async def test_check_org_key_limits_on_update_overallocation():
+ """
+ Test that _check_org_key_limits raises HTTPException when updating a key
+ would exceed organization TPM limits.
+ """
+ existing_key = MagicMock()
+ existing_key.tpm_limit = 15000
+ existing_key.rpm_limit = 1500
+ existing_key.metadata = {}
+
+ mock_prisma_client = AsyncMock()
+ mock_prisma_client.db.litellm_verificationtoken.find_many = AsyncMock(
+ return_value=[existing_key]
+ )
+
+ org_table = LiteLLM_OrganizationTable(
+ organization_id="test-org-update-2",
+ organization_alias="test-org",
+ budget_id="budget-456",
+ models=["gpt-4"],
+ created_by="admin",
+ updated_by="admin",
+ litellm_budget_table=LiteLLM_BudgetTable(
+ budget_id="budget-456",
+ tpm_limit=20000,
+ rpm_limit=2000,
+ ),
+ )
+
+ data = UpdateKeyRequest(
+ key="sk-test-key",
+ tpm_limit=10000, # 15000 + 10000 = 25000 > 20000
+ tpm_limit_type="guaranteed_throughput",
+ rpm_limit_type="guaranteed_throughput",
+ organization_id="test-org-update-2",
+ )
+
+ with pytest.raises(HTTPException) as exc:
+ await _check_org_key_limits(
+ org_table=org_table,
+ data=data,
+ prisma_client=mock_prisma_client,
+ )
+ assert exc.value.status_code == 400
+ assert "TPM limit" in str(exc.value.detail)
+
+
+def test_update_key_request_has_organization_id():
+ """
+ Test that UpdateKeyRequest accepts organization_id field.
+ """
+ data = UpdateKeyRequest(
+ key="sk-test-key",
+ organization_id="test-org-123",
+ )
+ assert data.organization_id == "test-org-123"
+
+ # Also verify it defaults to None
+ data_no_org = UpdateKeyRequest(key="sk-test-key")
+ assert data_no_org.organization_id is None