Bill teams through Stripe
Billing charges a team its plan’s monthly base fee, and the computer time it uses past the unit-hours the plan includes. The usage ledger, compute_usage, is the source of truth. Each hour the server works out every billed team’s overage from the ledger and sends it to a Stripe Billing Meter. Stripe only invoices it. Billing is off until the server has a Stripe key, and it runs on test keys only.
How it counts
Section titled “How it counts”- A unit is 1 vCPU and 2 GiB for an hour. A team’s plan has a monthly base fee that includes some unit-hours each calendar month in UTC. The unit-hours past them are overage, charged at the plan’s overage price.
- A billed team has one row in
billing_meter_reportsfor each closed hour, written 5 minutes after the hour ends. A lease that crosses an hour or a month counts in each by the seconds it ran there. - Each plan has its own meter, whose event name is
agents_overage_<plan key>with each-in the key written as_. It also has a monthly price for its base fee,agents_plan_<plan key>_base, and a metered price for its overage,agents_plan_<plan key>_overage. An account’s subscription holds each plan’s base price once for every team on it, and each plan’s overage price. - Each hour with overage goes to the meter of the team’s plan as one event, dated inside its hour, under the identifier
agents-<team id>-<hour>. A send that failed goes again on the next pass, every 5 minutes, with the same identifier and value, so Stripe counts it once. - Each pass first brings every subscription to the plans its teams are on. A plan no team is on any longer keeps its prices on the subscription, its base fee at 0, so overage already sent to it is still billed. A change takes effect from the next invoice, with no proration.
- The overage cap a team’s admin sets counts the month’s unit-hours the same way, so a lease that crosses the month’s end counts in each month by its share.
- A team in an organization is billed on the organization’s account. Any other team has its own.
- Hours from the one the account opened in are billed.
Set the plans
Section titled “Set the plans”You set every price. A team’s admin chooses among the plans you offer and sets the team’s overage cap, and never a price. The plan commands change only the database and need no Stripe key. Run them from a laptop with node ops/aws-deploy/admin.ts billing <command>, which runs server/billing/main.ts on the server host over SSM, as the API’s user with its .env. It needs the same setup as the other admin commands, which Add teams and people lists.
node ops/aws-deploy/admin.ts billing plan team name=Team units=8 included=400 base-cents=<cents a month> overage-cents=<cents a unit-hour> cap=100 warm-pool=0 warm-light-pool=0 bucket-sleep-minutes=1440 idle-minutes=10node ops/aws-deploy/admin.ts billing offer teamnode ops/aws-deploy/admin.ts billing plansplan <key>with every term makes a plan, which no admin sees untiloffer <key>.retire <key>stops offering it, and the teams on it keep it until their admins choose another.- A plan’s name, units, default cap, warm pools and idle minutes may change later with
plan <key> name=….warm-light-poolkeeps light computers warm for teams whose chats are light, and a plan that leaves it out keeps none.bucket-sleep-minutesis how long a person’s own computer sleeps before its home moves to the object store, on a team whose admin turned that on, a day when left out. Its key, base fee, included unit-hours and overage price can’t, since its Stripe prices can’t. To change a price, make a new plan, offer it and retire the old one. - No plan comes priced. Teams that had a plan before the catalog were moved onto plans named
kept-…, offered to no one, with their old limits and overage price and no base fee.
Turn it on
Section titled “Turn it on”-
In Stripe, in test mode, make a restricted key that can write customers, products, prices, subscriptions, invoices, billing meters and meter events. Those are the calls the server makes. The Dashboard’s names for those permissions were not checked against this list.
-
Put the key in SSM as the SecureString
/agents/stripe/test-key, from a new file in a folder only you can open, so the key is in no shell history or process list.mktemp -dmakes the folder, asops/aws-deploy/forge.tsdoes for the forge’s tokens.Terminal window dir=$(mktemp -d)umask 077; pbpaste > "$dir/value"aws ssm put-parameter --profile gomoe --region us-east-1 --name /agents/stripe/test-key --type SecureString --overwrite --value "file://$dir/value"rm -rf "$dir"The server refuses a live key at start unless
AGENTS_STRIPE_LIVE=allow, whichhost.tsdoes not write. -
In Stripe, add a webhook endpoint at
https://gomoe.dev/v1/billing/stripe-webhookfor the eventscustomer.subscription.created,customer.subscription.updated,customer.subscription.deletedandinvoice.finalized, with the snapshot payload. Put its signing secret at/agents/stripe/webhook-secretthe same way. The web ACL’s country rule does not apply to this path, since Stripe may send from anywhere. -
Deploy, so
host.tswritesSTRIPE_SECRET_KEYandSTRIPE_WEBHOOK_SECRETinto the server’s.envand restarts the API. See Deploy a change. -
Make each plan’s meter and prices, and again whenever you add a plan.
Terminal window node ops/aws-deploy/admin.ts billing setup -
Open an account for an organization or a team that is in none, by its key.
Terminal window node ops/aws-deploy/admin.ts billing open <org or team key>It makes a Stripe customer, with the address of whoever made the organization or team, or, for a team with no recorded maker, its earliest admin’s, and a subscription to the base and overage prices of its teams’ plans, invoiced on the 1st of each month and sent for payment in 30 days. At least one of its teams must be on a plan.
sync <key>brings a subscription to its teams’ plans at once rather than at the next pass.
Read it
Section titled “Read it”- A team admin reads the Billing section of the People page,
GET /v1/team/billingorbin/agentsctl team billing. It shows the mode, the account and its subscription’s status, this month’s hours from the ledger and the overage sent, the hours Stripe has not taken yet, and the last reconciliation. A member does not see it. - An agent reads the same with
read_billing, on a team admin’s turn only.
Reconcile
Section titled “Reconcile”A reconciliation compares the quantity of an invoice’s overage lines, the base fees aside, with each team’s overage in the ledger over the lines’ period, and with what the report sent. Any difference beyond rounding is kept as one that did not match.
- Every
invoice.finalizedwebhook reconciles that invoice. billing reconcile <key>compares the preview of the next invoice, up to the last closed hour. Add an invoice id to compare that invoice.billing invoice <key>runs a report pass, waits until Stripe’s meter holds what was sent, moves the subscription’s billing cycle to now, which makes Stripe invoice the period so far, finalizes that invoice and reconciles it. This is the test-mode proof that an invoice matches the ledger.billing reportruns one report pass now.
Each runs through node ops/aws-deploy/admin.ts billing. On the developer’s Mac, bin/agentsctl billing <plans|plan|offer|retire|setup|open|sync|report|reconcile|invoice> runs the same commands on an instance started with STRIPE_SECRET_KEY in agentsctl’s environment.
Not built
Section titled “Not built”- Live mode is refused by default, since the Stripe account is a placeholder.
- No payment method is taken. Invoices are sent, not charged.
- Base fees are billed but not reconciled.
- A subscription holds at most 20 prices, so an account can use 10 plans in its life.
- Hours not taken by Stripe stay unsent until a pass sends them. One older than 35 days can’t be sent, and shows as unsent.
- A difference is reported, not corrected.