LLM을 평가자로 사용하는 패턴
강력한 LLM에 정확성, 유용성, 어조와 같은 기준으로 출력을 평가하거나 비교하도록 프롬프트하는 방법과 이 방식이 사람의 평가보다 더 쉽게 확장되는 이유를 이해합니다.
LLM을 평가자로 사용하는 패턴은(는) CoddyKit의 무료 AI Engineering Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 AI Engineering Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Automated Evaluation Is Necessary
Manually evaluating LLM outputs is slow and expensive. A team of human raters can evaluate a few hundred outputs per week, but a production system generates thousands per day. LLM-as-judge uses a powerful language model to evaluate the quality of other LLM outputs at scale, enabling automated regression testing and continuous quality monitoring without hiring an army of annotators.
The Core Idea: One LLM Evaluates Another
In LLM-as-judge, you send a system prompt defining evaluation criteria, the original question, the model's answer, and optionally a reference answer to a judge model (typically GPT-4o or Claude). The judge returns a numerical score, a label, or a ranking. This works because state-of-the-art models have sufficient understanding of quality concepts like correctness, helpfulness, and coherence.
JUDGE_SYSTEM_PROMPT = '''
You are an expert evaluator of AI-generated responses.
Given a question and an AI-generated answer, score the answer on:
- Correctness (0-5): Is the information factually accurate?
- Completeness (0-5): Does it fully address the question?
- Clarity (0-5): Is it easy to understand?
Return a JSON object with scores and a brief rationale.
'''Writing a Judge Prompt
A good judge prompt specifies explicit rubrics with score definitions rather than vague terms like 'good' or 'bad'. Define what a score of 5 vs 3 vs 1 means concretely for each criterion. Provide the question, the answer being judged, and optionally a reference answer. Ask for reasoning before the score (chain-of-thought) to reduce arbitrary scoring.
def build_judge_prompt(question: str, answer: str, reference: str = None) -> str:
ref_section = f'Reference answer:\n{reference}\n\n' if reference else ''
return f'''
Question: {question}
{ref_section}Answer to evaluate:
{answer}
Score this answer on correctness (1-5) where:
5 = Completely accurate, no factual errors
3 = Mostly accurate with minor errors
1 = Contains significant factual errors
First explain your reasoning, then provide the score as JSON:
{{"correctness": <1-5>, "rationale": "..."}}
'''Calling the Judge Model
Call the judge model with your evaluation prompt. Parse the JSON response to extract scores. Always use structured outputs or JSON mode to ensure the judge returns parseable data. Using the same model as both the system under test and the judge can introduce bias — prefer a different model or at least a different configuration for the judge.
from pydantic import BaseModel
import instructor
from openai import OpenAI
class JudgeScore(BaseModel):
correctness: int
completeness: int
clarity: int
rationale: str
judge_client = instructor.from_openai(OpenAI())
def judge(question: str, answer: str) -> JudgeScore:
return judge_client.chat.completions.create(
model='gpt-4o', # Use stronger judge than the model being tested
response_model=JudgeScore,
messages=[
{'role': 'system', 'content': JUDGE_SYSTEM_PROMPT},
{'role': 'user', 'content': build_judge_prompt(question, answer)}
]
)Reference-Free vs Reference-Based Judging
There are two modes of LLM judging. Reference-free judging asks the judge to evaluate quality without a ground truth answer — useful when ground truth does not exist, like in open-ended chat. Reference-based judging provides a gold-standard answer and asks whether the model's answer matches it — better for factual question answering where a correct answer is known. Use reference-based when you have a labeled test set.
# Reference-free: good for open-ended generation
judge_result = judge(question='What is machine learning?', answer=model_answer)
# Reference-based: better for factual QA
judge_result = judge(
question='What year was Python created?',
answer=model_answer,
reference='Python was created by Guido van Rossum and released in 1991.'
)Controlling Judge Bias
LLM judges have known biases: they prefer longer answers (verbosity bias), answers that sound confident, and answers that match the style of their training data. Mitigate verbosity bias by explicitly penalizing unnecessary length in your rubric. Reduce position bias in pairwise comparison by randomizing which answer appears first and averaging the scores from both orderings.
# Anti-verbosity note in rubric:
ANTI_VERBOSITY_CLAUSE = '''
Note: A concise, accurate answer should score higher than a long,
rambling answer that happens to contain the correct information.
Do not reward length for its own sake.
'''
# Position-debiasing for pairwise comparison:
async def debiased_pairwise(q, a, b):
score_ab = await compare(q, answer_a=a, answer_b=b)
score_ba = await compare(q, answer_a=b, answer_b=a)
# A wins if it wins in both orderings
a_wins = (score_ab == 'A' and score_ba == 'B')
return 'A' if a_wins else 'B' if (score_ab == 'B' and score_ba == 'A') else 'tie'Batching Evaluations for Speed
Run judge evaluations in parallel to evaluate large test sets quickly. With asyncio and a semaphore, you can evaluate hundreds of outputs per minute. Keep a separate rate limit budget for judge calls (they use tokens too) and consider using a smaller but still capable judge model like GPT-4o-mini for criteria that do not require deep reasoning, saving GPT-4o for the most critical criteria.
import asyncio
async def batch_judge(qa_pairs: list, concurrency: int = 20) -> list:
sem = asyncio.Semaphore(concurrency)
async def judge_one(item):
async with sem:
return await async_judge(item['question'], item['answer'])
return await asyncio.gather(
*[judge_one(item) for item in qa_pairs],
return_exceptions=True
)Aggregating Judge Scores
After evaluating a test set, aggregate scores into summary statistics: mean, median, and the percentage of responses scoring above a quality threshold (such as correctness ≥ 4). Compare these aggregates between model versions or prompt variations. A drop of more than 5% in the above-threshold rate should trigger a review before deploying the new version.
import statistics
def summarize_judge_results(scores: list) -> dict:
correctness = [s.correctness for s in scores if isinstance(s, JudgeScore)]
return {
'n': len(correctness),
'mean_correctness': round(statistics.mean(correctness), 2),
'median_correctness': statistics.median(correctness),
'pct_above_4': round(100 * sum(1 for s in correctness if s >= 4) / len(correctness), 1)
}Comparing LLM Versions with Judge
Use LLM-as-judge to compare two versions of your system — for example, before and after a prompt change. Run both versions on the same set of test questions, judge all outputs, and compute the win rate: the percentage of cases where version B outscores version A. A win rate above 55% on 100+ samples is typically statistically significant enough to justify shipping the new version.
async def ab_compare(test_questions: list, version_a, version_b) -> dict:
a_wins = b_wins = ties = 0
for q in test_questions:
answer_a = await version_a.answer(q)
answer_b = await version_b.answer(q)
winner = await debiased_pairwise(q, answer_a, answer_b)
if winner == 'A': a_wins += 1
elif winner == 'B': b_wins += 1
else: ties += 1
total = len(test_questions)
return {'a_win_rate': a_wins/total, 'b_win_rate': b_wins/total, 'tie_rate': ties/total}Calibrating Judge Scores Against Humans
Validate your judge by comparing its scores to human judgments on a calibration set of 50-200 examples. Compute Pearson correlation between judge and human scores. A correlation above 0.7 indicates the judge is reliable. If correlation is low, audit the judge prompt to see where it disagrees with humans and add examples or clarifications to the rubric. Never deploy a judge without calibration.
from scipy.stats import pearsonr
def calibrate_judge(human_scores: list, judge_scores: list) -> dict:
corr, p_value = pearsonr(human_scores, judge_scores)
mean_abs_error = sum(abs(h - j) for h, j in zip(human_scores, judge_scores)) / len(human_scores)
return {
'pearson_r': round(corr, 3),
'p_value': round(p_value, 4),
'mean_abs_error': round(mean_abs_error, 2),
'reliable': corr >= 0.7
}When Not to Use LLM-as-Judge
LLM-as-judge is not appropriate for every evaluation scenario. Avoid it when: the criterion requires domain expertise the judge model lacks (medical diagnosis accuracy, legal compliance), when you need legally defensible evaluation (human review is required), when evaluating a model stronger than the judge (the judge cannot reliably score output it could not produce), or when the evaluation budget is too tight for the additional API cost. In these cases, use human evaluation or deterministic metrics.
# Appropriate uses of LLM-as-judge:
# YES: General helpfulness, clarity, tone, factual accuracy (general knowledge)
# YES: Code correctness for common languages
# YES: Translation quality comparison
# YES: Content safety classification
#
# NOT appropriate:
# NO: Medical/legal/financial accuracy (needs domain expert)
# NO: Evaluating GPT-4o with GPT-4o (same capability ceiling)
# NO: Formal compliance audits (non-deterministic judge)
# NO: Streaming quality at individual token levelQuick Check
Test your understanding of the LLM-as-judge evaluation pattern.
Lesson Recap
In this lesson you learned: LLM-as-judge uses a strong model to evaluate quality at scale with explicit rubrics, reference-free and reference-based modes suit different evaluation scenarios, and calibration against human judgments validates that your judge is reliable before using it in production. Next up we implement pointwise and pairwise evaluation strategies.
자주 묻는 질문
“LLM을 평가자로 사용하는 패턴” 강의는 무료인가요?
네 — “LLM을 평가자로 사용하는 패턴” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 AI Engineering Academy 강의 전체를 잠금 해제할 수 있습니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“LLM을 평가자로 사용하는 패턴”에서 뭘 배우나요?
강력한 LLM에 정확성, 유용성, 어조와 같은 기준으로 출력을 평가하거나 비교하도록 프롬프트하는 방법과 이 방식이 사람의 평가보다 더 쉽게 확장되는 이유를 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 AI Engineering Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
AI Engineering Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 AI Engineering Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“LLM을 평가자로 사용하는 패턴” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 AI Engineering Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 AI Engineering Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- LLM을 평가자로 사용하는 패턴
- 점별 평가와 쌍별 평가
- 평가자 모델을 사람의 판단에 맞게 보정
- 지속적 평가 처리 과정 구축