Principle:A616567126 GPT WEB JAVA Credit Based Access Control
| Knowledge Sources | |
|---|---|
| Domains | Access_Control, Billing |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Access control mechanism that grants or denies service based on a pre-paid credit balance, deducting credits per usage to enforce fair resource allocation and monetization.
Description
Credit-based access control is a pattern where each user holds a balance of "credits" (also called times, tokens, or quota units). Before executing a requested operation, the system validates that the user has sufficient credits for the specific action type. If the balance is adequate, credits are deducted atomically; if insufficient, the request is rejected.
This pattern solves the problem of metering heterogeneous AI services (GPT-3.5, GPT-4, Midjourney, Stable Diffusion) that have different costs per invocation. Each service type has a defined credit cost (e.g., GPT-3.5 = 1 credit, GPT-4 = 5 credits, MJ imagine = 2 credits), allowing a unified billing model across diverse services.
Usage
Use this principle when:
- Building a platform with multiple paid services of varying cost
- Needing to prevent overuse and enforce per-action billing
- Supporting both pre-paid (regular users) and unlimited (admin/VIP users, type=-1) access
Theoretical Basis
Pre-Deduction Model:
# Abstract algorithm (NOT real implementation)
def check_user(user, cost):
if user.type == UNLIMITED:
return # Admin users bypass credit check
if user.remaining_credits < cost:
raise InsufficientCreditsError()
user.remaining_credits -= cost
save(user)
Atomicity Requirement: The check-and-deduct operation must be atomic to prevent race conditions where concurrent requests deplete credits below zero. This implementation uses @Transactional to ensure the credit deduction and user update occur as a single unit of work.
Cost Mapping: Different service types map to different credit costs:
- GPT-3.5 → 1 credit
- GPT-4 → 5 credits
- MJ Imagine (relax) → 2 credits
- MJ Upscale → 1 credit
- SD Image → 1 credit
Practical Guide
The repository implements credit validation in CheckService with three variants:
checkUser(MessageLog, Long id)— deducts credits and persists a MessageLog (used by GPT and SD)checkUser(Long userId, Integer number)— deducts credits only (used by MJ)checkWxUser(Long userId, Integer number)— returns boolean instead of throwing (used by WeChat bot)
For admin users (type == -1), the credit check is bypassed entirely.
Related Pages
Implemented By
- Implementation:A616567126_GPT_WEB_JAVA_CheckService
- Implementation:A616567126_GPT_WEB_JAVA_GptController
- Implementation:A616567126_GPT_WEB_JAVA_MjController
- Implementation:A616567126_GPT_WEB_JAVA_WxOutServiceImpl
- Implementation:A616567126_GPT_WEB_JAVA_CommonConst
- Implementation:A616567126_GPT_WEB_JAVA_ClientController
- Implementation:A616567126_GPT_WEB_JAVA_SparkServiceImpl