Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:A616567126 GPT WEB JAVA Credit Based Access Control

From Leeroopedia
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:

  1. checkUser(MessageLog, Long id) — deducts credits and persists a MessageLog (used by GPT and SD)
  2. checkUser(Long userId, Integer number) — deducts credits only (used by MJ)
  3. 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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment