Skip to content

Billing and Credits

Product Factory includes a complete billing system powered by the worker-billing service. It supports a credits-based usage model, subscription plans, and order management.

Credits System

Credits are the internal currency for consuming services (primarily AI completions). Users receive credits through:

SourceMechanism
Daily check-inFree credits awarded once per day via /checkin
Subscription planMonthly credit allocation based on plan tier
One-time purchaseDirect credit purchase through checkout flow

Credit Deduction

When a user makes an AI request, the flow is:

  1. worker-ai calls worker-billing to perform a credits preflight check.
  2. If sufficient credits exist, the AI request proceeds.
  3. After completion, worker-ai reports actual token usage.
  4. worker-billing deducts the calculated credit cost.

Subscription Plans

Plans are defined by admins and stored in D1. Each plan specifies:

FieldDescription
nameDisplay name (e.g., “Pro”, “Enterprise”)
priceMonthly price
creditsMonthly credit allocation
featuresJSON array of feature descriptions

Users subscribe through the checkout flow. The gateway handles subscription lifecycle events.

Orders

Every credit purchase or subscription payment creates an order record in D1:

FieldDescription
order_idUnique identifier
user_idBuyer
typecredits or subscription
amountPayment amount
statuspending, completed, failed, refunded

API Endpoints

See the Billing API Reference for the complete endpoint documentation.