solutions

Usage Quotas for SaaS Applications

Track and enforce per-customer usage limits, credits and quotas with a single API call.

The Problem

Most SaaS products eventually need per-customer limits: a free tier capped at 1,000 requests/month, a team plan with a shared token budget, an API product billed by the call. The requirements sound simple.

The first version is usually a database counter. Add a row per user, increment on use, check before allowing. It works until traffic picks up and two requests race through the same limit simultaneously.

So you move to Redis. Then you need to reset counters at month-end, so you add a cron job. Then a customer asks for credits. Then you need idempotency so retries do not double-count. Then you need per-team and per-API-key scopes. Each requirement is solvable, but the cumulative complexity is substantial.

By the time the system is production-ready, you have built significant infrastructure that has nothing to do with your core product.

Common DIY Approaches

Database counters

pros

  • No extra infrastructure
  • Easy to query and audit

cons

  • Race conditions under concurrency
  • Slow — adds a DB round-trip to every request
  • Window resets need cron jobs

Redis INCR

pros

  • Atomic increments
  • Fast

cons

  • You write the check logic
  • TTL-based resets are imprecise
  • Credits, idempotency and multi-metric quotas add significant complexity

Custom cron jobs

pros

  • Flexible reset schedules

cons

  • Operational burden — cron failures silently break limits
  • Clock drift across instances
  • Hard to test

Home-grown credit system

pros

  • Tailored to your model

cons

  • Typically takes weeks to build correctly
  • Edge cases multiply fast
  • Every team rebuilds the same thing

Why Usage Quotas Are Hard

Daily and monthly windows

Limits need to reset on calendar boundaries, not rolling windows. Getting UTC midnight right across time zones is non-trivial.

Atomic check-and-consume

Reading a counter and decrementing it are two operations. Without atomicity, concurrent requests can both pass a limit that only one should.

Credit systems

Credits are not plain rate limits. They accumulate, carry over, and have variable costs per operation. The data model is different.

Idempotency

Retried requests must not double-count. You need idempotency keys that are scoped, short-lived, and checked atomically with the quota.

Concurrent requests

Under load, multiple requests can race to consume the last units. A naive implementation lets all of them through.

Multi-tenant isolation

Quotas need to be scoped per user, per team, per API key, and sometimes per feature — often all at once.

Observability

You need to know who is near their limit, who hit it, and why. Building dashboards and alerting on top of a home-grown system adds weeks.

How counr Solves It

counr exposes a single endpoint — POST /v1/check-consume — that checks a quota and consumes from it atomically. One round-trip. One decision. No race conditions.

  • Atomic check-and-consume — no double-spending under concurrency
  • Redis-backed, runs in US East
  • Any subject: user ID, team ID, API key, or arbitrary string
  • Daily and monthly reset windows
  • Variable cost per call — suitable for token budgets and credit systems
  • Response includes remaining so your UI can show progress

example — enforce an AI token budget

import { CountrClient } from "countr-sdk";

const countr = new CountrClient({
  apiKey: process.env.COUNTR_API_KEY,
  failOpen: true, // allow if Countr is unreachable (default false); result.failedOpen marks it
});

// In your API handler — check and consume in one atomic call
const result = await countr.checkConsume({
  subject: "user_abc123",   // user, team, API key — any string
  metric: "ai_tokens",
  cost: 1500,               // tokens used by this request
});

if (!result.allowed) {
  return Response.json(
    { error: "Monthly token budget exhausted", remaining: result.remaining },
    { status: 429 }
  );
}

// Request is allowed — proceed

Example Use Cases

AI token budgets

Cap token spend per user or team per month. Cost per request varies — countr accepts any integer cost per call.

Free tier limits

Enforce a monthly request cap before prompting users to upgrade. The response includes remaining units so you can show progress bars.

Team plans

Set a shared quota across all members of a team. Use the team ID as the subject — individual members draw from the same pool.

API products

Sell API access by the call. Each API key is its own subject. Quotas reset monthly or can be credit-style — your choice.

Feature credits

Give users N credits for a premium feature per billing cycle. Credits are consumed atomically; over-consumption is prevented.

Why Not Build It Yourself?

You can. The components are well-understood: Redis, Lua scripts for atomicity, a cron for resets, a schema for credits. If you have the time and your requirements are stable, building in-house is a reasonable choice.

The part teams usually underestimate is maintenance. The initial implementation takes a week or two. Edge cases — idempotency, concurrent over-spend, multi-metric quotas, time zone handling — add another week. Then requirements change: a customer needs a custom window, another needs rollover credits. Every change touches infrastructure that affects every customer.

The deeper pattern: most product teams end up rebuilding the same quota infrastructure two or three times as the product grows. The first version handles the simple case. The second handles concurrency. The third handles credits and complex billing.

counr is the third version, already built.

Add Usage Quotas in Minutes

One endpoint. No Redis to manage. Works with any backend.