The platform for continuously improving AI agents.
"""Credit gate for paid work a person or agent starts.
The project's owner pays, so their balance must be above zero, whatever the work costs;
a negative balance is allowed and shown. ``completions.py`` gates separately to answer in
OpenAI's error shape. Background work pauses at zero instead (``billing_ledger.can_spend``).
"""
from rest_framework.exceptions import APIException
from overbae.services.billing_ledger import can_spend
__all__ = ["PaymentRequired", "require_credits"]
class PaymentRequired(APIException):
status_code = 402
default_detail = "Insufficient credits."
default_code = "insufficient_credits"
def require_credits(project) -> None:
"""``project`` is a project or its id."""
if not can_spend(getattr(project, "pk", project)):
raise PaymentRequired()