Tutorial
Write your first LLM eval
Turn real outputs into a small test set, add code-based checks in promptfoo and run them in CI to catch regressions on every change.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
An eval is a test for an LLM feature: a set of inputs, the behaviour you expect for each, and checks that score the outputs. Without one, every prompt change is a guess. You tweak a sentence to fix one bad answer and have no idea whether you broke three others.
This tutorial builds a small eval with promptfoo, an open-source command-line tool for testing prompts, for a support-email triage prompt. You will start from real cases, write checks in code, run them, and wire them into CI so they run on every pull request. Everything here applies to other eval tools too; promptfoo just keeps the setup to a YAML file.
Evals are not unit tests (but they rhyme)
Three differences shape how you write them:
- Outputs vary. The same input can produce different wording on each run. Check properties (“category is billing”, “valid JSON”, “under 160 characters”), not exact strings.
- A pass rate, not a pass. You will rarely get 100% on a meaningful set. You track the rate and investigate drops.
- Cases come from reality. The best test cases are real inputs that went wrong. Invented cases tend to test what you already thought about.
1. Start from real cases
Before writing any configuration, collect examples. If your feature is live, pull 20 to 50 real inputs from logs, including every one a user complained about. If it is new, write inputs the way real users would, typos included, and ask a colleague to add some.
Then read the outputs your current prompt produces for them, one by one, and write down what is wrong. This step (often called error analysis) tells you what to check. You may find that the problems cluster: “urgent requests get normal priority”, “summaries copy the customer’s angry tone”. Those clusters become your assertions.
2. Set up the project
Make a folder with three files. First the prompt, prompt.txt. Promptfoo fills in {{email}} from each test case:
You triage support emails for a project-management app.
Reply with JSON only, no prose, in this shape:
{"category": "billing" | "bug" | "feature-request" | "account" | "other", "priority": "low" | "normal" | "urgent", "summary": "<one sentence>"}
Email:
{{email}}
Then the configuration, promptfooconfig.yaml:
# yaml-language-server: $schema=https://promptfoo.dev/config-schema.json
description: Support email triage
prompts:
- file://prompt.txt
providers:
- id: anthropic:messages:claude-haiku-5-5
defaultTest:
assert:
- type: is-json
- type: javascript
value: |
const t = JSON.parse(output);
return ["billing", "bug", "feature-request", "account", "other"].includes(t.category);
- type: javascript
value: JSON.parse(output).summary.length <= 160
tests: file://cases.csv
Reading it from the top:
promptspoints at the prompt file. You can list several to compare versions side by side.providersis the model to test. Here it is a small, fast Claude model; for your own app, use the model you run in production. You can list more than one to compare models.defaultTest.assertholds checks that apply to every case: the output must be JSON, the category must be one of the allowed values, and the summary must be short.testsloads the cases from a CSV file.
3. Write the cases
cases.csv has one row per case. The email column fills the prompt variable. Columns named __expected (with an optional number) hold extra assertions for that row, and __description labels it in the report:
email,__expected1,__expected2,__description
"I was charged twice this month, please refund one.","javascript: JSON.parse(output).category === 'billing'","javascript: JSON.parse(output).priority !== 'low'",double charge
"The Gantt view crashes when I drag a task past December.","javascript: JSON.parse(output).category === 'bug'",,gantt crash
"Would love a dark mode for the mobile app!","javascript: JSON.parse(output).category === 'feature-request'","javascript: JSON.parse(output).priority === 'low'",dark mode request
"I can't log in since I changed my email address.","javascript: JSON.parse(output).category === 'account'",,login after email change
Four rows is enough to learn the mechanics. A useful first eval has 20 to 50, weighted towards the cases that went wrong.
Check the configuration before running anything:
npx promptfoo@latest validate -c promptfooconfig.yaml
4. Run it
export ANTHROPIC_API_KEY="your-key"
npx promptfoo@latest eval
npx promptfoo@latest view
eval runs every case against every prompt and provider and prints a table of passes and failures. view opens a local web page where you can read each output next to the assertions that judged it.
Read the failures before you change anything. For each one, decide whether the output is wrong or the assertion is. Both happen. An assertion that says a request for a refund must be “urgent” may simply encode a rule nobody agreed on.
5. Change the prompt and compare
Now the eval earns its keep. Copy prompt.txt to prompt-v2.txt, make a change (for example, add “A request for a refund or a mention of being charged twice is always at least normal priority”), and list both prompts:
prompts:
- file://prompt.txt
- file://prompt-v2.txt
Run the eval again. The report shows both prompts side by side on every case. You can see at a glance whether the change fixed what you meant to fix and whether it broke anything else.
Because outputs vary between runs, a single run can mislead on borderline cases. For decisions that matter, run each version more than once (promptfoo’s --repeat option does this) and compare pass rates rather than individual cells.
6. Run it in CI
An eval nobody runs stops protecting you. Add it to your pipeline so every change to the prompt is tested. With GitHub Actions, a minimal workflow looks like this:
# .github/workflows/llm-eval.yml
name: LLM eval
on:
pull_request:
paths: ["prompts/**", "evals/**"]
jobs:
eval:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npx promptfoo@latest eval -c evals/promptfooconfig.yaml
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
When any test fails, promptfoo eval exits with a non-zero status (100 by default), so the job fails and the pull request shows it. If you accept a pass rate below 100%, set the PROMPTFOO_PASS_RATE_THRESHOLD environment variable to the percentage you require. Limit the trigger to the paths that affect the LLM feature, because every run costs API calls.
Pitfalls
Writing cases you know will pass. An eval full of easy cases gives a reassuring 100% and tells you nothing. Lead with the failures.
Exact-match assertions on prose. They fail on harmless rewording. Assert on structure and facts.
Moving the goalposts. When a case fails, it is tempting to loosen the assertion. Sometimes that is right; make it a deliberate decision, not a reflex.
One giant score. An average across all cases hides a collapse in one category. Look at failures by type.
Never adding cases. Every bug report about the feature should become a new row. The eval set is a living record of what has gone wrong.
Next steps
- Coming from QA automation? Follow How to test LLM applications as a QA engineer for critical vs pass-rate release gates.
- Grade qualities that code cannot check in LLM-as-a-judge: grading outputs you can trust.
- Read why to begin with error analysis in Start your evals by reading outputs.
- Apply the same approach to retrieval with Evaluate RAG with a golden set.
- Make it a required check on pull requests with LLM evals in CI with promptfoo.
- Build a whole test suite around an LLM feature, from contract tests to a regression gate, in how to test LLM applications.
- Pick the right check for each failure with the LLM evaluation metrics cheat sheet.
- Choosing a framework for the next suite? Promptfoo vs DeepEval vs Ragas compares the three on how you write tests, built-in metrics and red-teaming.
- Use the same habit to pick a model in how to choose an LLM for a task.
- Use an eval to let DSPy tune a prompt, and see when that beats hand-tuning, in optimise a prompt automatically with DSPy.
Covers
- Tools
Check your understanding
First LLM eval quiz
Turning real outputs into test cases, writing code-based checks and running them on every change.
6 questions · pass mark 70%
Review
Related
- LLM-as-a-judge: grading outputs you can trust
Write narrow grading criteria, check the judge's verdicts against your own labels and decide when its agreement is good enough.
- Start your evals by reading outputs, not by picking metrics
The fastest way to useful LLM evals is to read real outputs and write down what is wrong. Why error analysis comes before metrics, and how to do it.
- Evaluate RAG answers with a golden dataset
Set up RAG evaluation with a golden dataset and promptfoo: score retrieval, correctness and grounding for every question, and fail CI when answer quality drops.
- Eval
A repeatable test that measures how well an AI system performs on a defined set of cases, so you can compare versions instead of guessing.
- Get reliable JSON from an LLM with structured outputs
Describe the output as a schema, let the provider enforce it, validate it with Pydantic AI or the AI SDK, and handle failures.