Resources / Guides

LLM billing: how to track and charge for AI usage per user

Meter LLM tokens per user or team, turn them into costs, enforce budgets and bill for usage — a practical design for apps built on OpenAI, Claude or Gemini APIs.

Your provider bills you per token. Once real users share your app, you need to know which user — or which team — spent those tokens, stop anyone from running up an unbounded bill, and possibly charge them for it. That is LLM billing, and it needs a little plumbing of its own.

1. Put one endpoint in front of the model

Browsers and mobile apps should never call the model API directly: the key would leak and you couldn't see who called. Route every request through your own endpoint, which:

This is the same proxy described in hiding API keys with a proxy endpoint; billing is one more step inside it.

2. Record usage from the response, not from estimates

Every major API returns token counts with each response — OpenAI as usage.prompt_tokens / usage.completion_tokens, Anthropic as usage.input_tokens / usage.output_tokens. Store one row per call:

{ "user_id": "u_123", "team_id": "t_9", "model": "gpt-x", "input_tokens": 1840, "output_tokens": 312, "cost_usd": 0.0071, "at": "2026-11-29T10:41:00Z" }

Compute cost_usd when you write the row, from a price table keyed by model. Prices change; keep the table in config, not in code, and never recompute old rows with new prices.

For streaming responses, the counts arrive at the end of the stream. Record them after the stream closes, and record a partial row if the client disconnects mid-stream — you still paid for those tokens.

3. Enforce budgets before the call

A billing system that only counts is a report. To control spend, check before every call:

Keep running totals in a fast store (a counter per user per day) rather than summing the usage table on every request. More patterns in API rate limiting and quotas.

4. Choose a pricing model

Whatever you choose, show users their usage in the product. Surprise bills cause more churn than high ones.

5. Send usage to your billing provider

Payment providers support metered billing: you report usage events, they total them per billing period and invoice. Report aggregates (for example, one event per user per hour) rather than one event per model call, and keep your own usage table as the source of truth so you can answer "why is my invoice this high?".

6. Reconcile

Once a month, compare your recorded cost with the provider's invoice. A gap means calls that bypassed the proxy, missing rows from failed streams, or an outdated price table.

Reducing the bill itself

Metering tells you where tokens go; controlling LLM API costs covers spending fewer of them — caching, smaller models for simple tasks and shorter prompts.

On Wisp

Describe the proxy — "an endpoint that calls OpenAI for signed-in users, logs tokens per user and blocks users over $5 a day" — and Wisp writes it with the model key stored as a secret, usage rows in a table and per-user limits, and every call visible in the invocation log.

Try it: Template: OpenAI API key proxy

Build it with Wisp — describe an API, get it live.

Related