Most model usage in Summa has an obvious owner. Somebody asks, and the tokens come out of that person’s daily and monthly allowance. In a shared room the rule is the same: the sender pays, whoever else reads the answer.
Some work has no sender. When a Teams meeting ends and its transcript becomes available, Summa extracts the decisions and action items into one shared record that every attendee reads. Nobody typed a request for it at that moment. The workspace turned the feature on, a poller found the transcript, and a worker did the rest.
Meeting extraction is where the question first came up, and it is where this essay stays. The same calls now also pay for background passes in Summa Work and for memory consolidation. All of them raise the same question: if nobody asked, who pays?
What goes wrong with the obvious approach
The first answer most people reach for is to charge the meeting organiser. They own the meeting, and Microsoft only releases the transcript to the organiser’s grant, so their account is already involved.
That goes wrong quickly. The organiser of a weekly all-hands would become the heaviest user in the company without ever asking the model anything. Their allowance would run out on work they never requested, and their own questions would start failing.
Charging whoever happens to be first adds a race: two people press “process” at once, and the bill lands on whoever won by a few milliseconds. Splitting it across attendees is no better. Guests and people who never open the record would carry a share of something they did not choose.
The honest description is that the workspace opted into this work. So the workspace pays, from a budget that is clearly not anybody’s personal allowance.
A third budget, kept apart from the other two
Every row in the usage ledger carries a cost_class: chargeable for a personal allowance, pooled for the company pool, and unbilled for work recorded only for cost visibility, such as conversation titles. Every allowance query filters to chargeable. A meeting extraction writes a pooled row with requested_by_user_id deliberately left empty, so it never shows up on anybody’s meter.
The cost class is a column, not inferred from an empty requester. That empty value already means something: the requester is set to null when a person is erased, so the accounting survives. Treating “no requester” as “pooled” would quietly move a departed employee’s spending into the company pool.
The separation runs both ways. Pool spend never touches a personal allowance, and personal spend never touches the pool. Consumer accounts have no pool at all, because a single-person workspace has no work without an owner.
Granted from seats, topped up by purchase
The pool has two sources, kept in two buckets.
The allowance comes from seats. Each seat tier contributes a fixed amount to the pool every month, and the grant is computed from the seats the workspace has purchased, not the ones assigned to people. That is what the customer pays for, and it stays stable while an admin moves people between tiers.
There is no monthly cron job. ensure_period_allowance tops the current month up to what the seat mix entitles. It runs when a seat change arrives from Stripe, before a job reserves capacity, and when an admin opens the pool page. Buying a seat mid-month adds capacity straight away, and running it twice grants once. A downgrade never takes back tokens already granted this month. That is fair, and it is the only behaviour that cannot pull a balance below zero under a job that already reserved against it.
Top-ups are bought outright by an admin. Each one is a one-off Stripe Checkout payment, separate from the seat subscription, and the tokens never expire.
The month is YYYY-MM in Europe/Amsterdam, the clock every other billing boundary uses, never UTC.
The balance is a sum, never a stored number
The pool is a table of signed rows, token_pool_entries. A grant is positive, a hold or a charge negative, and the balance is the sum. The reason is in the module: “A stored integer has to be correct after every crash, retry and duplicate webhook, and it never is.” A sum of rows is correct by construction, and every change is a row someone can read later.
The two buckets make expiry free. Allowance rows carry a period_key; top-up rows carry none. The balance counts allowance rows for the current month only and top-up rows always, so last month’s unused allowance simply falls out of the sum. There is no sweeper. A check constraint, (bucket = 'allowance') = (period_key IS NOT NULL), makes the database enforce the split rather than trusting every writer.
Postgres, not the chat reservation pattern
Chat already stops overspending by reserving each turn’s expected cost in Redis before admitting it. We did not reuse that, for three reasons.
- The concurrency is different. Chat reservations span two processes and an unbounded number of concurrent requests, which is why they live outside the database. Pool spend happens inside one leased worker job. Claiming that job uses
SKIP LOCKED, and a partial unique index allows only one extraction of a meeting in flight. The serialisation is already there. - Top-ups never expire. Every Redis key in the chat limiter is created with a time to live that ends at the next daily or monthly reset. A balance that never resets cannot share a counter with a window that does.
- Failing closed means different things. If Redis is unavailable, chat admission returns a 503. For an interactive turn that is right: the person sees an error and tries again. For a background worker it turns a short Redis outage into a failed job, and it uses up retries on a problem the job did not cause.
Reserve, settle, release
A pool charge happens in three steps.
Reserve. Once the worker has the transcript, it estimates the cost from the same windowing the extraction will use, and writes a negative hold row under a per-tenant advisory lock. A concurrent job sees the hold in the balance at once. The hold is keyed on the insight it is paying for, so a job picked up again after its lease expired refreshes its own hold instead of taking a second one.
Settle. On success, the hold is replaced by what the job actually cost, in the same transaction that marks the record ready and writes the usage row. A crash cannot bill without delivering, or deliver without billing. Spending takes the monthly allowance first and top-ups last, because the allowance is lost at month end and top-ups are money already paid.
Release. If extraction fails, the hold is dropped and nothing is charged. The model may have spent something before failing, but nothing measured it, and the code’s view is that charging a guess is worse than charging nothing. A worker that dies cannot release anything, so the recovery sweep releases every hold whose lease has passed. Without it, a crashed worker leaks capacity for good, and the workspace slowly stops processing meetings with no visible cause.
A job that has started may finish, even past its estimate. Stopping halfway saves nothing, because the tokens are spent, and it throws the answer away. The pool can end up below zero by whatever the jobs already running overshoot, and the next admission is refused until there is room again. Chat follows the same rule.
An empty pool is a 402, not a retry
The pool refills only when someone buys more or a new month starts. That shapes how an empty pool is reported.
When an organiser or admin presses “process”, the API checks that the pool has at least a fixed floor left. The real estimate needs the transcript, which is not fetched yet, so this check only avoids queueing a job that cannot be paid for. Below the floor, the response is a 402 with the code token_pool_exhausted, never a 429, which would promise that waiting helps. The client switches on the code and shows no reset date.
A separate daily limit on processed meetings does return a 429. It guards against runaway automation, such as a calendar sync that finds a backlog and would drain a month’s pool in an hour, and it clears by itself.
In the worker, an empty pool is final, not retryable. The record is marked meeting_pool_exhausted. Only a purchase changes the outcome, so a retry would only waste attempts.
Every event arrives twice
Webhooks are redelivered, leases expire, and two people press the same button. Each path has its own guard.
- Top-ups are keyed on the Checkout Session, not the webhook event. One session gives one grant, however many events mention it. That makes the webhook replay endpoint safe, though it skips replay protection on purpose.
- One partial unique index covers the ledger. It is unique on
(tenant_id, source, source_ref, bucket)wherever there is an external reference.bucketis included because one extraction that crosses from allowance to top-up writes two rows with the same reference. - Refunds and disputes reverse once. Both write a negative adjustment keyed on the charge. A refund followed by a dispute on the same charge removes the tokens a single time. Without this, a chargeback would leave the customer with both the money and the tokens.
- The usage row has its own latch. The usage ledger is unique on the run, but an extraction has no run. So the row is written in the transaction that sets
usage_recorded_at, and a re-run after a lease expires cannot bill twice. - A second “process” is free. If a finished record already exists with the same source checksums, a new request returns without calling the model.
What admins see
Admins with the billing capability get a pool page: this month’s allowance and what is left of it, the top-up balance, what jobs in flight hold, and when the allowance resets. Opening it runs the grant, so a seat bought a minute ago shows up at once.
Everyone else in a business workspace sees the pool’s used and total figures next to their own allowance. The pool pays for meetings they read, but only an admin can add to it. The admin usage chart draws personal and pool spend as separate series, and top-ups and refund reversals go into the audit log.
What it costs
- Nobody owns the bill. A pooled row names no person, so reports cannot say whose meetings cost the most. That is the point, and still a loss.
- Meetings can stop. When the pool is empty, meeting extraction stops until someone buys more or the month turns over. Personal chat keeps working.
- Estimates must be generous. Under-reserving lets a job overspend. Over-reserving can refuse a job the pool could have paid for.
- Failures are free, by choice. A failed extraction may have spent tokens that are never charged.
- Two spending systems. Chat reserves in Redis and the pool in Postgres. Anyone working on billing has to understand both.
What we would generalise
- Do not bill a person for work they did not ask for. Pick a budget that matches who actually decided the work should happen.
- Store the budget class on the usage row. Do not infer it from a missing requester.
- Derive balances from signed rows. A stored total will drift.
- Let expiry be a filter. If a balance resets, scope its rows to a period and let old ones fall out of the sum.
- Key idempotency on the thing that was bought, not on the message that told you about it.
- Match the failure mode to the caller. Fail closed for a person who can retry, and fail final for a background job that cannot change the outcome.
- Release what a dead worker held. A reservation without a sweep is a slow leak.