Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): reserve and settle billing quota through the generated client (#45133)
yyh906f4179e641
diff --git a/api/services/billing_service.py b/api/services/billing_service.pyindex 234b01f6b5d..660e0a40e03 100644--- a/api/services/billing_service.py+++ b/api/services/billing_service.py@@ -1,8 +1,8 @@import jsonimport loggingimport os-from collections.abc import Sequence-from typing import Any, Literal, NotRequired, TypedDict+from collections.abc import Mapping, Sequence+from typing import Any, Literal, TypedDictfrom warnings import deprecatedimport httpx@@ -64,68 +64,6 @@ class EducationAutocompleteResponseDict(TypedDict):_compliance_download_link_adapter = TypeAdapter(ComplianceDownloadLink)-class QuotaReserveResult(TypedDict):- reservation_id: str- available: int- reserved: int---class QuotaCommitResult(TypedDict):- available: int- reserved: int- refunded: int---class QuotaReleaseResult(TypedDict):- available: int- reserved: int- released: int---class QuotaBalanceResult(TypedDict):- available: int- reserved: int- quota: int- usage: int- exhausted_at: NotRequired[int]---class QuotaConsumeCappedResult(TypedDict):- deducted: int- available: int- reserved: int- quota: int- usage: int---_quota_reserve_adapter = TypeAdapter(QuotaReserveResult)-_quota_commit_adapter = TypeAdapter(QuotaCommitResult)-_quota_release_adapter = TypeAdapter(QuotaReleaseResult)-_quota_balance_adapter = TypeAdapter(QuotaBalanceResult)-_quota_consume_capped_adapter = TypeAdapter(QuotaConsumeCappedResult)---class _TenantFeatureQuota(TypedDict):- usage: int- limit: int- reset_date: NotRequired[int]---class TenantFeatureQuotaInfo(TypedDict):- """Response of /quota/info.-- NOTE (hj24):- - Billing may return int fields as str,- always keep non-strict mode to auto-coerce.- """-- trigger_event: _TenantFeatureQuota- api_rate_limit: _TenantFeatureQuota---_tenant_feature_quota_info_adapter = TypeAdapter(TenantFeatureQuotaInfo)--class LangContentDict(TypedDict):lang: strtitle: str@@ -151,9 +89,13 @@ class DismissNotificationDict(TypedDict):success: bool+def _quota_meta(meta: Mapping[str, object] | None) -> dict[str, str]:+ """The spec types quota metadata as a string map."""+ return {key: value if isinstance(value, str) else str(value) for key, value in (meta or {}).items()}++class BillingService:base_url = os.environ.get("BILLING_API_URL", "BILLING_API_URL")- quota_base_url = os.environ.get("BILLING_QUOTA_API_URL") or base_urlsecret_key = os.environ.get("BILLING_API_SECRET_KEY", "BILLING_API_SECRET_KEY")# Redis key prefix for tenant plan cache@@ -176,11 +118,8 @@ class BillingService:cls.get_vector_space(tenant_id, bypass_cache=True)@classmethod- def get_quota_info(cls, tenant_id: str) -> TenantFeatureQuotaInfo:- params = {"tenant_id": tenant_id}- return _tenant_feature_quota_info_adapter.validate_python(- cls._send_quota_request("GET", "/quota/info", params=params)- )+ def get_quota_info(cls, tenant_id: str) -> billing.QuotaGetInfoReply:+ return billing.quota_get_quota_info(tenant_id=tenant_id)@classmethoddef quota_reserve(@@ -189,21 +128,20 @@ class BillingService:feature_key: str,request_id: str,amount: int = 1,- meta: dict | None = None,+ meta: Mapping[str, object] | None = None,bucket: str = "",- ) -> QuotaReserveResult:+ ) -> billing.QuotaReserveReply:"""Reserve quota before task execution."""- payload: dict = {- "tenant_id": tenant_id,- "feature_key": feature_key,- "request_id": request_id,- "amount": amount,- }- if bucket:- payload["bucket"] = bucket- if meta:- payload["meta"] = meta- return _quota_reserve_adapter.validate_python(cls._send_quota_request("POST", "/quota/reserve", json=payload))+ return billing.quota_reserve(+ body=billing.QuotaReserveReq(+ tenant_id=tenant_id,+ feature_key=feature_key,+ request_id=request_id,+ amount=amount,+ meta=_quota_meta(meta),+ bucket=bucket,+ )+ )@classmethoddef quota_commit(@@ -212,43 +150,36 @@ class BillingService:feature_key: str,reservation_id: str,actual_amount: int,- meta: dict | None = None,+ meta: Mapping[str, object] | None = None,bucket: str = "",- ) -> QuotaCommitResult:+ ) -> billing.QuotaCommitReply:"""Commit a reservation with actual consumption."""- payload: dict = {- "tenant_id": tenant_id,- "feature_key": feature_key,- "reservation_id": reservation_id,- "actual_amount": actual_amount,- }- if bucket:- payload["bucket"] = bucket- if meta:- payload["meta"] = meta- return _quota_commit_adapter.validate_python(cls._send_quota_request("POST", "/quota/commit", json=payload))+ return billing.quota_commit(+ body=billing.QuotaCommitReq(+ tenant_id=tenant_id,+ feature_key=feature_key,+ reservation_id=reservation_id,+ actual_amount=actual_amount,+ meta=_quota_meta(meta),+ bucket=bucket,+ )+ )@classmethoddef quota_release(cls, tenant_id: str, feature_key: str, reservation_id: str, bucket: str = ""- ) -> QuotaReleaseResult:+ ) -> billing.QuotaReleaseReply:"""Release a reservation (cancel, return frozen quota)."""- payload = {- "tenant_id": tenant_id,- "feature_key": feature_key,- "reservation_id": reservation_id,- }- if bucket:- payload["bucket"] = bucket- return _quota_release_adapter.validate_python(cls._send_quota_request("POST", "/quota/release", json=payload))+ return billing.quota_release(+ body=billing.QuotaReleaseReq(+ tenant_id=tenant_id, feature_key=feature_key, reservation_id=reservation_id, bucket=bucket+ )+ )@classmethod- def quota_get_balance(cls, tenant_id: str, feature_key: str, bucket: str = "") -> QuotaBalanceResult:+ def quota_get_balance(cls, tenant_id: str, feature_key: str, bucket: str = "") -> billing.QuotaGetBalanceReply:"""Get quota balance for a feature bucket."""- params = {"tenant_id": tenant_id, "feature_key": feature_key}- if bucket:- params["bucket"] = bucket- return _quota_balance_adapter.validate_python(cls._send_quota_request("GET", "/quota/balance", params=params))+ return billing.quota_get_balance(tenant_id=tenant_id, feature_key=feature_key, bucket=bucket)@classmethoddef quota_consume_capped(@@ -257,22 +188,19 @@ class BillingService:feature_key: str,request_id: str,amount: int,- meta: dict | None = None,+ meta: Mapping[str, object] | None = None,bucket: str = "",- ) -> QuotaConsumeCappedResult:+ ) -> billing.QuotaConsumeCappedReply:"""Consume up to the available quota and return the actual deducted amount."""- payload: dict = {- "tenant_id": tenant_id,- "feature_key": feature_key,- "request_id": request_id,- "amount": amount,- }- if bucket:- payload["bucket"] = bucket- if meta:- payload["meta"] = meta- return _quota_consume_capped_adapter.validate_python(- cls._send_quota_request("POST", "/quota/consume-capped", json=payload)+ return billing.quota_consume_capped(+ body=billing.QuotaConsumeCappedReq(+ tenant_id=tenant_id,+ feature_key=feature_key,+ request_id=request_id,+ amount=amount,+ meta=_quota_meta(meta),+ bucket=bucket,+ ))@classmethod@@ -302,12 +230,6 @@ class BillingService:reply = billing.invoice_get_invoices(prefilled_email=prefilled_email, tenant_id=tenant_id)return {"url": reply.url}- @classmethod- def _send_quota_request(- cls, method: Literal["GET", "POST", "DELETE", "PUT"], endpoint: str, json=None, params=None- ) -> dict[str, Any]:- return cls._send_request(method, endpoint, json=json, params=params, base_url=cls.quota_base_url)-@classmethod@retry(wait=wait_fixed(2),diff --git a/api/services/credit_pool_service.py b/api/services/credit_pool_service.pyindex 8584b83886d..9dd08956787 100644--- a/api/services/credit_pool_service.py+++ b/api/services/credit_pool_service.py@@ -213,9 +213,9 @@ class CreditPoolService:return CreditPoolBalance(tenant_id=tenant_id,pool_type=normalized_pool_type,- quota_limit=balance["quota"],- quota_used=balance["usage"],- exhausted_at=balance.get("exhausted_at"),+ quota_limit=balance.quota,+ quota_used=balance.usage,+ exhausted_at=balance.exhausted_at,)session = cls._require_session(session)@@ -273,7 +273,7 @@ class CreditPoolService:amount=credits_required,meta=reservation_meta,)- reservation_id = result.get("reservation_id", "")+ reservation_id = result.reservation_idif not reservation_id:raise QuotaExceededError("Insufficient credits remaining")return CreditPoolReservation(@@ -376,7 +376,7 @@ class CreditPoolService:amount=credits_required,meta=billing_metadata,)- reservation_id = result.get("reservation_id", "")+ reservation_id = result.reservation_idif not reservation_id:raise QuotaExceededError("Insufficient credits remaining")try:@@ -460,7 +460,7 @@ class CreditPoolService:amount=credits_required,meta=billing_metadata,)- return result["deducted"]+ return result.deductedsession = cls._require_session(session)diff --git a/api/services/feature_service.py b/api/services/feature_service.pyindex 45a498337f5..b9473289a40 100644--- a/api/services/feature_service.py+++ b/api/services/feature_service.py@@ -169,15 +169,13 @@ class FeatureService:else:features.is_allow_transfer_workspace = False- if "trigger_event" in features_usage_info:- features.trigger_event.usage = features_usage_info["trigger_event"]["usage"]- features.trigger_event.limit = features_usage_info["trigger_event"]["limit"]- features.trigger_event.reset_date = features_usage_info["trigger_event"].get("reset_date", -1)-- if "api_rate_limit" in features_usage_info:- features.api_rate_limit.usage = features_usage_info["api_rate_limit"]["usage"]- features.api_rate_limit.limit = features_usage_info["api_rate_limit"]["limit"]- features.api_rate_limit.reset_date = features_usage_info["api_rate_limit"].get("reset_date", -1)+ features.trigger_event.usage = features_usage_info.trigger_event.usage+ features.trigger_event.limit = features_usage_info.trigger_event.limit+ features.trigger_event.reset_date = features_usage_info.trigger_event.reset_date++ features.api_rate_limit.usage = features_usage_info.api_rate_limit.usage+ features.api_rate_limit.limit = features_usage_info.api_rate_limit.limit+ features.api_rate_limit.reset_date = features_usage_info.api_rate_limit.reset_datefeatures.members.size = billing_info.members.sizefeatures.members.limit = billing_info.members.limitdiff --git a/api/services/quota_service.py b/api/services/quota_service.pyindex 1a17c1cb41d..8ab01a812ba 100644--- a/api/services/quota_service.py+++ b/api/services/quota_service.py@@ -4,8 +4,11 @@ import loggingimport uuidfrom dataclasses import dataclass, field+import httpx+from configs import dify_configfrom enums import DeploymentEdition, QuotaType+from services.errors.billing import BillingErrorlogger = logging.getLogger(__name__)@@ -138,7 +141,7 @@ class QuotaService:amount=amount,)- reservation_id = reserve_resp.get("reservation_id")+ reservation_id = reserve_resp.reservation_idif not reservation_id:logger.warning("Reserve returned no reservation_id for %s, feature %s, response: %s",@@ -168,6 +171,12 @@ class QuotaService:raiseexcept ValueError:raise+ except BillingError as error:+ # Only a request that never reached billing fails open; an error answer or invalid body surfaces.+ if not isinstance(error.__cause__, httpx.RequestError):+ raise+ logger.exception("Failed to reserve quota for %s, feature %s", tenant_id, quota_type.value)+ return unlimited()except Exception:logger.exception("Failed to reserve quota for %s, feature %s", tenant_id, quota_type.value)return unlimited()@@ -214,15 +223,11 @@ class QuotaService:try:usage_info = BillingService.get_quota_info(tenant_id)- if isinstance(usage_info, dict):- feature_info = usage_info.get(quota_type.billing_key, {})- if isinstance(feature_info, dict):- limit = feature_info.get("limit", 0)- usage = feature_info.get("usage", 0)- if limit == -1:- return -1- return max(0, limit - usage)- return 0+ features = {"trigger_event": usage_info.trigger_event, "api_rate_limit": usage_info.api_rate_limit}+ feature_info = features[quota_type.billing_key]+ if feature_info.limit == -1:+ return -1+ return max(0, feature_info.limit - feature_info.usage)except Exception:logger.exception("Failed to get remaining quota for %s, feature %s", tenant_id, quota_type.value)return -1diff --git a/api/tests/e2e_tests/containers/services/test_app_generate_service.py b/api/tests/e2e_tests/containers/services/test_app_generate_service.pyindex 585be482a38..9fdc4b6129f 100644--- a/api/tests/e2e_tests/containers/services/test_app_generate_service.py+++ b/api/tests/e2e_tests/containers/services/test_app_generate_service.py@@ -16,6 +16,7 @@ from models.model import EndUserfrom models.workflow import Workflowfrom models.workflow import WorkflowType as ModelWorkflowTypefrom services.app_generate_service import AppGenerateService+from services.billing_client import billingfrom services.errors.app import WorkflowIdFormatError, WorkflowNotFoundErrorfrom tests.e2e_tests.containers.helpers import generate_valid_password@@ -48,16 +49,12 @@ class TestAppGenerateService:patch("configs.dify_config") as mock_global_dify_config,):# Setup default mock returns for billing service- mock_billing_service.quota_reserve.return_value = {- "reservation_id": "test-reservation-id",- "available": 100,- "reserved": 1,- }- mock_billing_service.quota_commit.return_value = {- "available": 99,- "reserved": 0,- "refunded": 0,- }+ mock_billing_service.quota_reserve.return_value = billing.QuotaReserveReply(+ reservation_id="test-reservation-id", available=100, reserved=1+ )+ mock_billing_service.quota_commit.return_value = billing.QuotaCommitReply(+ available=99, reserved=0, refunded=0+ )# Setup default mock returns for workflow servicemock_workflow_service_instance = mock_workflow_service.return_valuediff --git a/api/tests/unit_tests/core/plugin/backwards_invocation/test_model.py b/api/tests/unit_tests/core/plugin/backwards_invocation/test_model.pyindex d675d9745ca..0bd803a2f32 100644--- a/api/tests/unit_tests/core/plugin/backwards_invocation/test_model.py+++ b/api/tests/unit_tests/core/plugin/backwards_invocation/test_model.py@@ -3,6 +3,7 @@ from collections.abc import Callable, Iteratorfrom dataclasses import dataclass, fieldfrom unittest.mock import patch+import httpximport pytestfrom pydantic import BaseModel@@ -50,7 +51,7 @@ from graphon.model_runtime.model_providers.base.tts_model import TTSModelfrom graphon.model_runtime.protocols.tts_runtime import TTSChunkfrom models.account import Tenantfrom models.provider import ProviderType-from services.billing_service import BillingService+from services.billing_client import transportfrom tests.helpers.unit.knowledge_fs_core_fakes import Marker@@ -657,13 +658,14 @@ def test_model_invocation_preserves_billing_origin()requests: list[tuple[str, dict[str, object]]] = []- def billing_transport(method: str, endpoint: str, *, json: dict[str, object]) -> dict[str, object]:+ def billing_transport(method: str, url: str, *, json: dict[str, object], **_: object) -> httpx.Response:assert method == "POST"+ endpoint = url.removeprefix(transport._base_url(transport.Upstream.BILLING_QUOTA))requests.append((endpoint, json))if endpoint == "/quota/reserve":- return {"reservation_id": "reservation-1", "available": 197, "reserved": 3}+ return httpx.Response(200, json={"reservation_id": "reservation-1", "available": 197, "reserved": 3})assert endpoint == "/quota/commit"- return {"available": 197, "reserved": 0, "refunded": 0}+ return httpx.Response(200, json={"available": 197, "reserved": 0, "refunded": 0})chunk = LLMResultChunk(model="selected-model",@@ -680,7 +682,7 @@ def test_model_invocation_preserves_billing_origin(patch.object(ProviderConfiguration, "get_current_credentials", return_value={"api_key": "local-test-only"}),patch.object(ProviderConfiguration, "get_provider_model", return_value=None),patch.object(ModelInstance, provider_method, return_value=provider_result),- patch.object(BillingService, "_send_quota_request", side_effect=billing_transport),+ patch.object(transport, "_send", side_effect=billing_transport),):response = invoke_method("synthetic-end-user", _model_billing_attribution_tenant(), payload)assert get_credit_usage_metadata() is Nonediff --git a/api/tests/unit_tests/enums/test___init__.py b/api/tests/unit_tests/enums/test___init__.pyindex 77e3dc6ced4..56d5e4b8d8c 100644--- a/api/tests/unit_tests/enums/test___init__.py+++ b/api/tests/unit_tests/enums/test___init__.py@@ -3,12 +3,23 @@from collections.abc import Callablefrom unittest.mock import patch+import httpximport pytestfrom enums import DeploymentEdition, QuotaType+from services.billing_client import billing+from services.errors.billing import BillingUpstreamInvalidResponseError, BillingUpstreamUnavailableErrorfrom services.quota_service import QuotaCharge, QuotaService, unlimited+def _feature(*, limit: int, usage: int) -> billing.QuotaFeatureInfo:+ return billing.QuotaFeatureInfo(usage=usage, limit=limit, reset_date=0)+++def _quota_info(*, trigger_event: billing.QuotaFeatureInfo) -> billing.QuotaGetInfoReply:+ return billing.QuotaGetInfoReply(trigger_event=trigger_event, api_rate_limit=_feature(limit=0, usage=0))++class TestQuotaType:def test_billing_key_trigger(self) -> None:assert QuotaType.TRIGGER.billing_key == "trigger_event"@@ -39,7 +50,9 @@ class TestQuotaService:def test_reserve_success(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_reserve.return_value = {"reservation_id": "rid-1", "available": 99}+ mock_bs.quota_reserve.return_value = billing.QuotaReserveReply(+ reservation_id="rid-1", available=99, reserved=1+ )charge = QuotaService.reserve(QuotaType.TRIGGER, "t1", amount=1)@@ -54,7 +67,7 @@ class TestQuotaService:from services.errors.app import QuotaExceededErrorwith patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_reserve.return_value = dict[str, object]()+ mock_bs.quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="", available=0, reserved=0)with pytest.raises(QuotaExceededError):QuotaService.reserve(QuotaType.TRIGGER, "t1")@@ -68,6 +81,30 @@ class TestQuotaService:with pytest.raises(QuotaExceededError):QuotaService.reserve(QuotaType.TRIGGER, "t1")+ @pytest.mark.parametrize(+ "error",+ [+ BillingUpstreamInvalidResponseError("POST /quota/reserve returned 400"),+ BillingUpstreamUnavailableError("POST /quota/reserve returned 503"),+ ],+ )+ def test_reserve_billing_error_answer_propagates(self, error: Exception) -> None:+ with patch("services.billing_service.BillingService") as mock_bs:+ mock_bs.quota_reserve.side_effect = error++ with pytest.raises(type(error)):+ QuotaService.reserve(QuotaType.TRIGGER, "t1")++ def test_reserve_unreachable_billing_returns_unlimited(self) -> None:+ error = BillingUpstreamUnavailableError("POST /quota/reserve failed")+ error.__cause__ = httpx.ConnectError("connection refused")+ with patch("services.billing_service.BillingService") as mock_bs:+ mock_bs.quota_reserve.side_effect = error++ charge = QuotaService.reserve(QuotaType.TRIGGER, "t1")+ assert charge.success is True+ assert charge.charge_id is None+def test_reserve_api_exception_returns_unlimited(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:mock_bs.quota_reserve.side_effect = RuntimeError("network")@@ -78,8 +115,10 @@ class TestQuotaService:def test_consume_calls_reserve_and_commit(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_reserve.return_value = {"reservation_id": "rid-c"}- mock_bs.quota_commit.return_value = dict[str, object]()+ mock_bs.quota_reserve.return_value = billing.QuotaReserveReply(+ reservation_id="rid-c", available=9, reserved=1+ )+ mock_bs.quota_commit.return_value = billing.QuotaCommitReply(available=9, reserved=0, refunded=0)charge = QuotaService.consume(QuotaType.TRIGGER, "t1")assert charge.success is True@@ -122,7 +161,7 @@ class TestQuotaService:def test_release_success(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_release.return_value = dict[str, object]()+ mock_bs.quota_release.return_value = billing.QuotaReleaseReply(available=10, reserved=0, released=1)QuotaService.release(QuotaType.TRIGGER, "rid-1", "t1", "trigger_event")mock_bs.quota_release.assert_called_once_with(tenant_id="t1", feature_key="trigger_event", reservation_id="rid-1"@@ -135,17 +174,17 @@ class TestQuotaService:def test_get_remaining_normal(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": 100, "usage": 30}}+ mock_bs.get_quota_info.return_value = _quota_info(trigger_event=_feature(limit=100, usage=30))assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 70def test_get_remaining_unlimited(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": -1, "usage": 0}}+ mock_bs.get_quota_info.return_value = _quota_info(trigger_event=_feature(limit=-1, usage=0))assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == -1def test_get_remaining_over_limit_returns_zero(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": 10, "usage": 15}}+ mock_bs.get_quota_info.return_value = _quota_info(trigger_event=_feature(limit=10, usage=15))assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0def test_get_remaining_exception_returns_neg1(self) -> None:@@ -153,32 +192,11 @@ class TestQuotaService:mock_bs.get_quota_info.side_effect = RuntimeErrorassert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == -1- def test_get_remaining_empty_response(self) -> None:- with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = dict[str, object]()- assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0-- def test_get_remaining_non_dict_response(self) -> None:- with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = "invalid"- assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0-- def test_get_remaining_feature_not_in_response(self) -> None:- with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = {"other_feature": {"limit": 100, "usage": 0}}- remaining = QuotaService.get_remaining(QuotaType.TRIGGER, "t1")- assert remaining == 0-- def test_get_remaining_non_dict_feature_info(self) -> None:- with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.get_quota_info.return_value = {"trigger_event": "not_a_dict"}- assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0-class TestQuotaCharge:def test_commit_success(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_commit.return_value = dict[str, object]()+ mock_bs.quota_commit.return_value = billing.QuotaCommitReply(available=9, reserved=0, refunded=0)charge = QuotaCharge(success=True,charge_id="rid-1",@@ -198,7 +216,7 @@ class TestQuotaCharge:def test_commit_with_actual_amount(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_commit.return_value = dict[str, object]()+ mock_bs.quota_commit.return_value = billing.QuotaCommitReply(available=9, reserved=0, refunded=0)charge = QuotaCharge(success=True,charge_id="rid-1",@@ -213,7 +231,7 @@ class TestQuotaCharge:def test_commit_idempotent(self) -> None:with patch("services.billing_service.BillingService") as mock_bs:- mock_bs.quota_commit.return_value = dict[str, object]()+ mock_bs.quota_commit.return_value = billing.QuotaCommitReply(available=9, reserved=0, refunded=0)charge = QuotaCharge(success=True,charge_id="rid-1",diff --git a/api/tests/unit_tests/services/test_agent_llm_inner_service.py b/api/tests/unit_tests/services/test_agent_llm_inner_service.pyindex 2ca5946e44c..71c76df38ae 100644--- a/api/tests/unit_tests/services/test_agent_llm_inner_service.py+++ b/api/tests/unit_tests/services/test_agent_llm_inner_service.py@@ -21,6 +21,7 @@ from graphon.model_runtime.entities.message_entities import AssistantPromptMessafrom models.model import App, AppModefrom models.provider import ProviderTypefrom services.agent_llm_inner_service import AgentLLMInnerService, AgentLLMInnerServiceError, PreparedAgentLLMInvocation+from services.billing_client import billingfrom services.entities.agent_llm_inner import AgentLLMInvokeCaller, AgentLLMInvokeRequest, AgentLLMInvokeTargetfrom tests.helpers.unit.core.model_fixtures import make_model_instance@@ -299,17 +300,17 @@ def test_retried_gateway_delivery_uses_one_effective_billing_charge(effective_charges = 0reserve_request_ids: list[str] = []- def reserve(*, request_id: str, **_: object) -> dict[str, object]:+ def reserve(*, request_id: str, **_: object) -> billing.QuotaReserveReply:reserve_request_ids.append(request_id)reservation_id = reservations_by_request.setdefault(request_id, "reservation-1")- return {"reservation_id": reservation_id, "available": 97, "reserved": 3}+ return billing.QuotaReserveReply(reservation_id=reservation_id, available=97, reserved=3)- def commit(*, reservation_id: str, actual_amount: int, **_: object) -> dict[str, object]:+ def commit(*, reservation_id: str, actual_amount: int, **_: object) -> billing.QuotaCommitReply:nonlocal effective_chargesif reservation_id not in committed_reservations:committed_reservations.add(reservation_id)effective_charges += actual_amount- return {"available": 97, "reserved": 0, "refunded": 0}+ return billing.QuotaCommitReply(available=97, reserved=0, refunded=0)def provider_invoke(*_: object, **__: object) -> Generator[LLMResultChunk]:yield _chunk("done", usage=_usage())diff --git a/api/tests/unit_tests/services/test_billing_service.py b/api/tests/unit_tests/services/test_billing_service.pyindex d480c5c0724..836d444a636 100644--- a/api/tests/unit_tests/services/test_billing_service.py+++ b/api/tests/unit_tests/services/test_billing_service.py@@ -457,275 +457,119 @@ class TestBillingServiceSubscriptionInfo:get_vector_space.assert_called_once_with(tenant_id, bypass_cache=True)- def test_quota_get_balance_uses_quota_request(self):- tenant_id = "tenant-123"- with patch.object(BillingService, "_send_quota_request") as mock_send_quota_request:- mock_send_quota_request.return_value = {- "quota": "200",- "usage": "6",- "available": "194",- "reserved": "0",- "exhausted_at": "1748908800",- }-- result = BillingService.quota_get_balance(tenant_id, "credit_pool", bucket="trial")-- assert result == {"quota": 200, "usage": 6, "available": 194, "reserved": 0, "exhausted_at": 1748908800}- mock_send_quota_request.assert_called_once_with(- "GET",- "/quota/balance",- params={"tenant_id": tenant_id, "feature_key": "credit_pool", "bucket": "trial"},- )---class TestBillingServiceUsageCalculation:- """Unit tests for usage calculation and credit management.-- Tests cover:- - Quota information retrieval- """-- def test_get_quota_info(self):- """Test retrieval of quota info from new endpoint."""- # Arrange- tenant_id = "tenant-123"- expected_response = {"trigger_event": {"limit": 100, "usage": 30}, "api_rate_limit": {"limit": -1, "usage": 0}}- with patch.object(BillingService, "_send_quota_request") as mock_send_quota_request:- mock_send_quota_request.return_value = expected_response-- # Act- result = BillingService.get_quota_info(tenant_id)-- # Assert- assert result == expected_response- mock_send_quota_request.assert_called_once_with("GET", "/quota/info", params={"tenant_id": tenant_id})-class TestBillingServiceQuotaOperations:- """Unit tests for quota reserve/commit/release operations."""-- @pytest.fixture- def mock_send_request(self):- with patch.object(BillingService, "_send_quota_request") as mock:- yield mock-- def test_quota_reserve_success(self, mock_send_request):- expected = {"reservation_id": "rid-1", "available": 99, "reserved": 1}- mock_send_request.return_value = expected-- result = BillingService.quota_reserve(tenant_id="t1", feature_key="trigger_event", request_id="req-1", amount=1)-- assert result == expected- mock_send_request.assert_called_once_with(- "POST",- "/quota/reserve",- json={"tenant_id": "t1", "feature_key": "trigger_event", "request_id": "req-1", "amount": 1},- )-- def test_quota_reserve_coerces_string_to_int(self, mock_send_request):- """Test that TypeAdapter coerces string values to int."""- mock_send_request.return_value = {"reservation_id": "rid-str", "available": "99", "reserved": "1"}-- result = BillingService.quota_reserve(tenant_id="t1", feature_key="trigger_event", request_id="req-s", amount=1)-- assert result["available"] == 99- assert isinstance(result["available"], int)- assert result["reserved"] == 1- assert isinstance(result["reserved"], int)-- def test_quota_reserve_with_meta(self, mock_send_request):- mock_send_request.return_value = {"reservation_id": "rid-2", "available": 98, "reserved": 1}- meta = {"source": "webhook"}+ """Quota calls build the generated request bodies and return the generated replies."""- BillingService.quota_reserve(- tenant_id="t1", feature_key="trigger_event", request_id="req-2", amount=1, meta=meta+ def test_get_quota_info_returns_the_generated_reply(self):+ reply = billing.QuotaGetInfoReply(+ trigger_event=billing.QuotaFeatureInfo(usage=30, limit=100, reset_date=1767225600),+ api_rate_limit=billing.QuotaFeatureInfo(usage=0, limit=-1, reset_date=0),)- call_json = mock_send_request.call_args[1]["json"]- assert call_json["meta"] == {"source": "webhook"}+ with patch.object(billing, "quota_get_quota_info", return_value=reply) as get:+ assert BillingService.get_quota_info(TENANT_ID) is reply- def test_quota_reserve_with_bucket(self, mock_send_request):- mock_send_request.return_value = {"reservation_id": "rid-2", "available": 98, "reserved": 1}-- BillingService.quota_reserve(- tenant_id="t1", feature_key="credit_pool", request_id="req-2", amount=1, bucket="trial"- )-- call_json = mock_send_request.call_args[1]["json"]- assert call_json["bucket"] == "trial"-- def test_quota_commit_success(self, mock_send_request):- expected = {"available": 98, "reserved": 0, "refunded": 0}- mock_send_request.return_value = expected-- result = BillingService.quota_commit(- tenant_id="t1", feature_key="trigger_event", reservation_id="rid-1", actual_amount=1- )-- assert result == expected- mock_send_request.assert_called_once_with(- "POST",- "/quota/commit",- json={- "tenant_id": "t1",- "feature_key": "trigger_event",- "reservation_id": "rid-1",- "actual_amount": 1,- },- )+ get.assert_called_once_with(tenant_id=TENANT_ID)- def test_quota_commit_coerces_string_to_int(self, mock_send_request):- """Test that TypeAdapter coerces string values to int."""- mock_send_request.return_value = {"available": "97", "reserved": "0", "refunded": "1"}+ def test_quota_reserve_sends_bucket_and_string_meta(self):+ reply = billing.QuotaReserveReply(reservation_id="rid-1", available=99, reserved=1)++ with patch.object(billing, "quota_reserve", return_value=reply) as reserve:+ result = BillingService.quota_reserve(+ tenant_id="t1",+ feature_key="credit_pool",+ request_id="req-1",+ amount=2,+ meta={"source": "llm.invoke", "attempt": 1},+ bucket="trial",+ )- result = BillingService.quota_commit(- tenant_id="t1", feature_key="trigger_event", reservation_id="rid-s", actual_amount=1+ assert result is reply+ reserve.assert_called_once_with(+ body=billing.QuotaReserveReq(+ tenant_id="t1",+ feature_key="credit_pool",+ request_id="req-1",+ amount=2,+ meta={"source": "llm.invoke", "attempt": "1"},+ bucket="trial",+ ))- assert result["available"] == 97- assert isinstance(result["available"], int)- assert result["refunded"] == 1- assert isinstance(result["refunded"], int)+ def test_quota_reserve_defaults_to_empty_bucket_and_meta(self):+ reply = billing.QuotaReserveReply(reservation_id="rid-1", available=99, reserved=1)- def test_quota_commit_with_meta(self, mock_send_request):- mock_send_request.return_value = {"available": 97, "reserved": 0, "refunded": 0}- meta = {"reason": "partial"}+ with patch.object(billing, "quota_reserve", return_value=reply) as reserve:+ BillingService.quota_reserve(tenant_id="t1", feature_key="trigger_event", request_id="req-1")- BillingService.quota_commit(- tenant_id="t1", feature_key="trigger_event", reservation_id="rid-1", actual_amount=1, meta=meta+ assert reserve.call_args.kwargs["body"] == billing.QuotaReserveReq(+ tenant_id="t1", feature_key="trigger_event", request_id="req-1", amount=1, meta={}, bucket="")- call_json = mock_send_request.call_args[1]["json"]- assert call_json["meta"] == {"reason": "partial"}-- def test_quota_commit_with_bucket(self, mock_send_request):- mock_send_request.return_value = {"available": 97, "reserved": 0, "refunded": 0}+ def test_quota_commit_builds_the_request(self):+ reply = billing.QuotaCommitReply(available=98, reserved=0, refunded=0)++ with patch.object(billing, "quota_commit", return_value=reply) as commit:+ result = BillingService.quota_commit(+ tenant_id="t1",+ feature_key="credit_pool",+ reservation_id="rid-1",+ actual_amount=2,+ meta={"request_id": "req-1"},+ bucket="paid",+ )- BillingService.quota_commit(- tenant_id="t1",- feature_key="credit_pool",- reservation_id="rid-1",- actual_amount=1,- bucket="paid",+ assert result is reply+ commit.assert_called_once_with(+ body=billing.QuotaCommitReq(+ tenant_id="t1",+ feature_key="credit_pool",+ reservation_id="rid-1",+ actual_amount=2,+ meta={"request_id": "req-1"},+ bucket="paid",+ ))- call_json = mock_send_request.call_args[1]["json"]- assert call_json["bucket"] == "paid"-- def test_quota_release_success(self, mock_send_request):- expected = {"available": 100, "reserved": 0, "released": 1}- mock_send_request.return_value = expected+ def test_quota_release_builds_the_request(self):+ reply = billing.QuotaReleaseReply(available=100, reserved=0, released=1)- result = BillingService.quota_release(tenant_id="t1", feature_key="trigger_event", reservation_id="rid-1")+ with patch.object(billing, "quota_release", return_value=reply) as release:+ result = BillingService.quota_release(+ tenant_id="t1", feature_key="credit_pool", reservation_id="rid-1", bucket="trial"+ )- assert result == expected- mock_send_request.assert_called_once_with(- "POST",- "/quota/release",- json={"tenant_id": "t1", "feature_key": "trigger_event", "reservation_id": "rid-1"},+ assert result is reply+ release.assert_called_once_with(+ body=billing.QuotaReleaseReq(+ tenant_id="t1", feature_key="credit_pool", reservation_id="rid-1", bucket="trial"+ ))- def test_quota_release_coerces_string_to_int(self, mock_send_request):- """Test that TypeAdapter coerces string values to int."""- mock_send_request.return_value = {"available": "100", "reserved": "0", "released": "1"}+ def test_quota_get_balance_forwards_the_bucket(self):+ reply = billing.QuotaGetBalanceReply(available=194, reserved=0, quota=200, usage=6, exhausted_at=0)- result = BillingService.quota_release(tenant_id="t1", feature_key="trigger_event", reservation_id="rid-s")+ with patch.object(billing, "quota_get_balance", return_value=reply) as get:+ assert BillingService.quota_get_balance("t1", "credit_pool", bucket="trial") is reply- assert result["available"] == 100- assert isinstance(result["available"], int)- assert result["released"] == 1- assert isinstance(result["released"], int)+ get.assert_called_once_with(tenant_id="t1", feature_key="credit_pool", bucket="trial")- def test_quota_release_with_bucket(self, mock_send_request):- mock_send_request.return_value = {"available": 100, "reserved": 0, "released": 1}+ def test_quota_consume_capped_builds_the_request(self):+ reply = billing.QuotaConsumeCappedReply(deducted=3, available=0, reserved=0, quota=10, usage=10)- BillingService.quota_release(tenant_id="t1", feature_key="credit_pool", reservation_id="rid-1", bucket="trial")-- call_json = mock_send_request.call_args[1]["json"]- assert call_json["bucket"] == "trial"-- def test_quota_consume_capped_success(self, mock_send_request):- mock_send_request.return_value = {- "deducted": "2",- "available": "8",- "reserved": "0",- "quota": "10",- "usage": "2",- }-- result = BillingService.quota_consume_capped(- tenant_id="t1",- feature_key="credit_pool",- request_id="req-1",- amount=5,- bucket="paid",- meta={"source": "test"},- )-- assert result == {"deducted": 2, "available": 8, "reserved": 0, "quota": 10, "usage": 2}- mock_send_request.assert_called_once_with(- "POST",- "/quota/consume-capped",- json={- "tenant_id": "t1",- "feature_key": "credit_pool",- "request_id": "req-1",- "amount": 5,- "bucket": "paid",- "meta": {"source": "test"},- },- )-- def test_send_quota_request_uses_quota_base_url(self):- with (- patch.object(BillingService, "quota_base_url", "https://quota.example.com/v1"),- patch.object(BillingService, "_send_request") as mock_send_request,- ):- mock_send_request.return_value = {"ok": True}-- result = BillingService._send_quota_request("GET", "/quota/info", params={"tenant_id": "t1"})+ with patch.object(billing, "quota_consume_capped", return_value=reply) as consume:+ result = BillingService.quota_consume_capped(+ tenant_id="t1", feature_key="credit_pool", request_id="req-1", amount=5, bucket="trial"+ )- assert result == {"ok": True}- mock_send_request.assert_called_once_with(- "GET",- "/quota/info",- json=None,- params={"tenant_id": "t1"},- base_url="https://quota.example.com/v1",+ assert result is reply+ consume.assert_called_once_with(+ body=billing.QuotaConsumeCappedReq(+ tenant_id="t1", feature_key="credit_pool", request_id="req-1", amount=5, meta={}, bucket="trial"+ ))- def test_get_quota_info_coerces_string_to_int(self, mock_send_request):- """Test that TypeAdapter coerces string values to int for get_quota_info."""- mock_send_request.return_value = {- "trigger_event": {"usage": "42", "limit": "3000", "reset_date": "1700000000"},- "api_rate_limit": {"usage": "10", "limit": "-1", "reset_date": "-1"},- }-- result = BillingService.get_quota_info("t1")-- assert result["trigger_event"]["usage"] == 42- assert isinstance(result["trigger_event"]["usage"], int)- assert result["trigger_event"]["limit"] == 3000- assert isinstance(result["trigger_event"]["limit"], int)- assert result["trigger_event"]["reset_date"] == 1700000000- assert isinstance(result["trigger_event"]["reset_date"], int)- assert result["api_rate_limit"]["limit"] == -1- assert isinstance(result["api_rate_limit"]["limit"], int)-- def test_get_quota_info_accepts_int_values(self, mock_send_request):- """Test that get_quota_info works with native int values."""- expected = {- "trigger_event": {"usage": 42, "limit": 3000, "reset_date": 1700000000},- "api_rate_limit": {"usage": 0, "limit": -1},- }- mock_send_request.return_value = expected-- result = BillingService.get_quota_info("t1")-- assert result["trigger_event"]["usage"] == 42- assert result["trigger_event"]["limit"] == 3000- assert result["api_rate_limit"]["limit"] == -1-class TestBillingServiceComplianceDownload:"""Unit tests for compliance download requests."""diff --git a/api/tests/unit_tests/services/test_credit_pool_service.py b/api/tests/unit_tests/services/test_credit_pool_service.pyindex 2d2bb4bda53..782280288d9 100644--- a/api/tests/unit_tests/services/test_credit_pool_service.py+++ b/api/tests/unit_tests/services/test_credit_pool_service.py@@ -15,6 +15,7 @@ from core.errors.error import QuotaExceededErrorfrom enums import DeploymentEditionfrom models import TenantCreditPoolfrom models.enums import ProviderQuotaType+from services.billing_client import billingfrom services.credit_pool_service import (CREDIT_POOL_TENANT_LOCK_BLOCKING_TIMEOUT_SECONDS,CREDIT_POOL_TENANT_LOCK_TIMEOUT_SECONDS,@@ -253,13 +254,9 @@ def test_get_pool_uses_billing_quota_balance_when_enabled(config_overrides: Callwith (patch("services.billing_service.BillingService.quota_get_balance") as quota_get_balance,):- quota_get_balance.return_value = {- "quota": 1000,- "usage": 250,- "available": 750,- "reserved": 0,- "exhausted_at": 1748908800,- }+ quota_get_balance.return_value = billing.QuotaGetBalanceReply(+ quota=1000, usage=250, available=750, reserved=0, exhausted_at=1748908800+ )pool = CreditPoolService.get_pool(tenant_id=tenant_id, pool_type=ProviderQuotaType.PAID)@@ -282,7 +279,7 @@ def test_reserve_credits_commits_billing_reservation_once() -> None:patch("services.billing_service.BillingService.quota_commit") as quota_commit,patch("services.billing_service.BillingService.quota_release") as quota_release,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)reservation = CreditPoolService.reserve_credits(tenant_id="tenant-1",@@ -330,7 +327,7 @@ def test_reserve_credits_releases_billing_reservation() -> None:patch("services.billing_service.BillingService.quota_reserve") as quota_reserve,patch("services.billing_service.BillingService.quota_release") as quota_release,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)reservation = CreditPoolService.reserve_credits(tenant_id="tenant-1",@@ -378,7 +375,7 @@ def test_check_and_deduct_credits_uses_billing_reserve_and_commit_when_enabled(patch("services.billing_service.BillingService.quota_commit") as quota_commit,patch("services.billing_service.BillingService.quota_release") as quota_release,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)result = CreditPoolService.check_and_deduct_credits(tenant_id=tenant_id,@@ -422,7 +419,7 @@ def test_check_and_deduct_credits_forwards_deterministic_billing_identity(patch("services.billing_service.BillingService.quota_reserve") as quota_reserve,patch("services.billing_service.BillingService.quota_commit") as quota_commit,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)result = CreditPoolService.check_and_deduct_credits(tenant_id="tenant-1",@@ -464,7 +461,7 @@ def test_check_and_deduct_credits_raises_when_billing_reserve_is_insufficient(with (patch("services.billing_service.BillingService.quota_reserve") as quota_reserve,):- quota_reserve.return_value = {"reservation_id": "", "available": 1, "reserved": 0}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="", available=1, reserved=0)with pytest.raises(QuotaExceededError, match="Insufficient credits remaining"):CreditPoolService.check_and_deduct_credits(tenant_id="tenant-1", credits_required=3)@@ -479,7 +476,7 @@ def test_check_and_deduct_credits_releases_billing_reservation_when_commit_failspatch("services.billing_service.BillingService.quota_commit", side_effect=RuntimeError("commit failed")),patch("services.billing_service.BillingService.quota_release") as quota_release,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)with pytest.raises(RuntimeError, match="commit failed"):CreditPoolService.check_and_deduct_credits(tenant_id="tenant-1", credits_required=3)@@ -504,7 +501,7 @@ def test_check_and_deduct_credits_logs_when_billing_release_fails("services.billing_service.BillingService.quota_release", side_effect=RuntimeError("release failed")) as quota_release,):- quota_reserve.return_value = {"reservation_id": "reservation-1", "available": 7, "reserved": 3}+ quota_reserve.return_value = billing.QuotaReserveReply(reservation_id="reservation-1", available=7, reserved=3)with pytest.raises(RuntimeError, match="commit failed"):CreditPoolService.check_and_deduct_credits(tenant_id="tenant-1", credits_required=3)@@ -528,13 +525,9 @@ def test_deduct_credits_capped_uses_billing_consume_capped_when_enabled(with (patch("services.billing_service.BillingService.quota_consume_capped") as quota_consume_capped,):- quota_consume_capped.return_value = {- "deducted": 2,- "available": 0,- "reserved": 0,- "quota": 10,- "usage": 10,- }+ quota_consume_capped.return_value = billing.QuotaConsumeCappedReply(+ deducted=2, available=0, reserved=0, quota=10, usage=10+ )result = CreditPoolService.deduct_credits_capped(tenant_id=tenant_id,diff --git a/api/tests/unit_tests/services/test_feature_service.py b/api/tests/unit_tests/services/test_feature_service.pyindex 1e58d57bed9..6e813f32c92 100644--- a/api/tests/unit_tests/services/test_feature_service.py+++ b/api/tests/unit_tests/services/test_feature_service.py@@ -337,7 +337,13 @@ def test_get_features_exclude_vector_space_sets_vector_space_to_none(config_over)with (patch("services.feature_service.BillingService.get_info", return_value=billing_info) as get_info,- patch("services.feature_service.BillingService.get_quota_info", return_value={}),+ patch(+ "services.feature_service.BillingService.get_quota_info",+ return_value=billing.QuotaGetInfoReply(+ trigger_event=billing.QuotaFeatureInfo(usage=1, limit=3000, reset_date=0),+ api_rate_limit=billing.QuotaFeatureInfo(usage=2, limit=5000, reset_date=0),+ ),+ ),):features = FeatureService.get_features(tenant_id, exclude_vector_space=True)