Skip to content

Credit balance and expiry

An organization's credit balance never includes credit whose expiry has passed. The expiring list on the balance shows what is left of each grant that will expire, soonest first. A grant written with an expiresAt in the past is refused. This page states each of those rules exactly, as the server answers them. Last verified against hadron-server b02bc3bc on 2026-11-25.

For how credit, the ledger and expiry fit together, and why expiry is a new entry rather than an edit, see How credit expires.

Proposed

The two rules this page rests on, cor:dmo:170:10 (free credit expires and is settled by a new entry) and cor:dmo:170:11 (spend draws on the soonest-expiring credit first), are Decided. Some of their edge cases are still Proposed, awaiting confirmation. The main one in :10: that the balance already leaves out an expired grant before it is settled, and that a balance read settles first. Each line that rests on one is marked Proposed; the server builds them and they describe what it does today, but they may change. A line with no marker states a Decided rule or built behaviour. The refusal for a past expiresAt is built behaviour and is in no spec.

Who can do what

Action Operation Who
Read the balance creditBalance An organization admin (or a platform admin).
Read the ledger creditLedgerEntries An organization admin (or a platform admin).
Grant credit, with or without an expiry grantCredits A platform admin or the BILLING platform role.

Money is in micro-cents, written as base-10 integer strings. 1 USD is 100,000,000 micro-cents.

The balance

creditBalance(orgRef) returns:

Field Type Meaning
organizationId ID! The organization.
balanceMicroCents String! The balance. Never includes credit whose expiry has passed (Proposed until settled; see below).
expiring [ExpiringCredit!]! What is left of each unexpired grant that expires, soonest first. Empty when there is none.

Each ExpiringCredit is:

Field Type Meaning
amountMicroCents String! What is left of the grant. Always more than zero.
expiresAt DateTime! When what is left expires and stops counting in the balance.
  • The server works expiring out from the ledger. A client never replays the ledger itself.
  • A grant with nothing left is not listed. A grant that never expires is not listed either: expiring is only credit that will run out.
  • The parts can add up to more than the balance. That happens when credit that never expires is below zero (see Overdraft).
  • Order. Grants that expire at the same moment are ordered by expiry, then by when the grant was written, then by its id, so every reader lists the same grants in the same order. Proposed (cor:dmo:170:11).
query {
  creditBalance(orgRef: "hrn:org:acme.com") {
    balanceMicroCents
    expiring { amountMicroCents expiresAt }
  }
}

Does reading the balance write anything?

It can (Proposed, cor:dmo:170:10). A creditBalance read first settles the organization's expired grants (see Settlement), and only then reads the sum. So a read can write ledger entries, once for each grant that has expired and has not yet been settled.

  • An impersonated session writes nothing. It receives the same figure, worked out and stored nowhere. Proposed (cor:dmo:170:10).
  • If settling fails, the server logs one line and the read still answers, with the figure it worked out. The hourly job tries again.
  • The answer is the same either way. Proposed. Before an expired grant is settled, the balance already leaves out what is left of it.

Settlement

When a grant with an expiry passes it, whatever is left of it is removed by one settling entry. The grant itself stays on the ledger: the ledger is append-only, and expiry never edits or deletes a grant.

  • The entry is an adjustment whose source type is grant-expiry and whose source id names the grant it settles. That source type is written only by the server: grantCredits and adjustCredits refuse it, as they refuse usage-event, period-grant and cost-correction.
  • Once. A grant is settled once. Settling again writes nothing, and a grant that was fully used is still settled once, by an entry of zero, so it is never examined again. That entry is on the ledger like any other.
  • When. The entry is not written at the instant of expiry. It is written once the expiry is at least 60 seconds past, by the next creditBalance read for that organization or by an hourly job, whichever comes first. The 60 seconds is how the server behaves today, not a rule.
  • The balance does not wait. Proposed. From the instant of expiry the balance excludes what is left of the grant, whether or not its settling entry exists yet.
  • The hourly job can be switched off by the operator of a self-hosted server (DISABLE_CREDIT_EXPIRY_SWEEP=true). Reads of creditBalance settle the organization at hand either way, and every read already answers the balance without the expired credit.

Whether credit expires is decided by the grant. A grant written with an expiresAt expires. A grant written without one never does. Credit an owner bought does not expire; free credit does (cor:dmo:170:10). Time never reduces a grant that has no expiry.

Granting with an expiry

grantCredits takes an optional expiresAt. An expiresAt at or before now is refused:

Code BAD_USER_INPUT
Message expiresAt must be later than now.
Clock The database's, not the caller's.
Effect Nothing is written.

A grant that was already expired would be settled at once for its full amount, so it is refused instead. A value that is not a date is refused as BAD_USER_INPUT with expiresAt is not a valid date. Leave expiresAt out for credit that never expires.

mutation {
  grantCredits(input: {
    orgRef: "hrn:org:acme.com"
    amountMicroCents: "500000000"
    expiresAt: "2027-01-31T00:00:00Z"
    sourceType: "promo"
    sourceId: "welcome-2026-12"
  }) { id amountMicroCents expiresAt }
}

sourceType and sourceId are both required (BAD_USER_INPUT when either is missing or blank), and the pair is idempotent: a second call with the same pair, organization, amount and entry kind (here, a grant) returns the existing grant instead of crediting twice. A replay is not compared on expiresAt: the stored grant keeps its stored expiry, and a corrected expiry needs an adjustment, not a second grant. Any other entry on that pair is IDEMPOTENCY_CONFLICT.

Which credit spend uses first

A debit draws on the grant that expires soonest first, and on credit that never expires last (cor:dmo:170:11). Free credit is therefore used before purchased credit instead of being left to expire.

Example. A grant of 100 expires at month end, and the organization also holds 500 that never expires. A debit of 30 leaves 70 of the grant and all 500. At expiry the 70 is removed.

  • A debit never draws on expired credit. A debit written after a grant's expiry, and before its settlement, uses only grants whose expiry was later than the moment the debit was written. Proposed.
  • Other adjustments. A refund, a cost correction or a manual correction, of either sign, applies to credit that never expires and never to an expiring grant. Proposed.

Overdraft

Only credit that never expires can go below zero. An expiring grant cannot. Spend beyond all credit leaves a negative amount on the credit that never expires.

Command line

hadron billing balance <org> and hadron billing grant --expires-at are in the CLI reference, written against hadron-cli v0.61.0. billing balance prints the balance on the first line, which leaves out expired credit, then one line per expiring grant with something left, soonest first. Those lines are parts of the balance, not added to it. With --json the same list is expiring, an array of {amountMicroCents, expiresAt} ([] when nothing expires). The --expires-at rule is enforced by the server, so it applies whichever CLI version sends the grant. From v0.61.0 the help says so too; the help of earlier releases is out of date on this point.

Source

Checked against hadron-server at the merge of #5092 (edc54527) and its specs cor:dmo:170:03, :08, :10 and :11. The field descriptions come from the generated GraphQL reference.