Skip to main content
LLM-as-judge evaluation uses a language model to assess subjective qualities in agent outputs - things like helpfulness, tone, coherence, and factual accuracy. This approach bridges the gap between deterministic code checks and human judgment.

When to Use LLM-as-Judge

Use LLM-as-judge for criteria that are:
  • Subjective - Helpfulness, professionalism, empathy
  • Semantic - Correctness beyond exact string matching
  • Contextual - Appropriateness given the question
  • Nuanced - Requires reasoning about content
LLM-as-judge is powerful but slower and more expensive than code-based evaluation. Use it for subjective criteria where code-based checks aren’t sufficient.

Basic LLM-as-Judge

Here’s a simple evaluator using OpenAI:
simple_llm_judge.py

Structured Prompts

Provide clear evaluation criteria in your prompt:
structured_prompt.py

Real-World Example: Correctness Check

Evaluate if the agent’s answer is factually correct:
eval_correctness.py
Using temperature=0 makes LLM evaluations more deterministic and reproducible.

Using LangSmith’s Built-in Evaluators

LangSmith provides pre-built LLM evaluators:
langsmith_evaluators.py
Available built-in evaluators:
  • qa - Correctness for Q&A tasks
  • helpfulness - Overall helpfulness
  • relevance - Relevance to the question
  • coherence - Internal consistency
  • harmfulness - Detects harmful content

Chain-of-Thought Evaluation

Improve evaluation quality by asking the LLM to reason step-by-step:
cot_evaluation.py
Core need: [your analysis] Completeness: [your analysis] Accuracy: [your analysis] Tone: [your analysis] Verdict: PASS or FAIL
Chain-of-thought prompting often improves evaluation quality, especially for complex criteria, but increases latency and cost.

Multi-Model Consensus

Use multiple models for higher confidence:
consensus_eval.py

Best Practices

Use Specific Rubrics

Provide concrete criteria rather than vague instructions:
rubric_example.py

Calibrate Your Evaluator

Test on examples with known outcomes:
calibration.py

Set Temperature to 0

For reproducible evaluations:
reproducible_llm.py

Use Structured Outputs

Request JSON for easier parsing:
structured_output.py

Cost and Performance

Choose the Right Model

  • GPT-5-nano: Fast and cheap, good for simple judgments
  • GPT-4o-mini: Balanced cost/performance for most use cases
  • GPT-4o: Best quality for complex evaluations
  • Claude 3.5 Haiku: Fast and cost-effective alternative
  • Claude 3.5 Sonnet: High quality, good for detailed analysis

Batch Evaluations

Use async for better throughput:
async_llm_eval.py

Limitations

Bias and Inconsistency

LLMs can be inconsistent:
  • Position bias: May prefer first or last option
  • Length bias: May favor longer responses
  • Self-preference: May favor responses similar to their own style
Mitigate by:
  • Using multiple evaluators
  • Providing clear rubrics
  • Calibrating on known examples

Cost at Scale

LLM evaluation is expensive:
  • Use code-based evaluators where possible
  • Sample a subset for LLM evaluation
  • Use cheaper models for simpler criteria

Latency

LLM evaluation is slower:
  • Use async/parallel evaluation
  • Cache evaluation results
  • Consider running evaluations offline

Next Steps

Pairwise Eval

Compare agent versions side-by-side

Code-based Eval

Fast deterministic checks