Skip to content

< CollectionsLLM-as-a-Judge · 2 of 6 ·110 · Level 07, Evals

Swap the Answers Before You Trust the Judge

medium · implement · Evals

An LLM judge comparing two answers tends to prefer the one it reads first. Implement pairwise(judge, question, a, b), which asks the judge twice with the answers swapped and only declares a win the judge gives in both orders, and tally(judge, battles), which runs a batch. judge(question, first, second) is provided and returns the judge's reply as text.

  1. Two calls, in this order: judge(question, a, b), then judge(question, b, a).
  2. Reading a reply. The verdict is the last [[A]], [[B]] or [[C]] in the text, ignoring case and spaces inside the brackets: [[A]] means the answer shown first is better, [[B]] the one shown second, [[C]] a tie. A reply without one (Assistant A is better) has no verdict.
  3. pairwise returns {"winner": w, "verdicts": [v1, v2]}. Each verdict is the answer that call preferred ("a" or "b"), "tie", or None for a reply with no verdict. w is "a" or "b" only when both verdicts name it; anything else is "tie".
  4. tally takes battles as (question, model_a, answer_a, model_b, answer_b) and returns {"wins": {model: count}, "ties": n, "consistent": n, "favored_first": n}. wins has every model in the battles, with 0 for one that never won; ties counts battles whose winner is "tie".
  5. consistent counts battles whose two verdicts are equal and not None. favored_first counts battles where each call picked the answer it showed first.

The catch. In the second call b is shown first, so [[A]] there is a verdict for b. A judge that always replies [[A]] gives verdicts ["a", "b"]: a tie and one more favored_first, not a win for a.