Credits, price and limits
Model APIs bill by tokens, input and output separately. Because the model is stateless, every call carries the whole history, so cost grows faster than one request's price suggests. An agent needs limits on steps and cost. A rate limit (429) is retried with a pause, while an exhausted quota is not retried and a clear message is shown. Prompt caching works when the start of the request is identical.
AI for QA · Basics
Ten requests a day cost pennies, yet a night run with an agent burned the monthly balance. Where did the money go?
A nightly agent eval run failed in the third hour: the credits ran out. A single request is cheap, so it seemed the budget would last weeks. Three things explain the gap: token-based billing, the history being resent on every step and an agent with no step limit. The lesson covers what a request price consists of, which limits exist and what QA tests among them.
How it works and why. Model APIs bill by tokens, separately for input (everything you sent) and output (what the model generated). Vendors usually quote a price per million tokens, and output tokens typically cost more. The cost of a call is input × input price + output × output price. Actual rates change, so they are taken from the vendor page and kept in code as configuration.
The key feature: the model has no state, so every call in a dialogue or an agent loop carries the whole history. An illustration with made-up numbers: a system prompt of 1,000 tokens, each turn adds 500 tokens to the history (the question and the answer).
The input of one call grows with the turn
| What is counted | Tokens |
|---|---|
| Unique text over 10 turns | 6,000 |
| Input actually billed over 10 turns | 32,500 |
So over ten turns you pay for about five and a half times more input than there is unique text, and the gap grows with the dialogue length. An agent that calls tools dozens of times in a loop multiplies the effect. So an agent needs a budget and a step limit (see the lessons “Tool calling, agent and MCP” and “Testing an agent: tools, trajectory, outcome”).
Credits. This is an abstract unit of payment: a prepaid balance or subscription "points" into which the service converts tokens, requests or agent minutes. When credits run out, the service rejects the request or turns the feature off. For QA this is a separate class of scenarios: what the user sees at a zero balance, whether credits are charged for a failed request, whether the counter matches actual spending, whether the limit can be bypassed with parallel requests.
What else affects cost and speed:
- Prompt caching. A repeating start of the request (system prompt, long documents) can be cached, and re-reading it is cheaper and faster. The cache works only when the start of the request is identical, so variable parts (time, user name) go to the end;
- Rate limits. Limits on requests and tokens per minute. A 429 response usually means "try again later", and the
Retry-Afterheader says when; - Latency. Time to first token (TTFT) and total time are measured separately. Streaming shows the answer as it is generated, so the user does not wait for all of it;
- Model choice. A smaller model for simple steps lowers cost, but quality is checked on an eval set.
Example: telling a rate limit from an empty balance. Both situations often arrive with code 429, but the reaction is opposite. A temporary rate limit is retried with exponential backoff, while a missing quota is pointless to retry, and a retry loop would only burn time and load the service:
async function withRetry<T>(call: () => Promise<T>, tries = 4): Promise<T> {
for (let i = 0; ; i++) {
try {
return await call();
} catch (e) {
const err = e as ApiError;
// quota exhausted: a retry is pointless, the balance will not return
if (err.code === 'insufficient_quota') throw err;
const retriable = err.status === 429 || err.status >= 500;
if (!retriable || i >= tries) throw err;
// Retry-After if present, else exponential backoff with jitter
const ms = err.retryAfterSec ? err.retryAfterSec * 1000 : 2 ** i * 500 + Math.random() * 250;
await new Promise((r) => setTimeout(r, ms));
}
}
}
The quota error code and the response fields depend on the vendor, so a test substitutes a stub that imitates both cases. The test checks that on a rate limit the client retries with a pause (preferably with a controlled clock, with no real waiting), and on a quota it stops at once and the user sees a clear message, not an endless spinner.
Limits and alternatives. Caching and a smaller model lower the cost but do not remove the main cause of growth: the stretching history. What helps here is trimming or summarising the history, RAG instead of "everything into the context" and hard step and token limits in the agent. For eval runs in CI a cost limit is set as a separate check, and the full run goes at night, not on every commit.
Common mistakes.
- Counting the budget by the price of one request. Consequence: a dialogue or an agent costs several times more than the estimate.
- Retrying 429 without understanding the cause. Consequence: endless retries with an exhausted quota.
- Retrying without a pause and jitter. Consequence: all clients hit the limit at once and amplify the overload.
- No step and cost limit in an agent. Consequence: a looping agent burns the budget overnight.
- Putting variable data at the start of the request. Consequence: the cache misses and the full price is paid.
Check yourself
A 10-turn dialogue: a 1,000-token system prompt, each turn adds 500. How many input tokens does the dialogue pay for in total and why not 6,000?
Answer
The input of call n is 1,000 + 500 × (n − 1), because the history is sent every time. The sum over ten turns: 10 × 1,000 + 500 × (0 + 1 + … + 9) = 10,000 + 22,500 = 32,500 tokens. Unique text is only 6,000, the rest is the history being resent.
A user with a zero balance sees an endless loading state. What is probably wrong in the client and what do you check?
Answer
The client most likely retries the quota error as a temporary one. Check that no retries happen on an out-of-credits error and that the interface shows a balance message. Also check whether credits were charged for the failed request.
Why is putting the time or the user name at the start of the system prompt a poor choice for cost?
Answer
Prompt caching works when the start of the request is identical. A variable string at the start makes the prefix unique every time, the cache misses and the whole long prefix is paid for in full. Variable parts go to the end.