Kolkata ·  
AI engineering
evals
short · 3 min

Code-based evals

Checks a script can run on an output: exact match, regex, valid JSON, has a citation.

updated 29 Sept 2026

Here's one output from a support feature. It tags the ticket and drafts a reply:

tag: billing

Thanks for writing in. Under our refund policy you can get your money back within 30 days. I've flagged ticket TCK-88213 for the team. You'll hear back by email.

Is it any good? Before we reach for a model or a person to grade it, let's see how much a few lines of ordinary code can tell us.

Five checks on one reply

The tag should be billing. We wrote down the right label for this test case, so we compare, after trimming spaces and lowercasing. Exact match: pass.

A billing reply should mention the refund policy. Is that phrase in the text? Pass.

It shouldn't leak internal ticket IDs. Same check, flipped, maybe with a regex for the ID pattern. Fail. There's TCK-88213 sitting right there.

It should be two to six sentences. Count them: four. Pass.

And if the feature returns JSON, parse it and check the fields are there (that's structured output meeting schema validation). Pass.

That's a code-based eval: a grader written as plain code that takes the output and returns pass or fail by a fixed rule. Each one is tiny, fast, free to run and gives the same answer every time. Cheap enough to run on every change: a prompt edit, new retrieved context, a new model.

A good rule of thumb from people who ship these: write at least three checks per feature, from real inputs and outputs. If you can't think of three, the feature may not be defined well enough yet, or it's too open-ended for this, like a general chatbot.

There are a few other kinds worth knowing. For generated code, run it: if the user asked for a function called foo, then foo should exist and be callable afterwards. And there are scores that compare the output with a reference answer, like ROUGE-L (shared words, in order) or cosine similarity between embeddings (shared meaning). Those are code too, but they hand back a number, so we have to pick a threshold.

Some checks don't need an answer key

Notice the tag check needed the right answer stored next to the test. The ID check didn't. "No internal IDs", "valid JSON", "under six sentences" work on any output at all, including live traffic where nobody knows the right answer.

So the same function can do a second job. In production it can stop that reply before the customer sees the ticket ID. That's a guardrail, and it's why reference-free checks often live in both places.

Where code runs out

So can we grade everything this way? No. Two things go wrong.

Checks are brittle. Too strict and they fail good answers: an exact match on "Paris" fails "The capital is Paris." Too loose and they pass bad ones. The advice is to relax each check to the weakest thing any correct answer must satisfy, and no weaker. Check for "Paris", not the whole sentence. (Also test the check itself. A regex with a typo can pass everything, and nothing will tell you.)

And code can't read meaning. Our reply mentions the refund policy, but what if it got the policy wrong? Say it's really 14 days. Every check above still passes. A word-overlap score can't save us either; it only says the words look like the reference. For "is this right" or "is this helpful", we need a model grader or a person, and both cost more and need checking themselves. So code checks go first, in every eval, and the model grader only gets the questions code can't answer.

Further reading