Early Access Beta: free, no credit card needed. Sign up →

How to cap each API customer's usage

The situation

You sell an API or an MCP tool. Your pricing page says something like "500 calls a month on the basic plan." Then a customer connects a script or an AI agent. It retries, loops, and runs overnight. One customer may run several of them against the same account.

By the time you notice, the usage is already past the plan. The usual choices are:

  • Absorb it. You pay for the extra work.
  • Bill it later. Your customer gets an invoice they didn't expect.
  • Turn off the key. The customer's good traffic stops along with the bad.

What you want is simpler: check the plan before the work runs, and say no only to the caller that's over.

What a usage cap per customer looks like

  1. One key per caller. Give each customer integration or agent its own agent key. The key belongs to one customer and one plan.
  2. A plan is a count. A plan sets how many calls are allowed each month, day or minute, for one tool or for every tool. Each key counts on its own, even when several keys share a plan.
  3. Ask before the work. At the top of your handler, send the caller's agent key and the tool name to decide(). The answer is allow or deny, and the use is counted in the same step.
  4. Deny only that key. Over the cap, that key gets a clear "no" with when it resets. The same customer's other keys keep working.
  5. Keep a record. Each decision writes one audit row (key, customer, tool, answer, reason), so you can show a customer what happened.

That is what Ordainer Cap does. It sits in front of your tool as one check, not as a gateway: your request payloads don't pass through us.

What it looks like in code

Python, with the ordainer package (pip install ordainer). This is the sample from our Python quickstart:

decide.py
from ordainer import Client, deny_response

# Reads ORDAINER_KEY (or ORDAINER_API_KEY) and ORDAINER_BASE_URL. Timeout 0.3 s, fails open on timeouts and outages.
client = Client()

def run_research():
    return "expensive research done"

for call in range(1, 7):
    decision = client.decide(tool="research.run")  # a new idempotency key per call
    if decision.allowed:
        print(call, decision.decision, decision.remaining, run_research())
    else:
        blocked = deny_response(decision)  # what you send back to your caller
        print(call, blocked["status"], blocked["body"]["error"]["code"], decision.reason)
        print(blocked["body"]["error"]["message"])

On the sandbox's pilot key (5 allowed calls a month), calls 1 to 5 run and call 6 is denied with quota_exceeded. The tool doesn't run on the deny.

Node, with Express middleware (npm install ordainer). This is the sample from our Node quickstart:

server.js
const express = require('express');
const { Ordainer } = require('ordainer');

const ordainer = new Ordainer(); // reads ORDAINER_KEY and ORDAINER_BASE_URL. Timeout 300 ms, fails open on timeouts and outages.

const app = express();
app.post('/tools/research', ordainer.middleware({ tool: 'research.run' }), (req, res) => {
  // "ordainer" and "remaining" are only here so you can watch our decision while you try this. Leave them out of real handlers.
  res.json({ result: 'expensive research done', ordainer: req.ordainer.decision, remaining: req.ordainer.remaining });
});
app.listen(3001, () => console.log('listening on :3001'));

These samples use one key from the environment so you can see a deny fast. In your own service, you look up the key for the customer integration or agent that's calling; the Node quickstart shows how. SDKs are available for Python, Node, Ruby and Java, and plain HTTP works from any language, MCP tool handlers included.

Roll it out without surprising anyone

New plans you create start in shadow mode. Ordainer logs what it would have denied and lets those calls run. When the would-deny counts look right, switch the plan to enforce in the console. (The two sandbox plans you get at sign-up start in enforce, so you can see a deny right away.)

Common questions

Is this rate limiting?

No. A rate limit protects your servers from traffic spikes. Ordainer Cap enforces the plan you sold: a count of allowed calls per agent key, each month, day or minute. Keep your own rate limits for spikes.

Can it cap spend in dollars?

No. It counts calls, not dollars. If each call costs you money, cap how many calls each key may make.

Can one cap cover all of a customer's agents together?

Not today. Counts are per agent key. Several keys can share a plan, but each key gets its own count.

Do we have to change our billing?

No. Your invoices go out as they do today. You can export decisions as CSV or JSON from the console.

What happens if Ordainer can't answer?

By default the SDKs let the call run and log a warning. You can set them to block instead where an unchecked call is worse.

Try it

Ordainer is free during the beta: sign up for test keys with 1,000 decisions a month, no credit card needed.

Get test keys · Produce a deny in the live demo

If you've had to deal with a customer's integration running far past its plan, we'd like to hear how you handled it: support@ordainer.ai.