Resources / Guides

Controlling LLM API costs behind your own endpoint

Keep AI API bills predictable: proxy endpoints, per-user limits, caching, model choice, token caps and usage logging.

Calling an LLM from your app is easy. Getting a surprise bill because a script hammered your endpoint overnight is also easy. Put the model behind your own endpoint and you control every call.

Never call the model from the browser

A key in frontend code is a public key. Route calls through a proxy endpoint that adds the key on the server.

Limit per user, not just overall

Set per-client limits — per minute and per day — so one user or bot can't use everyone's budget. Return 429 with a retry-after header when they hit it. See Rate limiting and quotas.

Cap tokens on every request

Cache what repeats

Identical prompts with identical answers — classifications, summaries of the same document, FAQ answers — can be cached in a key-value store for minutes or days. Provider-side prompt caching also cuts the cost of long, repeated system prompts.

Pick the smallest model that works

Use a fast, inexpensive model for classification, extraction and routing; save the largest model for the hard parts. Test quality on real examples before switching.

Log usage and alert

Record tokens used per request and per user. Alert when daily spend crosses a threshold, and have a switch to pause the endpoint.

In Wisp

"A proxy for chat completions that adds our key, caps output at 500 tokens and limits each client to 20 calls a minute" becomes an endpoint with the key stored as a secret, limits enforced before the code runs, and every call logged.

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

Related