To validate AI responses in production, check every generated answer at runtime before the user sees it. Confirm that it is grounded in your sources, in the correct format, within policy and free of sensitive data. Back those checks with an evaluation set built from real user questions, guardrails on input and output, and a set fallback for when a check fails.
A model's offline benchmark score tells you how it did on someone else's questions. It doesn't tell you how it will answer your users, with your data, on a Tuesday afternoon after the provider has quietly updated the model. This guide covers what to check, when to check it and what to do when a check fails.
What does it mean to validate an AI response?#
Validating an AI response means checking each generated output against rules you have written down, then deciding whether to show it, repair it, replace it or send it to a person.
Validation happens at three points:
- Before launch: an evaluation set scores every prompt, model or retrieval change.
- At runtime: checks run on each answer before it reaches the user.
- After launch: monitoring and human sampling find the failures your checks missed.
The risks are well documented. NIST's Generative AI Profile (AI 600-1) lists twelve risks that generative AI creates or makes worse, including confabulation, which is the model stating false content with confidence. The OWASP Top 10 for LLM Applications 2025 includes Improper Output Handling (LLM05): model output that is not properly validated, sanitised or handled before other components use it. The same list also covers misinformation.
How is response validation different from testing AI-written code?#
AI-written code is checked once before release and then behaves the same way every time. A generated response is new on every request, so it has to be checked on every request as well as before release.
The discipline is the same, but the timing is different. We put every code change through three gates (tests, security checks and senior engineer review) whether a person or an AI wrote it. Our guide to verifying AI-generated code before production explains that process. Responses need equivalent gates that run in milliseconds, inside the request path.
| AI-written code | AI-generated responses | |
|---|---|---|
| When it's checked | Once, before merge and release | Before release and on every request |
| What stays fixed | The code, once shipped | Nothing: same prompt, different output |
| Pass criteria | Tests pass, scans clean, reviewer approves | Grounded, in format, in policy, no sensitive data |
| What happens on failure | Change is blocked | Answer is repaired, replaced or escalated |
| Main regression risk | New commits | Prompt, model, retrieval or provider changes |
How do you build an evaluation set for your own product?#
Collect real questions your users ask, write down what a good answer must contain, and score every prompt, model or retrieval change against that set before it ships.
- Gather real inputs. Use support tickets, search logs, sales call notes and beta transcripts. Questions you invent tend to be cleaner than real ones.
- Cover the hard cases on purpose. Include common questions, edge cases, out-of-scope requests the feature should refuse, prompt-injection attempts, and questions whose answers depend on what the user is allowed to see.
- Write criteria, not scripts. For each question, list the facts the answer must include, the facts it must not include, and the format it should take. Exact wording rarely matters.
- Pick the cheapest scorer that works. Use schema and regex checks for structured output, fact matching against reference answers for factual output, and a model-as-judge with a written rubric only for subjective qualities like tone.
- Calibrate the judge. Research on LLM-as-a-judge (Zheng et al., 2023) found position bias, verbosity bias and self-enhancement bias in model judges. Compare the judge's scores with human labels on a sample before you trust it.
- Run it in CI. Block a change if the overall score drops or any critical category regresses.
This is how our Generative AI Apps practice starts. We write the evaluation set from real user questions before choosing a model or a prompt. Then a model or prompt swap shows within minutes what got better or worse.
Which checks should run on every generated answer?#
Run cheap deterministic checks first, then grounding checks against the retrieved sources, and only then the slower model-based checks.
A typical runtime chain looks like this:
- Structure: Does the output parse? Does it match the expected schema, length and language?
- Sensitive data: Does it contain PII, secrets or internal identifiers? If so, redact it.
- Grounding: Is each claim supported by a retrieved document the user has permission to see? Can the answer show its source?
- Policy and topic: Does it stay inside the feature's scope and your content rules?
- Safe handling downstream: Treat output as untrusted input. Escape it before rendering, use parameterised queries, and never execute it directly. This is the core of OWASP's LLM05 guidance.
When a check fails, the answer takes a different path depending on which check failed and whether a retry is still allowed:
Allow one retry, not several. If the model fails the same check twice, a third attempt mostly adds latency and cost.
How do guardrails block unsafe or off-topic output?#
Guardrails are rules on both input and output that refuse, redact or redirect anything outside the feature's scope, so an unsafe answer never reaches the user.
On the input side:
- Detect prompt-injection attempts. Prompt injection holds the top spot (LLM01) in OWASP's 2025 list.
- Classify the topic and refuse out-of-scope requests early, before you pay for generation.
- Enforce document permissions at retrieval time, so the model never sees content the user can't see.
On the output side:
- Apply topic limits and PII filtering.
- Check grounding so that unsupported claims are removed or the answer is replaced.
- Define refusal paths that explain what the feature can help with.
Fallback behaviour is part of the guardrail design, not an afterthought. Decide in advance what the user sees when a check fails. In order of preference, the options are: a narrower answer built from the facts that did pass, a clear "I can't answer that" with links to the source documents, or a handoff to a person. Each response therefore has a short, predictable lifecycle:
When should a response go to a human instead?#
Send a response to a person when a wrong answer would be costly or hard to reverse, or when your checks can't confirm that it's correct.
Escalate when any of these applies:
- The answer commits the business to something: a refund, a price, a contract term or an account change.
- The topic is regulated or high-stakes, such as legal, financial or health guidance.
- Grounding is weak: few sources, sources that conflict, or a low confidence score.
- The user has rejected the previous answer or asked for a person.
Regulation is moving in the same direction. Article 14 of the EU AI Act requires high-risk AI systems to be designed so that people can oversee them effectively while they are in use. For agents that take actions, we keep a person on every risky step. Our post on production AI agents, guardrails and approvals covers how to scope that.
Two questions decide the routing: how bad would it be if this answer were wrong, and how well can your checks confirm it? A product FAQ answer is low-impact and easy to verify. A refund approval is high-impact even when the facts check out.
How do you monitor response quality after launch?#
Log every request with the result of each check, have people review a sample of answers on a regular schedule, and turn each confirmed failure into a new evaluation case.
Track these per feature:
- Failure rate for each check (schema, PII, grounding, policy)
- Fallback rate and escalation rate
- User signals: thumbs-down, rephrased questions, handoff requests
- Latency and cost per request
Use a standard trace format so these signals land in the observability tools you already run. OpenTelemetry has semantic conventions for generative AI spans. They are still marked as in development, so pin the version you emit.
Also re-run the full evaluation set on a schedule, not only when you change something. Providers update hosted models, and a silent update can shift quality without any change to your code.
How do you keep costs predictable while validating?#
Order checks from cheapest to most expensive, run model-based judges only where the risk justifies them, and set a token and latency budget for each feature.
- Fail fast. Deterministic checks cost almost nothing. Run them first so a malformed answer never reaches a paid judge call.
- Sample by risk. Judge every high-impact answer, and only a sample of low-impact ones.
- Use small models for classification. Topic, injection and PII classifiers rarely need your largest model.
- Cap retries and output length. One retry and a hard token limit stop runaway requests. OWASP's 2025 list includes Unbounded Consumption (LLM10) as a risk for exactly this reason.
- Cache and route. Cache repeat answers and send simple requests to cheaper model tiers, with a fallback when a provider slows down.
Our guide to cutting cloud and LLM costs at SaaS companies covers per-tenant cost visibility and token budgets in more detail.
Conclusion#
Validating AI responses is a gate system, not a single score. An evaluation set catches regressions before release. Runtime checks and guardrails catch bad answers before users see them. Fallbacks and human review handle the answers your checks can't confirm. Monitoring turns each miss into a new test. This is the same tested, secured, reviewed discipline we apply to AI-written code, adapted to output that changes on every request.
If you're shipping an LLM feature and want these checks built in from the first release, talk to an engineering lead →
Frequently asked questions
How many questions should an evaluation set have?
Start with enough real questions to cover every category the feature handles: common questions, edge cases, out-of-scope requests and injection attempts. Coverage matters more than raw size. Add a new case every time monitoring confirms a production failure, so the set grows with the ways your feature actually breaks.
Can I use one LLM to validate another LLM's answers?
Yes, for subjective qualities like tone or completeness, as long as the judge has a written rubric. Research on model judges has found position, verbosity and self-enhancement biases, so check the judge against human labels before trusting it. Use deterministic checks for anything that has a right answer, such as format, PII or citations.
Do runtime checks slow down responses noticeably?
Deterministic checks like schema, length and PII patterns add very little latency. Grounding checks and model-based judges cost more, which is why they should run after the cheap checks and, for low-risk answers, on a sample. Set a latency budget for each feature and measure what each check costs against it.
What should users see when a response fails validation?
Decide this before launch. The best options are a narrower answer built from the facts that passed, a plain I can't answer that with links to the relevant source documents, or a handoff to a person. Avoid showing a generic error, because users lose trust faster after an unexplained failure than after an honest refusal.
How often should I re-run my evaluation set?
Run it on every prompt, model or retrieval change, as a CI gate. Also run it on a schedule, because hosted models can change without any change to your code. A scheduled run catches silent quality shifts before users report them.
- AI validation
- LLM evaluation
- Generative AI
- AI guardrails
- AI engineering
- Production AI
Newsletter
Get new articles by email
A short monthly round-up of what we've written on engineering pods, quality and AI. No spam.
