Serving small models cheaply
Serving a model to many users costs money every hour. Learn batching, caching, CPU inference and autoscaling, and how to tell when a hosted API is simply cheaper.
In 60 seconds
Serving small models cheaply
Serving a model to many users costs money every hour. Learn batching, caching, CPU inference and autoscaling, and how to tell when a hosted API is simply cheaper.
Four ways to cut cost
| Technique | What it does | When it helps |
|---|---|---|
| Batching | Processes several requests together, so the hardware is kept busy | Many users at once; continuous batching is covered in Step 17 |
| Caching | Stores answers or work that repeat, so you do not compute them again | Common questions; the same long system prompt on every request |
| CPU inference | Runs a small quantised model on ordinary CPU servers | Low or steady traffic, small models, tasks where a slower reply is fine |
| Autoscaling | Adds servers when busy and removes them when quiet, sometimes down to zero | Traffic that goes up and down, like exam season or festival sales |
Caching, two kinds
- Response cache: if the same question comes again (for example "What are the library hours?"), return the stored answer. Only do this for answers that do not depend on the user and do not change often.
- Prefix or prompt cache: many runtimes can reuse the work done on a long, repeated start of the prompt, such as the system prompt. This saves time and compute on each request. Check what your runtime supports.
Self-hosted or hosted API? Do the maths
# All prices below are MADE-UP examples for practice. Use real quotes for real decisions.
api_price_per_million_tokens = 50.0 # rupees, input and output combined, example only
server_cost_per_month = 20_000.0 # rupees, including storage and bandwidth, example only
ops_hours_per_month = 10 # your team's time to run and update it
ops_cost_per_hour = 800.0 # rupees, example only
tokens_per_request = 1_500
self_hosted_monthly = server_cost_per_month + ops_hours_per_month * ops_cost_per_hour
for requests_per_day in (500, 5_000, 50_000):
monthly_tokens = requests_per_day * 30 * tokens_per_request
api_monthly = monthly_tokens / 1_000_000 * api_price_per_million_tokens
cheaper = "API" if api_monthly < self_hosted_monthly else "self-hosted"
print(f"{requests_per_day:>6} req/day: API Rs {api_monthly:,.0f} vs self-hosted Rs {self_hosted_monthly:,.0f} -> {cheaper}")A cheap serving checklist
- 1
Measure your traffic
Requests per day, peak per minute, average tokens in and out. - 2
Pick the smallest model that passes
Use your test set from earlier lessons. - 3
Quantise and cache
Lower-bit weights, prompt caching, and a response cache for safe repeated questions. - 4
Set limits
Maximum input length, maximum output length, and per-user rate limits. - 5
Scale with demand
Autoscale, and consider scale to zero if slow first requests are acceptable. - 6
Re-check every few months
Prices, models and traffic all change.
- A big GPU server running all night for ten users
- No limit on answer length
- Choosing self-hosting "because APIs are expensive" without doing the maths
- A hosted API while traffic is small, self-hosting when the numbers say so
- Short, capped answers and cached repeated questions
- A written cost comparison, updated when prices change
Watch and read more
Lab
A cost comparison between a hosted API and a self-hosted small model for one realistic app.
The problem
App: "a question-answering bot for a coaching institute, used by students mostly in the evenings and heavily during exam season."
- Estimate traffic for a normal month and an exam month: requests per day, average tokens per request, and peak requests per minute.
- Find current public prices for one hosted API option and one server option (a cloud CPU or GPU server). Write the date you checked. If you cannot find them, use clearly labelled made-up numbers.
- Adapt the break-even script from the lesson. Add your team's time. Compute both months.
- Write down which techniques you would use: batching, caching, CPU inference, autoscaling, limits. Say why for each.
- Make a recommendation in three sentences, including the traffic level at which you would switch.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Self-hosting looks cheaper in exam month but more expensive in normal months. What designs could get the best of both?Reveal
Use a hosted API for normal traffic and add self-hosted capacity only for the peak, or use autoscaling so self-hosted servers run only when busy. Another option is a hybrid: answer common questions from a cache or a small self-hosted model, and send hard questions to the hosted API. Each adds complexity, so check the savings are worth it.
Q2Why can doubling the batch size make each request cheaper but also make some users wait longer?Reveal
Bigger batches keep the hardware busier, so the cost per request falls. But the server may wait to collect more requests, and each step handles more work, so individual requests can take longer to start or finish. Continuous batching reduces this problem, but there is still a trade-off between throughput (cost) and latency (waiting time) that you set with limits and testing.
Questions people ask
Is CPU serving good enough for real users?
For small quantised models, short answers and modest traffic, it can be. For long answers or many users at once, a GPU is usually more cost-effective per request. Measure speed on the actual server before deciding.
What does "scale to zero" mean?
When there are no requests, all model servers are shut down, so you pay almost nothing. The next request has to wait while a server starts and loads the model. It suits internal tools and background jobs more than live chat.
Why include my team's time in the cost?
Someone has to update the server, patch security issues, watch for failures and upgrade the model. That time has a real cost, and it is often the part people forget when comparing with an API.
Are hosted API prices stable?
No. Prices change, often downwards, and new cheaper models appear. Keep your comparison in a small script or spreadsheet so you can re-run it with current prices.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.