**Open-source policy engine for plans, limits, and usage enforcement.**
npm install @formata/limitrpricing.ts:
typescript
if (user.plan === 'free' && user.seats >= 1) {
throw new Error('Upgrade to add more seats');
}
`
This breaks down with usage-based pricing, AI products, and self-hosted deployments.
Limitr separates policy from code so limits are explicit, testable, and easy to evolve.
Install
`bash
npm install @formata/limitr
`
$3
Limitr uses Stof for policy enforcement (@formata/stof). This is sandboxed WebAssembly, and needs to be initialized once before use.
> This step is for font-end (browser) apps only. For Node.js, Deno, & Bun, this step is handled automatically by Limitr (you can skip this).
`typescript
// Vite
import { initStof } from '@formata/stof';
import stofWasm from '@formata/stof/wasm?url';
await initStof(stofWasm);
// Browser with bundler - Pass WASM explicitly (e.g. @rollup/plugin-wasm)
import { initStof } from '@formata/stof';
import stofWasm from '@formata/stof/wasm';
await initStof(await stofWasm());
// Node.js, Deno, & Bun - Auto-detects and loads WASM (you can skip this though, Limitr does it)
import { initStof } from '@formata/stof';
await initStof();
`
Quick Start (Local)
`typescript
import { Limitr } from '@formata/limitr';
// Define policy (YAML, JSON, TOML, STOF) (load from DB, API, file, etc.)
const policy = await Limitr.new(
, 'yaml');
// Create/load customers
await policy.createCustomer('user_123', 'free');
await policy.createCustomer('user_456', 'pro');
// Enforce limits
await policy.increment('user_123', 'seats'); // true - succeeds (1/1 used)
await policy.increment('user_123', 'seats'); // false - fails (limit hit)
await policy.increment('user_456', 'seats'); // true - succeeds (1/10 used)
await policy.allow('user_456', 'seats', 5); // true - succeeds (6/10 used)
`
Common Use Cases
Seat-based plans:
`typescript
if (await policy.increment('org_123', 'seats')) {
// Add user to org
}
`
Usage-based limits:
`typescript
if (await policy.allow('user_456', 'chat_ai_tokens', 4200)) {
// allowed: process LLM request
// usage recorded and synced in the background (*Limitr Cloud)
} else {
// denied: limit exceeded
}
`
Feature gates:
`typescript
const hasAdvancedFeatures = await policy.allow('user_789', 'advanced_analytics');
`
Local vs Cloud
$3
`typescript
const policy = await Limitr.new(policyDocument);
`
- Runs entirely in your app
- No external dependencies
- Perfect for self-hosted deployments
$3
`typescript
const policy = await Limitr.cloud({
token: 'limitr_...'
});
``