@intelligo-dev/billing
Quota engine, credits, Stripe, feature gates, trials and rate limiting.
Install
pnpm add @intelligo-dev/billing drizzle-orm react stripe zoddrizzle-orm, react, stripe and zod are peers; the trial and payment
emails render with React.
What it owns
Admission (estimateQuota / reserveQuota) and settlement, the credit ledger,
Stripe checkout and the webhook receiver, feature gating, trial grants and
expiry, and per-plan rate limits.
Use
The plan catalogue is the product’s, registered from the composition root:
// lib/intelligo.ts
import {
registerProductFeatures,
registerProductPlans,
setDefaultProductSlug,
} from "@intelligo-dev/billing/plans";
import { fromMajor } from "@intelligo-dev/core/money";
setDefaultProductSlug("acme");
registerProductPlans("acme", {
free: {
name: "Free",
slug: "free",
description: "Try it",
priceOneTime: 0,
targetAudience: "Everyone",
aiModelLabel: "Base",
monthlyAllowance: fromMajor(0.5, "USD"),
limits: { chatMessages: 30 },
features: ["30 messages a month"],
},
});
registerProductFeatures("acme", { exports: ["pro"] });A transport then gates on the workspace’s plan:
import { requireFeature } from "@intelligo-dev/billing";
await requireFeature(workspace.id, "exports"); // throws FeatureNotAvailableErrorA row in feature_flags overrides the registered matrix — isActive: false
is a kill switch for every plan — and while the flags cannot be read, a
feature is refused rather than granted. AI spend goes through the execution boundary
instead: the composition root binds reserveQuota, extendReservation,
recordTokenUsage and releaseReservation to the ports of
@intelligo-dev/executions,
so a run is admitted against the plan’s allowance and the credit balance, and
settled exactly once.
A fixed price per unit of work
Not everything a product sells is a model’s tokens. An execution can carry a
fixed price instead — a report, an export, a document — and goes through the
same admission and settlement as a turn:
import { fromMajor } from "@intelligo-dev/core/money";
const run = await executions.begin({
workspaceId: workspace.id,
userId: session?.user.id ?? null, // null for work nobody signed in started
capability: "report.generate",
price: fromMajor(1, "USD"),
});
if (!run.allowed) return refuse(run.code); // insufficient_credits, allowance_depleted…
try {
const report = await generateReport(recordId);
await run.complete(); // charges exactly the price
return report;
} catch (error) {
await run.fail({ error }); // charges nothing and releases the hold
throw error;
}Work priced by what it turns out to be (minutes transcribed, pages read, images made) begins with the most it can cost and completes with what it measured:
const run = await executions.begin({
workspaceId: workspace.id,
capability: "audio.transcribe",
price: fromMajor(10, "USD"), // ten minutes at most, held while it runs
});
if (!run.allowed) return refuse(run.code);
try {
const transcript = await transcribe(file);
// 3.4 minutes: charges 3.40 and releases the other 6.60
await run.complete({ charge: fromMajor(transcript.minutes, "USD") });
return transcript;
} catch (error) {
await run.fail({ error });
throw error;
}charge is a positive whole number of micros in the price’s currency and no
more than the price. One that is not throws before anything is charged and
leaves the run open, so it can still complete with a charge that stands; it is
never trimmed to fit. Work that came to nothing ends with fail. A run begun
with a providerCost can name the measured one the same way,
complete({ charge, providerCost }), no more than it began with. The execution
row then carries the measured charge as its price and keeps what was held in
reserved_micros, so reconcile() settles a run that died mid-settlement at
what it measured. runWithExecution and streamWithExecution take the same
price, and a measure(result) that returns the charge.
executions is the one lib/intelligo.ts exports (createBillingExecutions()). begin
holds exactly the price under the workspace lock and refuses when the plan
allowance, top-up and trial cannot cover it, so two concurrent runs against a
balance that fits one admit one. complete records a usage_records row of
type fixed_charge and the charge on the execution, which the usage page
shows; reconcile() settles a run stuck mid-settlement at the same price,
which the row keeps. A requestId names one attempt and is unique across the
deployment: a refused attempt’s id is spent, so a retry begins with a new one.
begin with a used id throws an ExecutionError (request_id_taken). With a
model as well, the price wins for the hold and the charge, and the usage row
keeps the model, tokens and provider cost, so the margin on fixed-price work
stays visible. The price must be a positive whole number of micros in the
deployment’s billing currency; one that is not throws a ChargeError
(invalid_amount, currency_mismatch) rather than refusing. estimateQuota(workspaceId, { amount }) answers the same question
without holding anything. estimateQuotaForModels(workspaceId, modelIds)
answers it for several models at once, keyed by model id, from one read of the
workspace’s balances: what a page asks when its picker offers a list. A model
with no registered price is refused in its own entry (unknown_model) and
fails no other.
Refunds and credits
A charge that should not have happened is a failed run, not a refund. For money that was rightly taken and is given back:
import { creditWorkspace, refundCharge } from "@intelligo-dev/billing";
await refundCharge(workspace.id, run.requestId, {
reason: "delivered late",
amount: fromMajor(0.5, "USD"), // optional: all of the charge by default
actorId: admin.id,
});
await creditWorkspace(workspace.id, fromMajor(5, "USD"), {
reason: "launch promotion",
requestId: `promo-2026-10:${workspace.id}`, // the idempotency key
actorId: admin.id,
});Both land on the top-up balance, whichever pool paid the charge, and are
recorded three ways: the ledger, a usage_records row of type credit with a
negative charge, and a billing.refund or billing.credit audit event with
the actor and the reason. Neither is an execution, so execution totals show
what ran, not what was given back. One refund per charge: a second call
replays the first, a partial refund included. Their keys are stored as
refund:<requestId> and credit:<requestId>, so neither meets a charge’s. The replay check runs under the
workspace lock, so concurrent calls with one key credit once. An amount the
ledger cannot take throws a ChargeError.
Subpaths that import neither Stripe nor server-only
| Subpath | What |
|---|---|
/plans |
Plan types and the per-product plan helpers |
/plan-registry |
Every register/clear pair: plans, features, upgrade copy, trials, rate limits |
/payment |
The payment provider contract and the mock provider |
/quota-types |
Quota result and admission types, no enforcement |
The four are safe to reach from a client bundle or an edge runtime. An
architecture test walks their imports so a Stripe or server-only import
cannot creep in.
What a deployment bills in
ensureBillingSettingsRow writes the currency, the USD rate and the margin on
first boot and never touches an existing row again. After that the row changes
through updateBillingSettings({ currency?, usdRateMicros?, marginBp? }), which
refuses a non-positive rate or margin and an invalid currency code, and drops
the cache. Changing the currency converts nothing: balances, trial grants and
the period’s allowance usage stay in the currency they were written in, and the
engine refuses a write in another one until those rows are migrated.
The plan allowance is money, so the 80% and 100% notices (in-app and email)
state an amount used out of an amount allowed. The billing period is the
calendar month in UTC (getCurrentPeriodStart, getCurrentPeriodEnd,
getCurrentPeriodKey), whatever zone the host runs in.
A cap on what the deployment spends
A workspace’s allowance bounds one customer. registerSpendCap({ dailyUsd, totalUsd }), called once from the composition root, bounds the deployment:
the most its providers may be paid per UTC day and in total, across every
workspace — a free tier, a public demo, a launch day.
Size it against the hold, not the bill. What is held for a run is its worst case: the prompt and the model’s full output limit, for every step a run that is not stepwise may take. A cap must be larger than the largest hold a run can ask for, or that run is refused on an empty day too; the refusal then names no time to retry and says the run is larger than the cap, and the error log carries both amounts. Because holds count until they settle, the cap also bounds how many runs can be in flight at once: the cap divided by one hold.
While a cap is registered, admission holds each run’s worst-case provider cost
(before the margin, in USD) against it, under one deployment-wide lock taken
after the workspace’s own, and refuses the run that would cross it with the
code spend_cap_reached; a daily cap’s refusal carries retryAt, the next UTC
midnight. Each step a stepwise run extends is held the same way. Settlement
adds what the run cost to deployment_spend_daily in the charge’s own
transaction, and the total counts what settled while a cap was registered.
Without a registered cap nothing is locked, refused or recorded; a
reservation still carries its provider estimate, so the holds open at the
moment a cap is registered count against it from then on.
getSpendSummary() returns the caps, today’s and the total spend, what is held
and the last days as aggregates only, so a status page can show them. A
fixed-price run that pays a provider per call names that cost with
executions.begin({ price, providerCost }); without it a fixed price is held
at nothing against the cap and its tokens count when it settles. The price
is in the deployment’s currency and the provider cost in US dollars, a whole
number of micros, zero or more: anything else is refused before the run is
held or recorded. A test that composes a fresh root turns the cap off again
with clearSpendCap().
A bounded wait for a workspace’s lock
Admission, a hold’s extension, settlement and a credit adjustment each take
one lock per workspace for the length of their transaction. By default a
transaction waits for it as long as it takes, so one that stalls while
holding it leaves every later request for that workspace waiting on a pooled
connection. setWorkspaceLockTimeout(3_000), called once from the composition
root, bounds the wait:
- admission and extension refuse with
workspace_busyand aretryAta second away, having decided nothing; the chat transport answers 429RATE_LIMITEDwithreasonCode: "workspace_busy"andRetry-After; - settlement, whose charge is owed, waits again, three times in all, before
the timeout becomes its error and the execution is left for
reconcile; - a credit adjustment or reversal throws Postgres’
55P03to its caller.
It is off unless set. Choose a value above a settlement’s usual length and well under the route’s time limit, and watch the refusals before relying on it.
Rate limits and the payment provider
checkRateLimit(workspaceId, planSlug, endpoint?) counts in the "chat" bucket
unless the caller names another; a route that should not spend chat’s allowance
passes its own name. getWorkspacePlanSlug(workspaceId) is the plan to pass:
the slug of the plan in force, read from the subscription’s own row and
nothing else, and "free" for a workspace with no subscription or one that no
longer entitles.
A plan’s limits are kept in a column an operator can edit. A row whose limits
are not an object of numbers is reported as a PlanLimitsError naming the
plan to fix (isPlanLimitsError(error)), by whichever limit check read it. checkRateLimit(subject, { limit, windowMs, endpoint })
counts any subject — a workspace, or a hashed IP for requests nobody signed in
to make — against its own limit per window of whole seconds, a day included:
const daily = await checkRateLimit(`ip:${sha256(ip)}`, {
endpoint: "export",
limit: 3,
windowMs: 86_400_000,
});Windows are fixed and aligned to the Unix epoch in UTC — a day resets at 00:00
UTC. planSlug gives a plan’s per-minute rate and goes with the default
one-minute window only.
getPaymentProvider() resolves PAYMENT_MODE, which defaults to mock.
Outside production the in-memory mock needs no registration; in production it
is refused, and any other mode must be registered from the composition root.
Local payments
A QR-and-poll payment (QPay, PIX, UPI…) goes through two calls.
openLocalInvoice({ workspaceId, userId, reference, price, grant }) issues the
invoice through the provider and records it in payments at the price the
server decided, with the grant it buys — { plan: slug, days? } or
{ credits: Money }. settleLocalInvoice({ invoiceId, workspaceId, fulfil })
asks the provider that issued it; when it is paid, the row is marked fulfilled
and its stored grant is applied in the same transaction, so concurrent polls
grant once and a change to the offer after the invoice was opened changes
nothing it buys. fulfil decides the grant only for an invoice opened
without one. A plan with days lapses that long after it is granted; bought
again before then, the days add to the running grant. An invoice of another
workspace is payment_not_found.
A buyer who pays in their bank’s app and closes the tab is not left
unfulfilled: settlePendingLocalInvoices({ fulfil }) settles every
unfulfilled invoice opened at least a minute ago (which the tab may still be
polling) against the workspace that recorded it, until it is paid or past its
expiry, and settlePendingLocalInvoices({ invoiceId, fulfil }) settles the
one a provider’s callback names. An invoice still unpaid
LOCAL_INVOICE_EXPIRY_GRACE_MS after the expiresAt its provider returned is
cancelled at the provider (cancelPayment) and marked failed. Both ask the
provider; a callback’s body is never taken as payment.
A provider is registered from the composition root with
registerPaymentProvider(mode, { currency, createPayment, checkPayment, cancelPayment, invoiceIdFromCallback?, callbackResponse? }) from /payment.
openLocalInvoice records the invoice before it asks the provider and passes
the row’s id as paymentId: use it as the provider’s merchant-side invoice
number or in the callback URL. A provider whose callbacks name an invoice
implements invoiceIdFromCallback(request), which may return that
paymentId in place of the provider’s own id, and one that expects a
particular answer (a status, a fixed body) implements
callbackResponse({ settled }).
currency is the ISO 4217 code the provider charges in. createPayment
takes bare minor units, as toMinor gives them: a zero-decimal currency such
as MNT or JPY arrives in whole units. With currency set, openLocalInvoice
refuses a price in any other currency (currency_mismatch). Without it, such
a price is sent as that many of the provider’s own units, and registering the
provider in production logs a warning.
A plan is not sold through a local invoice to a workspace a live Stripe
subscription bills: openLocalInvoice refuses it (subscription_active),
since the subscription’s next webhook would put its own plan back.
The payment-poll registry item is the transport: it serves the sweep at
/api/payments/local/settle (a CRON_SECRET schedule) and the provider’s
callback at /api/webhooks/local-payment. creditBundleOffer(bundle) turns
one of the product’s credit bundles into the offer the lib/local-payment.ts
seam returns.
Granting a plan
grantPlan({ workspaceId, planSlug, reason, actorId?, endsAt? | days? }) puts
a workspace on a plan as a product decision (a reward, a partner deal) rather
than a payment. With endsAt or days the grant lapses, and
processExpiredPlanGrants() — a step of the maintenance route — returns it to
the free plan, audited as billing.plan_grant.expired. days counts from the
end of a running grant of the same plan, so renewals stack. On a workspace
Stripe bills, the plan moves but the period stays Stripe’s.
The webhook receiver
createStripeWebhookHandler verifies the signature before anything else,
writes the receipt before running handlers, claims the event with
UPDATE … WHERE processed_at IS NULL so two concurrent deliveries cannot both
run, and answers 500 on a handler error so Stripe retries. Answering 202 to an
error tells Stripe the event was handled and silently drops it.
Stripe does not deliver events in order. customer.subscription.updated reads
the subscription back from Stripe and writes what it holds now, so a retried
older update cannot undo a newer one. charge.refunded and
charge.dispute.created take the refunded or disputed share of a credit
purchase’s grant back out of the balance, never below zero, recorded as a
credit usage row and a billing.credit_reversed audit event; a purchase
refunded or disputed before it was granted is never granted.
Entry points
@intelligo-dev/billing@intelligo-dev/billing/plans@intelligo-dev/billing/payment@intelligo-dev/billing/plan-registry@intelligo-dev/billing/quota-types@intelligo-dev/billing/rate-limit@intelligo-dev/billing/quota