Module 9 · 6 lessons
Cost and latency
What one request costs, how sending easy questions to a small model saves money, and how to cap spending per customer.
Before you start
From this track: Modules 0 to 8. Each module uses only ideas from the ones before it.
Builds on: Capacity and cost estimation, Rate limiting, Observability, SLOs and error budgets
Already know these? Start with the first lesson below.
1. What one request costs
The picture
Two meters on every request
Each request pays one price for the tokens it sends and a higher one for the tokens the model writes.
Scene 1 of 4 · Two prices
At example prices, input costs $3 per million tokens and output costs $15: 5 times more.
- One request
- not shown yet
- 1,000,000 requests
- not shown yet
- Two prices. At example prices, input costs $3 per million tokens and output costs $15: 5 times more.
- Count the input. The 2,000 prompt tokens cost $0.006.
- Count the output. The 300 answer tokens cost $0.0045: close to the input, from far fewer tokens.
- Add them up. One request costs $0.0105; 1,000,000 requests a month cost about $10,500.
- Now you try. Change the prompt size; the answer stays at 300 tokens. For example: $0.0285 a request, 84% of it for the prompt; 1,000,000 requests cost about $28,500. Output costs more per token, but a long enough prompt still makes input the bigger part.
Scene 1 of 4: Two prices. At example prices, input costs $3 per million tokens and output costs $15: 5 times more.
Key points
- Input and output tokens have separate prices.
- Output often costs several times more per token.
- A long prompt can still cost more than the answer.
- Tiny sums per request add up across a month of traffic.
Model providers charge by the (A piece of text that a language model reads or writes, often part of a word. Prices and limits are counted in tokens.): one price for the tokens you send, and another, usually higher, for the tokens the model writes.
So one request costs its (Everything sent to a language model in one request: instructions, context and the question.) tokens times the input price, plus its answer tokens times the output price.
In system design: Long prompts, such as many retrieved (A short passage cut from a longer document, so it can be turned into an embedding and found on its own.), raise the cost of every request, so send only the context the answer needs.
Where it goes wrong
- Prices differ by model and change over time. Keep prices in settings, not in code, and record the model used for each call.
- Tokens you never see are billed too: instructions, tool descriptions and, on models that reason first, their working tokens.
In an interview
- Work out the cost of one request from tokens in and out, then multiply by requests a day.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Official docsPricingAnthropic docs (one provider)Real prices per million input and output tokens. Output tokens cost more than input tokens.
- Official docsToken countingAnthropic docs (one provider)Counting a prompt’s tokens before you send it, to know its cost in advance.
2. What really drives the bill
The picture
One extra call to the small model.
- One plain call
- $0.0105
- The real request
- $0.0376, 3.6 times as much
- Model calls per request
- 4
- Adds the most
- Extra agent steps
- A month at 10,000 requests a day
- $11,277
How it is worked out: each request has a 2,000-token prompt and a 300-token answer, at $3 in and $15 out per million tokens. An earlier turn is a 100-token question and its answer. Each agent step adds a 500-token tool result, and every step but the last writes a 50-token tool call.
What this simplifies. The token sizes and prices are round examples, not any provider's real figures. A refused call, such as a 429, is usually not billed, so only retries after the model has started count here. Cached prompt tokens, which many providers price lower, would cut the history and agent steps.
The price of one call is only the start. In a real feature, one user request often becomes several billed calls, and each can be larger than the last.
A chat resends the earlier turns with each new question, so the prompt grows as the chat goes on. An (A model that calls tools in a loop, deciding each next step from the results, until it can answer.) calls the model once per step, and each step resends the tool results so far.
A retry after the model has started writing pays for those tokens again. A separate safety check on each answer is one more call.
In system design: Measure tokens per user request, not per call. Then limit each extra: trim old turns, cap agent steps, and retry only when it is worth paying again.
Where it goes wrong
- Measuring cost per call hides the real cost per user request, which can be several calls.
- A retry after a (The time a caller waits before giving up on a call and treating it as failed.) pays again for tokens the model already wrote.
In an interview
- Name the multipliers in your design, such as chat history, agent steps, retries and checks, and cap each one.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideBuilding effective agentsAnthropic engineering blogAgents trade higher cost and latency for better results on open-ended tasks.
- Official docsPrompt cachingAnthropic docs (one provider)One way to cut the cost of resending the same long prompt start.
3. Cost and speed go together
The picture
Fewer tokens, faster and cheaper
Trimming the prompt cuts both the bill and the wait, while streaming only changes how soon the first words appear.
Scene 1 of 4 · Eight chunks
With 8 retrieved chunks, the prompt is 3,450 tokens, and the chunks are most of it.
- Prompt tokens
- 3,450
- Input cost (example prices)
- $0.0104
- Prefill time
- 138 ms
- First words, streamed
- not shown yet
- Whole answer
- not shown yet
- Eight chunks. With 8 retrieved chunks, the prompt is 3,450 tokens, and the chunks are most of it.
- Four chunks. Sending the best 4 instead makes it 1,850 tokens, about half.
- Cheaper and faster. Input cost falls from $0.0104 to $0.00555, and prefill from 138 to 74 ms.
- Streaming helps the feel. Streaming costs the same, but the first words show after 123 ms, not after the whole 14.8 s answer.
- Now you try. Change how many retrieved chunks go into the prompt. For example: 5,050 prompt tokens: $0.0152 of input and 202 ms of prefill per request. Each chunk adds tokens you pay for and wait for; the 300-token answer still takes about 14.9 s, so stream it.
Scene 1 of 4: Eight chunks. With 8 retrieved chunks, the prompt is 3,450 tokens, and the chunks are most of it.
Key points
- Retrieved chunks are often most of the prompt.
- Fewer prompt tokens cost less and read faster.
- Streaming does not cut the cost at all.
- It does show the first words long before the answer is done.
A longer prompt takes longer to read, and a longer answer takes longer to write. So trimming tokens saves time as well as money.
Streaming the answer does not make it cheaper. But users see the first words sooner, which makes the wait feel shorter.
In system design: Give each part of the prompt a token budget: the instructions, the retrieved chunks and the conversation so far.
Where it goes wrong
- Sending extra context "to be safe" slows every request and raises every bill.
- Cutting context too far drops the passage the answer needed, so quality falls. Test each cut on your saved questions.
In an interview
- Give each part of the prompt a token budget, and say how you chose it.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Official docsReducing latencyAnthropic docs (one provider)Fewer tokens in and out means a faster answer; streaming shows the first words sooner.
- Research paperDistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM Serving (2024)Zhong et al., USENIX OSDIWhy the wait for the first token and the speed of later tokens are separate costs.
4. Sending easy questions to a small model
The picture
- One request, large model
- $0.0105
- One request, small model
- $0.0009
- A month with this mix
- $1,129
- A month, all on the large model
- $3,150
- Saved by routing
- $2,021 (64%)
Example prices per million tokens: large model $3 in and $15 out; small model $0.25 in and $1.25 out.
What this simplifies. The prices are examples, not any provider's real prices, which vary and change often. The small model is assumed to answer its share well; in practice you check that with a test set. Cached input tokens, retries and failed calls also change the real bill.
Where one request's time goes
Adds 150 ms. The text it finds is part of the prompt tokens above.
- First words on screen after
- 229 ms
- Whole answer after
- 14.9 s
How it is worked out: network 100 ms; each prompt token takes 0.04 ms to read; each answer token takes 49 ms to write. So a longer prompt delays the first words, and a longer answer delays the end.
What this simplifies. These times are worked out from example speeds. They are not measurements. The large model uses the Serving lab's speeds for a full batch of 8 requests. The small model is assumed to be three times as fast. Network and search are fixed example times. Real times change with load, hardware and the provider.
Small models usually cost far less per token and answer faster, but they make more mistakes on hard questions.
(Sending each request to the cheapest model likely to answer it well, such as easy questions to a small model.) sends each question to the cheapest model likely to answer it well, such as simple lookups to a small model.
The (A network device that passes each packet on, one step at a time, towards the machine it is addressed to.) can be simple rules or a small model that sorts questions into easy and hard. Hard or failed cases can go up to the large model.
In system design: Measure answer quality on each route with a test set, so the savings do not quietly cost you correct answers.
Where it goes wrong
- The router can be wrong and send hard questions to the small model. Measure quality on each route.
- Two models mean two sets of prompts, tests and failures to watch.
In an interview
- Say how the router decides, what share goes to each model, and how you check quality on each route.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperRouteLLM: Learning to Route LLMs with Preference Data (2024)Ong et al.A router that sends each question to a cheap or a strong model, with measured savings.
- Research paperFrugalGPT: How to Use Large Language Models While Reducing Cost and Improving Performance (2023)Chen, Zaharia and ZouTrying a small model first and calling a bigger one only when the answer looks weak.
5. Capping spend per customer
The picture
A budget checked on every call
Each customer gets a token budget, checked before every model call, with a warning before the limit and a clear refusal at it.
Scene 1 of 5 · A monthly budget
The plan allows 2,000,000 tokens a month, checked in a fast store before every model call.
- Customer — healthy
- Token budget — healthy
- Language model — healthy
- Requests this month
- 0
- A monthly budget. The plan allows 2,000,000 tokens a month, checked in a fast store before every model call.
- Hold the most needed. Before the call, it holds 1,700 prompt tokens plus the 1,000-token longest answer: 2,700 in all.
- Record the real count. The answer used 300 tokens, so the hold is released and the real 2,000 is recorded.
- Warn near the end. After 800 requests of 2,000 tokens, 80% is used, and the customer gets a warning.
- Request 1,000 refused. After 999 requests, 2,000 tokens are left: too few to hold 2,700, so request 1,000 is refused.
- Now you try. Change the tokens each request really uses, and see how far the plan goes. For example: 499 requests run, with a warning after request 400; request 500 is refused. The budget counts tokens, not requests, so longer requests use it up faster.
Scene 1 of 5: A monthly budget. The plan allows 2,000,000 tokens a month, checked in a fast store before every model call.
Key points
- Count tokens per customer, the way a rate limiter counts requests.
- Hold room for the longest answer allowed, then record the real count.
- Warn customers well before they reach the limit.
- At the limit, refuse with a clear message, not a surprise bill.
One customer, or one runaway script, can send thousands of long requests and run up a large bill.
Give each customer a token budget per day or month, and a (A cap on how many requests a client can make in a given time; requests over the cap are delayed or rejected.) per minute, checked before the model is called.
The answer length is unknown in advance, so reserve the largest answer allowed, then record the real count afterwards.
When a customer nears their limit, warn them first, then slow or refuse requests, instead of surprising them with a bill.
In system design: Count tokens per customer in a fast store, the way a rate limiter counts requests, and check that count on every call.
Where it goes wrong
- A budget counted on each server alone lets a customer go over the total. Count in one shared store.
- A hard stop with no warning surprises customers. Warn them first, such as at 80%.
In an interview
- Describe the budget check before each call, and what the customer sees near the limit and at it.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideOWASP Top 10 for LLM Applications: LLM10 Unbounded ConsumptionOWASP GenAI Security ProjectHow one user or script can run up a bill, and quotas and rate limits that stop it.
- Official docsRate limitsAnthropic docs (one provider)A provider’s own spend limits and per-minute limits, as an example of the same idea.
6. Tracing one request
The picture
One request, every step timed
A trace gives one request an ID and records every step under it, so a slow or costly request shows which step to blame.
Scene 1 of 5 · One ID per request
The request gets a trace ID, and each step records its time and tokens under it.
- Your app — starts the trace — healthy — trace 7f3a
- Document search — healthy
- Language model — healthy
- Order system — healthy
- One ID per request. The request gets a trace ID, and each step records its time and tokens under it.
- Search, then plan. Search takes 150 ms, and the first model call takes 1.0 s to choose a tool.
- Tool, then answer. The order lookup takes 300 ms, then writing the answer takes 5.9 s.
- Where the time goes. Of 7.4 s in all, writing the answer token by token takes the most.
- A slow request. On a slow day, the same trace shows search grew from 150 ms to 1.8 s.
- Now you try. Pick a request, and read its trace to find the step that grew. For example: Total 11.1 s, up from 7.4 s. "Tool: look up the order" grew by 3.7 s. The span that grew points at the part to fix, before anyone guesses.
Scene 1 of 5: One ID per request. The request gets a trace ID, and each step records its time and tokens under it.
Key points
- Every step of one request is recorded under the same trace ID.
- Each step, or span, keeps its time, tokens and cost.
- Put a slow trace beside a normal one to see which step grew.
- Writing the answer is often the longest span, one token at a time.
One AI request can touch several parts: a search, two model calls and a tool. When it is slow or costly, averages for each part cannot say which step was to blame this time.
A (The record of one request across every part it touched, kept under one ID.) gives the request one ID and records every step under it. Each step is a (One step inside a trace, with what ran, when it started and how long it took.): what ran, when it started, how long it took, and its tokens and cost.
To find a problem, compare a slow trace with a normal one, and look for the span that grew. Often the longest span is writing the answer, one token at a time.
Traces hold prompts and answers, so they need the same care as logs: limit who can read them, hide private fields and delete old ones.
In system design: Use the same tracing as the rest of your services, and add the model, tokens and cost to every model span.
Where it goes wrong
- Traces that store full prompts copy personal data into one more system. Hide private fields and limit who can read them.
- Keeping every trace costs a lot. Keep a sample of normal ones and all the slow or failed ones.
In an interview
- Say what each span records, such as model, tokens, cost and time, and how one trace ID joins the steps.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Official docsSemantic conventions for generative AI systemsOpenTelemetry (marked as still in development)Standard names for the spans of a model call, so a trace shows tokens and time per step.
- Official docsTracesOpenTelemetry documentationWhat a trace and a span are, and how one ID links every step of a request.