@formata/limitr

中文文档
0.8.5发布于 2026/11/09Apache-2.0TypeScriptES Module近 30 天下载 2206

Limits, credits, and billing inside your AI product: an open-source engine that decides what each customer can do, what it costs you, and what to bill, on every call, in-process.

原始使用文档
文档目录9 节
  1. Quick start
  2. Billing you own, not billing that owns you
  3. What you can build with it
  4. Limitr Network: services agents pay for
  5. Every deal, enforced as written
  6. Made for AI coding agents
  7. Self-hosting: on your own, or with Limitr Cloud
  8. Learn more
  9. License

Limitr
npm downloads license docs stars

Limits, credits, and billing, inside your AI product.
An open-source engine that decides, on every call, what each customer can do, what it costs you, and what to bill. You build on it: your rules, your UI, your coding agent. Limitr Cloud keeps every plan, price, and contract live, with no release.

Limitr is an open-source engine for usage control and billing. It meters usage (AI tokens by model, API calls, agent runs, seats, anything you count), enforces plans and limits, prices every call at what it costs you and what you charge, and tells your code what to bill. It's a library, not a service: it runs in your process, in Node, Bun, Deno, or the browser, so there's nothing to deploy or self-host and no network call in the middle of a request. It's built for usage that doesn't follow the plan, which is most AI usage.

Quick start

npm i @formata/limitr
import { Limitr } from '@formata/limitr';

const limitr = await Limitr.new(`
policy: {
    credits: {
        ai_token: {
            price: { amount: 0.00002 }      // you charge $20 per million tokens
            overhead_cost: 0.000008         // the model costs you $8 per million
        }
    }
    plans: {
        pro: {
            default: true
            entitlements: {
                export_pdf: {}                                                   // a feature flag
                chat: { limit: { credit: 'ai_token', mode: 'soft', value: 1_000_000, resets: true, reset_sch: 'monthly:1' } }
            }
        }
    }
}`);

limitr.addHandler('billing', (name, value) => {
    if (name === 'meter-overage') console.log('bill it:', JSON.parse(value as string).overage_price);
});

await limitr.ensureCustomer(userId);
if (await limitr.allow(userId, 'chat', tokens)) {
    // call the model
}

That's a plan with a monthly AI token allowance, overage billed past it, and margin on every call. The tutorial builds on it in 15 short, runnable chapters.

Billing you own, not billing that owns you

Pricing never sits still. Costs, usage, and deals change every month, and every change lands on your code and the customers you already have. Most billing platforms are fast to start, and put your pricing in their system: usage is sent to a vendor, counted on their servers, one event at a time, after it happens. Building it yourself keeps control, and costs engineering time on every plan and every deal.

Limitr is the control without the build. The engine is yours, like your code:

  • Inside your app. Every check runs in your process, before the work runs, with no network call.
  • Priced across the task, not one call at a time. One task is dozens of calls across models, tools, and vendors. The engine sees every one with the customer, their plan, and what the task has cost so far, so it prices and limits the whole task as it runs.
  • Shaped to your business. Plans, prices, costs, limits, and every customer's deal in one policy document.
  • Extends like code. Your own pricing functions, feature conditions, rules, and tools run in the policy, written in Stof.
  • Safe in your process. A policy runs sandboxed: no files or environment, no network unless it's allowed (and never your private network unless you list it), and time and memory limits on every call. The engine is sealed when it loads, so a policy can build on it but can't change it.
  • Margin on every call, priced at what you charge and what it costs you.
  • Never a share of your revenue, and yours if you leave: with Limitr Cloud or without it, it's the same engine.

What you can build with it

  • Any pricing model in one policy: usage-based, seats, credits, flat plans, or a hybrid, per plan and per feature.
  • AI features that stay profitable: per-model costs, per-call margins, usage predicted before the call, and spend caps that stop runaway usage.
  • Feature flags and rollouts: kill switches, percentage rollouts, and rules per call, next to the plans, with events to measure them.
  • Custom deals and contracts: give one customer a feature their plan doesn't have, take one away, or change their limits, with expiry, without a new plan for each deal. For a deal many customers sign, write it once as a rule in the policy and assign it to each of them (below).
  • Customers that belong together: refs put a user in a team and a team in an org, any way you model it. A user with no plan of its own is on its org's plan, with the org's deals, seats, and shared budgets.
  • Prepaid credits: credit packs with expiry, resets, and rollover, spent across features at their own rates.
  • Rate limits next to paid usage: protect your infrastructure without touching what customers pay for.
  • Billing events: overage, limits hit, and thresholds crossed, sent to your code as they happen.
  • Services agents can buy: sell your API to agents and other services with Limitr Network (below), metered by the same policy.

Limitr Network: services agents pay for

An agent that needs a forecast, a search, or a translation usually needs an account and an API key first. With Limitr Network, a service publishes its plans and routes from its Limitr policy, and an agent (or any app or service with a policy) signs a voucher: up to this much, on this plan, until then. The service meters it like any customer, with the voucher's spend as a hard cap; the agent gets the service's routes as tools; and every request and receipt is signed, so both sides hold the same record. No keys exchanged, no integration work. Optional operators (Limitr Cloud is one, set up for you on a Limitr.cloud() engine, and anyone can run one) add shared state across servers, budgets across services, identities, and discovery. It's the Limitr Network Protocol (LNP): start with src/network and the tutorial track.

Every deal, enforced as written

A customer signs for more than their plan: 250,000 API calls a month, SSO, audit logs from your Business plan, and no exports. Write the deal once as a rule, and give it to every customer that signs it. Their terms apply on their next call, with no new plan and no engineering ticket:

rules: {
    acme_2027: {
        label: 'Acme 2027'
        given: { sso: {}, audit_log: { plan: 'business' } }
        taken: { exports: {} }
        overrides: { api: { credit: 'request', value: 250_000, resets: true, reset_sch: 'monthly:1' } }
    }
}
await limitr.setCustomerRules('acme', ['acme_2027']);
await limitr.entitlementSource('acme', 'api'); // { access: 'plan', limit: 'rule:acme_2027', customer: 'acme' }

For every customer, the order is the same: its own changes first, then its rules (highest priority first), then its plan. Rules can also cap spend, and a rule can't invent an entitlement: a give always uses a plan's definition. A user on its org's plan gets the org's rules too.

Made for AI coding agents

The npm package ships three skills in node_modules/@formata/limitr/skill: limitr (the policy reference, every engine function, and every event), limitr-ts (the TypeScript SDK), and limitr-network (Limitr Network). The fourth, stof (the language policies are written in), ships with Stof in node_modules/@formata/stof/skill/stof. Point your coding agent at them and it can write, review, and debug policies and integrations correctly, instead of guessing. The Limitr skills are also packaged as zips in content/ for uploading to an agent.

Self-hosting: on your own, or with Limitr Cloud

There is no Limitr server to deploy. The engine is the npm package, running inside your app wherever your app runs, open source (Apache 2.0) and free. "Self-hosting" Limitr is npm i @formata/limitr and a policy in your repo. The same policy and the same calls work with or without Limitr Cloud, so you can start on one and move to the other.

On your own (Limitr.new(policy)), you own the parts around the engine:

  • The policy lives in your repo, reviewed and deployed like code.
  • Customer state lives in your database: save each customer as its events say it changed, and restore it with loadCustomers when your app starts. Load a customer's refs with it (its team and org), since its plan, deals, and shared caps come through them. Running it in your app shows the whole loop.
  • Billing periods follow your subscriptions (setCustomerPeriod), or the calendar.
  • Billing comes from the events: overage, limits, and thresholds go to your code as they happen, with the price and cost of each, for Stripe or whatever you bill with.

With Limitr Cloud (Limitr.cloud({ token })), those parts come with it, and every check still runs in your process:

  • Limitr Analyst, an agent beside you on every page. It writes and tests your policy, puts a signed contract in place from the order form, answers questions about customers, usage, and margin with charts and dashboards, and shows what a price change would do to real bills before you publish. It works in a draft and proposes the rest: nothing is published or changed until you apply it.
  • Policies you edit, review, and publish to every running copy of your app, with no deploy, and roll back.
  • Customers kept for you, with the customers their refs lead to loaded alongside them.
  • Billing: ledgers, invoices through your own Stripe, and accounts that bill a customer and everyone under it as one invoice.
  • Contracts: rules on a schedule, with ramps, fees, commitments, and recognition.
  • Analytics: usage, cost, and margin by customer, account, feature, model, or task.

Limitr Cloud has a free plan, and never takes a share of what you bill: start free and have the Analyst write your first policy.

Learn more

Have a pricing model you're not sure fits? Talk with the founders.

License

Apache 2.0. See LICENSE for details.




Limitr

版本与发布记录

@formata/limitr 共发布 63 个版本,首次发布于 2026/02/06;过去 12 个月发布了 63 个版本。

版本发布日期距今
0.8.5latest2026/11/097 天前
0.8.32026/11/0610 天前
0.8.22026/11/0411 天前
0.8.12026/10/3115 天前
0.8.02026/10/3116 天前
0.7.52026/10/2819 天前

关于 @formata/limitr 的常见问题

@formata/limitr 是什么?

Limits, credits, and billing inside your AI product: an open-source engine that decides what each customer can do, what it costs you, and what to bill, on every call, in-process.

如何安装 @formata/limitr?

在项目目录运行 npm install @formata/limitr,或使用 pnpm add / yarn add / bun add。

@formata/limitr 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@formata/limitr 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@formata/limitr 可以商用吗?

@formata/limitr 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@formata/limitr 的下载量有多少?

根据 npm 官方下载统计,@formata/limitr 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,206 次,日均约 74 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@formata/limitr 还在维护吗?

最新版本 0.8.5 发布于 2026/11/09(7 天前);至今共发布 63 个版本,其中过去 12 个月发布了 63 个;首次发布于 2026/02/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。