> ## Documentation Index
> Fetch the complete documentation index at: https://docs.integrateledger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Credits

> Price every action from one shared balance, each at its own cost in credits, and reprice any action without a new plan.

> **Pro:** 1,000 credits a month for \$20. An image costs 1 credit, an HD image 4, a PDF export 3.

Credits give customers one balance that pays for everything in your product, and give you a single lever to price each action by what it costs you.

A `credits` feature spends from one shared credit pool. Each feature has a `creditCost`, how many credits one use spends, and plans grant `monthlyCredits` rather than uses. One balance then covers every action, so 1,000 credits buy 1,000 images, 250 HD images, or any mix, and you can price an expensive action higher without a new plan. When each action should have its own allowance instead, use [metered allowances](https://docs.integrateledger.com/pricing/metered.md).

## The catalog

```ts ledger.config.ts theme={"system"}
import { defineCatalog, feature, plan } from "integrateledger-cli";

export default defineCatalog({
	features: [
		feature({ id: "generate_image", name: "Generate an image", creditCost: 1 }),
		feature({ id: "generate_image_hd", name: "Generate an HD image", creditCost: 4 }),
		feature({ id: "export_pdf", name: "Export a PDF", creditCost: 3 }),
	],
	plans: [
		plan({ id: "free", name: "Free", monthlyCredits: 50, priceCents: 0 }),
		plan({ id: "pro", name: "Pro", monthlyCredits: 1000, priceCents: 2000 }),
		plan({
			id: "studio",
			name: "Studio",
			monthlyCredits: 5000,
			priceCents: 9900,
			items: [{ featureId: "generate_image", unlimited: true }],
		}),
	],
	creditPacks: [],
});
```

```json ledger.catalog.json theme={"system"}
{
  "schemaVersion": 1,
  "features": [
    { "id": "generate_image", "name": "Generate an image", "creditCost": 1 },
    { "id": "generate_image_hd", "name": "Generate an HD image", "creditCost": 4 },
    { "id": "export_pdf", "name": "Export a PDF", "creditCost": 3 }
  ],
  "plans": [
    { "id": "free", "name": "Free", "monthlyCredits": 50, "priceCents": 0 },
    { "id": "pro", "name": "Pro", "monthlyCredits": 1000, "priceCents": 2000 },
    { "id": "studio", "name": "Studio", "monthlyCredits": 5000, "priceCents": 9900,
      "items": [{ "featureId": "generate_image", "unlimited": true }] }
  ],
  "addOns": []
}
```

`type` defaults to `credits`, so a feature with only a `creditCost` is a credits feature. A credits feature needs no plan item: every plan's `monthlyCredits` pays for it. The one item a credits feature takes is `unlimited: true`, which makes it free on that plan (Studio's standard images above).

`creditCost` belongs to the feature, so it is the same on every plan and for every customer. Model a per-plan price as its own feature: define `generate_image_business` at `creditCost: 2` and send its `featureId` from your code for customers on that plan. Every plan's credits pay for every credits feature, and an item makes one unlimited. [Overrides](https://docs.integrateledger.com/customers/overrides.md) set a customer's overage terms.

## What check and track return

```typescript TypeScript theme={"system"}
const { allowed } = await billing.checkCredits({
	customerId: user.id,
	featureId: "generate_image_hd",
});
if (!allowed) {
	return showUpgradeOffer();
}

const image = await generateImage(prompt, { hd: true });

await billing.trackUsage({
	customerId: user.id,
	featureId: "generate_image_hd",
	requestId: image.id,
});
```

```python Python theme={"system"}
decision = billing.check_credits(customer_id=user.id, feature_id="generate_image_hd")
if not decision["allowed"]:
    return show_upgrade_offer()

image = generate_image(prompt, hd=True)

billing.track_usage(customer_id=user.id, feature_id="generate_image_hd", request_id=image.id)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/check \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "generate_image_hd"}'

curl https://api.integrateledger.com/v1/track \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "generate_image_hd", "request_id": "img_0001"}'
```

`check` answers `{ "allowed": true, "availableCredits": 996, "creditCost": 4 }`. The track answers what it spent:

```json Response theme={"system"}
{
  "allowed": true,
  "availableCredits": 992,
  "chargedCredits": 4,
  "overageCredits": 0,
  "overageRemainingCents": null,
  "request_id": "img_0001",
  "idempotent": false
}
```

When the cost is more than the balance, `check` answers `allowed: false` with `reason: "insufficient_credits"` and the customer's `plan`, and `track` is refused with `409 insufficient_credits`, with `availableCredits` and `creditCost` in the body. Nothing is charged. On a plan that makes the feature unlimited, `check` answers `creditCost: 0` and `unlimited: true`, and the track spends nothing.

## Several uses at once

Send `quantity` to `check` and `track` when one request is several uses, such as a batch of 10 HD images. It costs `quantity × creditCost`: 40 credits here, decided all or nothing.

```typescript TypeScript theme={"system"}
await billing.trackUsage({
	customerId: "user_123",
	featureId: "generate_image_hd",
	requestId: "batch_0001",
	quantity: 10,
});
```

```python Python theme={"system"}
billing.track_usage(
    customer_id="user_123",
    feature_id="generate_image_hd",
    request_id="batch_0001",
    quantity=10,
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/track \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "generate_image_hd", "request_id": "batch_0001", "quantity": 10}'
```

## Variable costs

`creditCost` is the price of one unit, and `quantity` says how many units a use took. When the size depends on the work, price the feature by its unit and send the real count: a video render at `creditCost: 2` a second tracks `quantity: 160` for a 160-second video and spends 320 credits.

When you work out the price yourself, such as model tokens at cost or GPU time, give the feature `creditCost: 1` and send the cost in credits as `quantity`. [Prepaid dollar balance](https://docs.integrateledger.com/pricing/dollar-balance.md) builds a whole balance this way.

To make sure the customer can cover the work before it starts, check with `reserve: { quantity: estimate }` and a `request_id` first: the check holds the estimate, and the track with the same `request_id` and the real `quantity` settles it and gives back the rest. [Reservations](https://docs.integrateledger.com/customers/reservations.md) covers reserve and settle in full.

## Cheaper at volume

`creditCostTiers` makes an action cost less the more a customer uses it in a period: the first 1,000 images of the month cost 2 credits each, every one after that 1.

```ts ledger.config.ts theme={"system"}
feature({
	id: "generate_image",
	name: "Generate an image",
	creditCost: 2,
	creditCostTiers: [
		{ upTo: 1000, creditCost: 2 },
		{ upTo: null, creditCost: 1 },
	],
}),
```

```json ledger.catalog.json theme={"system"}
{
  "id": "generate_image",
  "name": "Generate an image",
  "creditCost": 2,
  "creditCostTiers": [
    { "upTo": 1000, "creditCost": 2 },
    { "upTo": null, "creditCost": 1 }
  ]
}
```

* **Graduated by use.** Each band's `upTo` is its last use of the period, and every use is charged at its own band: a track of 3 uses from the 999th costs 2 + 2 + 1.
* **Counted per period.** Uses are counted per customer over the allowance period (each month of an annual plan that resets monthly, the calendar month without a plan) and start again from 1 each period.
* **Exact under load.** Concurrent tracks are counted one after another, so a burst across a band costs exactly what the same uses one at a time would.
* **Check and reserve agree.** `check` answers the cost at the bands the uses would reach. A [reservation](https://docs.integrateledger.com/customers/reservations.md) holds its uses at the next use's band, and the track that settles it charges the bands its uses reach then, counting them toward the period.

[Tiered pricing](https://docs.integrateledger.com/pricing/tiers.md) prices seats and overage in bands the same way.

## Choosing costs

* **Start from your own cost.** If an HD image costs you four times a standard one, make it 4 credits. Set plan credits so the plan's price covers a heavy month.
* **Keep costs whole numbers.** `creditCost` is a whole number above 0. For cheap actions, make a credit worth less: 10 credits per message rather than 0.1.
* **Changing a cost applies from then on.** Usage already tracked keeps the cost it was charged at, and a feature change doesn't create a plan version.

## Rules

* **`creditCost` goes on credits features.** It's required there and refused on metered, held and boolean features.
* **Credit cost tiers go on credits features.** `creditCost` is the first band's cost, each `upTo` is above the one before, and the last band's `upTo` is `null`.
* **Credits last one period.** Unspent plan credits end with the period, and the next one starts with a fresh `monthlyCredits`, unless the plan has [rollover](https://docs.integrateledger.com/pricing/rollover.md). Credit pack and [top-up](https://docs.integrateledger.com/pricing/top-ups.md) credits never expire, and [grant code](https://docs.integrateledger.com/billing/discounts.md#grant-codes) credits last as long as you set. [Balances](https://docs.integrateledger.com/concepts/balances.md) has the spend order.
* **All or nothing.** A use is charged in full or refused; with [overage](https://docs.integrateledger.com/pricing/overage.md), the part past the balance is billed later instead.
* **Entities.** To split a customer's credits between users or workspaces, see [Entities](https://docs.integrateledger.com/customers/entities.md).

## Related

* [Credit packs](https://docs.integrateledger.com/pricing/credit-packs.md): sell more credits as a one-time purchase.
* [Top-ups and auto top-ups](https://docs.integrateledger.com/pricing/top-ups.md): any amount, or bought automatically when the balance runs low.
* [Overage](https://docs.integrateledger.com/pricing/overage.md): bill credits past the allowance.
* [Metered allowances](https://docs.integrateledger.com/pricing/metered.md): one balance per feature instead.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.