-
+
+
{row.original.custom_llm_provider}
),
From 8b6d4e047fb44f4894a0f3b1aec571b19a7915a4 Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Thu, 29 Oct 2026 07:24:39 -0700
Subject: [PATCH 6/8] fix(usage): resolve named-credential ChatGPT accounts and
keep admin viewers read-only on the fee card
---
litellm/llms/subscription_accounts.py | 6 ++--
litellm/proxy/_types.py | 1 -
.../common_daily_activity.py | 11 +++----
tests/unit/llms/test_subscription_accounts.py | 21 ++++++++++++
.../components/UsagePageView.test.tsx | 32 +++++++++++++++++--
.../_components/components/UsagePageView.tsx | 6 ++--
6 files changed, 63 insertions(+), 14 deletions(-)
diff --git a/litellm/llms/subscription_accounts.py b/litellm/llms/subscription_accounts.py
index 0d3e5f2737c..9364f6243ac 100644
--- a/litellm/llms/subscription_accounts.py
+++ b/litellm/llms/subscription_accounts.py
@@ -8,13 +8,15 @@ from types import MappingProxyType
from typing import Final
from litellm.llms.chatgpt.authenticator import Authenticator, get_cached_authenticator, get_chatgpt_auth_file
+from litellm.utils import with_named_credential
def _chatgpt_account_id(litellm_params: Mapping[str, object]) -> str | None:
- auth_file: Final = get_chatgpt_auth_file(litellm_params)
+ params: Final = with_named_credential(litellm_params)
+ auth_file: Final = get_chatgpt_auth_file(params)
if auth_file:
return get_cached_authenticator(auth_file).read_account_id()
- api_key: Final = litellm_params.get("api_key")
+ api_key: Final = params.get("api_key")
return Authenticator().read_account_id(api_key if isinstance(api_key, str) and api_key else None)
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index ce81f457bc5..f44eab46888 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -667,7 +667,6 @@ class LiteLLMRoutes(enum.Enum):
"/team/spend/by_user",
# gateway request counts (SGR); deployment-wide, admin-only
"/gateway/daily/activity",
- # subscription account fees; deployment-wide, admin-only
"/subscription_accounts/usage",
"/subscription_accounts/new",
"/subscription_accounts/update",
diff --git a/litellm/proxy/management_endpoints/common_daily_activity.py b/litellm/proxy/management_endpoints/common_daily_activity.py
index 5bf9190b337..8dca6ab8c84 100644
--- a/litellm/proxy/management_endpoints/common_daily_activity.py
+++ b/litellm/proxy/management_endpoints/common_daily_activity.py
@@ -773,12 +773,11 @@ def _aggregate_grouping_sets_records_sync(
metadata: Mapping[str, object] | None = None,
) -> None:
existing: Final = target.get(key)
- if existing is None:
- target[key] = MetricWithMetadata(metrics=metrics, metadata=dict(metadata or {}))
- return
- existing.metrics = metrics
- if metadata:
- existing.metadata = {**existing.metadata, **metadata}
+ target[key] = (
+ MetricWithMetadata(metrics=metrics, metadata=dict(metadata or {}))
+ if existing is None
+ else existing.model_copy(update={"metrics": metrics, "metadata": {**existing.metadata, **(metadata or {})}})
+ )
def assign_api_key_breakdown(
target: dict[str, MetricWithMetadata],
diff --git a/tests/unit/llms/test_subscription_accounts.py b/tests/unit/llms/test_subscription_accounts.py
index 2dd8db7055c..fa8f83c4f19 100644
--- a/tests/unit/llms/test_subscription_accounts.py
+++ b/tests/unit/llms/test_subscription_accounts.py
@@ -5,9 +5,11 @@ from typing import Final
import pytest
+import litellm
from litellm.constants import SUBSCRIPTION_BACKED_PROVIDERS
from litellm.llms.chatgpt.common_utils import OPENAI_AUTH_CLAIM_KEY
from litellm.llms.subscription_accounts import SUBSCRIPTION_ACCOUNT_ID_RESOLVERS, resolve_subscription_account_id
+from litellm.types.utils import CredentialItem
def _write_login(path: Path, account_id: str) -> Path:
@@ -73,3 +75,22 @@ def test_resolving_an_account_leaves_the_login_file_untouched(signed_in_chatgpt_
assert resolve_subscription_account_id("chatgpt", {"chatgpt_auth_file": str(login)}) == "acct-derived"
assert login.read_text() == saved
+
+
+@pytest.mark.parametrize("credential_field", ["chatgpt_auth_file", "api_key"])
+def test_a_named_credential_resolves_to_the_account_it_signs_in_as(
+ credential_field: str, signed_in_chatgpt_token_dir: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ login: Final = _write_login(signed_in_chatgpt_token_dir / "named" / "auth.json", "acct-named-credential")
+ values: Final = {
+ "chatgpt_auth_file": {"chatgpt_auth_file": str(login)},
+ "api_key": {"api_key": _access_token_for("acct-named-credential")},
+ }[credential_field]
+ monkeypatch.setattr(
+ litellm,
+ "credential_list",
+ [CredentialItem(credential_name="chatgpt-team", credential_values=values, credential_info={})],
+ )
+ litellm_params: Final = {"model": "chatgpt/gpt-5.6-terra", "litellm_credential_name": "chatgpt-team"}
+
+ assert resolve_subscription_account_id("chatgpt", litellm_params) == "acct-named-credential"
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx
index 860b1df143e..9d39e9c080c 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx
@@ -148,8 +148,8 @@ vi.mock("./EntityUsage/SpendByProvider", () => ({
}));
vi.mock("./SubscriptionCost/SubscriptionCostCard", () => ({
- default: ({ usage }: { usage: unknown }) => (
-
+ default: ({ usage, canEdit }: { usage: unknown; canEdit: boolean }) => (
+
Subscription Cost
),
@@ -636,6 +636,34 @@ describe("UsagePage", () => {
}
});
+ it("lets a proxy admin edit subscription fees but shows an admin viewer the same card read-only", async () => {
+ const adminRender = renderWithProviders(
);
+ await waitFor(() => {
+ expect(screen.getByTestId("subscription-cost-card")).toHaveAttribute(
+ "data-usage",
+ JSON.stringify(mockSubscriptionUsage),
+ );
+ });
+ expect(screen.getByTestId("subscription-cost-card")).toHaveAttribute("data-can-edit", "true");
+ adminRender.unmount();
+
+ const adminViewerSession = {
+ ...nonAdminSession,
+ userRole: "Admin",
+ userRoleLabel: "Admin Viewer",
+ isViewOnly: true,
+ };
+ mockUseAuthorized.mockReturnValue(adminViewerSession);
+ renderWithProviders(
);
+ await waitFor(() => {
+ expect(screen.getByTestId("subscription-cost-card")).toHaveAttribute(
+ "data-usage",
+ JSON.stringify(mockSubscriptionUsage),
+ );
+ });
+ expect(screen.getByTestId("subscription-cost-card")).toHaveAttribute("data-can-edit", "false");
+ });
+
it("never sets an account-wide subscription fee beside one user's spend when an admin filters by user", async () => {
renderWithProviders(
, { searchParams: "?user=user-001" });
await waitFor(() => {
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.tsx
index 12144a873c7..7c891754195 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.tsx
@@ -113,7 +113,7 @@ interface UsagePageProps {
}
const UsagePage: React.FC
= ({ teams, organizations, teamsLoading = false, defaultTimeRange }) => {
- const { accessToken, userRole, userId: userID, premiumUser, isLoading: authorizationLoading } = useAuthorized();
+ const { accessToken, userRole, userId: userID, premiumUser, isViewOnly, isLoading: authLoading } = useAuthorized();
const [gatewayActivityData, setGatewayActivityData] = useState(null);
const defaultRange = useMemo(() => defaultUsageDateRange(defaultTimeRange), [defaultTimeRange]);
@@ -188,7 +188,7 @@ const UsagePage: React.FC = ({ teams, organizations, teamsLoadin
};
const { isLoading: organizationsLoading } = useOrganizations();
- const authorizationSettled = !authorizationLoading && userRole !== null;
+ const authorizationSettled = !authLoading && userRole !== null;
const accessSettled = authorizationSettled && !organizationsLoading && !teamsLoading;
useEffect(() => {
if (!accessSettled) return;
@@ -551,7 +551,7 @@ const UsagePage: React.FC = ({ teams, organizations, teamsLoadin
endTime={endTime}
enabled={isAdmin && effectiveUserId === null}
meteredSpend={totals.spend}
- canEdit={isProxyAdminRole(userRole || "")}
+ canEdit={!isViewOnly && isProxyAdminRole(userRole || "")}
/>
}
topKeys={
From 32fe58ad00c8cac3576c2ea5da3a05072932b669 Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Thu, 29 Oct 2026 18:18:56 -0700
Subject: [PATCH 7/8] fix(usage): count a subscription fee only for accounts
behind a current deployment
Usage listed a fee row for an account no deployment uses while another ChatGPT
deployment existed and dropped it when none did. It now lists only accounts behind a
current deployment in both cases. Adds a real proxy and Postgres integration test for
the fee API and for ChatGPT Responses rows on a deployment's own login, and names the
card's multi-component test with the integration suffix.
---
.../subscription_accounts.py | 2 +-
.../spend/test_chatgpt_subscription_usage.py | 302 ++++++++++++++++++
.../test_subscription_accounts.py | 11 +-
...=> SubscriptionCosts.integration.test.tsx} | 0
4 files changed, 308 insertions(+), 7 deletions(-)
create mode 100644 tests/integration/spend/test_chatgpt_subscription_usage.py
rename ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/SubscriptionCost/{SubscriptionCosts.test.tsx => SubscriptionCosts.integration.test.tsx} (100%)
diff --git a/litellm/proxy/management_endpoints/subscription_accounts.py b/litellm/proxy/management_endpoints/subscription_accounts.py
index 566fd495295..4cd282ae70f 100644
--- a/litellm/proxy/management_endpoints/subscription_accounts.py
+++ b/litellm/proxy/management_endpoints/subscription_accounts.py
@@ -169,7 +169,7 @@ def build_subscription_usage(
deployment_tuple: Final = tuple(deployments)
fees_by_account: Final = {(row.custom_llm_provider, row.account_id): row for row in fee_rows}
deployment_keys: Final = {(d.custom_llm_provider, d.account_id) for d in deployment_tuple}
- account_keys: Final = sorted(deployment_keys | set(fees_by_account), key=lambda key: (key[0], key[1] or "~"))
+ account_keys: Final = sorted(deployment_keys, key=lambda key: (key[0], key[1] or "~"))
accounts: Final = tuple(
_account_usage(
provider,
diff --git a/tests/integration/spend/test_chatgpt_subscription_usage.py b/tests/integration/spend/test_chatgpt_subscription_usage.py
new file mode 100644
index 00000000000..28018d93b9e
--- /dev/null
+++ b/tests/integration/spend/test_chatgpt_subscription_usage.py
@@ -0,0 +1,302 @@
+import uuid
+from collections.abc import Iterator, Mapping
+from dataclasses import dataclass
+from datetime import UTC, datetime
+from typing import Final
+
+import httpx
+import pytest
+from integration._support import codex_vendor as cv
+from integration._support import responses_vendor as rv
+from integration._support.client import Gateway, Scenario, eventually, gateway_from_environment, string_value
+from integration._support.process import OwnedProxy, owned_proxy_process
+from integration._support.wire import Wire, wire_server
+from pydantic import BaseModel, ConfigDict, JsonValue, TypeAdapter
+
+_MODEL: Final = "chatgpt/gpt-5.5"
+_PROVIDER: Final = "chatgpt"
+_FEE_TABLE_QUERY: Final = (
+ 'SELECT subscription_account_id FROM "LiteLLM_SubscriptionAccountTable" WHERE custom_llm_provider = %s'
+ " AND account_id = %s"
+)
+_FEE_ROW_QUERY: Final = (
+ 'SELECT subscription_account_id FROM "LiteLLM_SubscriptionAccountTable" WHERE subscription_account_id = %s'
+)
+_MONTHLY_FEE: Final = 200.0
+_BILLING_PERIOD_START: Final = "2026-01-15"
+_RANGE: Final = {"start_date": "2026-03-01", "end_date": "2026-03-31"}
+_PERIOD_IN_RANGE: Final = ("2026-03-15", "2026-04-14")
+_TOKENS: Final = TypeAdapter(int)
+
+
+class _Metrics(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ spend: float
+ prompt_tokens: int
+ completion_tokens: int
+ api_requests: int
+ successful_requests: int
+ failed_requests: int
+
+
+class _ProviderUsage(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ metrics: _Metrics
+ metadata: Mapping[str, JsonValue]
+
+
+class _Breakdown(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ providers: Mapping[str, _ProviderUsage]
+
+
+class _Day(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ breakdown: _Breakdown
+
+
+class _DailyActivity(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ results: tuple[_Day, ...]
+
+
+class _Fee(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ subscription_account_id: str
+ custom_llm_provider: str
+ account_id: str
+ monthly_fee: float
+ currency: str
+ billing_period_start: str
+
+
+class _Period(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ start: str
+ end: str
+
+
+class _Account(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ custom_llm_provider: str
+ account_id: str | None
+ deployments: tuple[str, ...]
+ fee: _Fee | None
+ billing_periods: tuple[_Period, ...]
+ fixed_cost: float | None
+
+
+class _SubscriptionUsage(BaseModel):
+ model_config = ConfigDict(frozen=True)
+
+ accounts: tuple[_Account, ...]
+
+
+@dataclass(frozen=True, slots=True)
+class _Rig:
+ wire: Wire
+ proxy: OwnedProxy
+ model: str
+
+ @property
+ def gateway(self) -> Gateway:
+ return self.proxy.gateway
+
+
+@pytest.fixture(scope="module")
+def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]:
+ directory: Final = tmp_path_factory.mktemp("chatgpt-subscription-usage")
+ no_default_login: Final = directory / "no-default-login"
+ no_default_login.mkdir()
+ deployment_login: Final = cv.login(directory) / "auth.json"
+ with gateway_from_environment() as gateway, wire_server(cv.CodexVendor().respond) as wire:
+ overrides: Final = {"CHATGPT_TOKEN_DIR": str(no_default_login), "CHATGPT_API_BASE": wire.url}
+ config: Final = cv.proxy_config(directory, probe=False)
+ with (
+ owned_proxy_process(gateway, directory, overrides, config=config, workers=2) as owned,
+ owned.gateway.scenario() as scenario,
+ ):
+ model: Final = scenario.model(
+ model=_MODEL, api_base=wire.url, api_key=None, chatgpt_auth_file=str(deployment_login)
+ )
+ yield _Rig(wire, owned, model)
+
+
+def _responses_body(model: str, marker: str, *, stream: bool) -> Mapping[str, JsonValue]:
+ return {
+ "model": model,
+ "input": [{"role": "user", "content": f"Say marker-{marker}"}],
+ "stream": stream,
+ "cache": {"no-cache": True},
+ }
+
+
+def _assert_answered(response: httpx.Response, marker: str, content_type: str) -> None:
+ assert response.status_code == 200, response.text
+ assert response.headers["content-type"].startswith(content_type), response.headers
+ assert rv.answer(marker) in response.text, response.text
+
+
+def _chatgpt_usage_on(gateway: Gateway, day: str) -> _ProviderUsage | None:
+ response: Final = gateway.request("GET", "/user/daily/activity", params={"start_date": day, "end_date": day})
+ assert response.status_code == 200, response.text
+ activity: Final = _DailyActivity.model_validate_json(response.content)
+ buckets: Final = tuple(
+ result.breakdown.providers[_PROVIDER] for result in activity.results if _PROVIDER in result.breakdown.providers
+ )
+ assert len(buckets) <= 1, response.text
+ return buckets[0] if buckets else None
+
+
+def _usage(gateway: Gateway, *, key: str | None = None) -> httpx.Response:
+ return gateway.request("GET", "/subscription_accounts/usage", params=_RANGE, key=key)
+
+
+def _accounts(gateway: Gateway) -> tuple[_Account, ...]:
+ response: Final = _usage(gateway)
+ assert response.status_code == 200, response.text
+ return _SubscriptionUsage.model_validate_json(response.content).accounts
+
+
+def _deployment_account(rig: _Rig) -> _Account:
+ (account,) = _accounts(rig.gateway)
+ assert (account.custom_llm_provider, account.account_id, account.deployments) == (
+ _PROVIDER,
+ cv.ACCOUNT,
+ (rig.model,),
+ ), account
+ return account
+
+
+def _assert_no_fee(account: _Account) -> None:
+ assert (account.fee, account.billing_periods, account.fixed_cost) == (None, (), None), account
+
+
+def _fee_body(account_id: str, monthly_fee: float = _MONTHLY_FEE) -> Mapping[str, JsonValue]:
+ return {
+ "custom_llm_provider": _PROVIDER,
+ "account_id": account_id,
+ "monthly_fee": monthly_fee,
+ "currency": "USD",
+ "billing_period_start": _BILLING_PERIOD_START,
+ "label": "integration",
+ }
+
+
+def _delete_fee_if_present(gateway: Gateway, identity: str) -> None:
+ if gateway.rows(_FEE_ROW_QUERY, (identity,)):
+ gateway.post("/subscription_accounts/delete", {"subscription_account_id": identity})
+ assert gateway.rows(_FEE_ROW_QUERY, (identity,)) == []
+
+
+def _set_fee(scenario: Scenario, account_id: str) -> str:
+ created: Final = scenario.gateway.post("/subscription_accounts/new", _fee_body(account_id))
+ identity: Final = string_value(created["subscription_account_id"])
+ scenario.cleanups.callback(_delete_fee_if_present, scenario.gateway, identity)
+ return identity
+
+
+def _assert_fee_reported(account: _Account, identity: str) -> None:
+ assert account.fee is not None, account
+ assert (
+ account.fee.subscription_account_id,
+ account.fee.account_id,
+ account.fee.monthly_fee,
+ account.fee.currency,
+ account.fee.billing_period_start,
+ ) == (identity, cv.ACCOUNT, _MONTHLY_FEE, "USD", _BILLING_PERIOD_START), account
+ assert tuple((period.start, period.end) for period in account.billing_periods) == (_PERIOD_IN_RANGE,), account
+ assert account.fixed_cost == _MONTHLY_FEE, account
+
+
+def test_responses_calls_on_a_deployment_login_count_as_subscription_covered_chatgpt_usage(rig: _Rig) -> None:
+ day: Final = datetime.now(UTC).date().isoformat()
+ rig.wire.drain()
+ plain_marker: Final = uuid.uuid4().hex
+ stream_marker: Final = uuid.uuid4().hex
+ plain: Final = rig.gateway.request("POST", "/v1/responses", _responses_body(rig.model, plain_marker, stream=False))
+ _assert_answered(plain, plain_marker, "application/json")
+ streamed: Final = rig.gateway.request(
+ "POST", "/v1/responses", _responses_body(rig.model, stream_marker, stream=True)
+ )
+ _assert_answered(streamed, stream_marker, "text/event-stream")
+ forwarded: Final = rig.wire.drain()
+ assert len(forwarded) == 2, [request.target for request in forwarded]
+ for request, marker in zip(forwarded, (plain_marker, stream_marker), strict=True):
+ cv.forwarded(request, marker)
+ usage: Final = eventually(
+ lambda: _chatgpt_usage_on(rig.gateway, day),
+ lambda found: found is not None and found.metrics.api_requests >= 2,
+ seconds=70,
+ )
+ assert usage is not None
+ assert (
+ usage.metrics.api_requests,
+ usage.metrics.successful_requests,
+ usage.metrics.failed_requests,
+ usage.metrics.spend,
+ ) == (2, 2, 0, 0), usage
+ assert (usage.metrics.prompt_tokens, usage.metrics.completion_tokens) == (
+ 2 * _TOKENS.validate_python(cv.USAGE["input_tokens"]),
+ 2 * _TOKENS.validate_python(cv.USAGE["output_tokens"]),
+ ), usage
+ assert usage.metadata.get("subscription_covered") is True, usage
+
+
+def test_fee_on_the_deployment_account_reports_its_fixed_cost_until_deleted(rig: _Rig) -> None:
+ _assert_no_fee(_deployment_account(rig))
+ with rig.gateway.scenario() as scenario:
+ identity: Final = _set_fee(scenario, cv.ACCOUNT)
+ _assert_fee_reported(_deployment_account(rig), identity)
+ deleted: Final = rig.gateway.post("/subscription_accounts/delete", {"subscription_account_id": identity})
+ assert deleted["subscription_account_id"] == identity, deleted
+ assert rig.gateway.rows(_FEE_ROW_QUERY, (identity,)) == []
+ _assert_no_fee(_deployment_account(rig))
+
+
+def test_second_fee_for_the_same_account_answers_400_and_keeps_the_first(rig: _Rig) -> None:
+ with rig.gateway.scenario() as scenario:
+ identity: Final = _set_fee(scenario, cv.ACCOUNT)
+ duplicate: Final = rig.gateway.request(
+ "POST", "/subscription_accounts/new", _fee_body(cv.ACCOUNT, monthly_fee=3 * _MONTHLY_FEE)
+ )
+ assert duplicate.status_code == 400, duplicate.text
+ assert rig.gateway.rows(_FEE_TABLE_QUERY, (_PROVIDER, cv.ACCOUNT)) == [{"subscription_account_id": identity}]
+ _assert_fee_reported(_deployment_account(rig), identity)
+
+
+def test_update_and_delete_of_an_unknown_fee_answer_404(rig: _Rig) -> None:
+ unknown: Final = str(uuid.uuid4())
+ updated: Final = rig.gateway.request(
+ "POST", "/subscription_accounts/update", {"subscription_account_id": unknown, "monthly_fee": _MONTHLY_FEE}
+ )
+ assert updated.status_code == 404, updated.text
+ deleted: Final = rig.gateway.request("POST", "/subscription_accounts/delete", {"subscription_account_id": unknown})
+ assert deleted.status_code == 404, deleted.text
+ assert rig.gateway.rows(_FEE_ROW_QUERY, (unknown,)) == []
+
+
+def test_internal_user_is_refused_setting_a_fee_and_reading_usage(rig: _Rig) -> None:
+ with rig.gateway.scenario() as scenario:
+ key: Final = scenario.key(user_id=scenario.user(user_role="internal_user"))
+ created: Final = rig.gateway.request("POST", "/subscription_accounts/new", _fee_body(cv.ACCOUNT), key=key)
+ assert created.status_code == 401, created.text
+ assert rig.gateway.rows(_FEE_TABLE_QUERY, (_PROVIDER, cv.ACCOUNT)) == []
+ usage: Final = _usage(rig.gateway, key=key)
+ assert usage.status_code == 401, usage.text
+
+
+def test_fee_on_an_account_no_deployment_uses_is_neither_listed_nor_counted(rig: _Rig) -> None:
+ unused: Final = f"acct-unused-{uuid.uuid4().hex}"
+ with rig.gateway.scenario() as scenario:
+ identity: Final = _set_fee(scenario, unused)
+ assert rig.gateway.rows(_FEE_TABLE_QUERY, (_PROVIDER, unused)) == [{"subscription_account_id": identity}]
+ _assert_no_fee(_deployment_account(rig))
diff --git a/tests/unit/proxy/management_endpoints/test_subscription_accounts.py b/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
index 0256c74028b..c50066fc803 100644
--- a/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
+++ b/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
@@ -174,18 +174,17 @@ def test_fee_counts_once_per_billing_period_however_many_deployments_share_the_a
assert between_starts["accounts"][0]["billing_periods"] == []
-def test_fee_without_a_matching_deployment_is_still_listed(client):
+def test_fee_of_an_account_no_deployment_uses_is_neither_listed_nor_counted(client):
test_client, _ = client
other = {**_TEST_FEE, "account_id": "org-other", "monthly_fee": 200.0, "currency": "EUR"}
assert test_client.post("/subscription_accounts/new", json=other).status_code == 200
+ assert test_client.post("/subscription_accounts/new", json=_TEST_FEE).status_code == 200
body = _usage(test_client, "2026-10-01", "2026-10-07")
- assert [account["account_id"] for account in body["accounts"]] == ["org-other", _ACCOUNT]
- assert body["accounts"][0]["deployments"] == []
- assert body["accounts"][0]["fee"]["currency"] == "EUR"
- assert body["accounts"][0]["fixed_cost"] == 200.0
- assert body["accounts"][1]["fee"] is None
+ (account,) = body["accounts"]
+ assert account["account_id"] == _ACCOUNT
+ assert account["fixed_cost"] == 25.0
@pytest.mark.parametrize(
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/SubscriptionCost/SubscriptionCosts.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/SubscriptionCost/SubscriptionCosts.integration.test.tsx
similarity index 100%
rename from ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/SubscriptionCost/SubscriptionCosts.test.tsx
rename to ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/SubscriptionCost/SubscriptionCosts.integration.test.tsx
From d39df0d4ac70c675657030c62bfcf1c4ee2f8be5 Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Thu, 29 Oct 2026 19:58:09 -0700
Subject: [PATCH 8/8] fix(usage): keep a stored subscription fee listed after
its account's deployments are removed
The usage route lists every account behind a subscription deployment plus every account carrying a stored fee, and reads the fees on every proxy, so a fee stays visible, counted, and editable for past ranges after its last deployment goes. The integration test now polls every day the calls spanned, so a UTC midnight crossing no longer fails it.
---
.../subscription_accounts.py | 8 +--
.../spend/test_chatgpt_subscription_usage.py | 64 ++++++++++++-------
.../test_subscription_accounts.py | 30 ++++-----
ui/litellm-dashboard/src/lib/http/schema.d.ts | 4 +-
4 files changed, 59 insertions(+), 47 deletions(-)
diff --git a/litellm/proxy/management_endpoints/subscription_accounts.py b/litellm/proxy/management_endpoints/subscription_accounts.py
index 4cd282ae70f..363d6cfba5d 100644
--- a/litellm/proxy/management_endpoints/subscription_accounts.py
+++ b/litellm/proxy/management_endpoints/subscription_accounts.py
@@ -169,7 +169,7 @@ def build_subscription_usage(
deployment_tuple: Final = tuple(deployments)
fees_by_account: Final = {(row.custom_llm_provider, row.account_id): row for row in fee_rows}
deployment_keys: Final = {(d.custom_llm_provider, d.account_id) for d in deployment_tuple}
- account_keys: Final = sorted(deployment_keys, key=lambda key: (key[0], key[1] or "~"))
+ account_keys: Final = sorted(deployment_keys | set(fees_by_account), key=lambda key: (key[0], key[1] or "~"))
accounts: Final = tuple(
_account_usage(
provider,
@@ -219,8 +219,8 @@ async def get_subscription_usage(
deployments: Deployments,
) -> SubscriptionUsageResponse:
"""
- Subscription-backed accounts behind the proxy's deployments, each with its configured
- monthly fee and the fixed cost attributed to the date range: the fee once per billing
+ Subscription-backed accounts behind the proxy's deployments or carrying a stored fee, each with
+ its configured monthly fee and the fixed cost attributed to the date range: the fee once per billing
period whose start date falls inside the range, however many deployments share the account.
"""
if not user_api_key_has_admin_view(user_api_key_dict):
@@ -231,8 +231,6 @@ async def get_subscription_usage(
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail={"error": "end_date must not be before start_date"}
)
- if not deployments:
- return build_subscription_usage((), (), start, end)
return build_subscription_usage(deployments, await repository.list_all(), start, end)
diff --git a/tests/integration/spend/test_chatgpt_subscription_usage.py b/tests/integration/spend/test_chatgpt_subscription_usage.py
index 28018d93b9e..8f1fb2d0f74 100644
--- a/tests/integration/spend/test_chatgpt_subscription_usage.py
+++ b/tests/integration/spend/test_chatgpt_subscription_usage.py
@@ -145,15 +145,30 @@ def _assert_answered(response: httpx.Response, marker: str, content_type: str) -
assert rv.answer(marker) in response.text, response.text
-def _chatgpt_usage_on(gateway: Gateway, day: str) -> _ProviderUsage | None:
- response: Final = gateway.request("GET", "/user/daily/activity", params={"start_date": day, "end_date": day})
+def _today() -> str:
+ return datetime.now(UTC).date().isoformat()
+
+
+def _chatgpt_usage_between(gateway: Gateway, first_day: str, last_day: str) -> tuple[_ProviderUsage, ...]:
+ response: Final = gateway.request(
+ "GET", "/user/daily/activity", params={"start_date": first_day, "end_date": last_day}
+ )
assert response.status_code == 200, response.text
activity: Final = _DailyActivity.model_validate_json(response.content)
- buckets: Final = tuple(
+ return tuple(
result.breakdown.providers[_PROVIDER] for result in activity.results if _PROVIDER in result.breakdown.providers
)
- assert len(buckets) <= 1, response.text
- return buckets[0] if buckets else None
+
+
+def _summed(buckets: tuple[_ProviderUsage, ...]) -> _Metrics:
+ return _Metrics(
+ spend=sum(bucket.metrics.spend for bucket in buckets),
+ prompt_tokens=sum(bucket.metrics.prompt_tokens for bucket in buckets),
+ completion_tokens=sum(bucket.metrics.completion_tokens for bucket in buckets),
+ api_requests=sum(bucket.metrics.api_requests for bucket in buckets),
+ successful_requests=sum(bucket.metrics.successful_requests for bucket in buckets),
+ failed_requests=sum(bucket.metrics.failed_requests for bucket in buckets),
+ )
def _usage(gateway: Gateway, *, key: str | None = None) -> httpx.Response:
@@ -218,7 +233,7 @@ def _assert_fee_reported(account: _Account, identity: str) -> None:
def test_responses_calls_on_a_deployment_login_count_as_subscription_covered_chatgpt_usage(rig: _Rig) -> None:
- day: Final = datetime.now(UTC).date().isoformat()
+ first_day: Final = _today()
rig.wire.drain()
plain_marker: Final = uuid.uuid4().hex
stream_marker: Final = uuid.uuid4().hex
@@ -232,23 +247,24 @@ def test_responses_calls_on_a_deployment_login_count_as_subscription_covered_cha
assert len(forwarded) == 2, [request.target for request in forwarded]
for request, marker in zip(forwarded, (plain_marker, stream_marker), strict=True):
cv.forwarded(request, marker)
- usage: Final = eventually(
- lambda: _chatgpt_usage_on(rig.gateway, day),
- lambda found: found is not None and found.metrics.api_requests >= 2,
+ last_day: Final = _today()
+ buckets: Final = eventually(
+ lambda: _chatgpt_usage_between(rig.gateway, first_day, last_day),
+ lambda found: _summed(found).api_requests >= 2,
seconds=70,
)
- assert usage is not None
- assert (
- usage.metrics.api_requests,
- usage.metrics.successful_requests,
- usage.metrics.failed_requests,
- usage.metrics.spend,
- ) == (2, 2, 0, 0), usage
- assert (usage.metrics.prompt_tokens, usage.metrics.completion_tokens) == (
+ totals: Final = _summed(buckets)
+ assert (totals.api_requests, totals.successful_requests, totals.failed_requests, totals.spend) == (
+ 2,
+ 2,
+ 0,
+ 0,
+ ), buckets
+ assert (totals.prompt_tokens, totals.completion_tokens) == (
2 * _TOKENS.validate_python(cv.USAGE["input_tokens"]),
2 * _TOKENS.validate_python(cv.USAGE["output_tokens"]),
- ), usage
- assert usage.metadata.get("subscription_covered") is True, usage
+ ), buckets
+ assert all(bucket.metadata.get("subscription_covered") is True for bucket in buckets), buckets
def test_fee_on_the_deployment_account_reports_its_fixed_cost_until_deleted(rig: _Rig) -> None:
@@ -294,9 +310,13 @@ def test_internal_user_is_refused_setting_a_fee_and_reading_usage(rig: _Rig) ->
assert usage.status_code == 401, usage.text
-def test_fee_on_an_account_no_deployment_uses_is_neither_listed_nor_counted(rig: _Rig) -> None:
+def test_fee_on_an_account_no_deployment_uses_stays_listed_and_counted(rig: _Rig) -> None:
unused: Final = f"acct-unused-{uuid.uuid4().hex}"
with rig.gateway.scenario() as scenario:
identity: Final = _set_fee(scenario, unused)
- assert rig.gateway.rows(_FEE_TABLE_QUERY, (_PROVIDER, unused)) == [{"subscription_account_id": identity}]
- _assert_no_fee(_deployment_account(rig))
+ accounts: Final = _accounts(rig.gateway)
+ assert frozenset(account.account_id for account in accounts) == {cv.ACCOUNT, unused}, accounts
+ (unused_account,) = (account for account in accounts if account.account_id == unused)
+ assert unused_account.fee is not None, unused_account
+ assert (unused_account.deployments, unused_account.fee.subscription_account_id) == ((), identity)
+ assert unused_account.fixed_cost == _MONTHLY_FEE, unused_account
diff --git a/tests/unit/proxy/management_endpoints/test_subscription_accounts.py b/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
index c50066fc803..bcd31efd4b7 100644
--- a/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
+++ b/tests/unit/proxy/management_endpoints/test_subscription_accounts.py
@@ -174,7 +174,7 @@ def test_fee_counts_once_per_billing_period_however_many_deployments_share_the_a
assert between_starts["accounts"][0]["billing_periods"] == []
-def test_fee_of_an_account_no_deployment_uses_is_neither_listed_nor_counted(client):
+def test_fee_of_an_account_no_deployment_uses_stays_listed_and_counted(client):
test_client, _ = client
other = {**_TEST_FEE, "account_id": "org-other", "monthly_fee": 200.0, "currency": "EUR"}
assert test_client.post("/subscription_accounts/new", json=other).status_code == 200
@@ -182,9 +182,10 @@ def test_fee_of_an_account_no_deployment_uses_is_neither_listed_nor_counted(clie
body = _usage(test_client, "2026-10-01", "2026-10-07")
- (account,) = body["accounts"]
- assert account["account_id"] == _ACCOUNT
- assert account["fixed_cost"] == 25.0
+ unused, used = body["accounts"]
+ assert (unused["account_id"], unused["deployments"], unused["fee"]["currency"]) == ("org-other", [], "EUR")
+ assert unused["fixed_cost"] == 200.0
+ assert (used["account_id"], used["fixed_cost"]) == (_ACCOUNT, 25.0)
@pytest.mark.parametrize(
@@ -286,22 +287,15 @@ def test_rejects_an_infinite_monthly_fee():
)
-class _MissingTableStore:
- async def list_all(self) -> list[LiteLLM_SubscriptionAccountTable]:
- raise RuntimeError('relation "LiteLLM_SubscriptionAccountTable" does not exist')
+@pytest.mark.parametrize("deployments", [()])
+def test_fee_stays_listed_after_the_last_subscription_deployment_is_removed(client):
+ test_client, _ = client
+ assert test_client.post("/subscription_accounts/new", json=_TEST_FEE).status_code == 200
+ body = _usage(test_client, "2026-10-01", "2026-10-07")
-def test_proxy_without_subscription_deployments_answers_without_reading_fees():
- app: Final = FastAPI()
- app.include_router(router)
- app.dependency_overrides[get_subscription_account_repository] = _MissingTableStore
- app.dependency_overrides[get_subscription_deployments] = lambda: ()
- app.dependency_overrides[user_api_key_auth] = _resolve_auth
-
- with TestClient(app) as test_client:
- body = _usage(test_client, "2026-10-01", "2026-10-07")
-
- assert body["accounts"] == []
+ (account,) = body["accounts"]
+ assert (account["account_id"], account["deployments"], account["fixed_cost"]) == (_ACCOUNT, [], 25.0)
def test_usage_rejects_a_malformed_or_inverted_range(client):
diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts
index 0dc29ecf16e..9c7837e8897 100644
--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts
+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts
@@ -16122,8 +16122,8 @@ export interface paths {
};
/**
* Get Subscription Usage
- * @description Subscription-backed accounts behind the proxy's deployments, each with its configured
- * monthly fee and the fixed cost attributed to the date range: the fee once per billing
+ * @description Subscription-backed accounts behind the proxy's deployments or carrying a stored fee, each with
+ * its configured monthly fee and the fixed cost attributed to the date range: the fee once per billing
* period whose start date falls inside the range, however many deployments share the account.
*/
get: operations["get_subscription_usage_subscription_accounts_usage_get"];