Sub topic 2
Hosted APIs: what you get and what you give up
This chapter assumes you have called an HTTP API or SDK before, can read basic Python, and know the terms prompt, token, and context. A hosted API is a model that a provider runs on its own servers. You send a request over the network and get a completion back. You never see the weights, which are the learned numbers that define the model.
What you get
Hosted APIs remove most of the operational work.
- No hardware to manage. The provider handles GPUs, scaling, and uptime.
- Access to the largest models. Many of the strongest models are available only through an API.
- Pay per use. You pay for the tokens you send and receive. There is no idle server cost.
- Fast start. A key and a few lines of code are enough for a prototype.
- Ongoing improvements. The provider updates serving software and often releases new model versions.
What you give up
Each benefit has a matching cost.
- Control. The provider decides which versions exist and when old ones are retired. A retired model can change your application's behavior.
- Data location. Your prompts leave your network. You must check the provider's data retention and training policies against your requirements.
- Cost predictability. Spend grows with usage and prompt length. Price changes are outside your control.
- Availability. Rate limits, outages, and latency spikes are shared with other customers.
- Lock-in. Provider-specific features make switching harder over time.
A minimal call
The code below uses the OpenAI-style chat interface. The endpoint, key, and model name come from environment variables, so the application code does not hardcode a provider.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["LLM_BASE_URL"],
api_key=os.environ.get("LLM_API_KEY", "not-needed-for-local"),
)
MODEL = os.environ["LLM_MODEL"]
def ask(prompt: str) -> str:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
temperature=0,
)
return response.choices[0].message.contentKeeping these settings outside the code is the first step toward the portability covered in a later chapter.
Common errors
- Assuming the model name is stable. Alias names such as "latest" can point to a new version without notice. Pin a specific version where the provider offers one.
- Ignoring rate limits. A load test that works in development can fail with rate-limit errors in production. Plan for retries with backoff.
- Not reading the data policy. Defaults differ between providers and between plans. Check retention and training use before sending real user data.
- Estimating cost from the prompt alone. Output tokens are usually priced higher than input tokens, and both count.
When hosted is a good fit
Hosted APIs suit prototypes, variable or low traffic, tasks that need top-tier quality, and teams without infrastructure staff. They fit less well when data cannot leave your network, when volume is high and steady, or when you need to guarantee a model's behavior for years.
Exercise
Name two things you gain and two things you give up by using a hosted API instead of running a model yourself.
Show answer
Gains: no hardware to operate, and access to the largest models. Also acceptable: pay-per-use pricing, fast start. Gives up: control over model versions and availability, and data staying inside your network. Also acceptable: predictable cost, and freedom from lock-in.