Notes
Measure per answer
Start by measuring every answer. ShopRung's chat returns its token usage and latency with each response: prompt tokens are everything sent to the model, the instructions, the help extracts and the question; completion tokens are what the model wrote. Real providers report the same numbers in their API responses. Log them with the model and prompt version, and you can compare any two runs.
{"answer": "Shipping costs
$4.99 per order ...",
"model": "llama3.2:3b",
"promptVersion": "v1",
"usage": {"promptTokens": 277,
"completionTokens": 44},
"latencyMs": 892}
prompt tokens: what we send
completion: what it writes Where tokens go
Here is where the tokens went on the real model. A normal answer used about two hundred and ninety five prompt tokens and thirty five completion tokens, about three hundred and thirty in all. Order questions call the tool, so the model runs twice, with around seven hundred and ninety prompt tokens. Refused questions cost nothing, because the model is never called. And sending every help section tripled the prompt.
golden answers (33): prompt 232-339, avg 295 completion 12-64, avg 34.5 total about 330 order questions (tool): 2 model calls, 783-799 prompt refused (out of scope): 0 all help sections in the prompt (bug): 818 vs 277
Latency
Latency, as percentiles, like any performance test. The median answer took seven hundred and sixty six milliseconds and the ninety fifth percentile about one point four seconds. The slowest, almost four seconds, was the very first call, while the model was loading: a cold start. Report it separately from warm runs, or one restart ruins your numbers. Order questions took 1.3 to two point four seconds, because of the second model call.
p50 766 ms
p95 1,388 ms
max 3,908 ms ← the first call:
model loading
order questions 1.3-2.4 s
refusal about 1 ms
report warm runs and the cold
start separately Do the math
Turn tokens into money with simple math: prompt tokens times the input price, plus completion tokens times the output price, which is usually higher. Ten thousand questions a day at three hundred and thirty tokens is three point three million tokens a day; order questions cost about two point six times more. A local model has no token bill, but it costs hardware and time per answer. Either way, tokens per answer is the number to guard.
cost per answer = prompt tokens x input price + completion x output price 10,000 questions a day x 330 = 3.3 million tokens a day order questions: about 2.6x local model: no token bill, but hardware time per answer
Budget tests
Now make it a test. For every golden question, assert the prompt tokens, completion tokens and latency stay under a budget, set with some margin above today's numbers. And assert the out of scope refusal uses zero tokens. In CI this runs against the deterministic fake model in a fraction of a second, so it can run on every pull request.
@pytest.mark.parametrize("item",
GOLDEN)
def test_answer_within_budget(item):
r = ask(item["q"])
assert r["usage"]["promptTokens"]
<= 400
assert r["usage"]
["completionTokens"] <= 150
assert r["latencyMs"] <= 1000
refusal: usage must be 0, 0 Caught
We ran the suite against two of ShopRung's bugs. With the long context bug, all thirteen tests failed: prompts of eight hundred and thirteen to eight hundred and eighteen tokens. It also exposed a side effect: the capital of France was no longer refused, because every section now counts as found, so it cost eight hundred and sixteen tokens instead of zero. With the slow chat bug, twelve answers took three seconds. Functional checks would have passed both.
normal: 13 passed chat-long-context: 13 failed assert 813 <= 400 ... 818 France: refused False, 816 tokens instead of 0 chat-slow: 12 failed assert 3000 <= 1000 ... 3004 answers still correct: functional tests pass
Real-model budgets
On the real model, set budgets by what is stable. Prompt tokens were identical across runs of the same question, two hundred and seventy seven for shipping every time, so they can have a tight budget. Completion tokens varied from twelve to sixty four, so give them more room. Latency depends on hardware and load, so check the ninety fifth percentile over many runs, on the same machine, after a warm up, in a nightly job rather than on every pull request.
prompt tokens: the same every run for the same question (shipping: 277, 277, 277) → tight budget completion: 12-64 tokens → a looser budget latency: varies by machine and load → p95 over many runs, same hardware, after warm-up
In production
In production, keep measuring: log tokens, latency, model and prompt version for every request, and chart the median, the ninety fifth percentile and the cost per conversation. Alert on jumps after releases. The levers to reduce cost are the same things you tested: shorter prompts, fewer context sections, offering tools only when needed as ShopRung does, caching repeated answers, and a smaller model for easy questions.
- log tokens, latency, model and prompt version per request
- dashboards: p50, p95, cost per conversation, per feature
- alerts on jumps after releases
- levers: shorter prompts, top-k context, routing (tools only when needed), caching, a smaller model for easy cases
Common mistakes
Mistakes. Not logging usage per answer, so nobody notices a doubled prompt. Reporting averages instead of percentiles. Mixing the cold start into warm runs. Enforcing tight latency budgets for a real model on shared CI machines, where timing is noisy. Ignoring tokens spent on answers that guardrails block or that get retried. And having no budget test at all, so a cost regression ships with green builds.
- no usage logged per answer
- averages instead of p95
- cold start mixed into warm runs
- latency budgets on shared CI machines with a real model
- ignoring tokens spent on blocked or retried answers
- no budget test in CI
Say it like this
In the interview: every response logs its tokens, latency, model and prompt version. In CI, budget tests run the golden set against a deterministic fake and assert prompt tokens, completion tokens, latency, and zero cost for refusals. Nightly, the real model is checked on the ninety fifth percentile after a warm up, on fixed hardware. In production, dashboards and alerts compare each release with the last.
- "Every response logs tokens, latency, model and prompt
- version. CI runs budget tests
- on the golden set with a deterministic fake: prompt and completion tokens, latency, zero
- for refusals. Nightly, the real
- model: p95 after warm-up. In production: dashboards and alerts per release."