Evaluation is the discipline of systematically measuring how well your model or agent performs on tasks that matter to your application. Without evals, every model update, prompt change, or configuration tweak is a leap of faith. With a solid eval suite, you can catch regressions before they reach production, compare model versions objectively, and build confidence that your system is doing what you intend. This guide covers the main evaluation patterns — rule-based grading, LLM-as-a-judge, the Evals API, and hallucination detection — so you can build the right combination for your use case.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
Why evaluation matters
Measure quality
Quantify how often your model produces correct, relevant, or on-policy responses rather than relying on qualitative impressions.
Catch regressions
Detect when a new model version, prompt edit, or configuration change silently degrades performance on inputs that worked before.
Compare options
Make data-driven decisions when choosing between model sizes, prompting strategies, or fine-tuned variants.
Types of evaluation
There are three main evaluation approaches, each with different trade-offs in cost, speed, and sensitivity.Rule-based evals
Rule-based evals
Rule-based evals use deterministic code to grade model outputs. They are fast, cheap, and fully reproducible. Use them for outputs with a clear correct answer.Common rule-based graders:Rule-based evals break down when outputs are open-ended or when multiple valid phrasings exist. In those cases, move to LLM-as-a-judge.
- Exact match — output must equal the expected string (useful for classification or short factual answers)
- Contains — output must include a specific substring or token
- Regex — output must match a regular expression pattern
- JSON parse — output must be valid JSON, optionally conforming to a schema
- F1 / token overlap — partial credit based on shared tokens between output and reference
LLM-as-a-judge
LLM-as-a-judge
LLM-as-a-judge uses a capable model (typically GPT-4o) to score or compare model outputs. This approach handles the ambiguity of natural language and can evaluate qualities that are hard to specify as rules — such as helpfulness, fluency, and factual accuracy relative to a reference.LLM-as-a-judge is slower and more expensive than rule-based evals, but is far more flexible. It is the right choice when:
- Outputs are free-form and multiple valid answers exist
- You need to evaluate subjective qualities like tone or clarity
- You are comparing two responses and need a preference judgment
Human evaluation
Human evaluation
Human evaluation is the gold standard — it captures nuances that neither code nor models can reliably detect. Use it to:
- Calibrate your automated evals (check that your grader agrees with humans)
- Audit a sample of LLM-as-a-judge decisions for bias or systematic errors
- Evaluate safety-critical outputs before deployment
LLM-as-a-judge pattern
The core of the LLM-as-a-judge pattern is a prompt that gives the grading model all the context it needs: the original question, the reference answer (if available), and the model’s response. The grader returns a score and optionally a brief rationale.Design considerations
Prompt the grader carefully. The quality of LLM-as-a-judge depends entirely on the grading prompt. Be explicit about the rubric — what makes an answer a 5 versus a 3 versus a 1. Without clear criteria, the grader will be inconsistent. Use a rubric with defined levels. Vague instructions like “rate from 1-5” produce noisy scores. Define each level:The Evals API
OpenAI’s hosted Evals API provides a structured way to run evaluations at scale without managing your own infrastructure. You define an eval — a dataset of prompts with expected outputs and a grader — and the API runs it, records results, and lets you compare runs over time.Define your eval dataset
Create a JSONL file where each line is an evaluation example. Include the input prompt and the reference (correct) output.
Create and run the eval
Use the API to create an eval and a run. Specify the model to evaluate and the grader type.
Hallucination detection
Hallucinations — outputs that are fluent and confident but factually wrong — are one of the most important failure modes to detect and guard against. The standard approach is to build a grader that checks whether every factual claim in the model’s response is supported by a provided context.Identify the criteria
Define what counts as a hallucination for your use case. Common criteria:
- The response contains a claim not found in the provided source documents
- The response contradicts a known fact in the context
- The response fabricates a citation, name, or statistic
Build a hallucination grader
Use GPT-4o to check each criterion and return a binary verdict plus a list of hallucinated claims.
Building an eval pipeline
A mature eval pipeline runs automatically, stores results over time, and alerts you to regressions. The key components are:- Dataset — a representative set of inputs covering common and edge-case scenarios
- Grader — one or more grading functions suited to your output type
- Runner — code that feeds each input to your model and records the output
- Storage — a persistent log of scores and outputs per run, keyed by model version and date
- Alerting — a threshold below which a regression is flagged for human review
Next steps
Fine-tuning
Use your eval results to decide when to fine-tune, and then measure the improvement with the same eval suite.
Reinforcement fine-tuning
For tasks with verifiable outputs, learn how RFT uses reward signals — essentially grader functions — to train models that reason more reliably.