Token budgeting controls how many tokens you spend and how fast you consume provider capacity. It covers input and output pricing, per-user and per-tenant budgets, and staying under RPM and TPM limits (requests and tokens per minute).
Estimating and capping tokens
- Estimate input and output tokens with a tokenizer that matches your model, such as Tiktoken for OpenAI models.
- Cap the output with the
max_tokensparameter. - Trim context and history when the input would be large.
- Throttle on the client with a token bucket so requests stay under the provider limit.
- Enforce a token budget per tenant.
Output tokens can cost several times more than input tokens, and output length is variable, so token estimates are probabilistic. Your bill follows model output you do not fully control, which sets it apart from ordinary metered APIs or fixed server costs.
Cheaper paths: caching, batch and smaller models
- Prompt caching reuses work when the input prefix is the same, which lowers the cost of repeated queries.
- Batch APIs discount token prices for non-urgent work that can wait for results. OpenAI's Batch API, for example, lists a 50% discount and a 24-hour completion window. Check the current terms with your provider.
- Smaller models usually cost less per token.
Unbudgeted spikes, 429 errors and backoff
Calling the API with no budget or controls can produce an unexpectedly high bill after a usage spike, because response length varies and output tokens cost more.
When a request exceeds the allowed rate, the provider returns a 429 error. Client-side throttling avoids most of them. For the rest, retry with exponential backoff. Without any controls, heavy load can cause cascading failures as retries hit the limit again.
Per-tenant metering in multi-tenant systems
In a SaaS product, per-tenant budgets and throttles keep one tenant from monopolising the shared provider quota. Hard limits per tenant protect every other tenant's share of the quota and keep each bill predictable. When the provider's ceiling is reached, the system can degrade gracefully instead of failing hard.
Choosing controls by workload
| Workload | Controls |
|---|---|
| Latency-sensitive user experience | Prompt caching and context trimming |
| Large non-urgent jobs | Batch APIs and smaller models |
| Multi-tenant SaaS | Hard per-tenant token budgets and throttles |
Example scenario: a monthly cost spike
Suppose monthly LLM cost rises from about $20K to about $250K while answer quality stays the same. Tokens consumed per request are the first suspect: oversized contexts, inefficient retries, a model tier that is too large for the task, or missing caching.
- Measure tokens per request and compare with previous months.
- Log retries per request to find waste.
- Check the size of the context sent with each request.
The fix combines token budgets, routing to cheaper models and caching for repeated queries. Responses may get slightly slower. Track cost per request continuously so the next spike shows up in days, not at month end.