Estimating LLM API Costs from Call Logs to Optimize Spending
Subscription plans often issue credits that different models consume at different rates, making it hard to tell which model is actually cheap. This article shows how to log real API calls, fit each model's billing coefficients with regression, convert results into usable token budgets per billing cycle, and route tasks of different difficulty to different model tiers to control cost.
Why a Model That “Feels Cheap” May Not Be Cheap
Many subscription plans grant quotas as “credits usable within a billing cycle,” and each model deducts credits at its own consumption rate. Platforms rarely surface the per-model deduction coefficients, so developers pick models by impression. With the same quota, one person can work for a month while another runs out in days.
The approach in this article is: record real call logs, fit the billing coefficient of each model, and then use those coefficients for budgeting and model selection. The numbers shown come from the author’s personal tests on one plan and are only meant to illustrate the method; they do not represent any platform’s official pricing.
Step 1: Collect Call Logs
Record at least the following fields for every call:
- Model identifier;
- Input tokens (prompt_tokens);
- Output tokens (completion_tokens);
- Cached tokens (cached_tokens);
- Credit balance change before and after the call;
- Request ID, timestamp, whether streaming was used, and retry count.
The request ID and retry count are used to exclude duplicate charges. An example record format:
record = {"request_id": "req_0001","model": "model_x","prompt_tokens": 12800,"completion_tokens": 2400,"cached_tokens": 9600,"credits_delta": -152.3,}
Accumulate at least several dozen records per model, covering different input/output ratios and cache hit rates. If samples concentrate on a single call pattern, the coefficients cannot be separated from each other.
Step 2: Fit a Billing Formula with a Cache Term
In most APIs, the returned prompt_tokens already include cached_tokens. Therefore, the number of uncached input tokens should be prompt_tokens minus cached_tokens. Fit using this formula:
consumption = α × (prompt_tokens − cached_tokens) + β × completion_tokens + γ × cached_tokens + δ
Here, α is the unit price of uncached input tokens, β is the output unit price, γ is the cached-token unit price, and δ is an intercept that absorbs any fixed or minimum per-call charge.
If you put prompt_tokens and cached_tokens into the regression as independent variables at the same time, the input gets priced twice, the two variables become highly correlated, and the coefficients become unstable.
Split the data into a training set and a holdout set, for example 7:3. Solve the coefficients with least squares on the training set, then check residuals on the holdout set. R² alone is not enough—an R² close to 1 can still be overfitting. If holdout residuals grow noticeably, the billing rule is probably nonlinear; common causes include minimum charges, per-call surcharges, and long-context surcharges. In that case, build piecewise models.
If the cache hit rate barely varies in your data, γ is not identifiable, and you need additional call records with different cache hit rates.
Step 3: Convert to Usable Tokens per Billing Cycle
Let the cycle quota be Q credits, and the per-million-token prices be α, β, γ (in credits). Assuming a call structure of 80% input and 20% output with no cache hits, the blended cost is:
Blended cost per million tokens = 0.8 × α + 0.2 × β
Usable tokens per cycle = Q ÷ blended cost
The following is an example with hypothetical values, not data from any platform. The cycle quota is 60,000 credits (a 5-hour cycle). Assume the input price is 1,500 credits per million tokens and the output price is 7,500 credits per million tokens, i.e., output costs 5× input.
Blended cost = 0.8 × 1500 + 0.2 × 7500 = 2700 credits per million tokens
Usable tokens = 60000 ÷ 2700 ≈ 22.2 million tokens
If 60% of the input hits the cache, and cached tokens are billed at 2% of the uncached price, the effective input unit price becomes:
1500 × (0.4 + 0.6 × 0.02) = 618 credits per million tokens
The output part stays unchanged under the same convention, so blended cost = 0.8 × 618 + 0.2 × 7500 = 494.4 + 1500 = 1994.4 credits per million tokens
Usable tokens = 60000 ÷ 1994.4 ≈ 30.1 million tokens, about 35% more than without caching. The 2% figure comes from the author’s samples; another platform or model may differ.
The original data also included a weekly quota, but its accounting convention is inconsistent with the 5-hour cycle quota. Cycles cannot simply be multiplied—there are about 33.6 five-hour cycles in a week. If a plan also imposes a weekly quota, compute both limits separately and take the tighter one.
Usable tokens also do not equal the number of completed tasks. The same task may consume different amounts of tokens on different models, so measure again with your own task samples.
Step 4: Assign Model Tiers by Task Difficulty
The idea behind multi-model tiering is that expensive models handle planning and review, while cheaper models handle execution. One common set of task labels:
| Task label | Typical tasks | Suggested tier |
|---|---|---|
| plan | Breaking down complex requirements, designing architecture | Flagship reasoning model |
| review | Final review of critical code | Flagship reasoning model |
| reason | Intermediate tasks requiring multi-step derivation | Mid-to-high-end reasoning model |
| execute | Code generation, rewriting, debugging, documentation | High cost-performance workhorse model |
| batch | Bulk formatting, simple classification, sample expansion | Lightweight fast model |
The routing layer picks a model by label and falls back to the workhorse model on timeout. The code below is illustrative, where llm is a hypothetical client object:
import loggingMODEL_ROUTE = {"plan": "tier_flagship","review": "tier_flagship","reason": "tier_mid","execute": "tier_value","batch": "tier_lite",}FALLBACK = "tier_value"def call(task_type, prompt, timeout=60):
