How do you estimate and control LLM costs in production?
831 views
27 of 83
1
The problem
Question presented to candidate:
"We are about to launch three LLM features: a support chat, ticket tagging, and an agent that resolves simple tickets on its own. Finance wants a monthly cost estimate before launch, and a plan for bringing it down if it is too high. How do you estimate it, and which levers would you pull?"
What a strong answer should cover:
The cost equation per call: input tokens times input price plus output tokens times output price (output is usually priced several times higher), with cache reads, cache writes and batch discounts as multipliers; summed over calls per feature per day
Estimating from token profiles, not from the demo: system prompt and tools, retrieved context, history, expected output, requests per day, and the agent trap where each step re-sends the growing transcript so input grows roughly quadratically with steps
Levers, ranked by impact for the workload: model routing (small model for easy tasks), prompt caching of stable prefixes, batch APIs for anything without a waiting user, trimming context and history, capping and shortening output
Metering: log usage from every response with feature, tenant, model and purpose (answer, retry, repair) so cost is attributed, and report cost per outcome (per resolved ticket), not just per token
Prices and discounts change by model and vendor: read them from the current pricing page and keep them in config, never hard-coded
Clarifying questions expected:
Expected traffic per feature, and how long are prompts and answers in realistic samples?
Which work is interactive and which could run asynchronously?
What quality bar must each feature hold if we move it to a cheaper model?
Code / implementation expected: Optional โ a back-of-envelope estimate with stated assumptions is expected; a small cost calculator or metering sketch is a plus.
cost
tokens
optimization
llmops
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Engineers asked "what will this cost per month?" before an LLM launch, and anyone whose first invoice was a surprise.
Difficulty: Medium
Verified with: Node 22.22.0 and Python 3.12.3 (a cost estimator and a metering ledger). All prices in the examples are a hypothetical price card with round numbers so the arithmetic is easy to follow; they are not any vendor's current prices. Discount mechanics (cache read and write multipliers, batch discounts) are quoted from the Anthropic and OpenAI docs, checked October 2026.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the examples; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
A taxi meter does
The solution is hidden so you can try first
15 min read ยท 11 sections
4
Read the code
Monthly cost estimate and the levers that change it