What a request costs: tokens, caching and routing
- Price a request from its input and output tokens, and say which part of the bill is bigger
- Work out what prompt caching saves at a given traffic, and pick no cache, a 5-minute cache or a 1-hour one
- Cost a small model that answers first and sends hard questions on to a large one
- Say when renting GPUs beats paying per token, and why a GPU's price per token follows its traffic
- Cut a feature's monthly bill step by step and say what guards its quality
Pricing an LLM feature, the way the ML round asks
An Airbnb ML round asked candidates to trade off precision, recall, latency and infrastructure cost (1 report). For an LLM feature, the cost side comes down to three questions, and this lesson's practice problem answers them in code, in three levels:
request_cost(price, input_tokens, output_tokens, read, write, ttl): what one request costs, with cached input priced as a read or a write.hit_rate(per_hour, ttl_minutes),expected_cost(...)andbest_ttl(...): what caching the shared prompt saves at a given traffic, and which cache lifetime to buy.cheaper(api_per_mtok, hourly, capacity_tps, demand_tps): whether renting GPUs beats paying per token.
Call Returns request_cost(sonnet, 1860, 250)0.00622 dollars hit_rate(6, 5)0.393 of requests hit best_ttl(sonnet, 1800, 60, 250, 0.5)"none": too rare to cache cheaper(0.50, 4.89, 22_852, 350)("api", 0.63): the API, at 63 cents an hour
The lesson follows the outdoor shop's support assistant at 3.6 million questions a month: one request's price, caching the shared prompt, routing and batching, and renting GPUs. Seven steps end with a small task in the editor.
A request's price is its input tokens and its output tokens, priced separately
The outdoor shop's support assistant gets 120,000 questions a day, 3.6 million a month. Each request is the same shape as before: 1,800 tokens of instructions and catalog, a 60-token question, and an answer of about 250 tokens.
Running the model on a rented GPU was one way to serve it. Calling a provider's API is the other, and the shop pays by the token. Prices change often, so every price here is dated, and the method is what lasts.
Anthropic lists Claude Sonnet 5.5 at $2 per million input tokens and $10 per million output tokens. OpenAI lists gpt-6.2-sol at the same $2 and $10. A million tokens is written MTok on both price pages.
A request costs its input tokens at the input price plus its output tokens at the output price. request_cost adds the two, and you'll write it in the next step.
# $ per million tokens. The write and read prices are for caching, two steps on.
SONNET = {"input": 2.00, "output": 10.00, "write_5m": 2.50, "write_1h": 4.00, "read": 0.10}
HAIKU = {"input": 0.10, "output": 0.50, "write_5m": 0.125, "write_1h": 0.20, "read": 0.01}
print(f"input ${1860 * SONNET['input'] / 1e6:.5f}, output ${250 * SONNET['output'] / 1e6:.5f}")
print(f"${request_cost(SONNET, 1860, 250):.5f} a request, ${request_cost(SONNET, 1860, 250) * 3_600_000:,.0f} a month")
Output tokens cost 5 times as much as input tokens. Which part of the shop's request costs more?
A small model, priced the same way
Anthropic lists Claude Haiku 5.5, its small model, at $0.10 per million input tokens and $0.50 per million output tokens, for prompts up to 100,000 tokens.
for name, price in (("Sonnet 5.5", SONNET), ("Haiku 5.5", HAIKU)):
print(f"{name}: ${request_cost(price, 1860, 250) * 3_600_000:,.0f} a month")
Haiku's prices are a twentieth of Sonnet's for both input and output. What's the monthly bill on Haiku?
A hit reads the prefix for a twentieth of the price, and a miss writes it for a quarter more
With prompt caching, the provider keeps the computed keys and values of a prompt's opening, the same prefix cache the latency lesson ran on its own GPU. Anthropic charges a 5-minute cache write at 1.25 times the input price and a 1-hour write at 2 times. Reading the cache, a hit, costs 0.1 times the input price in general, and 0.05 times on Sonnet 5.5 and Opus 5.5, which is the $0.10 in the price dict.
def cached_cost(price: dict, shared: int, own: int, output_tokens: int, hit: bool) -> float:
"""One request whose first shared input tokens are read from the cache (a hit) or written to it."""
shared_price = price["read"] if hit else price["write_5m"]
return (shared * shared_price + own * price["input"] + output_tokens * price["output"]) / 1e6
print(f"no cache ${request_cost(SONNET, 1860, 250):.5f}")
print(f"a miss ${cached_cost(SONNET, 1800, 60, 250, False):.5f}")
print(f"a hit ${cached_cost(SONNET, 1800, 60, 250, True):.5f}")
A hit reads the 1,800 shared tokens at $0.10 per million instead of $2. What does a hit cost, against $0.00622 with no cache?
A cache stays warm only while requests keep coming
Anthropic's cache lives 5 minutes by default, and every hit refreshes it for no extra charge. If 5 minutes is too short, a 1-hour lifetime costs more per write.
The cache lives 5 minutes and every hit restarts its clock. Requests come exactly every 4 minutes all day. How many of them miss?
A prefix is requested 6 times an hour, at random, and its cache lives 5 minutes. About what share of requests hit?
Pick the cache lifetime from the traffic
With the hit rate, each lifetime has an expected price for the shared tokens.
def shared_price(price: dict, per_hour: float, ttl: str) -> float:
"""Expected dollars per million shared tokens: a hit reads them, a miss writes them."""
if ttl == "none":
return price["input"]
hit = hit_rate(per_hour, 5 if ttl == "5m" else 60)
return hit * price["read"] + (1 - hit) * price["write_" + ttl]
for per_hour in (0.5, 1, 6, 60):
row = ", ".join(f"{ttl} ${shared_price(SONNET, per_hour, ttl):.2f}" for ttl in ("none", "5m", "1h"))
print(f"{per_hour:4} an hour: {row}")
A prefix gets one request every two hours. Which is cheapest: no cache, a 5-minute cache or a 1-hour one?
A small model answers first, and only the hard questions go on
Most of the shop's questions are about order status and return windows, and a small model can answer them. Some aren't. One way to handle both: Haiku answers every question first, a check decides whether its answer is good enough, and the rest go on to Sonnet. Both models read the shared prompt from their caches.
small_first adds up that bill for a month of requests.
haiku_hit = cached_cost(HAIKU, 1800, 60, 250, True)
sonnet_hit = cached_cost(SONNET, 1800, 60, 250, True)
for sent_on in (0.1, 0.2, 0.5):
print(f"{sent_on:.0%} sent on to Sonnet: ${small_first(haiku_hit, sonnet_hit, sent_on, 3_600_000):,.0f} a month")
print(f"all on Sonnet: ${sonnet_hit * 3_600_000:,.0f} a month")
print(f"it costs as much as Sonnet alone at {1 - haiku_hit / sonnet_hit:.0%} sent on")
Every request pays for Haiku, and the ones sent on pay for Sonnet too. With 20% sent on, what's the bill, against $10,080 for Sonnet alone?
Work that can wait costs half
Both providers sell a batch API that finishes within a day: Anthropic charges all batch usage at 50 percent of the standard prices, and OpenAI's batch API comes at a 50% discount. The batch-inference lesson shows why a provider can afford it: batch work fills the gaps that interactive traffic leaves.
Every night the shop summarizes the day's 120,000 conversations for its support team. Which call fits?
A rented GPU costs the same busy or idle, so its price per token follows its traffic
Renting a GPU is the other way to serve the shop's model.
Lambda rents one H100 SXM for $4.89 an hour, and Runpod for $4.39. The latency lesson worked out a best case of 22,852 output tokens a second for the shop's 7-billion-parameter model on one H100 with a full batch.
The shop's 1.4 questions a second at 250 tokens each make 350 output tokens a second.
print(f"busy all hour: ${gpu_per_mtok(4.89, 22_852, 1.0):.3f} per million")
print(f"the shop's traffic: ${gpu_per_mtok(4.89, 22_852, 350 / 22_852):.2f} per million")
At the shop's 350 tokens a second, what does the rented H100 cost per million output tokens?
When renting beats paying per token
The rent is fixed, so the GPU wins once enough tokens share it.
for hourly in (4.39, 4.89):
print(f"${hourly} an hour beats $0.50 a million above {hourly / 0.50 * 1e6 / 3600:,.0f} output tokens a second")
How many output tokens a second does one GPU at $4.89 an hour need, to cost less than an API at $0.50 per million?
A worked example: cut the assistant's monthly bill below 5,000 dollars
The prompt: the shop's assistant costs $22,392 a month on Sonnet 5.5, and finance wants it under $5,000 without customers noticing. An Airbnb round asked the same kind of question, trading off precision, recall, latency and infrastructure cost (1 report).
Before any change, which number would you ask for first?
Take the bill down one measured step at a time
3. The plan. Each step is cheaper and each one gets checked before the next.
bill = {
"Sonnet, no cache": request_cost(SONNET, 1860, 250),
"cache the shared prompt": sonnet_hit,
"Haiku first, 20% sent on": haiku_hit + 0.2 * sonnet_hit,
"answers cut to 180 tokens": cached_cost(HAIKU, 1800, 60, 180, True) + 0.2 * cached_cost(SONNET, 1800, 60, 180, True),
}
for step, each in bill.items():
print(f"{step:26} ${each * 3_600_000:7,.0f} a month")
Which step saves the most dollars?
A week after launch, the cache hit rate drops from 99% to almost 0. What do you check first?
Agents send their whole history every turn, so tokens grow fast
The follow-up that changes the numbers most is an agent.
# each turn sends everything so far
turns = [2000 + 400 * i for i in range(8)]
print(f"{sum(turns):,} input tokens over 8 turns, against {turns[-1]:,} in the last turn alone")
An agent starts with 2,000 tokens and adds 400 a turn, sending the whole history each time. Over 8 turns, about how many input tokens is that?
Three costs that hide in the bill
Three smaller follow-ups come up too: retries, billing each customer, and long prompts.
Failed and retried requests still cost tokens
A request that times out after 200 tokens has still been billed for them, and a retry pays the input again. Count the tokens of failed requests in the cost per conversation, and cap retries, as the batch-inference lesson does for its workers.
Bill each customer for their own tokens
A business selling an LLM feature records the input, cached and output tokens of every request against the customer who sent it. Then a per-customer budget can cut off one customer's runaway loop before it costs the business a month's margin.
A longer prompt can cost more per token
Haiku 5.5's price page lists $0.10 per million input tokens for prompts up to 100,000 tokens. Above that line the price is higher, so a feature that stuffs whole order histories into the prompt can cross into a pricier tier. Retrieve what the question needs instead.
The practice problem, start to finish
That's every piece the practice problem needs. It asks for the three levels from the start of the lesson, from a blank file, and the interviewer follows up after each one.
1.A provider charges $2 per million input tokens and $10 per million output tokens. What does a request with 1,000 input tokens and 100 output tokens cost?
2.A prompt starts with the current time, then 1,800 tokens of instructions that never change. You turn prompt caching on. What happens to the bill?
3.Each customer's conversation is cached as its own prefix, and customers reply about every 10 minutes. Which cache lifetime fits?
4.Questions go to a small model first, and the ones it can't handle go on to a large model. What gets worse for the questions sent on?
5.A rented H100 costs $4.89 an hour, and your feature produces 350 output tokens a second. Why does it cost more per token than an API charging $0.50 per million?
Where this shows up
The reports behind this lesson, each with its source.
Same round: trading off precision, recall, latency and infrastructure cost.
Multi-agent systems use about 15 times as many tokens as chats, per Anthropic's Research feature write-up.
“multi-agent systems use about 15× more tokens than chats.”
Practice
Sources (16)
- Pricing · Anthropic, Claude Platform Docs · Nov 19, 2026
“Claude Sonnet 5.5 The best combination of speed and intelligence $2 / MTok $10 / MTok $2.50 / MTok $4 / MTok $0.10 / MTok”
- Pricing · Anthropic, Claude Platform Docs · Nov 19, 2026
“Claude Haiku 5.5 For high-volume, latency-sensitive tasks such as classification, extraction, and routing $0.10 / MTok for prompts up to 100,000 tokens $0.50 / MTok $0.125 / MTok $0.20 / MTok $0.01 / MTok”
- Prompt caching · Anthropic, Claude Platform Docs · Nov 19, 2026
“Cache write tokens with a 5-minute TTL are 1.25 times the base input tokens price. Cache write tokens with a 1-hour TTL are 2 times the base input tokens price.”
- Prompt caching · Anthropic, Claude Platform Docs · Nov 19, 2026
“Cache hits and refreshes on Claude Opus 5.5 and Claude Sonnet 5.5 are priced at 0.05x the base input price.”
- Prompt caching · Anthropic, Claude Platform Docs · Nov 19, 2026
“Cache read tokens are 0.1 times the base input tokens price”
- Prompt caching · Anthropic, Claude Platform Docs · Nov 19, 2026
“By default, the cache has a 5-minute lifetime. The cache is refreshed for no additional cost each time the cached content is used.”
- Prompt caching · Anthropic, Claude Platform Docs · Nov 19, 2026
“If 5 minutes is too short, you can use a 1-hour cache duration at additional cost”
- Batch processing · Anthropic, Claude Platform Docs · Nov 19, 2026
“The Batches API offers significant cost savings. All usage is charged at 50 percent of the standard API prices.”
- Pricing · OpenAI API docs · Nov 19, 2026
“| gpt-6.2-sol | $2.00 | $10.00 | $0.10 | $2.50 | $5.00 | $4.00 | $15.00 | $0.20 | $5.00 | $10.00 |”
- Prompt caching · OpenAI API docs · Nov 19, 2026
“Prompt caching is enabled by default for supported OpenAI models.”
- Prompt caching · OpenAI API docs · Nov 19, 2026
“The minimum cacheable prompt length is 1,024 tokens for GPT-5.6 and later and varies by request settings for earlier models.”
- Prompt caching · OpenAI API docs · Nov 19, 2026
“For GPT-5.6 and later, cache writes cost 1.25× the standard, uncached input-token rate for the default 30-minute lifetime.”
- Batch API · OpenAI API docs · Nov 19, 2026
“Better cost efficiency: 50% cost discount compared to synchronous APIs”
- GPU cloud pricing · Lambda, pricing page (1 GPU, price per GPU per hour) · Nov 19, 2026
“NVIDIA H100 SXM 80 GB 26 225 GiB 2.75 TiB SSD $4.89”
- GPU Cloud Pricing · Runpod, pricing page · Nov 19, 2026
“H100 SXM 80 GB VRAM 125 GB RAM 20 vCPUs $ 4.39 /hr”
- How we built our multi-agent research system · Anthropic Engineering · Jun 13, 2025
“In our data, agents typically use about 4× more tokens than chat interactions, and multi-agent systems use about 15× more tokens than chats.”