LLM Evaluation Metrics in Python 2026

📘 Tutorials 2026-07-16 · Updated 2026-10-31 3 min read

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

  1. Judge and model from the same vendor inflate scores.
  2. Fewer than 50 samples is noise.
  3. Averages hide disaster cases - look at the distribution.
  4. Optimizing the metric instead of the user experience.
  5. 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

Related Articles
2026-07-19
AI Model Deployment: From Dev to Production
2026-08-13
ControlNet Guide 2026: Pose, Depth and Canny Control for Stable Diffusion, Explained
2026-08-10
OpenAI Install Guide 2026: Set Up the Python SDK, CLI, and Local Models Step by Step
2026-08-02
Self-Hosted Vector Database 2026: Qdrant vs Milvus vs Chroma - Which to Run Yourself
2026-10-21
Best AI Invoice Software in Poznan 2026: KSeF Deadlines, 23% VAT and a Two-Person Studio
2026-08-01
LLM Finetuning Toolkit 2026: Unsloth vs Axolotl vs TRL Compared

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.

💬 Comments (0)

No comments yet. Be the first!

Login to comment