GenAIHub
← Back to Technical Section

LLM-as-a-Judge

Using LLMs to Evaluate LLM Outputs at Scale

What is LLM-as-a-Judge?

LLM-as-a-Judge is an evaluation technique where a powerful LLM (like GPT-4 or Claude) is used to assess the quality of outputs from another LLM. This approach enables scalable, nuanced evaluation of subjective qualities like helpfulness, coherence, and accuracy—tasks that traditionally required expensive human review.

"LLM-as-a-Judge has emerged as a practical middle ground between expensive human evaluation and limited automated metrics. When properly calibrated, it can achieve high correlation with human judgments while scaling to thousands of examples."

Best For

Subjective quality, open-ended tasks

Use Caution

Factual accuracy, domain expertise

Avoid

Same model judging itself

Evaluation Patterns

Single-Point Scoring

Rate a single response on a scale (e.g., 1-5) across one or more criteria.

"Rate this response 1-5 for helpfulness"

Pairwise Comparison

Compare two responses and pick the better one (or tie). Reduces calibration issues.

"Which response is better: A or B?"

Reference-Based

Compare response against an ideal "golden" answer. Good for factual tasks.

"How well does the response match the reference?"

Multi-Criteria

Score on multiple dimensions separately (accuracy, helpfulness, safety, etc.).

"Score 1-5 on: accuracy, clarity, completeness"

Implementation Example

# LLM-as-a-Judge with structured output
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI()

class EvalResult(BaseModel):
    accuracy: int  # 1-5
    helpfulness: int  # 1-5
    clarity: int  # 1-5
    reasoning: str

JUDGE_PROMPT = """You are an expert evaluator. Assess the response quality.

**Question:** {question}
**Response:** {response}

Rate 1-5 on each criterion:
- Accuracy: Is the information correct?
- Helpfulness: Does it address the user's needs?
- Clarity: Is it well-organized and easy to understand?

Provide your reasoning, then scores."""

def evaluate(question: str, response: str) -> EvalResult:
    result = client.beta.chat.completions.parse(
        model="gpt-4o",
        messages=[{
            "role": "user",
            "content": JUDGE_PROMPT.format(
                question=question, 
                response=response
            )
        }],
        response_format=EvalResult
    )
    return result.choices[0].message.parsed

# Usage
scores = evaluate(
    question="What is RAG?",
    response="RAG stands for Retrieval-Augmented Generation..."
)
print(f"Accuracy: {scores.accuracy}, Helpfulness: {scores.helpfulness}")

Pairwise Comparison

# Pairwise comparison with position swapping
import random

PAIRWISE_PROMPT = """Compare these two responses to the question.

**Question:** {question}

**Response A:**
{response_a}

**Response B:**
{response_b}

Which response is better? Reply with:
- "A" if Response A is better
- "B" if Response B is better  
- "TIE" if they are equally good

Your choice:"""

def pairwise_compare(question: str, resp_1: str, resp_2: str) -> str:
    # Randomly swap positions to reduce position bias
    if random.random() > 0.5:
        a, b = resp_1, resp_2
        mapping = {"A": "first", "B": "second"}
    else:
        a, b = resp_2, resp_1
        mapping = {"A": "second", "B": "first"}
    
    result = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": PAIRWISE_PROMPT.format(
            question=question, response_a=a, response_b=b
        )}]
    )
    
    choice = result.choices[0].message.content.strip()
    return mapping.get(choice, "TIE")

Known Biases & Mitigations

Bias Type Description Mitigation
Position Bias Prefers first or last option in comparisons Randomly swap positions, average results
Verbosity Bias Favors longer, more detailed responses Normalize for length, penalize over-explanation
Self-Enhancement Model prefers its own style/outputs Use different model as judge
Sycophancy Agrees with user's implied preferences Neutral prompts, no hints about preferred answer
Calibration Drift Inconsistent scoring across runs Use structured output, clear rubrics

Common Evaluation Criteria

Accuracy

Is the information factually correct?

Helpfulness

Does it address the user's actual needs?

Clarity

Is it well-organized and easy to understand?

Completeness

Does it fully answer the question?

Relevance

Is it on-topic without unnecessary content?

Safety

Is the content appropriate and harmless?

Best Practices

Do This

  • Use a stronger model as judge
  • Require chain-of-thought reasoning
  • Use structured output (JSON/Pydantic)
  • Calibrate with human judgments
  • Randomize positions in comparisons
  • Provide clear scoring rubrics

Avoid This

  • Same model judging itself
  • Vague evaluation criteria
  • Ignoring position bias
  • Trusting scores without reasoning
  • Skipping calibration step
  • Only using LLM-judge for factual claims

LLM-as-Judge Tools

Related Topics