Claude Templates / S-tier · Foundation

Foundation

The Credit Metering Playbook

A credit ledger, atomic spend RPC, refund-on-failure, and cost analytics that hold up.

CREDIT-METERING-PLAYBOOK.md · 13 sections · 2,547 words · 20 KB · markdown

By Moe Ameen, founder of BILT · Published · Updated

The Credit Metering Playbook gives Claude Code a usage-billing layer for apps that charge per output, such as a render or a generation. It builds an admin pricing matrix (provider cost + infra cost + margin = credits charged), a transaction ledger, an atomic spend_render_credit Postgres function, automatic refunds when a provider call fails, cost-analytics views and a cron that catches orphaned charges.

Key takeaways

  • Price each output from real provider cost plus infra cost plus margin.
  • Spending runs in one atomic Postgres function, safe under concurrency.
  • Failed provider calls refund credits automatically and idempotently.
  • A daily quota gate is optional and separate from the balance.

If your app charges per output (a render, a generation, an enrichment), this is the metering layer. An append-only ledger, an atomic spend RPC that can't double-charge under concurrency, automatic refunds when a provider call fails, and the admin views that tell you whether your pricing is actually profitable.

What does the Credit Metering Playbook build?

It builds the ledger and spend path that turns provider costs into credits: credit_pricing, credit_transactions and credit_refunds tables, server helpers to spend and refund, and service-role-only cost views.

A safety-net cron audits charges that never got linked to a finished job.

When does an app need credit metering?

You need it when each action has a real variable cost, such as an AI render, a video, or an enrichment lookup. Run it in week 3, after Stripe Billing.

Flat-rate apps with no per-use cost can skip it entirely.

What inputs does it ask for?

It asks for your billable output types with their real unit costs from provider invoices, a margin multiplier, any beta or founding-tier balance rules and a daily cap if you want one.

Pull actual invoice lines before you start. The pricing matrix is only as accurate as those numbers.

What's inside

What does the playbook give you?

When to use it

Week 3, only if the app charges per-output.

Straight from the file

What does the writing look like?

Seed credit_pricing with on conflict (format_key) do update set that refreshes metadata only — never blow away admin cost overrides:

Table of contents

Which sections does the file cover?

  1. 0. Use this spec
  2. 1. Architecture
  3. 2. Schema
  4. 3. Pricing table seed
  5. 4. Atomic spend RPC
  6. 5. Server spend helper + pre-spend validation
  7. 6. Refund-on-failure (worker pattern)
  8. 7. Beta auto-refill + founding tier
  9. 8. Daily quota gate (optional)
  10. 9. Cost analytics views
  11. 10. Audit-orphan-charges cron (safety net)
  12. 11. Common pitfalls
  13. 12. Kickoff prompt

How do you run it?

Drop CREDIT-METERING-PLAYBOOK.md into your repo, then tell Claude Code: “Read CREDIT-METERING-PLAYBOOK.md and implement it in this codebase.” It's written for an agent to execute end to end. Search the file for angle-bracket placeholders and swap in your own project names, domains, and keys before the first run.

↓ Download the playbook

Who is this for, and who should skip it?

Best for

  • AI apps paying per call to model or media providers
  • Products selling credit packs on top of subscriptions
  • Founders who need to see margin per output type

Not ideal for

  • Flat-fee SaaS with no variable cost per action
  • Apps billing usage through Stripe's metered billing instead
  • Products with no Stripe Billing setup yet
  • Builders who won't adapt paths: the file points to a reference app you won't have

What else do people ask?

What is in the Credit Metering Playbook download?

The Credit Metering Playbook is a markdown file of about 2,500 words. It has SQL for the credits, credit_transactions, credit_refunds and credit_pricing tables, the full spend_render_credit function, a pricing seed, a daily quota function, three cost-analytics views, a worker refund pattern and a kickoff prompt.

Where are credits charged, and where are they refunded?

The Credit Metering Playbook charges credits in the dispatch route before the worker is triggered and refunds them inside the worker when the job fails. Refunding in the dispatch route is ruled out, because retries would refund twice.

Why does the spend function take an advisory lock?

The Credit Metering Playbook runs the balance check, debit and ledger insert in one transaction behind a per-user advisory lock. Without it, parallel renders can each pass the balance check against the same starting balance and push the account negative.

Can I skip the beta tier, founding tier or daily quota?

Yes. The Credit Metering Playbook's kickoff prompt accepts "skip" for the beta and founding-tier settings and "none" for the daily quota cap, and the quota gate is marked optional.

Can I seed credit prices from a provider's pricing page?

The Credit Metering Playbook says to calibrate costs against real invoices, not vendor spec sheets, and to audit them quarterly. In its reference app, a seed that assumed $0.15 to $0.56 per HeyGen render left those formats under-priced 2.5 to 9 times for two weeks, because invoices showed $1.31 per render.

How do we know this?

This page is based on the playbook file itself, CREDIT-METERING-PLAYBOOK.md, which Moe Ameen wrote from the Kompozy codebase (called BKE in the file). We read the file on November 4, 2026; the facts here come from it, not from independent testing in other codebases.

More foundation playbooks

← All 28 Claude templates