1010from django .core .exceptions import PermissionDenied
1111from django .http import HttpRequest , QueryDict
1212from django_ratelimit .exceptions import Ratelimited
13+ from flags .state import flag_enabled
1314
1415from llmstack .connections .actors import ConnectionActivationActor
1516from llmstack .connections .models import (
@@ -30,6 +31,19 @@ class UsageLimitReached(PermissionDenied):
3031 pass
3132
3233
34+ class OutOfCredits (PermissionDenied ):
35+ pass
36+
37+
38+ @database_sync_to_async
39+ def _usage_limit_exceeded (request , user ):
40+ return flag_enabled (
41+ "HAS_EXCEEDED_MONTHLY_PROCESSOR_RUN_QUOTA" ,
42+ request = request ,
43+ user = request .user ,
44+ )
45+
46+
3347@database_sync_to_async
3448def _build_request_from_input (post_data , scope ):
3549 session = dict (scope ["session" ])
@@ -253,6 +267,11 @@ async def connect(self):
253267 def _run_app (self , request_uuid , request , ** kwargs ):
254268 from llmstack .apps .apis import AppViewSet
255269
270+ if _usage_limit_exceeded (request , self ._run_app ):
271+ raise OutOfCredits (
272+ "You have exceeded your usage credits. Please add credits to your account from settings to continue using the platform." ,
273+ )
274+
256275 return AppViewSet ().run_playground_internal_async (
257276 session_id = self ._session_id ,
258277 request_uuid = request_uuid ,
0 commit comments