Cost estimation forecasts spend before traffic hits production: per request, per user journey, and monthly rollups. It is how PMs and eng agree on model choices.
Combines tiktoken counts, traffic assumptions, and the price matrix from prior lessons.
Related foundations: recount tokens with tiktoken (Vol. 12) and keep payloads inside the context window (Vol. 11).
Learning Objectives
By the end of this lesson, students should be able to:
- Build per-request and monthly estimators.
- Use p50/p95 token distributions, not only means.
- Include retries, tool loops, and failed calls.
- Sensitivity-test rate and traffic changes.
- Separate COGS estimate from retail price-to-user.
- Document assumptions next to the spreadsheet/code.
Cost estimation is predicting USD (or credits) for LLM usage from expected tokens volumes × rates, including uncertainty and non-happy-path calls.
| Layer | Inputs | Output |
|---|---|---|
| Unit | Tokens/request × rates | $/request |
| Journey | Calls per task × unit | $/task |
| Fleet | Tasks × users × days | $/month |
| Buffer | Retries, spikes, p95 | Contingency |
Code: Monthly Sketch
Point estimate
- Fast
- Hides tail risk
- OK for drafts
Distributional
- p95 aware
- Needs traces
- Better budgets
Scenario table
- What-if rates/traffic
- Extra work
- Exec friendly
Strengths
- Aligns model choice with finance
- Surfaces RAG/top-k as $ knobs
- Prevents surprise invoices
Tradeoffs
- Garbage assumptions → garbage $
- Price cards drift
- Omitting agent loops underestimates
“Average tokens × average price × requests is always enough.” Agent retries and p95 prompts dominate bills. Estimate with tails and failure paths.
Knowledge Check
- Short Answer: What three factors enter a basic estimate? Answer: Token volumes, rates, and request/traffic counts.
- True/False: Retries should be included in estimates. Answer: True.
- Multiple Choice: p95 token usage helps: (a) ignore tails, (b) plan for expensive outliers, (c) style CSS. Answer: (b).
- Short Answer: How does tiktoken help estimation? Answer: Measures prompt tokens for templates/samples.
- True/False: COGS estimate equals what you charge customers. Answer: False—retail may differ.
- Multiple Choice: Agent tool loops mainly: (a) reduce calls always, (b) multiply calls/tokens, (c) delete rates. Answer: (b).
- Short Answer: Why document assumptions? Answer: So finance/eng can refresh when traffic or prices change.
- Short Answer: Name a sensitivity to test. Answer: Rate change, traffic 2×, or top-k change.
- Multiple Choice: Monthly rollup uses: (a) only one demo call, (b) unit cost × volume, (c) font kerning. Answer: (b).
- True/False: Estimation is optional if you have a big credit card. Answer: False—still need control.
Key Takeaways
- Estimate unit, journey, and monthly layers.
- Include tails, retries, and loops.
- Version assumptions with the price matrix.
- Use estimators to choose models and k.
- Next: Rate Limiting.
Lab: Estimate monthly COGS for 10k DAU with 3 calls/user; show p50 vs p95 token cases.
Discussion: How do you present uncertainty to executives without losing the decision?
Recap: Estimation turns tokens and rates into planned spend. Continue with Rate Limiting.