MODULE 4 · LESSON 16 · 12 MIN
AI Infrastructure: APIs, Hosting, and Cost Models
By Siten Sanghvi · Published · Updated
With a hosted model API, the provider runs the model and you pay by the token: text sent in (input) and text generated (output). Self-hosting swaps that variable bill for servers you pay for by time, busy or idle, and software you operate. On a hosted API the bill follows two design choices: tokens per request and requests per task. Self-hosting turns it into how much capacity you keep running and how busy you keep it.
A token is the chunk of text a model reads or writes (Tokens, Embeddings, and Context Windows Explained). Prices below are vendor list prices checked on 2026-11-09, and they change on a schedule: Google's pricing page lists Gemini 4 Argon's input price doubling on 2026-12-01. Treat them as worked examples, not typical prices.
Three ways to run a model
| Hosted API | Managed cloud platform | Self-hosted open-weight model | |
|---|---|---|---|
| Who runs inference | The model provider | A cloud provider hosts the model for you | You |
| How you are billed | Per token | Per token, or hourly for reserved capacity | Servers by time, plus staff |
| You operate | Your app, keys, budgets | Same, inside a cloud account | Weights, serving software, scaling, patching, uptime |
| Where prompts go | To the provider, under its terms | To the cloud platform, under its terms | Stay on hardware you control; you secure them |
Managed platforms. The same models are sold through clouds with their own billing. Amazon Bedrock also sells Provisioned Throughput: capacity billed hourly at a fixed cost per model unit, on no-commitment, one-month or six-month terms; a committed term cannot be deleted early, and billing continues until you delete it. That bets on steady volume; the Bedrock pages do not say when it beats per-token billing.
Self-hosting. There is no per-token fee, but AWS bills on-demand instances by the hour or second from launch until you stop them, busy or idle. Whether an open model is good enough is a question for evaluation (BT-L19).
How API cost is built
A request costs input tokens × input price plus output tokens × output price, quoted per million tokens (MTok). Output costs more: 5 times input on every model on Anthropic's list, 4 to 8 times across OpenAI's standard list, about 8.3 times for Gemini 3.5 Flash-Lite. The pages give no reason; one-token-at-a-time generation (BT-L09) is the usual technical explanation, but price is the vendor's choice.
List prices, USD per MTok, from the Anthropic, OpenAI and Google pricing pages, checked 2026-11-09 (illustrative; verify before use):
| Model | Input | Output | Cached read | Cache write | Batch in / out |
|---|---|---|---|---|---|
| Claude Sonnet 5.5 | 2 | 10 | 0.10 | 2.50 (5 min) | 1 / 5 |
| Claude Haiku 5.5 (prompts up to 100,000 tokens) | 0.10 | 0.50 | 0.01 | 0.125 (5 min) | 0.05 / 0.25 |
| OpenAI gpt-5.6-terra (older) | 2 | 12 | 0.20 | 2.50 (30 min) | 1 / 6 |
| Gemini 3.5 Flash-Lite | 0.30 | 2.50 | 0.03 plus storage | none listed | 0.15 / 1.25 |
Things the table hides:
- Spread. Among Anthropic's current models, input runs from $0.10 (Haiku 5.5) to $10 per MTok (Fable 5.1): 100 times. Use the smallest model that passes your evaluation.
- Tokens differ by model. Anthropic says its newer tokenizer yields about 30% more tokens for the same text, so compare cost per task, not price per MTok.
- Hidden output. Reasoning ("thinking") tokens bill as output at Anthropic, OpenAI and Google. OpenAI notes they are not visible through the API.
- Input you did not type. Tool definitions and results count as input.
- Context size. You pay for tokens sent, not window size, though Anthropic (Haiku 5.5), OpenAI and Google list higher prices above long-prompt thresholds.
Where the bill grows: history is sent again
Anthropic's Messages API is stateless by default: you send the whole conversation each time. APIs that hold history for you still bill those earlier tokens as input (BT-L12), so every turn re-bills earlier turns. Example at Sonnet 5.5's list price: 1,500 tokens of instructions and tool definitions, then five turns of a 100-token question and a 150-token reply.
| Turn | 1 | 2 | 3 | 4 | 5 | Total |
|---|---|---|---|---|---|---|
| Billed input tokens | 1,600 | 1,850 | 2,100 | 2,350 | 2,600 | 10,500 |
Only 2,600 of those tokens are distinct. Input costs 10,500 × $2 ÷ 1,000,000 = $0.0210 and output 750 × $10 ÷ 1,000,000 = $0.0075: $0.0285 per conversation. Pricing it as one request ($0.0047) is six times too low. At 100,000 conversations a month that is $2,850; at 10 million, $285,000.
Fixed against variable: a hypothetical $4-an-hour server costs $2,920 over 730 hours, used or not. That equals about 102,000 conversations at $0.0285, before staff, capacity limits and model quality. The $4 is invented.
EXHIBIT 16 / DEPLOYMENT
Explore the mechanismWhere the bill comes from.
See how history, caching and capacity change an illustrative bill.
Illustrative bill: $0.028500 per conversation
Each turn resends the prior history. With caching on, this example assumes every eligible read hits an unexpired prefix.
Inspect the example data and reasoning
| Element | What to notice |
|---|---|
| Input | $2.000 per million tokens |
| Output | $10.000 per million tokens |
| Cache write | $2.500 per million tokens |
| Cache read | $0.100 per million tokens |
| Turn 1 | 1600 input tokens |
| Turn 2 | 1850 input tokens |
| Turn 3 | 2100 input tokens |
| Turn 4 | 2350 input tokens |
| Turn 5 | 2600 input tokens |
Illustrative rates and capacity. The example excludes staffing, retries, reasoning tokens and differences in model quality.
Read the mechanism without interacting
- Hosted API
- A provider runs inference; investigate contract, region, capacity and data handling.
- Reserved capacity
- Plan capacity and utilization rather than assuming a fixed allocation is cheaper.
- Self-hosting
- You operate serving and security; token billing disappears, but compute and staff costs do not.
Compare five turns with and without a cache, then see when a hypothetical server at $4 per hour crosses the per-token bill.
Try explaining it: Why can writing to a cache cost more than leaving it off for one turn?
Loops multiply requests. In Anthropic's own data on its research system (published 2025-06-13), agents used about 4 times a chat's tokens and multi-agent systems about 15 times. That is one vendor's system, not a rule; at $0.0285 the multiples would be $0.114 and $0.43. Agents and Multi-Step Workflows covers step and budget caps.
Prompt caching: paying less for a repeated start
Prompt caching lets the provider reuse its work on an unchanged start of your request, the prefix, and bill those tokens less. Anthropic says the response is identical to an uncached one; the model remembers nothing (BT-L12). Vendors differ:
- Anthropic: a write costs 1.25 times the input price for a 5-minute lifetime, refreshed on each use (2 times for an hour); a read costs 0.1 times on most models (0.05 on Sonnet 5.5; some differ). A hit needs identical content up to the breakpoint, and tool definitions come first, so changing one invalidates the whole cache.
- OpenAI: on by default for supported models. From GPT-5.6, writes cost 1.25 times (30-minute minimum lifetime) and reads 0.1 times (0.05 for GPT-6.1 Sol and Terra); earlier models have no write charge.
- Google: implicit caching is on by default for Gemini 2.5 and newer; explicit caches add an hourly storage price.
In the chat example, assume every request hits the cache and arrives within five minutes. Turn 1 writes 1,600 tokens; later turns write the 250 new tokens and read the rest: 2,600 written, 7,900 read. Cost: 2,600 × $2.50 + 7,900 × $0.10 + 750 × $10, each ÷ 1,000,000, is $0.00650 + $0.00079 + $0.00750 = $0.01479, 48% less, not 90%: writes carry a premium and output is never discounted. With one turn, caching costs $0.0055 against $0.0047.
Other levers
- Smaller model. The example on Haiku 5.5's list price costs $0.001425, one twentieth, if it passes your evaluation.
- Batch. Anthropic's Batch API, OpenAI's Batch API and Google's take 50% off asynchronous work. Anthropic says most batches finish within an hour and expire after 24; OpenAI promises 24 hours.
- Fewer tokens, measured. Cap output and loop steps, trim history, log tokens per request (BT-L20).
Rate limits, quotas and spend caps
Limits control how fast you can use a service, separately from price. Anthropic limits requests, input tokens and output tokens per minute by model class and usage tier, using a token bucket, so a short burst can trip a limit even under your per-minute average; cache reads do not count toward the input-token limit. OpenAI's limits cover requests, tokens and images per minute or day at organization and project level; you hit whichever comes first.
Spend caps are separate. Each standard Anthropic tier has a monthly cap ($500 on Start). At the cap, requests return HTTP 429 without a retry-after header (the error code enforced_spend_limit_reached confirms it; other 429s can lack the header too) and keep failing until access resumes, so Anthropic's SDK, which retries transient failures twice by default, cannot fix it. A cap you set yourself returns HTTP 400. Anthropic's errors page does not say whether failed requests are billed; check. Backoff and retries: Latency, Throughput, and Reliability in AI Systems.
Data handling and region
These are vendor-documentation statements checked 2026-11-09 to 2026-11-12, not legal advice; your contract governs.
- OpenAI: API data is not used for training unless you opt in; abuse-monitoring logs holding prompts and responses are kept up to 30 days by default; Zero Data Retention needs approval.
- Anthropic: its docs retention page says conversation content is not retained by default, except Covered Models (30 days), and under zero data retention prompts and responses are not stored at rest; the Batch API is not eligible, and flagged content may be kept up to two years. Its commercial policy says API inputs and outputs are deleted within 30 days, with exceptions. The pages differ in scope, so confirm which applies to your account.
- Google: the Gemini pricing page says free-tier content is used to improve products, paid-tier content is not.
- Region: Anthropic's data residency setting
inference_geocan pin inference to US infrastructure on Claude 4.6 and later, at 1.1 times every token price.
Try it
Price one feature of your own: count tokens with the provider's counter, then billed input = n × (prefix + question) + (question + reply) × n(n − 1) ÷ 2. Check it on the example: 5 × 1,600 + 250 × 10 = 10,500. Then price a 10-step agent loop that starts at 2,200 tokens and adds 1,000 a step: billed input is 67,000, about six times the 11,200 distinct tokens.
The takeaway
On token-billed APIs, cost is tokens in and out, each times its price (output costs more), and each turn or loop step re-bills the history. Hosting shapes the bill: per token, hourly capacity, or servers plus staff. Caching, batch and smaller models cut cost only under their conditions; limits, spend caps and data terms differ by vendor and date. Measure your own token use first.
Related lessons
- Agents and Multi-Step Workflows covers the loops and step caps.
- Context Engineering: Memory, Files, and Instruction Design covers what goes into the context you pay for.
- AI Governance, Compliance, and Enterprise Policy covers which data may go to which external AI tool or API.
Up next
Latency, Throughput, and Reliability in AI Systems: latency, throughput and reliability, including retries after rate-limit errors.
VISUAL FIELDNOTES / 16
Keep the big picture.
A one-page visual companion. Save it, print it, or explain it to someone else.
View the full-size sheet ↗Where the bill comes from.
See how history, caching and capacity change an illustrative bill.
One US Letter page · Vector artwork · Fits A4 when printed to fit
Read the key points as text
- Hosted API
- A provider runs inference; investigate contract, region, capacity and data handling.
- Reserved capacity
- Plan capacity and utilization rather than assuming a fixed allocation is cheaper.
- Self-hosting
- You operate serving and security; token billing disappears, but compute and staff costs do not.
Remember: A provider runs inference; investigate contract, region, capacity and data handling.
Try it: Compare five turns with and without a cache, then see when a hypothetical server at $4 per hour crosses the per-token bill.
Recall: Why can writing to a cache cost more than leaving it off for one turn?
OPTIONAL CHECK
Check your understanding.
The optional check requires JavaScript. You can continue reading the lesson.
A team prices a five-turn support chat as one request, and the invoice is far higher. What best explains the gap?
A hypothetical GPU server costs $4 an hour for 730 hours, busy or idle. The API costs $0.0285 per conversation. At about what monthly volume are they equal, ignoring staff and quality?
A release rewords one tool description, and the prompt-cache hit rate on Anthropic's API drops to near zero. What is the likely cause?
Caching cut the five-turn chat's cost by 48%, not 90%. What explains most of the gap?
Two million archived tickets must be classified overnight, and nobody needs an immediate reply. Which lever fits best?
Every request to Anthropic's API now returns HTTP 429, the SDK's automatic retries also fail, and the response carries no retry-after header. What is the most likely cause?
On Amazon Bedrock, a team buys Provisioned Throughput on a one-month term but uses little of it. What does the billing imply?
A reasoning model's visible answer is 200 tokens, but usage shows 3,200 output tokens. Why?
A manager says, 'Multi-agent systems use 15 times a chat's tokens, so budget 15 times.' What does the lesson support?
A team wants prompts to stay on hardware it controls and accepts running the weights, serving software and patching itself. Which option from the lesson's table fits?
Quiz complete
YOUR LEARNING
Keep your place.
Your progress is saved on this device.