Skip to content
gethardcountPublic

About

Open-source alternative to Stripe Billing and Lago for AI products: price tokens by model, sell prepaid credits, see margin per customer. Self-hosted; Stripe only charges the card (and works out tax, if you turn on Stripe Tax).

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

8 stars

Watchers

1 watching

Forks

Repository files navigation

Hardcount

Prepaid usage billing for AI products, on your own servers.

CI Go License Release

For teams that resell model access to their own customers through an LLM gateway such as LiteLLM. You sell a customer credit, and they pay for it by card on a hosted page. Your gateway sends each call's tokens, Hardcount prices them per model, and your gateway reads what the customer has left. Each invoice is paid from the credit, and you see what each customer costs you. Your billing data stays in your own Postgres.

Free and open source (MIT): no paid edition, no fee on what you bill, and light enough to run yourself. Stripe charges the card at its normal fee. Why Hardcount →


What it looks like

From the demo, in test mode: a customer bought $50.00 of credit and used $5.73 of it. The invoice has one line per model and token type, and the credit pays it, which leaves $44.27. Your margin on it is 51.9% (Sonnet 67.1%, Haiku 20.0%), shown only to you.

Invoice HC-000002 in the dashboard, from Example AI Ltd to ACME Corp: input, output and cache_read lines for claude-sonnet-4.5 and input and output lines for claude-haiku-4.5, priced per 1M tokens. Subtotal $5.73, credits applied $5.73, amount due $0.00.

Quick start

Try Hardcount on your own computer. It runs in test mode, where no real money moves, and needs no Stripe account.

You need git and Docker 24 or newer with the Compose plugin. On Windows, run it inside WSL.

1. Start Hardcount

git clone --branch v0.11.0 https://github.com/gethardcount/hardcount.git
cd hardcount/deploy/compose
./quickstart.sh

You see: "Hardcount is running.", then your dashboard address, email, password and a test API key. The password is shown only once; the script also saves all four in deploy/compose/quickstart-credentials.json.

Open http://localhost:8080 and sign in. Hardcount opens in test mode, and it is empty.

2. Run the demo

./quickstart.sh demo

The demo sets up AI token prices, creates a customer and sells them $50.00 of prepaid credit. It sends token usage the way a LiteLLM proxy would, and shows the credit left as your gateway reads it, the invoice so far and the margin report shown above. Then it cancels the test customer's subscription, so Hardcount issues the invoice now and pays it from the credit.

You see: eleven steps, each ending in a green ✓, then links to the invoice and the customer. Each demo step, and where to click

3. Stop or remove it

./quickstart.sh stop keeps your data. ./quickstart.sh destroy deletes it, and cannot be undone. If port 8080 is taken, start with HARDCOUNT_PORT=9000 ./quickstart.sh.

Next: send your own usage and get a first invoice · connect Stripe and charge a test card · run it for real on one VM · change Hardcount itself


How it works

flowchart LR
    T["Top-up<br/>the customer pays by card"] -->|"credit"| B["Hardcount<br/>prices usage,<br/>keeps the balance"]
    A["Your gateway<br/>or LiteLLM"] -->|"usage events"| B
    B -->|"what is left"| A
    B -->|"period ends"| C["Invoice + PDF<br/>paid from credit"]
    C -.->|"credit does not cover it"| D["Stripe charges<br/>the saved card"]
Loading

You sell a customer credit with a top-up: an invoice for credit that they pay by card on its page, or that you charge to their saved card. The credit arrives with the payment. Sell prepaid credit, step by step

Your gateway tells Hardcount what each customer used. Once the customer and its prices exist, one usage event looks like this:

curl -X POST http://localhost:8080/v1/usage-events \
  -H "Authorization: Bearer $HARDCOUNT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"event_name": "tokens",
       "external_customer_id": "acme",
       "quantity": 1200,
       "dimensions": {"model": "claude-sonnet-4.5", "token_type": "input"},
       "idempotency_key": "req-42:input"}'

external_customer_id is your own ID for the customer. The dimensions pick the price: here, Sonnet 4.5 input tokens. If you send the same idempotency_key again, Hardcount counts the event once. Already run a LiteLLM proxy? It sends these events for you. Your first invoice, step by step


Why Hardcount

The gaps Hardcount is built for:

The gap What Hardcount does
Stripe Billing takes a cut: 0.7% of billing volume on its pay-as-you-go plan, and its basic usage billing can't price each model and token type on one meter. No Stripe Billing fee. Stripe still charges the card. One meter prices every model and token type.
Key parts sit behind a paid licence, such as automatic failed-payment retries and invoice previews in Lago. One free edition. MIT, with no paid edition.
Some are heavy to run: Flexprice's default setup runs nine services, including Kafka and ClickHouse. One Go program, Postgres and Redis. The ready-made Compose setup runs five small containers.
A customer can cost you more than they pay, because AI costs move with usage. Margin per customer and per model, from the provider cost Hardcount records on each usage event as it arrives. OpenMeter and Flexprice report cost too.

Stripe still charges its normal card fee, and Stripe Tax's fee if you use it. Every claim above has a source in Why Hardcount exists, and How it compares lists where the other projects are ahead.


Is Hardcount right for you?

Pre-1.0. Hardcount is young: one maintainer, Stripe and cards only, no SDKs yet, no production users yet. Until 1.0.0, a minor release can break the API, and a patch release can change something you rely on (versioning before 1.0).

A good fit if:

  • You resell model access through your own LLM gateway, such as LiteLLM, and charge per token, by model and token type. Any product that charges by usage can send its usage the same way.
  • You want billing on your own servers. Usage and invoice records stay in your own Postgres. Stripe sees each charge, and the invoice lines too if you use Stripe Tax.
  • You are a small or mid-size team. One engineer can run it, on one server or several.
  • Your volume is up to about a million usage events a day. A €6.49-a-month server took that rate for 20 minutes with no errors (measured on v0.8.0). Plan 15 to 18 GB of database disk a month at that volume. How we measured

Not a fit if you need:

  • Simple per-seat pricing paid by card. Stripe Billing already does that well.
  • Automatic charging through a processor other than Stripe. Stripe is the only one built in today. Tell us which one you need.
  • A marketplace that splits payouts between sellers (Stripe Connect). Hardcount bills your own customers.
  • Revenue recognition or accounting reports. Export the data to your own warehouse.
  • Quotes or multi-phase contracts for sales-led deals.
  • Customers who start a credit purchase themselves, or are stopped at zero without your code. You create each top-up, and the customer pays it by card on its page. Hardcount records usage and never refuses it: your gateway stops a customer whose credit has run out.

What's inside

  • Pricing: per-unit, graduated and package prices; rates per model and token type on one meter; a base fee plus usage on one invoice; prepaid credit that later usage draws down. AI pricing
  • Billing you can trust: Hardcount never bills the same period twice, because the database refuses a second invoice for it. Every usage line on an invoice links to the events behind it. How Hardcount keeps a bill right
  • Getting paid: cards through Stripe, with automatic retries and emails when a payment fails. Customers can also pay another way, such as a bank transfer, and you mark the invoice paid. Payment failure handling
  • Running it: one Go program with its background jobs inside, Postgres and Redis. No telemetry and no licence check. Self-host

Every feature: What Hardcount does.


Docs

docs/README.md lists every doc by what you want to do: decide if Hardcount fits, try it, connect your product, run it in production, or contribute. Good places to start:


Community and contributing

  • Questions, or evaluating a deployment: GitHub Discussions
  • Bugs: Issues. Money-path bugs are triaged first.
  • Security: SECURITY.md. Report privately, not in a public issue.
  • Contributing: we are not accepting outside pull requests at this time. A bug report or a proposed fix in an issue is welcome, and credited: CONTRIBUTING.md.

License

MIT

About

Open-source alternative to Stripe Billing and Lago for AI products: price tokens by model, sell prepaid credits, see margin per customer. Self-hosted; Stripe only charges the card (and works out tax, if you turn on Stripe Tax).

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages