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, 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.
- TypeScript
- Python
npm i @formata/limitr
pip install limitr
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.
- Why Limitr: where usage-based billing gets hard, and what Limitr does about it
- Your first policy: plans, features, and the
allowcall - Customers and plans: default plans, orgs and their users, upgrades
- Limits and resets: units, daily and monthly periods
- Hard, soft, and observe: deny, bill the overage, or just watch
- Pricing and margins: prices, costs, tiers, and the margin of every call
- AI calls: reserve and settle: costs that change per call, and usage you only know afterwards
- Stacking entitlements: one action, several meters
- Control and monetize: rate limits that protect you, next to the usage you sell
- Seats, top-ups, and spend caps: org seats, prepaid credits, and budgets
- Running it in your app: one engine per server, middleware, and saving state
- Validation and errors: policy formats and what happens when something's wrong
- Going to Cloud: the same code, with managed policies, billing, and analytics
- The language underneath: Stof: reading a policy from your app, and asking it questions
- Feature flags and custom deals: rollouts, kill switches, and giving or taking features per customer
A second track, Limitr Network, covers services that sell to agents and to each other, paid with signed vouchers instead of API keys.
The reference 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.