Hosted, Open and Local Models
There are three main ways to run the model behind an AI feature. A hosted model API is someone else's model on someone else's servers, paid per token. An open model you host is a model with downloadable weights running on servers you rent or own. A local model runs on the user's own device, such as a laptop or phone. Each wins in different situations, and many products combine them.
"Open and Closed Models" explained the licenses. This lesson is about running them: cost, speed, privacy and effort.
The Three Options
| Option | Description |
|---|---|
| Hosted API | It is used to get the most capable models with no servers to manage; you pay per token and send data to the provider |
| Open model, self-hosted | It is used when you need control over data, versions and cost at high volume; you run and maintain the servers |
| Local, on device | It is used for privacy, offline use and zero per-request cost; limited to models small enough for the device |
| Hybrid | A small local or self-hosted model for easy requests, and a large hosted one for hard ones |
Example: When Does Your Own Server Pay Off?
A hosted API costs a little per request; your own server costs a fixed amount per month. This program finds the break-even point using example numbers, not real quotes, to show the method.
The programs in this chapter need pip install fastapi httpx sentence-transformers scikit-learn torch transformers. Several use faq.json from "Semantic Search" and eval_set.json from "Building an Evaluation Set". Download and loading messages are left out of the output shown.
# EXAMPLE numbers to show the method; replace them with real quotes for your project
api_price_in, api_price_out = 0.25, 1.25 # dollars per million tokens, a small hosted model
server_per_month = 600.0 # one rented GPU server, running all month
server_capacity = 3_000_000 # requests per month it can handle
tokens_in, tokens_out = 1_200, 150 # per Campus Helper request: prompt with documents, answer
api_per_request = (tokens_in * api_price_in + tokens_out * api_price_out) / 1e6
print(f"Hosted API: ${api_per_request:.6f} per request")
print(f"Own server: ${server_per_month:,.0f} a month, up to {server_capacity:,} requests\n")
print(f"{'requests a month':>17} {'hosted API':>11} {'own server':>11} cheaper")
for requests in [10_000, 100_000, 1_000_000, 3_000_000]:
api = requests * api_per_request
print(f"{requests:17,} ${api:10,.2f} ${server_per_month:10,.2f} {'API' if api < server_per_month else 'server'}")
print(f"\nBreak-even: about {server_per_month / api_per_request:,.0f} requests a month")Hosted API: $0.000487 per request
Own server: $600 a month, up to 3,000,000 requests
requests a month hosted API own server cheaper
10,000 $ 4.88 $ 600.00 API
100,000 $ 48.75 $ 600.00 API
1,000,000 $ 487.50 $ 600.00 API
3,000,000 $ 1,462.50 $ 600.00 server
Break-even: about 1,230,769 requests a monthExplanation:
- Each request costs $0.000487 with the API at these example prices: 1,200 input tokens (the question, instructions and documents) and 150 output tokens.
- At 10,000 requests a month, the API costs $4.88 against $600 for a server: no contest. Most new products start here.
- At 3,000,000 requests, the API costs $1,462.50 and the server is cheaper.
- The break-even is about 1.23 million requests a month. Below it, pay per token; above it, self-hosting can save money, but only if the open model is good enough and someone can run the servers, which is a real cost the table leaves out.
Example: How Fast Is a Local Model?
This program measures how fast two small open models generate text on this laptop's CPU.
import time
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
for name in ["HuggingFaceTB/SmolLM2-135M-Instruct", "HuggingFaceTB/SmolLM2-360M-Instruct"]:
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name).eval()
chat = tokenizer.apply_chat_template([{"role": "user", "content": "Explain photosynthesis in three sentences."}],
add_generation_prompt=True, return_tensors="pt", return_dict=True)["input_ids"]
model.generate(chat, max_new_tokens=5, do_sample=False, pad_token_id=tokenizer.eos_token_id) # warm up
start = time.perf_counter()
out = model.generate(chat, max_new_tokens=64, min_new_tokens=64, do_sample=False,
pad_token_id=tokenizer.eos_token_id)
seconds = time.perf_counter() - start
new = out.shape[1] - chat.shape[1]
weights_mb = sum(p.numel() * p.element_size() for p in model.parameters()) / 1e6
print(f"{name.split('/')[1]:24} {weights_mb:5.0f} MB of weights, {new} tokens in {seconds:.2f} s"
f" = {new / seconds:5.1f} tokens per second on this laptop's CPU")SmolLM2-135M-Instruct 269 MB of weights, 64 tokens in 0.65 s = 98.4 tokens per second on this laptop's CPU
SmolLM2-360M-Instruct 724 MB of weights, 64 tokens in 1.18 s = 54.3 tokens per second on this laptop's CPUExplanation: The 135M model produced about 98 tokens per second and the 360M model about 54. Speed falls as models grow, because every token needs the whole model's weights. Both are fast enough for a chat app, but they are far less capable than large hosted models; see the base-model mistakes in "Instruction Tuning and RLHF". These timings are one run on this laptop; a laptop GPU, a phone chip or a server GPU would give very different numbers.
Choosing
| Question | Points toward |
|---|---|
| Need the best quality on hard tasks? | Hosted API with a frontier model |
| Low or unpredictable volume? | Hosted API |
| Data must not leave your company or country? | Self-hosted open model, or a provider with suitable data agreements |
| Must work offline, or on private data on a device? | Local model |
| Very high, steady volume on a narrow task? | Self-hosted, often a fine-tuned small open model |
| Small team, no infrastructure experts? | Hosted API |
| Need a model version that never changes? | Self-hosted or local open model |
Common Mistakes
Comparing only the price per token. Self-hosting also costs engineering time, monitoring, security and idle servers.
Assuming open means free to use any way you like. Check each model's license.
Expecting a small local model to match a frontier model. Test it on your own evaluation set.
Ignoring data terms. Read how a provider uses and stores your data before sending anything sensitive.
Interview Corner
When would you self-host an open model instead of using a hosted API?
When data must stay under your control, when volume is high and steady enough to beat per-token prices, when you need a fixed model version, or when a fine-tuned small model is good enough for a narrow task. You take on the cost of running and securing the servers.
What are the benefits of running a model on the device?
Privacy (data never leaves the device), offline use, low latency and no per-request cost. The limits are model size and capability, and the device's memory and battery.
Practice
Exercise: your own numbers. Change the example prices and token counts in the first program to those of a real provider and model you might use, and set tokens_in to match your own app. Where is the break-even now?
Check your understanding:
- Why is the hosted API cheaper at 10,000 requests a month?
- What costs of self-hosting does the break-even calculation leave out?
- Why was the 360M model slower than the 135M model?
Key Takeaways
Quick reference
| Need | Formula or practice |
|---|---|
| API cost per request | (tokens_in × price_in + tokens_out × price_out) / 1,000,000 |
| Break-even requests | server cost per month / API cost per request |
| Local speed | tokens generated / seconds |
| Choose | Evaluation set results + cost + privacy + effort |
Was this tutorial helpful?