SALE! Use code blackfriday for 40% off everything!
Hurry, sale ends soon! Click to see the full catalog.

Build a Lightweight LLM Evaluation Harness in Python

In this article, you will learn how to build a lightweight, reusable LLM evaluation harness in Python that combines exact-match scoring, an LLM-as-a-judge, and versioned regression tracking to catch prompt regressions.

Topics we will cover include:

  • How to score deterministic tasks like ticket routing with a normalized exact-match scorer.
  • How to grade open-ended responses with a rubric-based LLM-as-a-judge that returns structured Pydantic output.
  • How to log each prompt version’s results and compare runs case by case to detect regressions that an overall pass rate can hide.

Build a Lightweight LLM Evaluation Harness in Python

Introduction

When teams need to evaluate prompts passed to LLMs, they typically do so “by eye”: they run several examples in a playground environment while tweaking a system instruction, pick the one that looks better, and ship it. This might seem to work at first, but a tiny wording change can silently break a case that passed just a few days before.

The good news: we don’t need a heavy MLOps stack or observability platform to make this evaluation procedure more robust. This article will guide you through constructing, in under 150 lines of Python code, a reusable evaluation harness that combines multiple metrics:

  • For deterministic tasks like entity extraction or classification, we use Exact-Match (EM) scoring.
  • For open-ended response generation with varying wording, we consider rubric-based LLM-as-a-judge.
  • To catch any PASS that flips to FAIL in successive prompt versions, we will use versioned regression tracking.

Setup

The code example accompanying this article, which consists of a lightweight LLM evaluation harness, is ready to copy and run in a Python notebook environment, such as Colab. It runs on the Gemini API’s free tier, and it also includes a built-in “mock mode” that lets you test the harness without creating and using your own API key. Let’s start by installing some necessary packages:

Next, notice that we use USE_MOCK_LLM=True to run this code on the fly with simulated outputs. To run live calls to Gemini LLMs at no cost, get a free API key from Google AI Studio, add it as a new secret in Google Colab (the key icon in the left-hand menu) named GEMINI_API_KEY, and set USE_MOCK_LLM=False.

LLM Evaluation Harness Step-by-Step

The first building block for our LLM evaluation harness is a wrapper function that decouples the overall harness from the model calls. The third argument, version_tag, indicates the specific version of a prompt or use case being tested via multiple model calls.

To make the code accessible and help readers understand the rationale of the evaluation framework, we will use, by default, a set of mock LLM outputs — for real, generated LLM outputs, you will need to use your own Gemini API key and perform model calls for inference, as stated earlier. My main purpose is to have you learn the key steps and elements to consider throughout the process of building the framework:

Importantly, setting temperature=0.0 helps keep the evaluation runs as deterministic as possible, avoiding noise between runs so that score changes reflect prompt edits instead of random sampling.

Next, to build a golden test dataset, we don’t really need hundreds or thousands of examples, as long as we have a few high-signal ones. Our set of examples mixes two types of tasks for a support assistant: ticket routing that requires an exact label (exact_match) and replies where facts and tone matter the most (rubric).

Time to implement the first main pillar of the evaluation harness: a deterministic exact-match scorer. This is a cheap and fast approach aimed at catching outputs such as Category: BILLING.

The exact-match scorer returns 5 or 1 to keep every scorer on the same 1-5 scale used by the rubric-based LLM judge we will add next, so no normalization logic is needed when we aggregate the scores of all metrics.

Meanwhile, instead of using string matching to assess open-ended answers, we will pass the user prompt, model response, and rubric to a judge model. This forces the judge to return JSON matching a Pydantic schema that includes a 1-5 score, a boolean flag indicating whether the test is passed, and a brief reasoning string. Having structured model outputs at this point is important, as it saves us from writing brittle parsers based on complex regular expressions:

Time to wire the scorers and the dataset into an overarching run_eval() function. At each run, the results are logged to a local file named eval_history.json, using the prompt version as the key. This output log acts as the regression ledger, but it could be replaced by a database if you migrate this approach to a production setting:

Now we start the real evaluation, using two prompt versions for the same scenario: the first, baseline version is a loose prompt, while the second version is stricter and also asks for empathy in one of its rules.

Let’s see the evaluation results:

Although the code uses mocked examples to avoid API costs, the failure mode is a realistic one: tc_04_plan_switch returns a bare label in response to an open-ended billing question. This is a classic, easily identifiable bug in prompt engineering. Overall, a pass rate that increases from 50% to 75% might look like a good reason to celebrate.

Finally, we add the regression tracker: a function that compares two saved runs from the evaluation history log and explicitly flags status changes:

Output:

At first glance, the second, stricter version looks like the winner, but the per-case analysis shows that this conclusion might be a mistake. Two examples flipped from failure to pass, and these are real wins. However, one case shows a score drop from 5 to 1, i.e. the stricter rule bled into an open-ended question about billing, causing the model to reply with a bare label where an actual answer was expected.

Next Steps

The lightweight LLM evaluation harness shown throughout this article is designed to be incrementally expanded into a production-ready workflow. Ways to do this include adding an automated CI gate — a checkpoint that runs the tests before updated prompt or model versions go live — turning real bugs into new test cases to ensure the same errors don’t quietly return, and tracking cost and speed to avoid paying twice as much for a marginal improvement.

No comments yet.

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.