Sub topic 5
Pick the smallest model that meets the bar
This chapter assumes you can call an LLM API from Python and that you have the baseline measurements and small evaluation set from the earlier chapters. Model choice is usually the largest single lever on both cost and latency, so it comes before the smaller optimizations.
Why model size matters
Providers sell several tiers of the same model family. Larger models usually cost more per token and generate tokens more slowly. A small model can be many times cheaper and noticeably faster, and for many tasks it produces answers that are just as good.
The goal is not the cheapest model. The goal is the smallest model that meets a quality bar you define in advance. Without a bar, "good enough" turns into a feeling, and feelings drift toward the biggest model.
Define the bar first
Write the bar down before you test any model. A bar has three parts:
- A metric from your evaluation set, such as exact-match accuracy or the share of outputs that pass a rubric.
- A threshold, such as "at least 92% pass".
- Limits on cost and latency, such as "under $2 per 1,000 requests and p95 under 3 seconds".
Setting the threshold after you see the results lets you rationalize whatever you got.
Compare candidates on your own data
Public benchmarks measure general ability. Your task is narrower. Run each candidate model on the same evaluation set, with the same prompt and sampling settings, and record quality, cost, and latency together.
def compare_models(models, eval_set, run_fn, grade_fn, price_fn):
"""Return quality, mean cost, and mean latency for each model."""
results = {}
for model in models:
passed, cost, latency = 0, 0.0, 0.0
for case in eval_set:
output, seconds, usage = run_fn(model, case["input"])
passed += grade_fn(case, output)
cost += price_fn(model, usage)
latency += seconds
n = len(eval_set)
results[model] = {
"quality": passed / n,
"cost_per_request": cost / n,
"latency_s": latency / n,
}
return resultsHere run_fn returns the output, elapsed seconds, and token usage, and grade_fn returns 1 or 0. These are the pieces you built in the measurement and evaluation chapters.
Read the results as a table
A result might look like this:
| Model tier | Quality | Cost per 1,000 requests | Mean latency |
|---|---|---|---|
| Large | 96% | $12.00 | 4.1 s |
| Medium | 93% | $3.00 | 1.8 s |
| Small | 84% | $0.60 | 0.9 s |
With a 92% bar, the medium model wins. It is four times cheaper than the large one and more than twice as fast. The small model is cheapest but fails the bar.
Improve the small model before giving up on it
If a smaller model nearly meets the bar, try these before moving up a tier:
- Tighten the instructions and add one or two examples of correct output.
- Request a structured output format to reduce ambiguity.
- Look at the failures. If they cluster on one kind of input, the cheaper model may handle the rest, which is the idea behind routing in a later chapter.
Change one thing at a time and rerun the evaluation, so you know which change helped.
Common errors
- Too few test cases. With 20 cases, one answer changes the score by 5 points. Use enough cases that the gap between models is larger than the noise.
- Judging by a few good outputs. A model can look fine on examples you picked by hand and fail on the long tail.
- Ignoring latency and cost in the comparison. Quality alone picks the largest model every time.
- Not rechecking. Providers release new models and change prices. Rerun the comparison when they do.
Exercise
Your evaluation set gives these results: Model A passes 90% at $1 per 1,000 requests, Model B passes 94% at $5, and Model C passes 95% at $20. Your bar is 93%. Which model do you pick, and what would you try first if cost still felt too high?
Show answer
Pick Model B. It is the cheapest model that meets the 93% bar, and Model C adds only one point for four times the cost. To cut cost further, first try improving the prompt for Model A with clearer instructions and examples, then rerun the evaluation. Check whether its failures cluster on certain inputs, which could let you route only those inputs to Model B.