# Limitr tutorial: usage-based billing in 15 programs

> A short book on usage-based billing with Limitr: 15 chapters, each one a small program you can run, from a first policy to AI calls, org budgets, and feature flags.

# Tutorial

A short book on usage-based billing with Limitr. Each chapter is one small program you can paste into a file and
run. Start at chapter 1, or jump to the one you need.

Limitr policies are written in [Stof](https://stof.dev), a superset of JSON, and the tutorial introduces it along the
way: how a policy is written (chapter 2), units (chapter 4), functions in a policy (chapter 7), the document
underneath it all (chapter 14), and rules shared across a policy (chapter 15). No Stof knowledge is needed to start.

<Tabs groupId="lang" queryString>
<TabItem value="typescript" label="TypeScript">

```bash
npm i @formata/limitr
```

</TabItem>
<TabItem value="python" label="Python">

```bash
pip install limitr
```

</TabItem>
</Tabs>

Every example is in TypeScript and Python, and shows exactly what it prints. In TypeScript, save one as
`example.mts` and run it with `npx tsx example.mts` (or with Bun or Deno). In Python, save it as `example.py` and run
it with `python example.py`.
Limitr runs the same way in the browser.

1. [Why Limitr](./why-limitr.md): where usage-based billing gets hard, and what Limitr does about it
2. [Your first policy](./first-policy.md): plans, features, and the `allow` call
3. [Customers and plans](./customers-and-plans.md): default plans, orgs and their users, upgrades
4. [Limits and resets](./limits-and-resets.md): units, daily and monthly periods
5. [Hard, soft, and observe](./hard-soft-observe.md): deny, bill the overage, or just watch
6. [Pricing and margins](./pricing-and-margins.md): prices, costs, tiers, and the margin of every call
7. [AI calls: reserve and settle](./reserve-and-settle.md): costs that change per call, and usage you only know afterwards
8. [Stacking entitlements](./stacking-entitlements.md): one action, several meters
9. [Control and monetize](./control-and-monetize.md): rate limits that protect you, next to the usage you sell
10. [Seats, top-ups, and spend caps](./seats-topups-caps.md): org seats, prepaid credits, and budgets
11. [Running it in your app](./running-in-your-app.md): one engine per server, middleware, and saving state
12. [Validation and errors](./validation-and-errors.md): policy formats and what happens when something's wrong
13. [Going to Cloud](./going-to-cloud.md): the same code, with managed policies, billing, and analytics
14. [The language underneath: Stof](./stof.md): reading a policy from your app, and asking it questions
15. [Feature flags and custom deals](./feature-flags.md): rollouts, kill switches, and giving or taking features per customer

A second track, [Limitr Network](https://github.com/dev-formata-io/limitr/tree/main/typescript/tutorial/network), covers services that sell to agents and to each other, paid
with signed vouchers instead of API keys.

The [reference](../reference/policy.mdx) covers every policy field, engine function, and event. The same material ships
with the npm package and the Claude Code plugin as skills for AI coding agents.
