Billing
Monetize AI endpoints with a prepaid credit balance: the deduct-on-success ordering, an atomic single-UPDATE spend that cannot go negative under concurrency, a consistent 401/402/429 error envelope, rate limiting, an append-only ledger, and an owner bypass.
3 files
Description
Monetize AI endpoints with a prepaid credit balance: the deduct-on-success ordering, an atomic single-UPDATE spend that cannot go negative under concurrency, a consistent 401/402/429 error envelope, rate limiting, an append-only ledger, and an owner bypass.
A prepaid credit balance is the clean way to monetize expensive, variable-cost AI actions: the customer buys credits once, every action spends from one pool, and the same balance works across every endpoint and every product. This skill is the server-side pattern for doing that safely - the ordering, the atomicity, and the response contract that make it robust under real traffic.
It generalizes the deduct-on-success + envelope + rate-limit stack. It is about the pattern, not any one codebase.
Every metered request runs the same gauntlet, in this order:
401.429 with
retryAfterSeconds and a Retry-After header. Do this before doing work.402 with the current balance.reference/deduct-on-success.md.The load-bearing rule is step ordering: deduct after the work succeeds. Charge
first and every model timeout, safety refusal, or 500 becomes a silent theft you
will refund by hand. See reference/deduct-on-success.md.
reference/deduct-on-success.md.{ error, message } once. See reference/envelope-and-limits.md.reference/deduct-on-success.md - the ordering, the atomic single-UPDATE spend, the ledger, refunds, and the owner bypass.reference/envelope-and-limits.md - the 401/402/429/400/500 envelope, rate limiting, and the shared cost model.Related
Added 2026-07-01. Back to the Skill Library.

New tutorials, open-source projects, and deep dives on coding agents - delivered weekly.