> ## 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.

# How Ledger works

> Ledger turns your pricing page into live balances your backend checks in real time, and bills every use on the customer's Stripe invoice.

Ledger turns a pricing page into a catalog, and the catalog into balances your backend checks in real time, so every action is gated and every use is billed. Stripe takes the payments and sends the invoices.

```mermaid theme={"system"}
%%{init: {"theme": "base", "themeVariables": {"fontFamily": "IBM Plex Sans, sans-serif", "fontSize": "14px", "primaryColor": "#f7f6f3", "primaryBorderColor": "#c4bfb5", "primaryTextColor": "#1a1816", "lineColor": "#6b655c", "clusterBkg": "#ffffff", "clusterBorder": "#dbd7cf", "edgeLabelBackground": "#ffffff"}}}%%
flowchart LR
    features["Features<br/>credits · metered · held · boolean"]
    plans["Plans<br/>items and a price"]
    sub["Subscription<br/>the customer's plan"]
    balances["Balances<br/>grants per period"]
    app["Your backend<br/>check · track"]
    invoice["Stripe invoice<br/>plan · overage · seats"]

    features --> plans
    plans -- "attach" --> sub
    sub -- "grants" --> balances
    app <--> balances
    balances -- "usage past the allowance" --> invoice
    sub -- "renews" --> invoice

    style balances fill:#eff2fd,stroke:#2443c4,color:#1a1816
```

## 1. Features

A [feature](https://docs.integrateledger.com/concepts/features.md) is something in your app a plan can include. Its type says how it's used up:

| Type | Example | Used up by |
| - | - | - |
| `metered` | Messages | Each use, from a monthly allowance |
| `held` | Projects, seats | A count the customer holds, up to a limit |
| `boolean` | SSO | Nothing: it's on or off |
| `credits` | Image generation | Each use's `creditCost`, from one shared credit balance |

## 2. Plans with items and a price

A [plan](https://docs.integrateledger.com/concepts/plans-and-items.md) lists **items**, one per feature it includes: 500 messages, unlimited projects, SSO. It has a price per month or year, or once for a lifetime plan, and can grant credits. A metered item can price usage past its allowance, and a held item can price each seat, flat or in [tiers](https://docs.integrateledger.com/pricing/tiers.md). Plans can also carry [trials](https://docs.integrateledger.com/pricing/trials.md), [rollover](https://docs.integrateledger.com/pricing/rollover.md) and [promotion codes](https://docs.integrateledger.com/billing/discounts.md). You keep the catalog in a file in your repo and publish it with the [CLI](https://docs.integrateledger.com/cli.md).

## 3. Attach gives the customer a subscription

[Attaching](https://docs.integrateledger.com/billing/checkout-and-attach.md) a plan to a [customer](https://docs.integrateledger.com/concepts/customers-and-subscriptions.md) starts their subscription. A customer holds one base plan per [product line](https://docs.integrateledger.com/pricing/plan-groups.md), plus any [recurring add-ons](https://docs.integrateledger.com/pricing/add-ons.md) and [plans for its workspaces](https://docs.integrateledger.com/pricing/entity-plans.md), all on one invoice. A free plan starts at once. A paid plan charges a saved card or returns a Stripe Checkout URL, and starts when Stripe's signed webhook confirms the payment.

## 4. Grants become balances

Each period, the subscription **grants** what the plan includes: the metered allowance, the credits, the held limits. A customer's [balance](https://docs.integrateledger.com/concepts/balances.md) for a feature is what those grants leave. Allowances renew each period, [rollover](https://docs.integrateledger.com/pricing/rollover.md) carries what's unused forward, and credit packs and [top-ups](https://docs.integrateledger.com/pricing/top-ups.md) never expire.

## 5. Check before, track after

Your backend calls `check` before an action: is it allowed, and what's left? After the action succeeds, it calls `track` with a `request_id` unique to that use. Ledger decides again, spends the balance once, and records a [usage event](https://docs.integrateledger.com/concepts/usage-events.md). A retry with the same `request_id` charges nothing more. [Check and track](https://docs.integrateledger.com/customers/check-and-track.md) has the calls and the refusals.

## 6. Ledger bills the usage on the Stripe invoice

Stripe renews the subscription and invoices the plan's price. While the renewal invoice is still a draft, Ledger adds a line for each overage rate the customer used. Seats ride on the subscription as their own item, at the count the customer holds, so Stripe bills them beside the plan. [Invoices and Stripe](https://docs.integrateledger.com/concepts/invoices-and-stripe.md) shows what lands where.

## Sandbox and Live

Every workspace has two environments, Sandbox and Live, and the API key picks one: a `ledger_test_` key reaches Sandbox, a `ledger_live_` key reaches Live, on the same API host. Nothing is shared between them: each has its own catalog, customers, balances, usage and Stripe account, so a test can never touch a real customer. Sandbox comes with a Stripe test account Ledger creates for you ([Sandbox](https://docs.integrateledger.com/test/sandbox.md)); Live uses your own Stripe account ([Production checklist](https://docs.integrateledger.com/integrate/production-checklist.md)).

## Next steps

> [card] [Quickstart](https://docs.integrateledger.com/quickstart.md)
>
> Build the whole loop for a chat app in Sandbox.

> [card] [Choose a model](https://docs.integrateledger.com/pricing/choose-a-model.md)
>
> Find the pattern that matches your pricing page.


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