Exercise: one question through two providers
- Track
- AI Engineering Foundations
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
Larkspur Cloud's support team is choosing the model behind its handbook assistant. Two providers are on the shortlist. Before anyone runs a large evaluation, you send one customer question through both, with the same prompt and the same settings, and record what each answer costs, how long it takes and whether it is right.
The rule you apply comes from lessons 1 and 3 of T2.2: put both providers behind one function so that only the model name changes, and read cost from the token counts the response reports, never from the length of the reply.
02 Outcomes
What you will learn
- Write one wrapper over two providers that raises on a model it does not know.
- Send the same messages to both models and change nothing else.
- Time each call, and keep the seconds spent loading weights out of the model's speed.
- Price an answer from its reported input and output tokens.
- Check an answer against the reference facts, not only by reading it once.
03 Setup
Before you start
Lessons
- T2.2 LLM Provider APIs & Model Selection: lesson 1 of T2.2 Calling commercial LLM APIs for messages,
usageand the one-function wrapper, lesson 2 of T2.2 Open-source LLMs via Hugging Face Inference and locally for who runs the weights and how to price an open model, and lesson 3 of T2.2 Reasoning vs non-reasoning models for the cost arithmetic. - T2.3 Hugging Face Models & Gradio Demos: lesson 2 of T2.3 Pipelines, tokenization and pretrained models for why the same text is a different number of tokens to each model.
- T2.1 Python & Data Toolkit Refresher: lesson 3 of T2.1 NumPy, pandas and Matplotlib for the results table and its
groupby.
Dataset
The Larkspur Cloud handbook and its 30-question golden set, in data/. The exercise uses one question, g01, "How long does a refund take?", with its two evidence spans as context and its reference answer for scoring.
Code
load_jsonl from src/lab_kit.py, ChatOllama from langchain_ollama, time.perf_counter and pandas.
Environment
The course's default environment (uv run jupyter lab) and a local Ollama server with both models pulled: ollama pull qwen2.5:3b and ollama pull llama3.2:3b. No API keys. The two providers are two open models from two vendors, Alibaba's Qwen and Meta's Llama, both served on your machine by Ollama. They stand in for two paid providers. If you have keys, point the wrapper at two hosted APIs and keep every other step.
04 Architecture
Experiment workflow
The question, the context, the system message, temperature and the reply cap stay fixed. Only the model name changes.
Extension: run the same loop on five more golden questions and see whether the faster model stays the more accurate one.
05 Build
Implementation tasks
Task 1: Load the question
Load the golden set from
data/larkspur/golden.jsonland takeg01. Print its question and its reference answer.Task 2: Build one prompt
Write a system message that tells the model to answer only from the context, in one or two sentences. Put the two evidence spans and the question in the user message. Both models get exactly these messages.
Task 3: Write one wrapper
Write
ask(model)that sends the messages to the named model and returns the reply text, the seconds the call took and the input and output token counts. It must raise on a model name it does not know. Call it once with a typo to check.ChatOllama(model="qwen2.5:3b", temperature=0, num_predict=200) reply.usage_metadata # {"input_tokens": ..., "output_tokens": ...}New here:
ChatOllamais LangChain's client for a local Ollama server, andusage_metadatais where LangChain puts the token counts. LangChain itself comes in T4.1. Here it plays the part ofget_completion's provider branch from lesson 1 of T2.2.Task 4: Time the calls
Wrap the call in a timer. Make six calls to each model, alternating the two, and keep the load time of every call. Read each model's speed only from the calls that loaded nothing.
start = time.perf_counter() reply = client.invoke(messages) seconds = time.perf_counter() - startNew here: no lesson in this part times a call.
time.perf_counter()is the standard library's clock for measuring short intervals. Any call can include loading the model's weights into memory. That happens on the first call, and again after the server evicts the model to make room for another. Ollama reports that part asload_durationinresponse_metadata, in nanoseconds.Task 5: Price each answer
Cost is input tokens times the input price plus output tokens times the output price, each per million (lesson 3 of T2.2). Local models have no bill, so use one stand-in price for both: notebook 03a's
gpt-4o-miniprices, $0.15 in and $0.60 out per million tokens. Scale the cost to 10,000 answers so the numbers are readable, as lesson 4 of T2.2 does.Task 6: Score the answers
The reference answer holds two facts: refunds take 5 to 10 business days, and an annual plan can get a full refund within 30 days if it used less than 100 GB of outbound traffic. Check each answer for both facts, and read every distinct answer each model gave.
Task 7: Decide
Fill the table and say which provider you would pick for this question and why. Then say what one question cannot tell you.
06 Measure
Record results
What you enter stays in this browser only.
| Model | Calls | Calls that loaded weights | Seconds spent loading | Median call without loading (s) | Input tokens | Output tokens | Cost per 10,000 answers (USD) | States 5 to 10 days | States the 30-day annual rule |
|---|---|---|---|---|---|---|---|---|---|
| qwen2.5:3b | 6 | ||||||||
| llama3.2:3b | 6 |
The median is over the calls whose load time was under 0.1 seconds. Input tokens are the same on every call for one model, because the prompt does not change. Cost uses the stand-in price, so it differs between the two rows only through the token counts.
07 Verify
Done when
0 of 5 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Counting load time as speed
A call that loads the model's weights measures the server, not the model. On a shared server that can happen on any call, not only the first, so check every call's load time.
Estimating cost from the reply text
Bill from the reported input and output tokens. The prompt is most of the tokens here, and each model counts it with its own tokenizer (lesson 2 of T2.3).
Changing two things at once
For example the model and the system message. Then the row cannot say which change moved it.
Trusting one reading
A quick read can miss a dropped fact. Check the answer against each fact of the reference, and read every distinct answer, not only the first.
Calling a stand-in a price
The local models have no bill. The cost column shows how token counts differ, and a real choice needs each provider's own price sheet.
09 Transfer
Interview angle
“How would you choose between two LLM providers for a new feature?”
Show a model answer
Put both behind one interface so that only the model name changes. Send the same prompts with the same settings, then compare the answers against reference facts first and the latency and the cost from reported tokens second. Keep the time spent loading weights out of the latency numbers. Use enough real questions that one flipped answer does not decide it.
10 Review
Worked solution
The worked solution is solution/p01_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, and run it top to bottom with the default environment and Ollama running. Colab has no Ollama server, so there you replace ChatOllama with a hosted chat model and keep the other cells.
Show our run and the reasoning
Our run: qwen2.5:3b and llama3.2:3b through Ollama 0.40.6, langchain-ollama 1.1.0 and pandas 2.3.3, offline.
| Model | Calls | Calls that loaded weights | Seconds spent loading | Median call without loading (s) | Input tokens | Output tokens | Cost per 10,000 answers (USD) | States 5 to 10 days | States the 30-day annual rule |
|---|---|---|---|---|---|---|---|---|---|
| qwen2.5:3b | 6 | 1 | 11.20 | 1.88 | 112 | 47 | 0.45 | yes | yes |
| llama3.2:3b | 6 | 1 | 41.96 | 0.38 | 120 | 18 | 0.29 | yes | no |
Both models gave the same answer on all six calls, as expected at temperature 0. qwen2.5:3b answered "A full refund takes 5 to 10 business days to appear on your statement. For an annual plan, a full refund within 100 GB of outbound traffic usage is available within 30 days of purchase." Both facts are there, though the second sentence is clumsy. llama3.2:3b answered "A refund typically takes 5 to 10 business days to appear on your statement." It drops the annual-plan rule, which is half the reference answer.
llama3.2:3b is about five times faster per call and about a third cheaper per answer, mostly because its answer is shorter: 18 output tokens against 47. The input counts differ, 112 against 120, for the same two messages. Each model counts them with its own tokenizer and wraps them in its own chat template. For this question we pick qwen2.5:3b, because a faster answer that leaves out the annual-plan rule sends that customer to a support ticket.
The timings show why load time is kept per call. The server was shared with other jobs. Each model loaded its weights once, and not on the same call: llama3.2:3b on call 0 and qwen2.5:3b on call 1, after the server had swapped it out. qwen2.5:3b's call 0 also took 461 seconds with no loading at all. That call waited in the server's queue, which load_duration does not see, and it is why the median, not the mean, is the speed column. One question cannot say whether the faster model drops facts on other questions too, or what either provider would really charge.