Your Own LLM Test Harness in Python: 4 Modules and About 60 Lines of Code
Public benchmarks such as MMLU and GSM8K won't tell you whether your support bot follows refusal rules or whether your RAG pipeline retrieves the right context. For that you need your own test harness: 20–50 real prompts, an async run through LiteLLM, a judge, and a log. It fits in roughly 60 lines of Python with two dependencies. Raw responses are stored separately from scores, so you can change the criteria without repeating paid API calls.
AI-processed from Machine Learning Mastery; edited by Hamidun News
The classic way to find out whether a model fits your product is to look at its MMLU or GSM8K score. The problem is that these benchmarks measure general intelligence, not whether your support bot refuses dangerous requests or whether your RAG pulls the right chunks of documentation. For those questions you need your own test harness. Machine Learning Mastery author Vinod Chugani shows how to build one from scratch in Python: four small modules, two dependencies, and about 60 lines of code.
In essence, this is an ordinary test framework from software development. It feeds inputs into the system, collects the responses, and compares them with expectations. But instead of unit tests you have a set of prompts, and instead of a "green build" you track the pass rate, latency, and cost from version to version.
The Reference Set and the Run
The foundation is a so-called golden dataset: a list of cases, each with a unique id, an input query, and a short description of the expected result. For example, for the question "What does exit code 137 mean?" the expected concept is "out of memory," and for a request to write a script that deletes all database tables, the expected result is a refusal.
The author's main advice: start with 20–50 cases drawn from real user logs and known failures, not invented at a desk. Made-up examples tend to cluster around the "happy path," while real queries are messier, and that mess is exactly what the harness should catch. Expectations should be stated briefly and unambiguously, or the scores will drift.
An async runner built on LiteLLM executes the cases. The library provides a single interface to OpenAI, Anthropic, Google, and local models, so switching the model under test means changing one line. The call sets three retries on failure and a 30-second timeout, and all cases run in parallel via asyncio.gather. On large sets this noticeably shortens the run. An important detail: the runner returns only the raw response and evaluates nothing.
Two Judges and Saving on Tokens
Scoring is handled by a separate layer with two modes. The deterministic judge simply checks whether the expected string appears in the response. It is fast, free, and produces the same result on every run, which makes it suitable for checking format, keywords, and patterns.
The LLM judge is slower and costs money, but it understands meaning. The reply "this operation will permanently delete your data" is a refusal in substance, even though the word "refusal" never appears in it, and a string check would fail it. To keep the judge from answering vaguely, its output is bound to a Pydantic schema: a boolean passed field and a text reason field. If the model breaks the format, that surfaces immediately instead of crashing the parser in the middle of a run.
This suggests a practical cost-saving scheme: run everything through the cheap deterministic judge first, and send only the disputed cases to the paid LLM judge. For stability, the author recommends setting temperature=0 in judge calls.
The Log That Lets You Rescore
Each result is immediately appended to a .jsonl file, one record per line, with a timestamp. The format is resilient to failures: if a run stops halfway, everything already written is kept. Even more important, the log stores the raw response next to its score. When you change the judge's criteria, you only need to recompute scores from the saved responses, without paying for inference again. On a 500-case dataset with a paid API, this saves both time and money.
If you hit rate limits, wrap the gather in a Semaphore(10) to cap the number of concurrent requests.
Limitations and Next Steps
This is a teaching scaffold, not a finished product. Substring matching is crude and easily produces false positives, and the LLM judge remains nondeterministic and may be biased toward answer style. 20–50 cases are enough for first signals but not for statistically reliable conclusions. The example code also uses the deprecated datetime.utcnow(), and model names such as GPT-4o and Claude 3.5 Sonnet should be replaced with current ones.
The author next suggests adding latency and token cost to each record, wiring the harness into CI/CD to catch regressions on every model update, building a summary dashboard from the .jsonl files, and moving from pass/fail scoring to rubrics. The architecture of four independent modules lets you add all of this without rewriting it.
The practical takeaway is simple: build your test set from the real failures of your product, store raw responses separately from scores, and run the expensive judge only where the cheap check falls short.
- Nov 26, 2026OpenAI Pilots Private Inference: Business Queries Without Data Access
- Nov 26, 2026A Swarm of AI Agents Hunts for a Proof That P = NP: 600 Volunteers Supply 85% of the Compute
- Nov 26, 2026OpenAI Claims a 9/4 Exponent Limit for Matrix Multiplication and an Algorithm for Smale's Problem
- Nov 26, 2026OpenAI Merges Chat and Work in ChatGPT: Heavy Users Will Hit Their Limit Sooner
Key Questions Answered
What are the key details on The Reference Set and the Run?
The foundation is a so-called golden dataset: a list of cases, each with a unique id, an input query, and a short description of the expected result. For example, for the question "What does exit code 137 mean?" the expected concept is "out of memory," and for a request to write a script that deletes all database tables, the expected result is a refusal. The author's main advice: start with 20–50 cases drawn from real user logs and known failures, not invented at a desk.
What are the key details on Two Judges and Saving on Tokens?
Scoring is handled by a separate layer with two modes. The deterministic judge simply checks whether the expected string appears in the response. It is fast, free, and produces the same result on every run, which makes it suitable for checking format, keywords, and patterns. The LLM judge is slower and costs money, but it understands meaning. The reply "this operation will permanently delete your data" is a refusal in substance, even though the word "refusal" never appears in it, and a string check would fail it.
What are the key details on The Log That Lets You Rescore?
Each result is immediately appended to a .jsonl file, one record per line, with a timestamp. The format is resilient to failures: if a run stops halfway, everything already written is kept. Even more important, the log stores the raw response next to its score. When you change the judge's criteria, you only need to recompute scores from the saved responses, without paying for inference again. On a 500-case dataset with a paid API, this saves both time and money.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
