LLM Evaluation Metrics in Python 2026
There is no single correct answer for an LLM, so evaluation needs three layers. Here is how to score faithfulness, relevancy, context precision and recall in Python with RAGAS and DeepEval, plus CI thresholds and five common traps.
📜 Table of Contents
📖 Keep reading
LLM Evaluation Metrics in Python 2026: RAGAS and DeepEval
LLM output has no single correct answer, so evaluation runs in three layers: offline metric scoring, human sampling, and online feedback. This guide covers the first layer in code.
Metric map
| Metric | Question it answers | Inputs |
|---|---|---|
| Faithfulness | Is every claim supported by the retrieved context? | answer + context |
| Answer relevancy | Does it actually answer the question? | question + answer |
| Context precision | How much retrieved text was useful? | question + context + reference |
| Context recall | Were reference facts present in the context? | reference + context |
| Answer correctness | How close to the reference answer? | answer + reference |
The first two need no labels - use them before you have annotations.
RAGAS
from ragas import evaluate, EvaluationDataset
from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import Faithfulness, ResponseRelevancy
from ragas.llms import LangchainLLMWrapper
from langchain_openai import ChatOpenAI
judge = LangchainLLMWrapper(ChatOpenAI(model="gpt-4o-mini", temperature=0))
sample = SingleTurnSample(
user_input="How long do refunds take?",
response="Three to seven business days to the original payment method.",
retrieved_contexts=["Refunds are returned to the original payment method within 3-7 business days."],
reference="3-7 business days.",
)
result = evaluate(dataset=EvaluationDataset(samples=[sample]),
metrics=[Faithfulness(), ResponseRelevancy()], llm=judge)
The API changed after 0.2 - check the official docs rather than old blog posts. Use a judge model stronger than the model under test.
DeepEval
DeepEval is pytest-shaped: build a test case, assert against a threshold, fail the build.
from deepeval import assert_test
from deepeval.metrics import AnswerRelevancyMetric, FaithfulnessMetric, GEval
from deepeval.test_case import LLMTestCase, LLMTestCaseParams
case = LLMTestCase(input="...", actual_output="...", retrieval_context=["..."],
expected_output="...")
tone = GEval(name="tone", criteria="Conversational, no corporate filler.",
evaluation_params=[LLMTestCaseParams.INPUT, LLMTestCaseParams.ACTUAL_OUTPUT],
threshold=0.7)
assert_test(case, [AnswerRelevancyMetric(threshold=0.7), FaithfulnessMetric(threshold=0.8), tone])
GEval is the most useful piece: write the standard in plain language and let the judge score against it.
Wiring it into CI
Keep a fixed set of 50-200 real questions with human-written references, run it on every prompt or model change, block merges on regressions, store historical scores, and have humans review ~20 samples weekly. Set the threshold from your current baseline, not from ambition.
Five traps
- Judge and model from the same vendor inflate scores.
- Fewer than 50 samples is noise.
- Averages hide disaster cases - look at the distribution.
- Optimizing the metric instead of the user experience.
- Reporting without fixing the retrieval or prompt.
FAQ
No reference answers? Faithfulness and relevancy work without them.
Too expensive? Use a cheap judge, smaller samples, and sampled online evaluation.
RAGAS or DeepEval? RAGAS for pipeline diagnosis, DeepEval for engineering tests; they coexist fine.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.