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, at example prices
  • 2,000 tokens sent$0.006
  • 300 tokens written$0.0045
One request
not shown yet
1,000,000 requests
not shown yet
  1. Two prices. At example prices, input costs $3 per million tokens and output costs $15: 5 times more.
  2. Count the input. The 2,000 prompt tokens cost $0.006.
  3. Count the output. The 300 answer tokens cost $0.0045: close to the input, from far fewer tokens.
  4. Add them up. One request costs $0.0105; 1,000,000 requests a month cost about $10,500.
  5. 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

  1. Input and output tokens have separate prices.
  2. Output often costs several times more per token.
  3. A long prompt can still cost more than the answer.
  4. Tiny sums per request add up across a month of traffic.

Model providers charge by the : one price for the tokens you send, and another, usually higher, for the tokens the model writes.

So one request costs its tokens times the input price, plus its answer tokens times the output price.

Example. At example prices of $3 per million input tokens and $15 per million output tokens, 2,000 tokens in and 300 out cost $0.006 + $0.0045 = $0.0105.

In system design: Long prompts, such as many retrieved , 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

At $3 per million input tokens and $15 per million output tokens, which costs more?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

At $2 per million input tokens and $10 per million output tokens, what do 4,000 tokens in and 500 out cost?

Judge a design

A team budgets from the prompt and answer it can see, on a model that reasons before it answers. What is missing?

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.

Open this lesson on its own page

2. What really drives the bill

The picture

5

Each new question resends the earlier ones and their answers.

3

1 means one plain call. Each extra step is one more model call.

5%

One extra call to the small model.

  • One call: the prompt and the answer$0.0105
  • Earlier chat turns, sent again$0.0024
  • Extra agent steps$0.0228
  • Retries that pay again$0.0018
  • A safety check on each answer$0.0001
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 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.

Example. With the calculator above at its starting values, one request costs about $0.038 instead of $0.0105: 3.6 times one plain call. The extra agent steps add the most.

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 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

An agent takes 3 steps to answer one request. Why can it cost more than 3 times one plain call?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

One user request makes three model calls costing $0.01, $0.015 and $0.02, plus a $0.005 safety check. What does the request cost?

Judge a design

A team reports a cost of $0.01 per call and plans its budget from that, but each user request makes several calls. What is wrong?

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.

Open this lesson on its own page

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.

Where the prompt's tokens go
  • Instructions200 tokens
  • 8 retrieved chunks (highlighted)3,200 tokens
  • Question50 tokens
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
  1. Eight chunks. With 8 retrieved chunks, the prompt is 3,450 tokens, and the chunks are most of it.
  2. Four chunks. Sending the best 4 instead makes it 1,850 tokens, about half.
  3. Cheaper and faster. Input cost falls from $0.0104 to $0.00555, and prefill from 138 to 74 ms.
  4. Streaming helps the feel. Streaming costs the same, but the first words show after 123 ms, not after the whole 14.8 s answer.
  5. 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

  1. Retrieved chunks are often most of the prompt.
  2. Fewer prompt tokens cost less and read faster.
  3. Streaming does not cut the cost at all.
  4. 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.

Example. Illustrative: cutting retrieved context from 8 chunks to 4 might halve the prompt, lowering both the bill and the .

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

Does streaming the answer make a request cheaper?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

Retrieved chunks make up 6,000 of a 7,000-token prompt. What does cutting them to 3,000 do?

Judge a design

A team sends all 20 retrieved chunks with every request, "to be safe". What does that cost?

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.

Open this lesson on its own page

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

Model for this request

Adds 150 ms. The text it finds is part of the prompt tokens above.

  • Network, there and back100 ms
  • Search the documents0 ms
  • Read the prompt, write the first token129 ms
  • Write the rest of the answer14.7 s
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.

sends each question to the cheapest model likely to answer it well, such as simple lookups to a small model.

The 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.

Example. With the calculator above at its starting values, sending 70% of questions to the small model cuts the monthly bill by about 64%.

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

You send 70% of questions to a small model. What must you check?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A large model costs $10 per million tokens and a small one $1. Of 100 million tokens a month, 60% go to the small model. What is the bill?

Judge a design

A team sends every question under 10 words to the small model, and checks only the bill. What is the risk?

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.

Open this lesson on its own page

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.

CustomerCustomerToken budgetToken budgetLanguage modelLanguage model
  • Customer — healthy
  • Token budget — healthy
  • Language model — healthy
This month's plan: 2,000,000 tokens
  • Used0 tokens
  • Held for a call0 tokens
  • Left (highlighted)2,000,000 tokens
Requests this month
0
  1. A monthly budget. The plan allows 2,000,000 tokens a month, checked in a fast store before every model call.
  2. Hold the most needed. Before the call, it holds 1,700 prompt tokens plus the 1,000-token longest answer: 2,700 in all.
  3. Record the real count. The answer used 300 tokens, so the hold is released and the real 2,000 is recorded.
  4. Warn near the end. After 800 requests of 2,000 tokens, 80% is used, and the customer gets a warning.
  5. Request 1,000 refused. After 999 requests, 2,000 tokens are left: too few to hold 2,700, so request 1,000 is refused.
  6. 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

  1. Count tokens per customer, the way a rate limiter counts requests.
  2. Hold room for the longest answer allowed, then record the real count.
  3. Warn customers well before they reach the limit.
  4. 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 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.

Example. A plan with 2 million tokens a month allows about 1,000 requests of 2,000 tokens. The first request that no longer fits gets a clear message that the limit is reached.

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

Under a token budget, why hold room for the longest allowed answer before a call?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A request has a 2,000-token prompt and may write up to 1,000 tokens. How many tokens does the budget check hold before the call?

Judge a design

Each of 5 servers keeps its own count of a customer's monthly tokens. What goes wrong?

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.

Open this lesson on its own page

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 traceYour appstarts the tracetrace 7f3aDocument searchDocument searchLanguage modelLanguage modelOrder systemOrder system
  • Your app — starts the trace — healthy — trace 7f3a
  • Document search — healthy
  • Language model — healthy
  • Order system — healthy
Time spent in each span of one request
  • Search the documents150 ms
  • Model call 1: choose a tool1.0 s
  • Tool: look up the order300 ms
  • Model call 2: write the answer5.9 s
  1. One ID per request. The request gets a trace ID, and each step records its time and tokens under it.
  2. Search, then plan. Search takes 150 ms, and the first model call takes 1.0 s to choose a tool.
  3. Tool, then answer. The order lookup takes 300 ms, then writing the answer takes 5.9 s.
  4. Where the time goes. Of 7.4 s in all, writing the answer token by token takes the most.
  5. A slow request. On a slow day, the same trace shows search grew from 150 ms to 1.8 s.
  6. 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

  1. Every step of one request is recorded under the same trace ID.
  2. Each step, or span, keeps its time, tokens and cost.
  3. Put a slow trace beside a normal one to see which step grew.
  4. 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 gives the request one ID and records every step under it. Each step is a : 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.

Example. In the panel above, a normal request takes 7.4 s, and 5.9 s of that is writing a 120-token answer. When the search is overloaded, its span grows from 150 ms to 1.8 s.

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

A request was slow today. How does a trace help?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A normal request: search 200 ms, model call 4 s, tool 300 ms. A slow one: search 200 ms, model call 4.1 s, tool 3 s. Where do you look first?

Judge a design

A team keeps every trace for a year, with full prompts, and lets all engineers read them. What should change?

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.

Open this lesson on its own page