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
qa- Correctness for Q&A taskshelpfulness- Overall helpfulnessrelevance- Relevance to the questioncoherence- Internal consistencyharmfulness- Detects harmful content
Chain-of-Thought Evaluation
Improve evaluation quality by asking the LLM to reason step-by-step:cot_evaluation.py
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
- 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