Billing
How a team pays, and the one thing billing tells the rest of the product. Eight slices in the billing/ group, and between them and everything else there is a single field: Team.planId.
That narrowness is the design. Nothing outside billing/ asks who the provider is, what a subscription looks like on the wire, or whether an invoice was captured — it reads a plan off the team and gets on with it.

The cabinet separates payment history from team settings (AGNT2-405). Tokens and quota remain customer-facing; vendor/model spend belongs to the admin panel (AGNT2-409). The image above is a historical early-access screenshot. AGNT2-347 added invoice, payment and method-reference records. Invoices are created on completed-payment intake; reconciliation reports differences without silently correcting either side. Automatic usage-based charging is not established.
The eight slices
| slice | what it owns |
|---|---|
billing/product | the catalogue: one sellable thing — a name, a price, the planId a subscriber gets |
billing/subscription | our record of a team's subscription, and the Team.planId write |
billing/paymentProvider | the provider, behind a gateway — PayPal today |
billing/webhook | intake for the provider's events |
billing/usage | what a team has actually consumed, and against which quota |
billing/invoice | subscription period and charge purpose |
billing/payment | provider-reported payments and read-only reconciliation |
billing/paymentMethod | provider reference, never card data |
The provider is behind a gate
IPaymentProviderGateway lives in domain/; PayPal lives in data/. Consumers never see the SDK, the wire format, or the provider's vocabulary — the same shape infra/storage and infra/vector have, for the same reason.
1.x had the opposite: a slice called paypal, named by every consumer, whose spellings (APPROVAL_PENDING, BILLING.SUBSCRIPTION.ACTIVATED) reached into services that had no business knowing them.
The subscription flow
POST /v1/billing/subscriptions— our row is written first, because its id is the reference the provider carries and echoes back on every event. Then the provider is asked for a subscription, and the payer is handed an approval link.- The payer approves at the provider. Nothing is charged before that.
- The provider delivers
ACTIVATED. The webhook slice verifies it, claims it against the replay ledger, and applies it — which activates the subscription and writesTeam.planId. POST /v1/billing/subscriptions/:id/cancelstops future charges;CANCELLEDarrives and the plan comes off.
One open subscription per team. pending, active and suspended all count as open, so a payer who reloads a half-finished checkout is handed the same approval link rather than a second subscription to pay for.
Two rules that exist because 1.x broke them
Nothing here computes a date. currentPeriodEnd is read from the provider. 1.x added a month on every capture event, which is why a redelivered event was worth a free month.
Every write is an assignment, never an increment, so handling an event twice is harmless — as a second line of defence behind the replay ledger, not instead of it.
Why the replay ledger is worth its own table
The tempting objection is that applying "activated" twice changes nothing. True — and that is not the case that costs money. The case that costs money is out of order: activated, then cancelled, then the activated event redelivered. Every write involved is individually idempotent, and the result is a team on a paid plan that has stopped paying.
The order is the design:
verify → claim → handle → mark processed- Verify first, parse second — an unverified body is attacker input.
- Claim before handling — a claim written afterwards leaves a window where a second delivery arrives mid-handling, finds no claim, and handles the event again.
- Release on failure — a handler that threw did not act on the event, so the claim must go, or the provider's retry (the thing that would have fixed it) is discarded as a duplicate.
The claim is an INSERT against a unique key rather than a read followed by a write: two concurrent deliveries both read nothing and both proceed, so the database decides, once.
Usage
GET /v1/usage/summary what has been consumed, broken down
GET /v1/usage/total the single number
GET /v1/usage/quota what is left against the plan
GET /v1/usage/agents the same, per agentKnown gaps, stated rather than hidden
An abandoned checkout can be approved later. The provider has no cancel operation for an unapproved subscription — measured live, /cancel answers 404 RESOURCE_NOT_FOUND — so cancelling a pending subscription closes our row and leaves the approval link to expire on the provider's own schedule. Someone who abandoned a checkout, subscribed to something else, then went back and approved the first link before it expired would end up with two live subscriptions.
A real spend cap does not exist. Usage is measured and shown; nothing stops a team at a ceiling.
Where the code is
api/src/slices/billing/ — one directory per slice above. See also Slice breakdown and Resources.
Partner identity and payer specification
The proposed v1 design supports both organization-paid and team-paid teams. Read identity and user journeys, provisioning and payer contracts, and migration and self-audit. Partner customer access uses organization-scoped actors; it is separate from the existing guest embed, and linking never grants a partner access to direct accounts.
Team plans and approaching the limit
| Plan | Tokens per UTC calendar month | Conversations per minute | Highest model level |
|---|---|---|---|
| Free | 2,000,000 | 20 | Smart |
| Pro | 100,000,000 | 120 | Genius |
| Business | 500,000,000 | 300 | Genius |
These are shipped defaults. Platform settings plan.tokensPerPeriod, plan.turnsPerMinute and plan.maxLlmTier can override them. Billing shows the same effective limits used by admission. A model ceiling permits that level; it does not force every answer to use it. Input, output and cache tokens count.
In Billing, owners and admins can compare higher allowances and choose Upgrade. Business starts as a contact-only offer: its price is agreed with the owner, no zero-price checkout is offered, and an administrator applies the plan through the existing team plan change action. The catalogue's internal zero amount is a placeholder, not a published price; contactOnly is the checkout gate. Once pricing is agreed, an operator can set the real amount and clear that flag to enable the existing subscription checkout. An existing active subscription still needs operator coordination; this change does not introduce provider-side subscription replacement or proration.
At 80% and 95%, the app displays a warning and each current team owner receives a localized email linking to Billing, once per threshold per team per UTC month. The 95% message is urgent. Notices remain visible while usage is high; reopening a page does not send another email. A background sweep checks 100 teams every 30 seconds, independently of page visits. If usage crosses both thresholds between checks, both receipts are delivered. Organization-paid teams use their organization's budget and do not receive team-token warnings.
At 100%, new turns are refused with an upgrade suggestion. Upgrade to a plan with enough remaining allowance, or wait until the first day of the next UTC month. Upgrading does not reset usage or resend this month's warnings. Business owners who need more capacity can contact support. The quota cache may delay recognition of a changed plan by up to ten seconds.
Delivery recovery and rollout
UsageWarning keeps one receipt for (teamId, periodStart, threshold, userId). Receipts cascade when their team is deleted. The worker claims a receipt atomically before calling the configured mail transport. A confirmed send becomes sent. An error becomes unknown; a crash during a send leaves sending. These ambiguous outcomes are not automatically resent, because SMTP may already have accepted the message. Operators should check mail provider logs before marking a receipt sent or returning a confirmed unsent receipt to pending. Logs identify the team, threshold and period without printing email content or credentials. With mail disabled, no receipt is claimed; enabling it permits the next sweep to send. External delivery is not an exactly-once guarantee.
The additive migrations seed Business only when the business product key is absent and preserve operator products and prices. Existing team plans and platform overrides are unchanged. After rollout, the owner should identify teams using the temporary Pro 500M override, move those teams to Business, and restore Pro to 100M in platform settings. This requires an explicit operator choice; a migration must not guess which customers to move.