Sub topic 15
Design so the model can be swapped
This chapter assumes you can call an HTTP API or SDK from Python and know what a prompt, token, and context window are. You should also have read the earlier chapters on hosted APIs, open-weight models, and evaluation.
Why portability matters
The model you choose today is rarely the one you run in a year. Prices change, providers retire model versions, and a smaller model may start to meet your requirements. If the model name and endpoint are scattered through your code, each change becomes a project. If they are configuration, each change is an edit and an evaluation run.
Keep the endpoint, key, and model name in configuration
Many hosted providers and self-hosted servers such as Ollama and vLLM expose an OpenAI-compatible endpoint. One client can then talk to either, and only environment variables differ.
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.contentSwitching from a hosted API to a local server means changing three variables. The application code stays the same.
Keep the seam narrow
The ask function is the seam. Every other part of the application calls it and never imports a vendor SDK directly. When a provider needs a different request format, you change one file.
Keep these things behind the seam:
- Provider-specific parameters, such as special response-format options.
- Retry and timeout handling.
- Parsing of the response into plain text or a typed result.
What does not swap automatically
A compatible endpoint does not make models interchangeable. Watch for these differences:
- Prompt sensitivity. A prompt tuned for one model can perform worse on another. Treat a prompt as part of the model choice.
- Context window. A smaller window can break requests that worked before. Check input length against the limit.
- Feature support. Tool calling, structured output, and image input vary by model and server.
- Output style. Format and length differ, so code that parses output with fragile rules may fail.
- Tokenizers. The same text gives different token counts, which changes cost estimates.
Pair swapping with your evaluation set
Treat every swap as a change that needs a test. Run the same cases from the evaluation chapter against the new model before you switch.
def pass_rate(ask_fn, cases) -> float:
passed = sum(1 for c in cases if c["expected"] in ask_fn(c["prompt"]))
return passed / len(cases)Pass ask built from the current configuration and one built from the candidate, then compare the scores. If the candidate matches the current pass rate at lower cost, you have a reason to switch.
Exercise
Your team moves from a hosted API to a self-hosted vLLM server with an OpenAI-compatible endpoint. Which three settings change, and what must you do before switching production traffic?
Show answer
Change LLMBASEURL, LLMAPIKEY, and LLM_MODEL. Before switching, run your evaluation cases against the new setup and compare the pass rate with the current model. Also check context window and feature support, because compatible endpoints do not guarantee identical behavior.