Başar BallıözProduct Engineer
← All posts

LLM API cost control per user: how RPMBase keeps the AI bill bounded

An AI feature in a free app is an open tab at someone else's bar. RPMBase lets free users ask its assistant and scan receipts, so the first thing I built around the model was not a prompt. It was a budget that no client, no retry storm and no farm of accounts can push past.

By Başar Ballıöz · · 5 min read

The short answer

To keep LLM API costs under control per user, enforce limits on the server in four layers:

  1. Fixed request limits the client cannot change: input length, output tokens, image size, history length, model.
  2. A count quota per plan and month (messages, photos), taken before the call.
  3. A money budget per user: reserve the worst-case cost of a call atomically, then settle it with the real token usage.
  4. Shared pools and a hard cap for everything a single user's budget cannot see: free accounts as a group, and the whole project.

Some people call the threat this protects against denial of wallet: not taking your service down, just making it expensive. Here is each layer as it works in RPMBase's Node.js API.

The allowances users actually see, such as how many assistant questions and receipt photos the free plan includes each month, are on the RPMBase pricing page.

1. Limits the client cannot touch

Every number that changes the price of a call lives on the server:

LimitValue
Input text8,000 characters, clipped
Output400 tokens for structured replies, 900 for chat answers and receipts
Conversation historythe last 6 turns, 600 characters each
Image1.5 MB and 12 megapixels at most
Image detaillow by default; high only for receipts, where small print matters

And the server rejects any request body that tries to set model, max_tokens, temperature, detail, provider or system. A modified app or a script cannot ask for a bigger model or a longer answer, because the API refuses to hear the question.

2. A count quota, taken before the call

Each plan has a monthly number of assistant messages and photo messages. The counter is incremented atomically before the model is called, with a MongoDB findOneAndUpdate whose filter only matches while the user is still under the limit. Two parallel requests cannot both take the last message.

The increment is also permanent, even if the provider then fails. That sounds unkind, but the alternative is worse: if failures were free, a client could retry a failing request in a loop at no cost to itself. Users see a clear "monthly quota" message, never a silent failure.

3. A money budget: reserve the worst case, settle the real cost

Counting messages is not enough, because two messages can differ in cost by a factor of ten: a long question with a high-detail photo is not a "hi". So every plan also has a monthly budget in micro-dollars (one millionth of a dollar), and every call goes through the same wrapper:

  1. Price the call before making it. The server keeps a price table per model (input and output price per million tokens, and the worst-case token count of one image). The estimate is deliberately pessimistic: about 2 characters per token, every image at its largest, and the full output budget used.
  2. Reserve that amount atomically. One update adds the estimate to the user's spend for the month, but only if the result stays under the plan's budget. Concurrent calls cannot jointly pass the cap, because each one has already set its worst case aside.
  3. Call the model.
  4. Settle. The reservation is replaced with the real cost computed from the response's token usage, which is almost always lower. If the provider rejected the request, so it was not billed, the reservation is released. If the call timed out, the full reservation stays, because the provider may still bill it.

One rule follows from this: a model without a price is refused. If someone changes the model name in configuration to one the table does not know, every AI call fails with a clear error instead of running unmetered. A model you cannot price is a model you cannot hold to a budget.

4. Shared pools for free accounts, and a hard cap

Per-user budgets have a blind spot: creating accounts is free. A hundred fresh accounts each within their budget are still a hundred budgets. So free accounts also draw from shared pools, one per day and one per month, that every free call must fit into. A Premium user pays for their own budget and does not touch the pools.

A call reserves its cost in every pool it belongs to, or in none: if the second pool is full, the first reservation is rolled back. Free calls also have a daily limit per network address, which slows down a farm of accounts behind one connection. On top, an optional emergency cap across all plans can be switched on from configuration.

The last layer belongs outside the code, and it is on my production checklist rather than in the repository: a separate production project at the AI provider with a hard monthly spend limit and alerts well below it. If everything above failed at once, that limit is what stops the bill.

5. Seeing where the money goes

Every settled call is logged with the user, the model, the prompt and completion tokens, the reserved amount and the real cost. The admin dashboard reads the same ledgers the budget checks use: this month's spend across users, and how full today's and this month's free pools are, in-flight reservations included. The reserved-versus-real numbers are also a useful sanity check: if the real cost ever approaches the worst-case estimate, either the limits are too loose or the price table is out of date.

Common questions

How do I limit OpenAI API usage per user?

Keep a per-user counter and a per-user spend total for the current month in your database, update them atomically before each call, and settle the spend with the real token usage afterwards. Never let the client choose the model or the token limit.

Why reserve the worst-case cost instead of charging after the call?

Because calls run in parallel. If you only check the budget and charge afterwards, ten simultaneous requests can all pass the check before any of them is charged.

Is a provider-side spend limit enough?

It protects your bank account, not your users. When it trips, the feature stops for everyone. Per-user budgets and pools make sure one account, or one group of accounts, cannot use up what everyone else needed.

What I learned

Treat money as the unit, not messages. Message counts are what users understand; micro-dollars are what you pay. Enforce both.

Reserve, then settle. Atomic reservations are the only way a budget survives concurrency.

Design for the account farm. Per-user limits are necessary and not sufficient when sign-up is free. Shared pools close the gap.

The same budget covers the receipt scanner, which I wrote about in reading receipts with an LLM: its second, stronger read is affordable precisely because it runs inside these limits.

More posts

Keyword cannibalization in practice: one query, three moves, zero impressionsA real case from RPMVault: moving "motorcycle maintenance app" between pages three times cost its impressions. The query ownership rules, title limits and lock test I use now. Headless testing in Godot 4 without a frameworkHow Tibblo runs 42 headless GDScript test suites in Godot 4: SceneTree scripts, real input events, stepped time, a save sandbox and a guard for the player's real save.