Traditional software testing assumes that the same input gives the same output, so a test can assert an exact result. Applications built on large language models break that assumption. The same question can produce differently worded answers on each run, many answers can be acceptable, and a small change to a prompt or model version can quietly change behaviour across thousands of cases.
That does not make LLM applications untestable. It means testing shifts from exact assertions to evaluation: measuring behaviour across a representative dataset against clear criteria. This guide describes the practical approach our software testing and QA and generative AI development teams use for chatbots, assistants, retrieval-based search and AI agents.
Why LLM applications need a different approach
- Non-deterministic output. Even with low randomness settings, wording and sometimes substance vary between runs.
- Open-ended answers. Correctness is often a judgement about accuracy, completeness and tone, not a single expected value.
- Many moving parts. Behaviour depends on the prompt, the model version, retrieved documents, tools and conversation history, any of which can change.
- Adversarial users. People will try to make the system ignore its instructions, reveal data or produce harmful content.
- Silent regressions. A change that improves one type of question can degrade another without any error being raised.
Build a golden dataset first
Every other technique depends on a golden dataset: a versioned set of test inputs with expected answers or clear grading criteria. Start with a few dozen examples for each important task and grow it over time. Include:
- Typical cases taken from real user questions or support tickets, anonymised.
- Edge cases such as ambiguous questions, very long inputs, multiple languages and spelling mistakes.
- Out-of-scope requests the system should politely decline or hand to a human.
- Adversarial cases such as prompt injection attempts and requests for other users' data.
- Past failures: every bug found in testing or production becomes a new case, so it can never silently return.
For each case, record what a good answer must contain, must not contain, and which sources it should cite. Domain experts should review the expected answers; in sectors such as healthcare or finance, this review is essential rather than optional.
Three ways to score outputs
- Valid JSON or schema, required fields present
- Cited sources exist and were actually retrieved
- Forbidden terms, personal data patterns, length limits
- Cheap enough to run on every case, every time
- Cannot judge meaning or tone
- A model grades answers against a written rubric
- Covers relevance, faithfulness and completeness
- Scales to large datasets
- Has its own biases and errors
- Must be calibrated against human labels
- Domain experts grade a sample of outputs
- Catches subtle factual and tone problems
- Creates labels to calibrate automated judges
- Slow and costly, so used on samples
- Needs clear guidelines for consistency
verdictUse code checks wherever a rule can be written, an LLM judge for nuanced criteria at scale, and humans to calibrate both.
LLM-as-judge: useful, with caveats
Using one model to grade another's answers is practical, but the judge is not neutral. Judges can favour longer answers, favour whichever answer appears first in a comparison, and rate text in their own style more highly. They can also simply be wrong about facts. To keep a judge honest:
- Give it a specific rubric with pass and fail examples, rather than asking whether an answer is good.
- Prefer simple pass or fail decisions per criterion over a score out of ten.
- Provide the reference answer or source documents so it grades against evidence, not its own knowledge.
- Check its verdicts against a human-labelled sample and track how often they agree.
- Pin the judge model and prompt version, so a change in the judge is not mistaken for a change in your application.
Checking for hallucinations and grounding
Most business applications answer from your own content using retrieval-augmented generation; our explainer on what RAG is covers the architecture. Test the two halves separately. For retrieval, check whether the right documents appear among the results for each test question. For generation, check that every claim in the answer is supported by the retrieved text, that citations point to passages that actually say what is claimed, and that the system says it does not know when the answer is not in its sources. A wrong answer delivered with a confident citation is worse than an honest refusal. Include test questions whose answers are deliberately absent from the knowledge base, and questions where two sources disagree or one is out of date, because those are the cases where models most often fill gaps with plausible inventions.
Safety and security testing
Safety tests check what the system refuses to do as much as what it does. Cover direct prompt injection, indirect injection hidden in documents or web pages the system reads, attempts to extract the system prompt or other users' data, requests for harmful content, and, for agents, attempts to trigger tools or actions the user should not have. Our article on generative AI security explains these risks in more depth. Testing works best when the application has guardrails in layers, so each layer can be tested on its own.
Detect injection attempts, personal data and out-of-scope requests before they reach the model.
Clear system instructions, separation of trusted instructions from untrusted content, per-user data access.
A pinned model version chosen and evaluated for the task.
Schema checks, citation checks, filters for unsafe content and data leakage.
Least-privilege tools, spending and rate limits, human approval for irreversible actions.
Logging, alerts on guardrail triggers and sampled human review in production.
Regression testing for prompts and models
Treat every change to a prompt, model version, retrieval setting or tool definition like a code change. Run the evaluation suite in the CI/CD pipeline, compare results with the current production baseline, and block the release if key measures fall below agreed thresholds. Because outputs vary, run important cases several times and look at pass rates rather than single results. This is also how you compare models fairly when deciding how to choose an LLM.
- 01Propose a change
New prompt, model version, retrieval setting or tool, on a branch like any code change.
- 02Run the golden set
Execute every case, repeating important ones to measure variation.
- 03Score
Apply code checks, the calibrated LLM judge and a small human-reviewed sample.
- 04Compare with baseline
Look at overall pass rates and at each category, especially safety cases.
- 05Gate and release
Release only if thresholds are met, ideally to a small share of users first.
- 06Monitor and feed back
Add production failures and new question types to the golden dataset.
What to measure
Choose a small set of measures tied to what users and the business care about, and report them per category of test case rather than as one blended score:
- Task success: the share of cases where the answer or action meets the rubric.
- Faithfulness: the share of answers whose claims are fully supported by the retrieved sources.
- Correct refusals: declining out-of-scope or unsafe requests, without refusing legitimate ones.
- Safety pass rate: adversarial cases handled without leaking data, breaking instructions or taking unauthorised actions.
- Latency and cost per interaction: a model that is slightly better but much slower or more expensive may not be the right release.
Monitoring in production
Pre-release evaluation cannot anticipate every real question, so testing continues after launch. Log inputs, outputs, retrieved sources and tool calls; collect user feedback such as thumbs up or down; review a regular sample by hand; and alert on rising guardrail triggers, refusals, latency or cost. New topics users ask about are signals to extend both the content and the golden dataset. The broader practices of logs, metrics and traces are covered in our guide to application monitoring and observability.
These practices fit naturally into the testing approach described in types of software testing and the sprint process in our QA process for agile teams. You can see an AI agent in action in our live demo. DivTech builds and tests LLM applications, typically in Python, with evaluation suites from day one. Get a quote to set up evals and guardrails for your AI product.
Frequently asked questions
How do you test something that gives different answers each time?
By evaluating behaviour across a dataset instead of asserting exact outputs. You define criteria for a good answer, score many cases with code checks, a calibrated LLM judge and human review, run important cases several times, and track pass rates against a baseline.
Is LLM-as-judge reliable?
It is useful but imperfect. Judges show biases such as favouring longer answers and can make factual mistakes. Use specific rubrics, provide reference material, prefer pass or fail criteria and regularly compare judge verdicts with human labels.
How big should a golden dataset be?
Start with a few dozen well-chosen examples per important task, covering typical, edge, out-of-scope and adversarial cases, then grow it with every failure found in testing or production. Coverage of real scenarios matters more than raw size.
Do we need to re-test when the model provider updates the model?
Yes. Pin model versions where the provider allows it, and run your full evaluation suite before moving to a new version, exactly as you would for a change to your own code.