Monday , November 30 2026
Evaluate LLM apps: golden test sets, exact match and F1 in Python, LLM-as-a-judge, RAG metrics and best practice.

LLM Evaluation: Metrics for LLM and RAG Applications

Updated: 28 November 2026Level: IntermediateReading time: about 10 minutesPython 3 · tested

LLM evaluation means measuring how good an LLM application’s answers are – correct, relevant, grounded, safe and fast – in a repeatable way. Without it you cannot tell whether a new prompt, model or RAG setting made things better or worse.

What You Will Learn

  • What to measure in an LLM app
  • Build a small test set
  • Exact match and token-F1 metrics, in Python
  • LLM-as-a-judge and RAG metrics
  • Human review, safety and latency

Prerequisites

Prompt Engineering and F1 Score.

What to Measure

Dimension Question
Correctness Is the answer right?
Relevance Does it answer what was asked?
Faithfulness (RAG) Is every claim supported by the retrieved context?
Retrieval quality Did we fetch the right chunks?
Format Valid JSON, right length, right language?
Safety Free of harmful or private content?
Cost and latency Tokens used and seconds per answer

Step 1: A Test Set

Write 30–100 realistic questions with the expected answer (a “golden” set). Include easy, hard and tricky cases, and questions that should get “I don’t know”.

Step 2: Automatic Metrics

Example
def normalize(s):
    return " ".join(s.lower().replace(".", "").split())

def exact_match(pred, ref):
    return int(normalize(pred) == normalize(ref))

def token_f1(pred, ref):
    p, r = normalize(pred).split(), normalize(ref).split()
    common = sum(min(p.count(w), r.count(w)) for w in set(p))
    if common == 0:
        return 0.0
    precision, recall = common / len(p), common / len(r)
    return round(2 * precision * recall / (precision + recall), 3)

tests = [("8 weeks", "8 weeks"),
         ("The course lasts 8 weeks.", "8 weeks"),
         ("It costs Rs 15,000", "8 weeks")]
for pred, ref in tests:
    print(f"{pred!r:30} EM={exact_match(pred, ref)}  F1={token_f1(pred, ref)}")

Explanation: Exact match is strict: the correct but longer second answer scores 0. Token-F1 gives partial credit for overlapping words (2 of 5 predicted words match both reference words → F1 0.571). The wrong third answer scores 0 on both.

Output
'8 weeks'                      EM=1  F1=1.0
'The course lasts 8 weeks.'    EM=0  F1=0.571
'It costs Rs 15,000'           EM=0  F1=0.0

Step 3: LLM-as-a-Judge

Example
judge_prompt = """You are grading an answer.
Question: {q}
Reference answer: {ref}
Model answer: {pred}
Score 1-5 for correctness and give a one-line reason.
Return JSON: {{"score": int, "reason": str}}"""

Explanation: For open-ended answers, a strong LLM can grade them against a reference or a rubric. Check a sample of its grades by hand, and keep the judge prompt fixed so scores are comparable.

RAG-Specific Metrics

Metric Measures
Context recall / hit rate Is the needed chunk among the retrieved ones?
Context precision How many retrieved chunks are actually relevant?
Faithfulness Are answer claims supported by the context?
Answer relevance Does the answer address the question?

Libraries such as Ragas, DeepEval and promptfoo, and platforms such as LangSmith, automate these metrics.

Good Practice

  • Run the test set on every change of prompt, model or chunk size.
  • Track scores over time in a table.
  • Add every real failure from users to the test set.
  • Combine automatic metrics with regular human review.

Common Errors

Problem Cause and fix
High scores but users unhappy The test set is too easy or not realistic – add real questions.
Judge scores vary Use temperature 0 and a clear rubric.

Series

LLM series: LLM Fundamentals · Transformer Architecture · OpenAI API · Hugging Face · LangChain · LlamaIndex · Vector Databases · LLM Evaluation

Interview Questions

Q: What is faithfulness in RAG?
Whether the generated answer is supported by the retrieved context rather than invented.

Q: Why not only exact match?
Correct answers can be worded differently; token-F1 or an LLM judge handles this better.

Practice Exercises

  1. Create a 10-question golden set for one of your tutorials.
  2. Compute average EM and F1 for 10 model answers.
  3. Write a rubric for judging the “helpfulness” of an answer.

Summary

Evaluate LLM apps with a golden test set, automatic metrics such as EM and F1, LLM-as-a-judge for open answers, RAG metrics for retrieval and faithfulness, and human review.

Next: LLM Fundamentals · Generative AI series

About Machine Learning

Check Also

AI Agent Frameworks Compared: LangGraph vs CrewAI vs OpenAI Agents SDK

Compare AI agent frameworks in 2026: LangGraph, CrewAI, OpenAI Agents SDK, Claude Agent SDK, Google ADK and Microsoft Agent Framework, with code and how to choose.