Skip to content

How credit expires

Free credit has a date after which it stops counting; credit an organization bought does not. The balance you read never includes credit that has passed its date, and you can see what is about to expire. This page explains how that works and why it was built this way.

Some of the behaviour below is still Proposed in the platform specs: that the balance leaves out expired credit before it is settled, that a read in an impersonated session stores nothing, and that spend never draws on credit that had already expired. The server does all of this today; it may change. The reference marks each such line.

The ledger is a list of entries

An organization's credit is not a single number the server keeps up to date. It is a list of entries, and the balance is what they add up to. A grant adds credit. A debit is what an AI call costs. An adjustment corrects the list, for example a refund. Entries are only ever added. Nothing is edited or deleted, so the balance before and after any event can be explained from the entries.

A grant either expires or it does not

Whether credit expires is decided by the grant that created it. A grant written with an expiry date runs out on that date. A grant written without one never does, and time never reduces it. Free credit is given with a date. Credit the organization bought is not.

Why expiry is a new entry, not an edit

When a grant passes its date, the server does not change the grant. It adds one entry that removes whatever is left of it, and the grant stays on the list. An unused free grant of 100 therefore shows as the original +100 and, after its date, one adjustment of −100.

Editing the grant would break the rule that the ledger is append-only, and afterwards nobody could say what the balance had been the day before. A new entry keeps both balances explainable. It is also why a grant is settled only once: running the settling step again finds the entry already there and writes nothing.

Why the balance does not wait for the entry

The settling entry is written a little after the expiry (the server waits about a minute, and an hourly job or the next balance read does the writing). If the balance waited for it, you could read a figure that still counted credit you can no longer spend.

So the balance leaves out the expired remainder from the instant of expiry, and the entry catches the stored list up later. The figure is the same before and after. This is also why reading a balance can write: the read settles first so the stored entries match what it reports, except in an impersonated session, which is read-only and works the figure out without storing it.

Why spend uses the soonest-expiring credit first

Spend uses the grant that expires soonest, and credit that never expires last (the exact order, including ties, is in the reference). Otherwise free credit would sit unused until it expired while purchased credit paid for the work. With a free grant of 100 expiring at month end and 500 of purchased credit, a debit of 30 comes out of the free grant. At month end the 70 left is removed, and the 500 is untouched.

Two consequences follow. Only credit that never expires can go below zero: if spend outruns all credit, the shortfall shows as a negative amount on the credit that never expires, and an expiring grant is never negative. And the expiring amounts can add up to more than the balance, because the balance also carries that negative.

Why a past date is refused

A grant whose date is already past would be settled at once for its full amount: it would arrive and vanish in the same moment. The server refuses it (BAD_USER_INPUT, expiresAt must be later than now.) and writes nothing, judging "now" on its own clock so a caller's clock cannot move the boundary.

Where to look next