:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Expose a team's quota-limit state.
Backs the LLM gateway's `QuotaResolver`, which forwards the caller's auth
header here to learn whether a given team is currently over its AI credits
quota. Project-nested so org membership and token `scoped_teams`/
`scoped_organizations` enforcement come from the standard
`TeamAndOrgViewSetMixin` permission chain — see
`posthog.permissions.APIScopePermission.check_team_and_org_permissions`.
"""
from __future__ import annotations
from typing import Any
from drf_spectacular.utils import extend_schema
from rest_framework import serializers, viewsets
from rest_framework.request import Request
from rest_framework.response import Response
from posthog.api.routing import TeamAndOrgViewSetMixin
from ee.billing.quota_limiting import team_quota_snapshot
class QuotaResourceLimitSerializer(serializers.Serializer):
limited = serializers.BooleanField(
help_text=(
"True when the team is currently over its quota for this resource and limits are in "
"effect. A deactivated organization additionally reads as limited on the two credit "
"buckets `ai_credits` and `posthog_code_credits`, regardless of usage."
),
)
usage = serializers.FloatField(
allow_null=True,
help_text=(
"Units of this resource the organization has used so far this billing period, in the "
"resource's native unit (credits for credit buckets). Null when billing hasn't synced "
"usage for the resource."
),
)
limit = serializers.FloatField(
allow_null=True,
help_text="The organization's limit for this resource in the same unit. Null when unlimited or unknown.",
)
class QuotaLimitsResponseSerializer(serializers.Serializer):
limited = serializers.DictField(
child=QuotaResourceLimitSerializer(),
help_text=(
"Per-resource limit state for every `QuotaResource` value, e.g. `ai_credits`, "
"`posthog_code_credits`. Also carries the informational Desktop component resources "
"(`posthog_code_token_credits`, `sandbox_compute_credits`, "
"`sandbox_compute_cpu_millicore_seconds`, `sandbox_compute_memory_mib_seconds`) with "
"usage in their native units, a null limit, and `limited` always false — they are never "
"quota-enforced; only the combined `posthog_code_credits` is."
),
)
code_usage_billing_active = serializers.BooleanField(
help_text=(
"Whether the team's organization pays for PostHog Desktop usage: billing grants the "
"`posthog_code_usage` product feature only on the Desktop usage product's paid plan, "
"synced into the organization's available features. Consumers gate paid-tier Desktop "
"behavior on this; an org unknown to billing reads as not paying. Always false for "
"deactivated organizations."
),
)
@extend_schema(tags=["quota_limits"])
class QuotaLimitsViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
"""Read-only view of a team's quota-limit state."""
scope_object = "project"
required_scopes = ["project:read"]
http_method_names = ["get", "head", "options"]
@extend_schema(
summary="Get a team's quota-limit state",
description=(
"Return the current quota-limit state for the team identified in the URL, "
"keyed by `QuotaResource` value. Used by the LLM gateway to gate billable "
"products on AI credits exhaustion."
),
responses={200: QuotaLimitsResponseSerializer},
)
def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
data = QuotaLimitsResponseSerializer(team_quota_snapshot(self.team)).data
return Response(data)