# Build a Cost Model for the System

> How to build a monthly cost model for an LLM system: the parts of the bill, the numbers behind them and the levers that move it, for your AI interview.

**In short:** A cost model adds up every part of an AI system's bill: input and output tokens, retries, search, tool loops and fixed costs, multiplied by the real volume. Then it tests the levers, such as caching, batch processing and routing, to see which one moves the total most.

AI and LLM interview question Q368, LLM system design. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/build-a-cost-model-for-the-system

## Say it in 30 seconds

A cost model adds up every part of the bill for one month of an AI system. I start with the calls, add the retries, then price the input, cached and output tokens, plus search and the fixed lines. With made-up prices, a call-centre copilot with 180,000 calls a month comes to about five thousand dollars. Then I test the levers, like caching and routing, which means picking the right model for each call, to see which one moves the total most.

## Say it in 2 minutes

A cost model is the sum I write down before launch, so nobody is surprised by the bill. Every line is one part of the work: input tokens, cached input, output tokens, retries, tool loops, search, and the fixed costs like the search index and nightly tests. Each line is the volume times the price per unit, and the price comes from the provider's pricing page on the day I check it.

I start with one call and then count the calls. Take a call-centre copilot at a telecom firm, with 180,000 calls a month, 5 percent of them retried, and 60 percent of the input read from cache. With made-up prices, the base month comes to about five thousand dollars, and the model tokens are about half of that. A 60 percent increase in calls lifts the total about 30 percent, because the fixed lines stay the same.

Then I test the levers. Caching the repeated start is a big one, because a cache read costs a fraction of the normal input price. Batch processing is half price for work that can wait a day. Routing simple calls to a smaller model cuts the cost per token. I check each one against quality, so the cheaper answer is still a good one.

Last, I compare the model with the real bill every month and set a spending cap, because one open endpoint can be used to run up the cost. A model that nobody checks against the real bill goes stale quickly.

## What is a cost model, in plain words?

A cost model is a sum you write down before launch. It lists every part of the bill, works out what one unit of work costs, and multiplies by the work you expect each month.

A hospital finance team does the same for a ward: staff hours, medicines, beds and cleaning, each with its own driver. In an AI system the unit is the token, a small piece of text that the model reads and writes. The model charges for every token, in and out. So a call-centre copilot that reads 4,000 tokens of rules and transcript per call pays for every one of them.

> "A cost model helps segment expenses and estimate and forecast the total cost of ownership, including infrastructure, support, and implementation." (Microsoft Azure Well-Architected Framework)
>
> "You cannot optimize what you cannot see." (AWS Well-Architected Framework)

- **Token:** A small piece of text. The model bills for every token it reads and every token it writes.
- **Cached input:** Repeated text, like the same rules on every call, that the provider keeps ready and bills at a lower rate.
- **Retry:** Sending a call again after a bad reply or a timeout. The new call is billed too.
- **Cost driver:** The part of the bill that grows fastest as the volume grows.

## Which parts make up the bill?

*Diagram, What one call and its extras add to: One call sends tokens and can trigger extras. Every line goes into one monthly bill, so a missing line means a wrong total. Blue is the main path, orange the extra costs, green the total.*
1. Each call starts the token bill: the words sent in and the words sent back.
2. Retries, tool loops and search add their own lines, often more than people expect.
3. The token lines go into the monthly bill.
4. The extra lines go into the same bill, so nothing is left out.

- **Input tokens:** The rules, the call transcript so far and the policy passages that search pulled in.
- **Cached input:** The same start on every call, kept ready by the provider and billed at a lower rate.
- **Output tokens:** The summary and the suggested reply. In the example prices, output costs five times input per token, so long replies drive the bill.
- **Retries:** A bad reply or a timeout sends the call again, and you pay for the new call too.
- **Tool loops:** Each tool call can send the model back for another turn, with its own input and output.
- **Search and fixed costs:** A lookup for every question, plus the search index, logs and nightly tests every month.

## How do you turn the parts into one monthly number?

1. **Count the work:** Calls per month, plus the share that gets retried. A retry is a real call, so it counts.
2. **Measure the tokens:** Input, output and cached input for a typical call. Take them from logs, not from a guess.
3. **Price each part:** Tokens times the price per million, from the provider's pricing page on the day you check it.
4. **Add the fixed lines:** The search index, logs and nightly tests do not grow with each call, so add them once.
5. **Run three scenarios:** A base month, a peak month, and one lever switched off, so you can see what moves the bill.

Here it is in plain Python. The telecom copilot is made up, and so are the prices. The structure is what matters.

```python
# A made-up call-centre copilot at a telecom company: the monthly bill for the whole system.
PRICE_IN, PRICE_OUT = 3.00, 15.00  # made-up dollars per million tokens
CACHE_READ = 0.10                  # cached input is billed at 0.1 times the base input price
PROMPT, OUTPUT = 4000, 500         # tokens per call: rules and transcript in, summary out
CALLS = 180_000                    # calls answered in a month
RETRY = 0.05                       # 5% of calls are sent again after a bad reply or a timeout
SEARCH = 0.0004                    # made-up dollars per sent call for the policy search
FIXED = 2500                       # made-up dollars a month: search index, logs, nightly tests

def bill(calls, cached_share):
    sent = calls * (1 + RETRY)
    prompt = PROMPT * (cached_share * CACHE_READ + (1 - cached_share))
    model = sent * (prompt * PRICE_IN + OUTPUT * PRICE_OUT) / 1_000_000
    return round(model, 2), round(model + sent * SEARCH + FIXED, 2)

model, total = bill(CALLS, 0.6)
print("Base month: model", model, "total", total)
print("Peak month, 1.6 times the calls:", bill(CALLS * 1.6, 0.6)[1])
print("Caching switched off:", bill(CALLS, 0.0)[1])
print("Cost per call, base month:", round(total / CALLS, 4))
```

Output:

```text
Base month: model 2460.78 total 5036.38
Peak month, 1.6 times the calls: 6558.21
Caching switched off: 6261.1
Cost per call, base month: 0.028
```

This model leaves out one cost: writing the cache. Anthropic says cache writes on a 5-minute cache cost 1.25 times the base input price, so a cache that is rewritten often costs more than this shows. Add that line once the hit rate is measured from logs.

- **Base: 180,000 calls, 60% cached.** Total $5,036. Model tokens are $2,461 of it.
- **Peak: 1.6 times the calls.** Total $6,558. Calls rise 60 percent, the total about 30 percent, because the fixed lines stay flat.
- **Lever off: Caching switched off.** Total $6,261. Turning caching off adds $1,225 to the bill.

Cost per call in the base month is about 3 cents. If the copilot needs a person to finish some calls, also divide the bill by the calls that were solved. That gives the cost per solved case, which is the number a manager can act on.

## Which lever moves the number most?

Each lever changes the bill in a different way. Test each one with your own numbers, because the answer depends on how much of the work is repeated text, how much can wait, and how simple the calls are.

> "cutting costs by 50 percent and increasing throughput." (Anthropic, on its Batch API)
>
> "Cache read tokens are 0.1 times the base input tokens price" (Anthropic, on prompt caching)

*Diagram, Send each call to the cheapest model that can do it: Route each call to the cheapest model that can do it. Simple rules in code do the routing, and the same checks run on both paths.*
1. Each call arrives with its question attached.
2. Routine calls go to the small model, which costs less per token.
3. Hard or risky calls go to the large model.
4. The small model's answer goes out after the same checks.
5. The large model's answer goes out after the same checks.

- **Cache the repeated start:** Rules and tool definitions repeat on every call. The cache reads them back at a fraction of the normal price.
- **Batch what can wait:** Nightly summaries and test runs do not need an answer in seconds. OpenAI's batch service costs 50 percent less and completes each batch within 24 hours.
- **Route easy calls down:** Routine calls go to a smaller model that costs less per token. Check quality on a test set before you switch.
- **Trim the prompt:** Send only the policy passages the call needs, not the whole handbook.
- **Cap the loops:** Stop tool loops after a fixed number of steps, so one call cannot run on forever.

## How do you keep the cost model honest over time?

AWS calls cost optimisation a continual process, not a one-off. Track the real bill every month, compare it with the model, and fix the driver that moved.

Put a budget and a spending cap on every key and every user. OWASP's 2026 list, under Unbounded Consumption, describes Denial of Wallet, where attackers run up the bill of a pay-per-use AI service and the provider carries the cost. A public endpoint with no cap is an open bill.

> **Tip:** In an interview, a cap plus a monthly check against the real bill shows more care than a perfect first estimate.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Starts from units: calls, tokens, retries and the fixed lines.
- [ ] Names caching and batch as levers, and says to check their prices on the pricing page.
- [ ] Uses a peak month and a switched-off lever, not one number.
- [ ] Measures cost per solved case, so cost and quality sit in one number.
- [ ] Puts a spending cap and a rate limit on every public endpoint.
- [ ] Compares the model with the real bill every month.

**Common traps**

- Trap: "Token prices are tiny, so the bill will be small." Better: Tokens add up fast at scale. Multiply by calls, retries and the peak month before you call it small.
- Trap: "We will use the biggest model so quality is safe." Better: The biggest model costs the most per token. Route the easy calls down and check quality with a test set.
- Trap: "The provider's pricing page is the whole model." Better: The page gives unit prices only. Volume, retries, tools and the fixed lines decide the bill.

## Can you spot the wrong line in a cost model?

**Copilot cost model:** Which line gets the units wrong? Prices are per million tokens.

A. sent = calls * (1 + RETRY)
B. model = sent * (prompt * PRICE_IN + OUTPUT * PRICE_OUT) / 1_000
C. round(model + sent * SEARCH + FIXED, 2)
D. prompt = PROMPT * (cached_share * CACHE_READ + (1 - cached_share))

**Answer:** B. The prices are per million tokens, so the model line must divide by 1,000,000, as the code above does. Dividing by 1,000 makes the token bill a thousand times too big.

**Quick check:** A call-centre summariser runs 40,000 times a night on yesterday's transcripts, and nobody waits for the result. Which option is the cheapest way to run it?

A. Real-time calls at the standard price, so results land during the shift
B. The batch service, which costs less and completes within a day
C. A bigger model, so the summaries need fewer retries
D. Streaming each transcript as it ends, so the model reads it in pieces

**Answer:** B. Nobody waits, so the work can go into a batch. OpenAI and Anthropic both price batch work at half off. The other options pay the full price or add a bigger model.

## Can you answer these from memory?

**Flash cards**

- **What is the first step in a cost model?** Count the work: calls per month, plus the share that gets retried.
- **What is a cost driver?** The part of the bill that grows fastest as the volume grows.
- **What does a cached read cost on Anthropic's API?** 0.1 times the base input token price, according to the prompt caching docs.
- **Which work suits the batch service?** Jobs that do not need an immediate answer, like nightly summaries and test runs.
- **Why do fixed lines stay flat when calls rise?** Search index, logs and nightly tests do not grow with each call.
- **What is Denial of Wallet?** Attackers run up the cost of a pay-per-use AI service, and the provider carries the bill.

## Likely follow-up questions

- Q324 How do you estimate token cost for a feature? https://designervenkat.online/interview/ai/how-to-estimate-token-cost
- Q356 Design rate limiting. https://designervenkat.online/interview/ai/design-rate-limiting
- Q359 Design an insurance-claims agent with RAG and cost control. https://designervenkat.online/interview/ai/design-an-insurance-claims-agent
- Q369 How do you design for low latency? https://designervenkat.online/interview/ai/how-do-you-design-for-low-latency
- Q355 What is your fallback when a provider is down? https://designervenkat.online/interview/ai/what-is-your-fallback-when-a-provider-is-down

## Learn it in depth

- [Watching cost and speed](https://designervenkat.online/learn/ai-engineering/cost-latency-monitoring)
- [Prompt Caching](https://designervenkat.online/learn/ai-engineering/prompt-caching)
