Code-Based Graders & the Metrics That Mislead
You'll learn to
- -Use plain code to grade everything a rule can decide, including real outcomes
- -Explain why overlap metrics like BLEU and ROUGE mislead on open-ended answers
- -Spot the trap of generic scores such as "helpfulness 4.2"
- -Choose between a code grader and a judge for a given check
Before you pay a model to judge anything, ask a cheaper question: can a few lines of code decide this? Code graders are fast, free to run on every change, and give the same answer every time. They are also easy to misuse, and a few famous metrics look like graders but measure the wrong thing. This chapter covers both sides.
An Analogy: The Spelling Checker and the Editor
A spelling checker is perfect for one job: finding words that are not in its word list. It is fast and never tired. But it cannot tell you whether a paragraph is persuasive. For that you need an editor. Code graders are the spelling checkers of AI quality. Use them for everything they can decide, and save the editors, the judges, for what they cannot.
What Code Can Grade
- -Format: is the output valid JSON, does it match a schema, is it under the length limit?
- -Required and forbidden content: does it mention the refund window, does it avoid leaking an internal ID or an email address?
- -Tool use: did the agent call the right tool with the right arguments, in a sensible order?
- -Correctness with a checkable answer: run the generated code against unit tests, compare a computed number, query the database.
- -Outcome: is the world in the state it should be in afterward?
Grade the Outcome, Not the Story
An agent's transcript can say anything. The database says what happened. Anthropic's guidance on agent evals makes the same point: grade what the agent produced, not the exact path it took, because paths are brittle and a clever valid route should not be punished. The next block runs two trials of a refund agent. Both end with the agent saying it is done. Press Run and see which grader catches the difference.
The claim grader passes both trials. Only the outcome grader notices that trial 2 never wrote the refund. In a real harness the "refunds table" is your database or a test double of it, started from a clean state for each trial so one trial cannot affect the next.
The Metrics That Mislead
For years, text generation was scored by comparing the output to a reference answer with an overlap formula. BLEU, built for machine translation, measures how many word groups match. ROUGE, built for summaries, measures how much of the reference the output recovers. Both reward matching words, and that is the problem. Eugene Yan's survey of LLM patterns notes the poor correlation between these metrics and human judgment, and that scores were hard to reproduce across implementations.
A related metric, BERTScore, compares meaning through embeddings, so it handles synonyms and paraphrase better. It is a reasonable tool for search and retrieval debugging. It is not a substitute for deciding whether an answer is correct.
The next block makes the failure concrete. The reference says returns are accepted within 30 days. One candidate answer says 3 days, which is wrong and shares most of its words. Another says "you have a month", which is right and shares almost none. Press Run to see how a simple overlap score and a simple code grader treat each one.
The overlap score gives the wrong answer 0.88 and the correct paraphrase 0.00, the exact opposite of the truth. The code grader gets all three right, but notice why: it was written for this one fact, with a rule for "a month". That is the trade-off. Code graders are precise where you can write the rule, and silent about everything you did not think of. When the space of correct wordings is too wide for rules, you need a judge, which is the subject of the next module.
The Generic-Score Trap
Many tools ship ready-made scores called helpfulness, coherence, toxicity, or hallucination, usually from a model. They are tempting because they produce a tidy chart. The risk is false confidence. A feature can score 4.2 out of 5 on helpfulness while failing the one thing your product exists to do. Practitioners who teach evals are blunt about it: use generic scores, at most, to find traces worth reading, never as proof of quality. Measure the specific failures you found during error analysis.
Choosing Code or a Judge
A quick rule of thumb
- -Use code when: The answer is checkable: a format, a number, a required phrase, a test result, a database state.
- -Use a judge when: Correctness depends on meaning or tone and many wordings are valid: summaries, explanations, empathy, faithfulness to a source.
- -Use a person when: Stakes are high, the criteria are still being discovered, or you are checking the judge itself.
Always try a code check first, even for fuzzy-sounding problems. Often a failure that seems to need a judge, like "the bot leaked an internal ID", is really a pattern a few lines of code can catch.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!