Adding an AI feature is easy, keeping it reliable is hard: evaluation infrastructure for LLM integrations
Adding an AI feature to a product now takes an afternoon. Get an API key, write a prompt, and the demo works. The hard part starts later: does the feature still work on day thirty, on a thousand different inputs, after the model version changes?
The gap I see most often in software companies is this: an LLM integration is tested like an ordinary function, which means it is not tested at all. This post describes five practical pieces I use to close that gap.
1. Why classic tests are not enough
A classic unit test expects the same output for the same input. LLM output is probabilistic. Three problems follow:
- Silent degradation. When the provider updates the model, your code throws no error, but answer quality changes.
- Prompt side effects. A sentence you add to fix one case can break three others.
- The demo illusion. Ten examples you tried by hand work fine. A real user brings the eleventh.
2. A golden set
The first step is a list of real inputs for which you know the correct answer. You do not need hundreds; start with 50-100 well-chosen examples.
- Take them from real user data (with personal information removed).
- Put edge cases next to the easy ones: missing information, contradictory information, unexpected language.
- Add every wrong answer from production to the set. Over time it becomes a map of your real failures.
The evaluation script gives each example to the model, compares the answer with the expected one, and produces one accuracy number. When the prompt or model changes, you compare this number with the previous one. If it dropped, you roll the change back.
3. Force a structure and validate the output
Wiring free-text answers into code is fragile. Ask the model for a predefined JSON schema and validate the answer with your own code. The model follows the schema "usually", not "always".
$answer = json_decode($model->reply($input), true);
$valid = is_array($answer)
&& in_array($answer['category'] ?? null, $allowedCategories, true)
&& is_numeric($answer['confidence'] ?? null);
if (! $valid) {
return $this->ruleBasedFallback($input); // do not trust the model answer
}
The key point is the allowed-values list. If the model invents a category that is not on the list, the system must reject it.
4. A fallback path: if the AI fails, the product must not
External APIs slow down, quotas run out, providers have outages. The feature needs three states:
- Model answered and the answer passed validation: use it.
- Answer invalid or confidence low: fall back to rules, or ask the user.
- Model unreachable: turn the feature off quietly and keep the main flow working.
Showing a low-confidence answer as a "suggestion" instead of applying it automatically sharply reduces the cost of a mistake. The user confirms with one click; if it is wrong they correct it, and that correction goes into the golden set.
5. Measure cost and latency from the start
- Log input/output token counts, duration and result for every call.
- Set a monthly cost ceiling per feature; switch to the fallback automatically when it is reached.
- Cache repeated inputs. This lowers both cost and latency.
- Start with a small, cheap model and move to a larger one only where the golden set shows it is needed. Decide with numbers, not intuition.
Takeaway for a software company
Treat an AI feature not as "an API call" but as a measurable component. In practice that means: a golden set, a validation layer, a fallback path and a cost log. Without these four, the feature works in the demo and cannot be trusted in production.
If you want to build this infrastructure in your team, or review an existing LLM integration, you can write to me through the contact form.