Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 32 ยท Evaluation and Deployment

Evaluating LLM Applications

Test LLM apps like software: build a dataset, write evaluators, use LLM-as-judge, check agent trajectories and catch regressions. Runnable Python eval harness included.

Traditional code has tests: the same input gives the same output, and a failing test tells you something broke. LLM applications are different. Outputs vary, "correct" is often fuzzy, and a prompt tweak that fixes one case quietly breaks three others. Without evaluation you are steering by feel. With it, you can change a prompt, a model or a retriever and see within minutes whether the app got better or worse. This lesson shows how to build that safety net.

LangChain and LangSmith are not available on this site, so real code is read-only. The runnable examples are a small evaluation harness in plain Python, with a scripted stand-in for the app and the judge.

The building blocks

An evaluation has four parts.

  1. A dataset: inputs, and often a reference answer or a rubric ("must mention the 30-day window").
  2. The system under test: your chain or agent, wrapped as a function input -> output.
  3. Evaluators: functions that score an output, from 0 to 1 or pass/fail.
  4. A report: scores per example and in aggregate, compared between versions.

Start small: 20 to 50 representative examples beat none. Take them from real user questions, add the edge cases that already bit you, and grow the set whenever a bug is found (each bug becomes a permanent test).

Kinds of evaluators

Evaluator How it works Use for
Exact or regex match Compare to a reference Classification, extraction, ids
Heuristic checks Length, JSON validity, banned words, required citations Format and policy rules
Embedding similarity Meaning closeness to a reference Paraphrased answers
LLM-as-judge A model grades against a rubric Helpfulness, faithfulness, tone
Human review People label a sample Calibration, high-stakes cases
Trajectory check Compare the tool calls an agent made Agents

Prefer the cheapest evaluator that answers your question. Code checks are free, deterministic and fast; keep model judges for what code cannot see.

A tiny harness

Python Stand-in: dataset, app and evaluators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Everything passes here, and that is itself a warning sign: a dataset where nothing ever fails is too easy. Add harder cases (ambiguous questions, wrong premises, rephrasings) until some fail. Also remember that string checks are fragile (a paraphrase like "thirty days" would fail), so read the failing outputs instead of trusting averages alone.

LLM-as-judge

Some qualities cannot be coded: "is the answer faithful to the retrieved documents?", "is the tone polite?". A judge model receives the question, the answer and a rubric, and returns a score with a reason.

Python
# Real LangChain (read-only)
from pydantic import BaseModel, Field
from langchain.chat_models import init_chat_model

class Grade(BaseModel):
    reason: str = Field(description="One sentence justification")
    faithful: bool = Field(description="True only if every claim is supported by the context")

judge = init_chat_model("anthropic:claude-sonnet-4-5").with_structured_output(Grade)

def faithfulness(question: str, context: str, answer: str) -> Grade:
    return judge.invoke(
        f"Context:\n{context}\n\nAnswer:\n{answer}\n\n"
        "Is every claim in the answer supported by the context?"
    )

Good practice for judges:

  • Use a clear rubric with a small scale (pass/fail or 1 to 5), and ask for the reason first.
  • Judge one criterion at a time.
  • Use a different or stronger model than the one under test when possible.
  • Calibrate: have a person grade 30 examples and check the judge agrees. Judges have biases (they favor long answers, and their own style).
Python Stand-in: a rubric judge and agreement with humans

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If agreement is low, fix the rubric before trusting the numbers.

Evaluating agents: trajectories

For an agent the final answer is not the whole story. Did it call the right tool? Too many tools? In the right order? A trajectory evaluator compares the actual list of tool calls to an expected one, or checks rules like "must call search before answer" and "must not call delete_record".

Python Stand-in: check an agent's tool calls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Regression testing and CI

Treat evaluations as tests:

  • Run the fast, code-based checks in CI on every change (with a fake or recorded model so tests are cheap and stable).
  • Run the full model-based evaluation nightly or before a release, and compare against the last good run.
  • Set thresholds ("faithfulness at least 90 percent"), and fail the build when they drop.
  • Because outputs vary, run important cases several times, or use temperature 0 where it is available.
  • Keep the dataset in version control or in a dataset tool such as LangSmith, which also stores experiment results side by side.
Python
# Real LangSmith (read-only)
from langsmith import Client

client = Client()
results = client.evaluate(
    app,                                   # function: inputs dict -> outputs dict
    data="support-questions-v1",           # a dataset stored in LangSmith
    evaluators=[includes_all],             # functions that return a score
    experiment_prefix="prompt-v2",
)

Each run becomes an experiment you can compare with earlier ones in the web UI. The same ideas work with a plain pytest file if you prefer to stay local.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is evaluation needed for LLM apps?

    Evaluation shows whether a change helped or hurt across many examples.
  2. 2. Which evaluator should you try first when it fits the question?

    Code checks are free, fast and repeatable; use model judges only for what code cannot measure.
  3. 3. How do you make sure an LLM judge can be trusted?

    Compare judge verdicts with human grades on a sample and fix disagreements.
  4. 4. What does a trajectory evaluator inspect?

    It checks which tools were called, in what order, and whether forbidden or wasteful calls occurred.

Summary

  • Evaluate with a dataset, a function under test, evaluators and a comparable report.
  • Start with 20 to 50 real examples and add every new bug as a test.
  • Use code checks first, LLM judges for fuzzy qualities, humans to calibrate.
  • Check agent trajectories, not only final answers.
  • Run cheap checks in CI and full evaluations before releases.

Next: Tracing and Observability