Automating checks of LLM answers
LLM answer checks are automated as a pyramid by cost and reliability: cheap deterministic ones run often, semantic ones and the judge less often. Deterministic checks (schema, length, forbidden patterns, numbers) run on every commit. Embeddings tell numbers and conditions apart poorly, so those are checked by code. The judge runs selectively. The verdict is a share of N runs, and results history is kept to see regressions.
QA Fundamentals · AI in testing
There are hundreds of bot answer checks and an LLM judge costs money. What runs on every commit?
A team decided to "automate answer evaluation" and put an LLM judge on every request in every pull request. The run became slow and expensive, and results fluctuate from run to run. This lesson is about layering checks so that the fast and cheap ones run constantly while the expensive ones run less often and deliberately. Quality criteria are covered in the lesson on them and run statistics in the lesson on non-deterministic features; here is how to build the automatic set.
How it works and why. Checks differ in cost, speed and reliability, so they are stacked like a test pyramid. The bottom level is deterministic: the answer schema, required fields, length, language, keywords, forbidden patterns (for example a card number or an email), the presence of a source. It is fast, cheap and gives the same result on the same data. The middle level is semantic: the answer and a reference are turned into vector representations (embeddings) and their closeness is compared, with the threshold chosen on examples and not guessed. The top level is an LLM judge with a rubric that scores what code cannot count: correctness, relevance, tone. For RAG add metrics of faithfulness to the source and relevance of the retrieved context. Cost grows from bottom to top, so the bottom level should be the largest and the top one is needed only where the lower ones say nothing.
Three levels of checks by cost
Example: level 1 in an ordinary API test. This is an ordinary Playwright test, no LLM-specific tools:
import { test, expect } from '@playwright/test';
test('bot answer: structure and forbidden content', async ({ request }) => {
const res = await request.post('/api/assistant', {
data: { question: 'What is the transfer fee?' },
});
expect(res.ok()).toBeTruthy();
const body = await res.json();
expect(typeof body.answer).toBe('string');
expect(body.answer.length).toBeLessThan(400);
expect(body.answer).not.toMatch(/\b\d{16}\b/); // a card number
expect(body.sources?.length).toBeGreaterThan(0);
});
It checks structure and prohibitions, so it passes steadily despite different wordings. It does not prove the correctness of the content, but it catches a broken format, a leaked card-number pattern and a missing source within seconds. How all levels combine for one case:
Case: a question about the fee
Level 1 (code): JSON by schema, no card number, a source is present
Level 2 (embed): similarity to the reference >= the threshold the team set
Level 3 (judge): correctness by a 0-3 rubric, output a quote
Verdict: the share of passes over N runs, not a single run
Here the verdict is based on the share of N runs and not on a single run, because answers differ. Results are stored over time to see a regression after a prompt or model change: it shows in the difference between the current and the previous run.
Limits and alternatives. Embedding closeness is not correctness: the answers "fee 10%" and "fee 1%" can be very similar as vectors. So this level catches a change of topic well and facts less well, and numbers need level 1 checks. An LLM judge is non-deterministic and needs calibration on human labels, and its verdict on the borderline is better passed to a person. Whatever can be checked by code should not be given to a model. The final score is taken on production settings, not only on settings simplified for the test. Frameworks exist that hold a set of cases and runs (promptfoo, DeepEval, RAGAS), but they are only a tool: the threshold and the make-up of the set are decided by the team.
Common mistakes.
- Putting a judge on every commit: expensive, slow, and the judge’s noise blocks a pull request for no reason.
- Relying on embeddings alone for numbers and conditions: a similar text with a wrong figure will pass.
- One run instead of a share: the test turns red and green without a code change.
- Not storing the history of results: a regression after a prompt change is invisible.
- Passing real personal data from a run to the judge: the judge is also an external service.
Check yourself
The bot’s answer is "The fee is 10%", the reference is "The fee is 1%". An embedding check with the threshold 0.8 passes. Which level will find it and how?
Answer
The deterministic one: a check that the text contains exactly the expected number (a regular expression or value parsing). The vectors are similar because the words match, and they distinguish numbers poorly.
A run with a judge took 40 minutes and gave different results on the same code. What would you change in the structure of the checks?
Answer
Take the judge out of every commit: keep deterministic checks on commit and run the judge on a schedule or before a release, on a sample with several runs and calibration. The result is a share and not a single verdict.