Features
Billing and Limits
Plan limits, usage metering, Stripe lifecycle routes, and limit-enforcement behavior.
Billing is split into two concerns:
- Usage metering + limit checks (
@ai/billing) - Payment/subscription lifecycle (
@ai/stripe+ app billing routes)
@ai/billing determines plan limits, operation gating, and usage accounting per request.
@ai/stripe and billing routes manage customer/checkout/portal/webhook state transitions.
Core Package Responsibilities
@ai/billing
- defines plan limits and operation types
- checks limits per operation via
checkUsageLimits(...) - creates consistent limit responses via
createLimitExceededResponse(...) - records usage logs and aggregates token/cost counters
@ai/stripe
- creates customers and checkout sessions
- opens customer portal sessions
- verifies webhook signatures and parses subscription state
Enforcement Model
checkUsageLimits(...) supports operations:
chatuploaddeep_searchtranscriptionartifact_creationartifact_updatedocument_comparison
Enforcement behavior:
- if
shouldEnforceBilling()is false, operations are allowed (usage still tracked) shouldEnforceBilling()is false whenDEVELOPMENT_MODE=trueorFEATURE_BILLING_ENFORCEMENT_ENABLED=false
API Surface
GET /api/billing/plansGET /api/billing/usageGET /api/billing/subscriptionPOST /api/billing/subscriptionDELETE /api/billing/subscriptionPOST /api/billing/checkoutPOST /api/billing/portalPOST /api/billing/webhookGET /api/billing/telegram-upgrade
Stripe Webhook Behavior
Handled events:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.paidinvoice.payment_failed
Operational details:
- signature verification required (
stripe-signature) - webhook deduplication key stored in KV with TTL
- event processing is idempotent where subscription/payment records may already exist
Environment Variables
Stripe keys:
STRIPE_SECRET_KEY=
STRIPE_PUBLISHABLE_KEY=
STRIPE_WEBHOOK_SECRET=Plan linkage:
STRIPE_PRICE_ID_PRIME=
STRIPE_PRODUCT_ID_PRIME=Billing controls:
FEATURE_BILLING_ENFORCEMENT_ENABLED=true
DEVELOPMENT_MODE=false