Evaluating AI features
Why it matters
- Non-deterministic outputs make traditional unit testing insufficient for AI features.
- Small changes in a prompt or a model version can cause regressions in user experience.
- Interviewers look for your ability to move beyond 'it works on my machine' to systematic validation.
- Evaluating latency and cost trade-offs is critical for maintaining app performance and budget.
The simple explanation
- A chef testing a new recipe.
- They don't just taste one plate; they cook several batches.
- They check if the salt level is consistent across all of them.
- They compare the new recipe against the old one using a standard checklist.
Evaluating AI features requires moving from functional testing to probabilistic validation. Because LLM outputs are non-deterministic, you cannot assert that a string will exactly match a specific value. Instead, you must build a fixed test set of real-world inputs with expected properties (e.g., 'should be polite', 'should be under 50 words', 'should contain a JSON object').
There are two main stages of evaluation:
-
Offline Evaluation (Evals): Before shipping a new prompt or model, run your test set through the new configuration. Use a scoring mechanism to compare the new results against a baseline. You can use LLM-as-a-judge, where another model grades each output against a written rubric, but use this with care: it can introduce its own biases, so check its grades against human labels on a sample.
-
Online Evaluation: Once the feature is live, monitor real user signals. Common metrics include:
| Metric | Description |
|---|---|
| Thumbs up/down | Explicit user feedback on quality |
| Retry rate | How often users re-trigger the AI action |
| Latency | Time to first token or total response time |
| Fallback rate | How often the system hits a timeout or error |
Effective evaluation ensures that a prompt update intended to fix one bug doesn't break ten other use cases.
Notes
- Build a fixed test set of real inputs to ensure reproducible results.
- Use offline evals to score changes to prompts or models before release.
- Monitor online metrics like thumbs up/down and retry rates.
- LLM-as-a-judge is a powerful tool but can be biased or inconsistent.
- Always compare new model versions against a known baseline.
- Track fallback rates to see if new models are failing more often than old ones.
- Include latency and cost as part of your evaluation criteria.
- Test how the model handles edge cases or unexpected user inputs.
Code example
This code demonstrates a simple evaluation framework where we run a set of inputs through a mock AI service and score the results based on expected properties.
data class EvalInput(val prompt: String, val expectedProperty: String)
data class EvalResult(val input: EvalInput, val output: String, val passed: Boolean)
class MockAIService {
// Simulates an AI response
fun generateResponse(input: String): String = "The weather is sunny"
}
class AIEvaluator(private val aiService: MockAIService) {
fun runEvaluations(testSet: List<EvalInput>): List<EvalResult> {
return testSet.map {
val output = aiService.generateResponse(it.prompt)
// Simple check: does the output contain the expected property?
val passed = output.contains(it.expectedProperty, ignoreCase = true)
EvalResult(it, output, passed)
}
}
}
fun main() {
val service = MockAIService()
val evaluator = AIEvaluator(service)
val testSet = listOf(
EvalInput("What is the weather?", "sunny"),
EvalInput("Tell me a joke", "funny")
)
val results = evaluator.runEvaluations(testSet)
results.forEach {
println("Input: " + it.input.prompt + " | Passed: " + it.passed + " | Output: " + it.output)
}
}- The
EvalInputclass pairs a prompt with an expected property to allow for automated checking. - The
AIEvaluatoriterates through the test set, simulating the process of running a batch of tests. - The
passedboolean represents a simple pass/fail metric used in offline evaluation.
Run the idea in plain Kotlin
data class EvalInput(val prompt: String, val expectedProperty: String)
data class EvalResult(val input: EvalInput, val output: String, val passed: Boolean)
class MockAIService {
fun generateResponse(input: String): String {
return if (input == "Hello") "Hi there!" else "Unknown"
}
}
class AIEvaluator(private val aiService: MockAIService) {
fun runEvaluations(testSet: List<EvalInput>): List<EvalResult> {
val results = mutableListOf<EvalResult>()
for (item in testSet) {
val output = aiService.generateResponse(item.prompt)
val passed = output.contains(item.expectedProperty)
results.add(EvalResult(item, output, passed))
}
return results
}
}
fun main() {
val service = MockAIService()
val evaluator = AIEvaluator(service)
val testSet = listOf(
EvalInput("Hello", "Hi"),
EvalInput("Goodbye", "Bye")
)
val results = evaluator.runEvaluations(testSet)
for (result in results) {
println("Input: " + result.input.prompt + " | Passed: " + result.passed)
}
}In production
- Users reported the AI assistant stopped providing concise answers.
- The team updated the system prompt to improve accuracy.
- The new prompt caused the model to become overly verbose, breaking the UI layout.
- Created an offline evaluation suite with common user queries.
- Added a 'length' constraint to the evaluation scoring logic.
- Reverted the prompt and adjusted the instructions to include a word count limit.
- The dashboard showed a spike in 'empty' responses.
- A new model version was rolled out via server config.
- The model began returning empty strings for specific character encodings.
- Implemented an online metric to track the rate of empty responses.
- Set up an automated alert when the empty response rate increased.
- Rolled back the model version and updated the prompt to handle special characters.
Common mistakes
- Relying only on manual testing. Manual testing is not reproducible and cannot catch regressions in non-deterministic systems. Use a fixed test set instead.
- Using exact string matching for assertions. LLM outputs vary slightly even with the same input. Use property-based checks (e.g., contains, regex, or LLM-as-judge) instead.
- Ignoring latency in evaluations. A highly accurate model is useless if it takes a long time to respond on a mobile device. Always include latency in your metrics.
- Not monitoring online metrics. Offline tests don't capture real-world user behavior or edge cases. Always track thumbs up/down and retry rates.
- Over-reliance on LLM-as-a-judge. The judge model can have its own biases or fail to catch subtle errors. Use it alongside traditional heuristic checks.
Interview questions
Answer out loud first. Then reveal the answer and compare it at each depth.
“How do you test a feature that uses a non-deterministic LLM?”
“What are some online metrics for an AI-powered Android app?”
“Why shouldn't we just use a very powerful model for everything?”
“How do you know if a prompt change is a regression?”
Quiz
Which of these is an 'offline' evaluation method?
Why is exact string matching a bad idea for testing LLM outputs?
What does a high 'retry rate' usually indicate in an AI feature?
When using LLM-as-a-judge, what is a primary risk?
0 of 4 answered
Mini task
Task: Create a Simple Evaluation Script
Goal: Write a Kotlin script that evaluates a mock AI service to ensure it follows a specific format.
- Define a Data Class: Create a class
EvalResultthat holds the input prompt, the actual output, and a booleanisPassed. - Create a Mock Service: Write a class
MockAIServicewith a functiongetResponse(prompt: String): String. If the prompt is "color", return "The color is blue". For any other prompt, return "Unknown". - Implement the Evaluator: Write a class
Evaluatorthat takes a list of prompts. For each prompt, call the service and check if the output contains the word "is". - Run the Test: In your
mainfunction, create a list of prompts (including "color" and "hello"), run the evaluator, and print whether each prompt passed or failed.
Done Condition: The console output should clearly show which prompts passed the 'contains is' check and which failed.