AI agents · OpenClaw · self-hosting · automation

Quick Answer

How to Rate Limit LLM Usage Per User (2026 Guide)

Published:

The short answer

To rate limit LLM usage per user, count tokens per user in a shared store, check before each call, and charge the real token usage after it. As of November 29, 2026, two tools cover most stacks:

  • In your app code: Upstash Ratelimit (TypeScript SDK v2.2.3, November 26, 2026) on Redis, using the rate option to subtract tokens instead of one request.
  • At a gateway: the LiteLLM proxy (v1.111.1, November 29, 2026), which issues virtual keys and enforces RPM, TPM and dollar budgets per key, user, team or customer.

Per-user limits sit inside your provider spend caps; set those too, as described in how to set hard spend limits on LLM APIs.

Step 1: Decide what to limit

LimitProtects againstTypical setting
Requests per minuteRetry loops, scripted abuse20–60 per user
Tokens per minuteOne user saturating your provider TPM quotaSet from your provider limit ÷ expected concurrent users
Tokens or dollars per day or monthCost per user, plan allowancesTied to the user’s plan
Concurrent requestsLong agent runs piling up1–3 per user

Price tokens by model: a token on a frontier model can cost many times one on a small model, so convert usage to dollars with current API prices if users can switch models.

Step 2: Identify the user reliably

Rate limit on the authenticated user ID (or organisation ID for team plans), never only on IP address. For anonymous trials, combine IP with a device cookie and keep limits low. Pass the same identifier to the provider as well, for example OpenAI’s user or safety_identifier field, so abuse reports map to your users.

Step 3: Check before the call

Estimate the request’s tokens (prompt tokens plus max_output_tokens) and ask the limiter whether the user can spend them. With Upstash Ratelimit:

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const tokens = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(200_000, "1 h"), // 200k tokens per user per hour
  prefix: "llm-tokens",
});

const estimate = promptTokens + maxOutputTokens;
const { success, reset } = await tokens.limit(userId, { rate: estimate });
if (!success) return tooManyRequests(reset); // 429 + Retry-After

The rate field “determines the amount of tokens/requests to subtract”, so one call can charge thousands of tokens. Two defaults to know: Ratelimit allows a request through if Redis does not answer within the timeout (5 seconds by default), and its in-memory ephemeralCache blocks already-limited users without a Redis call when you create the limiter outside the request handler.

Step 4: Charge the actual usage after the call

Every major API returns input and output token counts. After the response (or the final streaming chunk), add the difference between actual and estimated tokens to the user’s counter, and record the dollar cost in your database for billing. Reserving max_output_tokens up front and refunding the unused part afterwards stops one long answer from overshooting the limit.

Step 5: Or enforce it at a gateway

If several services call models, put the limits in one proxy. In LiteLLM you create a virtual key per user or team with rpm_limit, tpm_limit, max_budget and budget_duration (for example 30d); team members can have individual budgets inside a team budget, and from v1.111 customer and end-user spend is handled through Customer Management. LiteLLM needs a Postgres database for any budget to apply. Compare gateways in the best LLM gateways.

Step 6: Fail gracefully

  1. Return 429 with Retry-After computed from the limiter’s reset timestamp.
  2. Show the remaining allowance in the UI so limits are not a surprise.
  3. For paid users, fall back to a cheaper model or queue the job instead of refusing.
  4. Alert yourself when a user hits the daily cap repeatedly: it is either abuse or a customer who should be on a bigger plan.

Common mistakes

  • Counting requests only. Long-context and agent loops make request counts meaningless for cost.
  • Per-instance memory counters. Serverless functions and multiple pods each see a fraction of traffic; use Redis or the gateway.
  • Ignoring streaming cancellations. Charge for tokens generated before the user cancelled.
  • Fail-open surprises. Know what happens when your limiter’s store is down, and choose deliberately.

Last verified: November 29, 2026.

Sources