Organizations, plans, credits, and billing in LLM Gateway: org kinds, the Pro plan, DevPass and Chat entitlements, Stripe webhooks, end-user wallets, cost and token math, audit logs, org-scoped emails and notifications. Use when changing apps/api/src/stripe.ts, apps/api/src/routes/dev-plans.ts, packages/actions/src/costs.ts, packages/db/src/email-recipients.ts, packages/scripts devpass-* scripts, subscription or credit logic, organization.kind or plan gating, DevPass status, audit_log actions...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Billing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/theopenco-billing)
---
name: billing
description: Organizations, plans, credits, and billing in LLM Gateway: org kinds, the Pro plan, DevPass and Chat entitlements, Stripe webhooks, end-user wallets, cost and token math, audit logs, org-scoped emails and notifications. Use when changing apps/api/src/stripe.ts, apps/api/src/routes/dev-plans.ts, packages/actions/src/costs.ts, packages/db/src/email-recipients.ts, packages/scripts devpass-* scripts, subscription or credit logic, organization.kind or plan gating, DevPass status, audit_log actions, or any organization email or notification.
---
# Billing and organizations
## Money
- Do all money, credit, and usage-threshold math with `Decimal` from `decimal.js`; convert DB decimal strings with `new Decimal(value ?? 0)`.
- Token extraction (`apps/gateway/src/chat/tools/extract-token-usage.ts`) treats completion tokens as including reasoning (it adds reasoning only when total = prompt + completion + reasoning shows it is separate); `calculateCosts` (`packages/actions/src/costs.ts`) bills completion, adding reasoning only when completion is missing, and reports `reasoningTokens` as detail. Verify a new reasoning provider's usage semantics live.
## Organization kinds and plans
- `organization.kind` (`default` / `devpass` / `chat`) is set at creation and never changes; DevPass and Chat signups get a separate organization (`apps/api/src/utils/personal-org.ts`). Joining on it for historical attribution is safe. `organization.plan` changes over time.
- The Pro plan exists only on `default` orgs. DevPass and Chat entitlements live in `devPlan` / `chatPlan` and their `*CreditsLimit` / `*StripeSubscriptionId` columns; `organization.stripeSubscriptionId` is reserved for a team org's Pro subscription.
- Every Stripe webhook that sets `plan: "pro"` or stamps `stripeSubscriptionId` first returns for non-default kinds, as handlers such as `checkout.session.completed`, `customer.subscription.created`, and `invoice.payment_succeeded` in `apps/api/src/stripe.ts` do. A `devpass` or `chat` org with `plan: "pro"` is corrupt state; gate product behavior for those orgs on their own entitlement columns.
- Pro `customer.subscription.updated` / `deleted` and `invoice.payment_succeeded` ignore events whose subscription id is not the org's current `stripeSubscriptionId` (`apps/api/src/stripe.ts`); keep that guard in new handlers.
- Stripe rejects `off_session` charges on India-issued cards (RBI e-mandate); fall back to on-session as `createDevPassCharge` does (`apps/api/src/routes/dev-plans.ts`).
- End-user wallet organizations are regular PAYG `default` orgs. `withWalletCredits` affects only downstream credit gating, never kind- or plan-gated gateway logic.
- For "has this org ever paid for DevPass", use `hasBillingHistory` from `GET /dev-plans/status`; plan end clears every `devPlan*` column.
- A win-back campaign uses a stricter test than `hasBillingHistory`: a completed, unrefunded plan charge (`PAID_PLAN_TRANSACTION_TYPES`: `dev_plan_start`, `dev_plan_renewal`, `dev_plan_upgrade`, `subscription_start`), on an active organization that is not risk-flagged, sent to members who are active and email-verified (no owner-role filter; see `loadDevPassMemberships` in `packages/scripts`).
## Audit log, emails, notifications
- `audit_log` records actions owned by one organization; valid actions are `auditLogActions` in `packages/db/src/schema.ts`. User-account events (email, password, profile) belong in a user-scoped store.
- Send org-scoped email only when the org owner's email is verified, via `isOrgOwnerEmailVerified` / `resolveVerifiedOrgRecipient` (`@llmgateway/db`). Only verification and password-reset emails reach unverified users.
- Prefix org-scoped notification event keys with the org id; valid types are `notificationTypes` in `packages/db/src/schema.ts`.