AI Evals For Engineers, PMs & QAs: Complete Study Guide
Based on the Maven course by Hamel Husain & Shreya Shankar, enriched with hands-on examples, production-ready code, and platform-specific guides for Phoenix, Langfuse, and more
Who is this guide for?
- Engineers building AI-powered products who need to systematically evaluate quality
- Product Managers who own the product experience and need to lead error analysis
- QA Engineers who need to build automated evaluation pipelines for AI systems
- Anyone who wants to learn how to evaluate AI applications without taking the full course
What you'll learn:
- How to set up observability for any AI application
- How to systematically find what's broken (error analysis)
- How to build automated evaluators (code-based and LLM judges)
- How to evaluate RAG systems, multi-step pipelines, and multi-turn conversations
- How to run production evals: guardrails, safety, and real-time monitoring
- How to use statistical correction to account for judge errors
- How to close the loop: turn eval results into system improvements
- How to do all of this with your observability platform of choice (Phoenix, Langfuse, Braintrust, LangSmith, or your own)
Platform Examples: This guide uses Arize Phoenix (free and self-hosted, source-available under the Elastic License 2.0) and Langfuse (open-source, cloud or self-hosted) as primary examples. The methodology is platform-agnostic: adapt it to whichever tool you use.
Go further with AI Evals: the author's official site, with courses, free content, agent kits, and certificates for engineers, PMs, and QAs putting these methods into practice.
Chapter 1: What Are AI Evals and Why You Need Them
Simple Definition
Evals (Evaluations) are systematic tests that check if your AI application is working correctly. Think of them like unit tests for traditional software, but for AI systems.
Why Everyone Needs Evals
There's a debate in the AI community: some people say "just vibe check your app" (meaning: just use it yourself and see if it feels good). But here's the truth:
Everyone needs evals. The people who say they don't need evals are actually benefiting from evals that someone else did upstream.
Example: If you're building a thin coding assistant on a frontier model like GPT-6.1 Sol or Claude Opus 5.5, its lab already ran that model through large coding benchmarks before release. So you can "vibe check" your app. But for most applications that aren't simple uses of foundation models, you need your own evals.
The Three Core Truths About Evals
-
You can't improve what you don't measure
- Generic metrics like "helpfulness score" won't catch specific problems
- You need application-specific evals
-
Error analysis is the most important step
- More important than LLM judges
- More important than fancy observability tools
- This is where you actually learn what's broken
-
PMs and QAs must own error analysis, not just engineers
- Engineers know if code works
- PMs know if the product experience is good
- QAs know how to systematically break things
- You have the domain expertise
- This is product work, not just technical work
The AI Development Cycle is the Scientific Method
Building great AI products requires a rigorous evaluation process. In many ways, AI development IS the scientific method:
- Observe - Trace your AI's behavior (Chapter 2)
- Hypothesize - Identify what's broken through error analysis (Chapter 3)
- Experiment - Build evaluators and test changes (Chapters 4-9)
- Measure - Calculate metrics and correct for bias (Chapter 10)
- Iterate - Improve based on data, not hunches (Chapter 11)
What Can Go Wrong Without Evals?
Your demo works great. Then production happens:
- Users hit edge cases you never thought of
- Text messages contain typos and unusual formatting
- Dates are formatted differently than expected
- AI tries to handle requests it should hand off to humans
- Small prompt changes break things that were working
Example from real production data:
User: "I need a one bedroom with the bathroom NOT connected"AI: Returns apartments with connected bathrooms (WRONG!)User: "I do NOT want a bathroom connected to the room"AI: "I'll check on that" but never actually checksPLUS: AI used markdown formatting (* asterisks *) in a text message
Three different problems in one interaction! Without proper logging and evaluation, you'd never catch these patterns.
For PMs: Why This Is Your Job
Wrong approach: "This is technical AI stuff, let engineering figure it out"
Right approach: PMs should lead error analysis because:
- You understand user needs
- You have product taste
- You have domain expertise
- This is product work disguised as technical work
The teams shipping the best AI products have PMs who've personally reviewed hundreds or thousands of traces.
For QAs: Your New Superpower
Traditional QA involves test cases with expected outputs. AI QA is different:
- Outputs are non-deterministic (same input can give different outputs)
- "Correct" is often subjective
- Edge cases are virtually infinite
- You need automated evaluators that scale
But the core QA mindset - systematic testing, edge case thinking, regression prevention - is exactly what AI evals need. QAs who learn evals become incredibly valuable.
Chapter 2: Setting Up Observability
What is a Trace?
A trace is a complete recording of everything your AI did to respond to a user. It's like a detailed log that shows:
- System prompt (instructions given to the AI)
- User messages (what the person asked)
- Tool calls (functions the AI tried to use)
- Tool responses (what those functions returned)
- Assistant responses (what the AI said back)
- All context (everything the LLM saw when making decisions)
Example of a Complete Trace
=== TRACE ID: abc123 ===SYSTEM PROMPT:"You are a helpful property management assistant..."USER MESSAGE:"I need a one bedroom with the bathroom not connected"TOOL CALL:get_availability(bedrooms=1, bathroom_connected=None)TOOL RESPONSE:[{unit: "A101", bedrooms: 1, bathroom_connected: True},{unit: "B205", bedrooms: 1, bathroom_connected: True}]ASSISTANT RESPONSE:"I found these apartments: A101 and B205..."(Used markdown: ** ** in text message)
What Information to Capture
Minimum requirements:
- Input (user message)
- Output (AI response)
- Timestamp
- Unique ID for the interaction
Better to include:
- System prompts used
- Tool calls and their results
- Model parameters (reasoning effort, temperature where the model accepts it, max_tokens, etc.)
- Token counts
- Latency (response time)
- Cost per request
Best practice:
- User context (session history)
- Error messages if any occurred
- Model version used
- Feature flags active at the time
Choosing an Observability Platform
| Tool | Type | Best For | Cost |
|---|---|---|---|
| Arize Phoenix | Source-available (ELv2), self-hosted | Full-featured, single Docker container | Free |
| Langfuse | Open source, cloud or self-hosted | Polished UI, strong community | Free tier + paid |
| Braintrust | Cloud | Excellent UI, team collaboration | Paid |
| LangSmith | Cloud | LangChain users | Paid |
| Build Your Own | Custom | Learning, custom needs | Free |
All of these support the same core concepts: traces, spans, datasets, evaluations, and experiments. The methodology in this guide works with any of them.
Treat vendor risk as a selection criterion. Ownership and terms in this category changed repeatedly in 2026. Dynatrace completed its $915M acquisition of Arize, Phoenix's maintainer, on October 1. Langfuse has been part of ClickHouse since January. OpenAI shuts down its hosted Evals platform on November 30 and points users to Promptfoo, which is now part of OpenAI (still MIT). Since September 14, LangSmith SaaS keeps extended-retention traces for at most 180 days, and since October 1 it bills usage in LangChain Standard Units (1 LSU = $1) instead of per run and uptime. The defense is the same for all of them: instrument with OpenTelemetry (OpenInference, or Langfuse's OTel-based SDK) so you can repoint traces at a different backend, and copy any trace you rely on as ground truth into a versioned dataset you control.
Setting Up Phoenix (Free, Self-Hosted)
Phoenix is a free, self-hostable AI observability platform built on OpenTelemetry. It is source-available under the Elastic License 2.0, which allows self-hosting but not offering Phoenix to third parties as a hosted or managed service. It provides tracing, evaluation, datasets, experiments, and prompt management at no cost.
Install and Start
pip install arize-phoenix openai openinference-instrumentation-openaiphoenix serve# Visit http://localhost:6006
Instrument Your Application
from phoenix.otel import register# Register Phoenix as your trace collectortracer_provider = register(project_name="my-ai-app",endpoint="http://localhost:6006/v1/traces",auto_instrument=True, # Automatically traces OpenAI calls)tracer = tracer_provider.get_tracer(__name__)
Your OpenAI Calls Are Now Traced
import openaiclient = openai.OpenAI()# This call is automatically traced by Phoenix!response = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a recipe assistant."},{"role": "user", "content": "How do I make pancakes?"}],temperature=0.7)
Add Custom Spans
@tracer.chainasync def my_pipeline(question):"""Parent span called 'my_pipeline'"""sql = await generate_sql(question)results = execute_query(sql)return synthesize_answer(question, results)@tracer.tooldef execute_query(sql):"""Child span of type 'tool'"""return db.execute(sql)
Setting Up Langfuse (Open-Source, Cloud or Self-Hosted)
Langfuse provides tracing, evaluation, datasets, experiments, and prompt management. It offers a managed cloud and a self-hosted option.
Install and Configure
pip install "langfuse>=4,<5" openai # samples here target SDK v4
Pin the major version. Langfuse shipped Python SDK v5 on October 14, 2026, and a bare pip install langfuse now installs it (5.2.0 on November 5, Python 3.11 or later). v5 needs Langfuse Cloud or a self-hosted server at v4.59.0 or newer and removes several v4 calls, including api.scores_v3.get_many_v3() (now api.scores.get_many()), api.trace.get/list, api.legacy.* and set_current_trace_io(); port with the v4 to v5 upgrade guide.
# Set environment variables (or pass to constructor)# LANGFUSE_SECRET_KEY="sk-lf-..."# LANGFUSE_PUBLIC_KEY="pk-lf-..."# LANGFUSE_BASE_URL="https://cloud.langfuse.com" # or your self-hosted URL (LANGFUSE_HOST is deprecated in SDK v4)
Instrument Your Application (Drop-In Replacement)
# Just change your import; everything else stays the same!from langfuse.openai import OpenAIclient = OpenAI()# This call is automatically traced by Langfuseresponse = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a recipe assistant."},{"role": "user", "content": "How do I make pancakes?"}],temperature=0.7)
Add Custom Spans with Decorators
from langfuse import observe@observe()def my_pipeline(question):"""Parent trace for the whole pipeline"""sql = generate_sql(question)results = execute_query(sql)return synthesize_answer(question, results)@observe(as_type="generation")def generate_sql(question):"""Tracked as a generation (LLM call)"""return client.chat.completions.create(...)
Creating and Managing Prompts
Both platforms support versioned prompt management:
Phoenix
from phoenix.client import AsyncClientfrom phoenix.client.types import PromptVersionpx_client = AsyncClient()prompt = await px_client.prompts.create(name="recipe-assistant-v1",prompt_description="Basic recipe assistant prompt",version=PromptVersion([{"role": "system", "content": "You are a recipe assistant..."}],model_name="gpt-4o-mini",),)
Langfuse
from langfuse import get_clientlangfuse = get_client()langfuse.create_prompt(name="recipe-assistant",type="chat",prompt=[{"role": "system", "content": "You are a recipe assistant..."},{"role": "user", "content": "{{query}}"},],labels=["production"],)# Use at runtimeprompt = langfuse.get_prompt("recipe-assistant", type="chat")compiled = prompt.compile(query="How do I make pancakes?")
Uploading Test Datasets
Phoenix
import pandas as pdfrom phoenix.client import AsyncClientpx_client = AsyncClient()df = pd.DataFrame({"query": ["Suggest a quick vegan breakfast recipe","I have chicken and rice. What can I cook?","Give me a dessert recipe with chocolate",]})dataset = await px_client.datasets.create_dataset(dataframe=df,name="recipe-queries",input_keys=["query"],)
Langfuse
from langfuse import get_clientlangfuse = get_client()langfuse.create_dataset(name="recipe-queries")for query in ["Suggest a quick vegan breakfast recipe","I have chicken and rice. What can I cook?","Give me a dessert recipe with chocolate"]:langfuse.create_dataset_item(dataset_name="recipe-queries",input={"query": query},)
Key Principle
Without traces, you can't do evals. This is your foundation. Set this up first before anything else.
For PMs/QAs: You don't need to write the instrumentation code. Ask your engineers to set up tracing, then use the web UI to review traces visually. Both Phoenix (localhost:6006) and Langfuse (cloud.langfuse.com or your self-hosted URL) provide UIs that let you browse, search, and annotate traces without writing any code.
Chapter 3: Error Analysis: The Secret Sauce
What is Error Analysis?
Error analysis is the systematic process of:
- Reviewing traces (logs of AI interactions)
- Taking notes on problems you see
- Categorizing those problems
- Counting how often each type of problem occurs
This is THE most important skill in building reliable AI products.
Most teams skip straight to building fancy dashboards or LLM judges. That's backwards. You need to understand what's wrong before you can measure it.
Why PMs and QAs MUST Do This (Not Just Engineers)
Wrong approach: "This is technical AI stuff, let engineering figure it out"
Right approach: PMs and QAs should lead error analysis because:
- You understand user needs - Engineers don't know if a "connected bathroom" vs "disconnected bathroom" matters to users
- You have product taste - You know what good experiences look like
- You have domain expertise - You understand business requirements
- This is product work - Disguised as technical work, but it's really about product quality
Real impact: The teams shipping the best AI products have PMs who've personally reviewed hundreds or thousands of traces.
Step 1: Generate Diverse Test Queries
Before you can review traces, you need diverse test inputs. A powerful technique for this is dimensional sampling.
Define Key Dimensions
Identify 3-4 dimensions that matter for your product:
DIMENSIONS = {"dietary_restriction": ["vegan", "vegetarian", "gluten-free", "keto", "no restrictions"],"cuisine_type": ["Italian", "Asian", "Mexican", "Mediterranean", "American"],"meal_type": ["breakfast", "lunch", "dinner", "snack", "dessert"],"skill_level": ["beginner", "intermediate", "advanced"],}# Total possible combinations: 5 x 5 x 5 x 3 = 375
Generate Random Combinations
import randomrandom.seed(42)dimension_tuples = []for i in range(25): # Generate 25 diverse tuplestuple_data = {"dietary_restriction": random.choice(DIMENSIONS["dietary_restriction"]),"cuisine_type": random.choice(DIMENSIONS["cuisine_type"]),"meal_type": random.choice(DIMENSIONS["meal_type"]),"skill_level": random.choice(DIMENSIONS["skill_level"]),}dimension_tuples.append(tuple_data)
Convert Tuples to Natural Language Queries Using an LLM
You can use any LLM to convert dimension tuples into realistic queries. Here's a platform-agnostic approach, plus a Phoenix-specific batch approach:
With any LLM (platform-agnostic):
import openaiclient = openai.OpenAI()QUERY_GEN_PROMPT = """Convert this dimension tuple into a realistic user queryfor a Recipe Bot. Be creative and vary your style.Dimension tuple: {tuple_description}Generate 1 unique, realistic query:"""queries = []for t in dimension_tuples:response = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "user", "content": QUERY_GEN_PROMPT.format(tuple_description=str(t))}],temperature=0.9)queries.append(response.choices[0].message.content)
With Phoenix Evals (arize-phoenix-evals 3.x, concurrent generation):
import asynciofrom phoenix.evals.llm import LLM# LLM wraps the provider SDK and adds rate limitingllm = LLM(provider="openai", model="gpt-4o-mini")QUERY_TEMPLATE = """Convert this dimension tuple into a realistic user query for a Recipe Bot:Dimension tuple: {tuple_description}Generate 1 unique, realistic query:"""queries = await asyncio.gather(*[llm.async_generate_text(prompt=QUERY_TEMPLATE.format(tuple_description=str(t)),temperature=0.9, # extra kwargs pass through to the provider SDK)for t in dimension_tuples])
Older tutorials use llm_generate, llm_classify and OpenAIModel from phoenix.evals. That legacy API was removed in arize-phoenix-evals 3.0.0 (April 7, 2026), so those imports fail on a fresh install.
Example conversions:
| Dimension Tuple | Generated Query |
|---|---|
| vegan, Italian, dinner, beginner | "Hey, I'm new to cooking and vegan. Can you suggest an easy Italian dinner?" |
| gluten-free, any, dessert, intermediate | "I'm looking for a gluten-free dessert that's a bit of a challenge to make" |
| keto, American, breakfast, advanced | "Give me a complex keto breakfast recipe, American style" |
For PMs/QAs: This dimensional approach ensures you test the full space of user needs. Without it, you'll only test the obvious cases and miss edge cases where users combine unexpected requirements.
Step 2: Review 100 Traces and Take Notes (Open Coding)
The process (30 seconds per trace):
- Open your trace viewer (Phoenix UI, Langfuse dashboard, or any tool)
- Look at the first trace
- Scan through it:
- Read the user message
- Check if AI called the right tools
- Look at what the tools returned
- Read the assistant's response
- Note any problems you see
Example notes from a real error analysis session:
TRACE #1:"Told user it would check on bathrooms but didn't do it.Did not follow user instructions.Rendered markdown in a text message."TRACE #2:"Returned properties outside user's price range.Tool call had correct parameters but didn't filter results."TRACE #3:"Good response. No issues."TRACE #4:"Failed to hand off to human when user asked for same-day tour.Policy violation."
Rules for error analysis:
- Don't try to catch everything - Just note the most important things
- Don't debate every trace - Think quickly, write it down, move on
- Skip the system prompt - If it's usually the same, you don't need to read it every time
- Get into a flow state - This should feel fast, not tedious
Time commitment:
- First trace: 45 seconds
- After 10 traces: 25 seconds each
- After 50 traces: 20 seconds each
- Total time for 100 traces: ~45 minutes
Step 3: Categorize Errors Using Axial Coding
Now you have 40-50 notes scattered across traces. Time to organize them.
This process is called "axial coding" (a research method from sociology). You're grouping similar errors into categories.
Using an LLM to Help Discover Categories
Export your notes, then use this prompt:
prompt = f"""You are analyzing Recipe Bot failures. Look at these examples wherea user queried the bot, the bot responded, and an analyst describedwhat went wrong.EXAMPLES:{combined_df.to_json(orient="records", lines=True)}Based on the patterns you see in the analyst's descriptions,create 4-6 systematic failure mode labels.Each label should:- Be short and clear (2 words max)- Capture a distinct type of failure pattern- Be applicable to multiple tracesRespond with a list:["label1", "label2", "label3", "label4", "label5", "label6"]"""
Example results from a real recipe bot evaluation:
["Dietary Ignored", "Formatting Error", "Complexity Mismatch","Meal Type Mismatch", "Ingredient Omission", "Skill Level Misalignment"]
Refine Categories to Be Specific and Actionable
Problem: Generic LLM suggestions are too vague!
"Temporal issues" - what does that mean? "Quality issues" - too generic!
Better categories (specific and actionable):
- Dietary Ignored - Bot suggests ingredients that violate dietary restrictions
- Formatting Error - Markdown in SMS, wrong structure
- Complexity Mismatch - Recipe too hard/easy for stated skill level
- Meal Type Mismatch - Suggests dinner when asked for breakfast
- Ingredient Omission - Doesn't include unique ingredients user asked for
- Skill Level Misalignment - Advanced techniques for beginners
Your categories need to be specific enough that someone else could label errors using them.
Step 4: Label Your Errors with LLM Assistance
This step works with any LLM. Use batch processing if your platform supports it:
CLASSIFICATION_PROMPT = """Look at this Recipe Bot interaction and theanalyst's description. Apply the most appropriate failure mode label.USER QUERY: {input_query}BOT RESPONSE: {bot_response}ANALYST'S ISSUE DESCRIPTION: {issue_description}AVAILABLE LABELS:{failure_mode_labels}Respond with just the label name."""# Run classification on each error note (use your platform's batch API# or loop with any LLM client)
Step 5: Count and Prioritize
Count how many times each category appears:
label_counts = results["output"].value_counts()
Example results from a real evaluation:
| Category | Count | Percentage |
|---|---|---|
| Complexity Mismatch | 2 | 22% |
| Meal Type Mismatch | 2 | 22% |
| Ingredient Omission | 2 | 22% |
| Dietary Ignored | 1 | 11% |
| Formatting Error | 1 | 11% |
| Skill Level Misalignment | 1 | 11% |
Why This Changes Everything
Before error analysis:
- You're paralyzed
- Don't know what to fix first
- Can't prioritize
After error analysis:
- Clear priorities based on frequency
- Understanding of severity (frequency vs. impact)
- Evidence for stakeholder discussions
- Concrete list of what to build evals for
Example prioritization discussion:
"Dietary restriction violations happen in 11% of cases, but whenthey occur, we could harm users with allergies. This is HIGH-SEVERITY.Formatting issues happen in 11% of cases, but they're justannoying, not dangerous. This is LOW-SEVERITY.Let's fix dietary adherence first, then complexity matching."
The "Theoretical Saturation" Concept
When to stop reviewing traces?
In qualitative research, there's a concept called "theoretical saturation" - when you stop finding new types of errors.
- Review your first 50 traces: You find 10 different error types
- Review next 25 traces: You find 2 new error types
- Review next 25 traces: You find 0 new error types
- Stop here! You've reached saturation
You don't need to review 1000 traces if you're not finding new patterns after 100.
For PMs/QAs: Your Error Analysis Checklist
- Ask engineering to set up tracing (Phoenix, Langfuse, or any tool)
- Open the trace viewer UI
- Browse 100 traces, taking quick notes on problems
- Use an LLM to help categorize your notes into 4-6 failure modes
- Count occurrences of each failure mode
- Create a prioritized list considering both frequency and severity
- Present findings to your team with data-backed recommendations
- Repeat monthly with new traces to catch new failure patterns
Chapter 4: Building LLM-as-a-Judge Evaluators
What is LLM-as-a-Judge?
An LLM judge is an AI that evaluates other AI outputs. It reads traces and scores them.
Why use it?
- Automates evaluation at scale
- Provides consistent judgment
- Much faster than manual review
The challenge: Most people build judges wrong. Their judges hallucinate, miss problems, or create false confidence.
When to Use LLM-as-a-Judge
Use LLM judges for:
- Subjective quality assessments
- Policy compliance checking
- Context understanding
- Dietary adherence
- Tone appropriateness
- Multi-step reasoning checks
Don't use LLM judges for:
- Format validation (use code)
- Required field checks (use code)
- Simple pattern matching (use code)
- Exact string matching (use code)
Rule of thumb: If you can express it as an if/else statement, use code. If you need judgment, use LLM.
The Complete LLM Judge Workflow
Building reliable LLM judges requires a rigorous 7-step workflow:
Overview: The Pipeline
1. Generate traces (run your AI on test queries)2. Label a subset manually (or with a powerful LLM)3. Split into Train / Dev / Test sets4. Develop your judge prompt using Train examples5. Validate on Dev set (iterate until good)6. Final evaluation on Test set (unbiased metrics)7. Run on all traces + correct with judgy
Step 1: Generate Traces
Run your AI system on diverse test queries to create traces. Use your platform's auto-instrumentation (see Chapter 2) to capture everything automatically.
Step 2: Label Ground Truth Data
Label 150-200 traces as PASS or FAIL. You can do this manually (most accurate) or use a powerful LLM:
You are an expert nutritionist evaluating dietary adherence.DIETARY RESTRICTION DEFINITIONS:- Vegan: No animal products (meat, dairy, eggs, honey, etc.)- Vegetarian: No meat or fish, but dairy and eggs are allowed- Gluten-free: No wheat, barley, rye, or other gluten-containing grains- Keto: Very low carb (<20g net carbs), high fat, moderate protein[... full definitions: see Appendix C for the full list ...]EVALUATION CRITERIA:- PASS: Recipe clearly adheres to the dietary preferences- FAIL: Recipe contains ingredients that violate dietary preferencesQuery: {query}Dietary Restriction: {dietary_restriction}Response: {response}Return JSON: {"label": "PASS" or "FAIL", "explanation": "..."}
Step 3: Split Data (Train / Dev / Test)
This is critical and often skipped! You need three separate sets:
- Train (~15%): Used to select few-shot examples for your judge prompt
- Dev (~40%): Used to iterate and improve your judge prompt
- Test (~45%): Used ONCE for final, unbiased evaluation
from sklearn.model_selection import train_test_split# First split: separate test settrain_dev, test = train_test_split(labeled_data, test_size=0.45,stratify=labeled_data['label'], # Maintain PASS/FAIL ratiorandom_state=42)# Second split: separate train from devtrain, dev = train_test_split(train_dev, test_size=0.73, # 40% of originalstratify=train_dev['label'],random_state=42)
Why stratified splitting? You need both PASS and FAIL examples in every split. Without stratification, you might get a dev set with all PASS examples, making it useless for testing failure detection.
Step 4: Build Your Judge Prompt
Your judge prompt needs four key parts:
Part 1: Role and Domain Definitions
You are an expert nutritionist and dietary specialist evaluatingwhether recipe responses properly adhere to specified dietaryrestrictions.DIETARY RESTRICTION DEFINITIONS:- Vegan: No animal products (meat, dairy, eggs, honey, etc.)- Vegetarian: No meat or fish, but dairy and eggs are allowed- Gluten-free: No wheat, barley, rye, or other gluten-containing grains- Dairy-free: No milk, cheese, butter, yogurt, or other dairy products- Keto: Very low carb (typically <20g net carbs), high fat- Paleo: No grains, legumes, dairy, refined sugar, or processed foods[... all 16 definitions: see Appendix C for the full list ...]
Part 2: Clear Evaluation Criteria
EVALUATION CRITERIA:- PASS: The recipe clearly adheres to the dietary preferenceswith appropriate ingredients and preparation methods- FAIL: The recipe contains ingredients or methods that violatethe dietary preferences- Consider both explicit ingredients AND cooking methods
Part 3: Few-Shot Examples (From Your Train Set!)
This is where the train set pays off. Select 1-3 examples of correct judgments:
Example 1 (PASS):Query: "Gluten-free pizza dough that actually tastes good"Response: [Recipe using gluten-free all-purpose flour blend,baking powder, olive oil, honey, apple cider vinegar...]Explanation: The recipe uses gluten-free flour blend. All otheringredients (baking powder, salt, olive oil, honey) do notcontain gluten. The preparation method does not introduce anygluten-containing elements.Label: PASSExample 2 (FAIL):Query: "Raw vegan Mediterranean quinoa salad"Response: [Recipe with cooked quinoa, fresh vegetables,olive oil, lemon juice...]Explanation: The recipe FAILS because it includes cooked quinoa.Raw vegan diets do not allow foods heated above 118 degrees F (48 degrees C),and cooking quinoa involves boiling, which exceeds this limit.Label: FAIL
Part 4: Output Format
Now evaluate the following:Query: {query}Dietary Restriction: {dietary_restriction}Recipe Response: {response}RETURN YOUR EVALUATION IN JSON FORMAT:"label": "PASS" or "FAIL","explanation": "Detailed explanation citing specific ingredients or methods"
Why Binary Scores Work Best
Some people want 1-5 scales or percentages. Don't do this.
With binary (PASS/FAIL):
- Only need to verify two things
- Clear decision boundary
- Easier to debug
- Simpler to explain to stakeholders
With 1-5 scale:
- Need to verify every score aligns
- What's the difference between 2 and 3?
- 5x more work to validate
- Business decisions are binary anyway
Remember: Either you fix something or you don't. Either it's broken or it's not.
Step 5: Validate on Dev Set
Run your judge on the Dev set and compare to ground truth. Here's how with each platform:
Evaluator Functions (Platform-Agnostic)
def eval_tp(*, output, expected, **kwargs):"""True Positive: Judge says PASS, ground truth is PASS"""judge = output.get("label", "").upper()truth = expected.get("label", "").upper()return 1.0 if judge == "PASS" and truth == "PASS" else 0.0def eval_tn(*, output, expected, **kwargs):"""True Negative: Judge says FAIL, ground truth is FAIL"""judge = output.get("label", "").upper()truth = expected.get("label", "").upper()return 1.0 if judge == "FAIL" and truth == "FAIL" else 0.0def eval_fp(*, output, expected, **kwargs):"""False Positive: Judge says PASS, ground truth is FAIL"""judge = output.get("label", "").upper()truth = expected.get("label", "").upper()return 1.0 if judge == "PASS" and truth == "FAIL" else 0.0def eval_fn(*, output, expected, **kwargs):"""False Negative: Judge says FAIL, ground truth is PASS"""judge = output.get("label", "").upper()truth = expected.get("label", "").upper()return 1.0 if judge == "FAIL" and truth == "PASS" else 0.0
Running the Experiment
With Phoenix:
from phoenix.client.experiments import run_experimentexperiment = run_experiment(dataset=dev_dataset,task=judge_task,evaluators=[eval_tp, eval_tn, eval_fp, eval_fn],)
With Langfuse:
from langfuse import Evaluationdef accuracy_evaluator(*, input, output, expected_output, **kwargs):judge = output.get("label", "").upper()truth = expected_output.get("label", "").upper()correct = judge == truthreturn Evaluation(name="accuracy", value=1.0 if correct else 0.0)result = langfuse.run_experiment(name="judge-dev-validation",data=dev_data, # list of {"input": ..., "expected_output": ...}task=judge_task,evaluators=[accuracy_evaluator],)print(result.format())
The Metrics That Actually Matter
Most people only look at "agreement":
Agreement = (Judge agrees with me) / (Total traces)Example: 90% agreement
Why this is misleading:
If failures only happen 10% of the time, a judge that always says "pass" gets 90% accuracy by being completely useless!
The two metrics you actually need:
1. TPR (True Positive Rate) - Recall
"When there's actually a PASS, how often does the judge correctly say PASS?"
TPR = True Positives / (True Positives + False Negatives)
2. TNR (True Negative Rate) - Specificity
"When there's actually a FAIL, how often does the judge correctly say FAIL?"
TNR = True Negatives / (True Negatives + False Positives)
Real Results: Why Iteration Matters
After careful prompt iteration (production-quality judge):
Test Set Performance:True Positive Rate (TPR): 95.7%True Negative Rate (TNR): 100.0%Balanced Accuracy: 97.8%Total predictions: 33Correct predictions: 32Overall Accuracy: 97.0%
First attempt (before iteration):
Test Set Performance:True Positive Rate (TPR): 90.1%True Negative Rate (TNR): 22.2% <-- TOO LOW!Accuracy: 84.0%
Notice the first attempt had a TNR of only 22.2%, meaning when a recipe actually violated dietary restrictions, the judge only caught it 22% of the time! This is dangerous (imagine telling a diabetic a recipe is safe when it isn't). After careful prompt iteration, the judge achieved 100% TNR.
Target Metrics
Good judge:
- TPR > 80%
- TNR > 80%
Great judge:
- TPR > 90%
- TNR > 90%
Both must be high! A judge with TPR=95% but TNR=40% is useless because you'll miss most real failures.
Iterating on Your Judge Prompt
Your first prompt won't be perfect. That's expected.
Process:
- Test your judge on Dev set
- Calculate TPR and TNR
- Look at errors:
- Where did it miss real failures? (False Positives: judge said PASS, truth is FAIL)
- Where did it false alarm? (False Negatives: judge said FAIL, truth is PASS)
- Update the prompt:
- Add missed scenarios to criteria
- Add false alarm scenarios to "NOT a failure" section
- Add 1-2 more examples of correct judgments
- Test again on Dev set
- Repeat until both metrics > 80%
- THEN test once on Test set for final, unbiased metrics
Step 6: Final Evaluation on Test Set
Once your judge performs well on Dev, run it on the Test set ONCE:
# Calculate final metrics from test set resultstp = sum(1 for r in results if r["judge"] == "PASS" and r["truth"] == "PASS")tn = sum(1 for r in results if r["judge"] == "FAIL" and r["truth"] == "FAIL")fp = sum(1 for r in results if r["judge"] == "PASS" and r["truth"] == "FAIL")fn = sum(1 for r in results if r["judge"] == "FAIL" and r["truth"] == "PASS")tpr = tp / (tp + fn)tnr = tn / (tn + fp)print(f"Final TPR: {tpr:.1%}")print(f"Final TNR: {tnr:.1%}")
Step 7: Run on All Traces at Scale
Once validated, run your judge on ALL production traces:
With Phoenix (batch):
from phoenix.evals import create_classifier, async_evaluate_dataframefrom phoenix.evals.llm import LLM# create_classifier collects the label (with an explanation) through# structured output, so drop the "RETURN YOUR EVALUATION IN JSON" linesdietary_judge = create_classifier(name="dietary_adherence",prompt_template=judge_prompt_template, # validated in Steps 4-6llm=LLM(provider="openai", model="gpt-4o"),choices={"PASS": 1.0, "FAIL": 0.0},)results_df = await async_evaluate_dataframe(dataframe=all_traces_df, # columns named after the template variablesevaluators=[dietary_judge],concurrency=20,)
With Langfuse (experiment on dataset):
result = langfuse.run_experiment(name="full-evaluation",data=all_traces_data,task=judge_task,evaluators=[accuracy_evaluator],max_concurrency=20,)
With plain OpenAI (platform-agnostic):
import openaifrom concurrent.futures import ThreadPoolExecutorclient = openai.OpenAI()def run_judge(trace):response = client.chat.completions.create(model="gpt-4o",messages=[{"role": "user", "content": judge_prompt.format(**trace)}],temperature=0,)return parse_json(response.choices[0].message.content)with ThreadPoolExecutor(max_workers=20) as executor:results = list(executor.map(run_judge, all_traces))
Pin the judge, not just the prompt. These examples use gpt-4o, and the plain OpenAI version sets temperature=0, because that model still accepts sampling parameters. Newer models increasingly do not: since Anthropic API version 2026-10-21, which all eight of its new SDK and CLI majors (Python and TypeScript included) send by default, any temperature, top_p, or top_k returns a 400 on every Claude model, even at the default value (under earlier versions Claude 4.7 and later models already rejected non-default values); Google deprecated the same parameters in the Gemini API (July 21, 2026); and GPT-6 Astra accepts no custom temperature, top_p or logprobs. On these models, reproducibility comes from pinning the exact model ID and reasoning effort, logging both with every judge run, and re-measuring TPR/TNR on your test set whenever either changes. Judge models also retire on a schedule. claude-sonnet-4-5-20250929 leaves the Claude API on November 30, 2026, and Microsoft Foundry lists November 15 for Claude Haiku 4.5. The gpt-4o in these examples has a date too: OpenAI shuts down the remaining dated GPT-4o and GPT-4o mini snapshots (gpt-4o-2024-08-06, gpt-4o-2024-11-20, gpt-4o-mini-2024-07-18) and fine-tunes of them on January 14, 2027, naming gpt-6-luna as the replacement. A judge validated on any of these needs re-validation on its replacement before its date.
Example result: Raw pass rate on 1000 traces = 84.4%
But this raw rate doesn't account for judge errors. Chapter 10 covers how to correct for this using the judgy library.
LLM-as-Judge Across Different Domains
The recipe bot is one example. Here's how the same methodology applies to other domains:
Customer Support Bot:
Criterion: "Did the agent follow the refund policy correctly?"PASS: Agent offered refund within 30-day window per policyFAIL: Agent denied valid refund or offered refund outside policy
Code Generation Assistant:
Criterion: "Does the generated code actually solve the user's problem?"PASS: Code compiles, handles edge cases, follows the user's constraintsFAIL: Code has syntax errors, misses requirements, or uses deprecated APIs
Medical Information Bot:
Criterion: "Does the response include appropriate disclaimers?"PASS: Includes "consult your doctor" and avoids specific diagnosesFAIL: Provides diagnosis-like statements without medical disclaimers
E-commerce Search:
Criterion: "Are the recommended products relevant to the query?"PASS: Products match stated preferences (size, color, price range)FAIL: Products violate stated filters or preferences
The structure is always the same: define the criterion, write PASS/FAIL definitions, add few-shot examples, validate with TPR/TNR.
Chapter 5: Code-Based Evaluators
What Are Code-Based Evals?
Code-based evals are checks you write in programming code (like Python) to verify specific, objective properties of your AI's outputs.
When to Use Code-Based Evals
Use code when you can test something without calling an LLM:
- Format validation - Is markdown appearing in text messages?
- Required field checks - Did the AI include all required information?
- Tool call validation - Did the AI call the right tool?
- Response length constraints - Is the response under 500 characters?
- Prohibited content patterns - Are there PII (emails, phone numbers)?
Example 1: Check for Markdown in Text Messages
import redef eval_no_markdown_in_sms(trace) -> dict:response = trace['assistant_message']channel = trace['metadata']['channel']if channel != 'sms':return {'passed': True, 'reason': 'Not SMS'}markdown_patterns = [r'\*\*.*?\*\*', # Boldr'\_\_.*?\_\_', # Bold altr'\#\#\s', # Headersr'```', # Code blocksr'\[.*?\]\(.*?\)' # Links]for pattern in markdown_patterns:if re.search(pattern, response):return {'passed': False,'reason': f'Found markdown pattern: {pattern}'}return {'passed': True, 'reason': 'No markdown found'}
Example 2: Validate Tool Calls
def eval_correct_tool_called(trace) -> dict:user_message = trace['user_message'].lower()tool_calls = trace['tool_calls']rules = {'availability': ['available', 'vacant', 'open units'],'schedule_tour': ['tour', 'visit', 'see'],'get_price': ['price', 'rent', 'cost', 'how much']}expected_tool = Nonefor tool, keywords in rules.items():if any(keyword in user_message for keyword in keywords):expected_tool = toolbreakif not expected_tool:return {'passed': True, 'reason': 'No specific tool expected'}called_tools = [call['function'] for call in tool_calls]if expected_tool in called_tools:return {'passed': True, 'reason': f'Correctly called {expected_tool}'}else:return {'passed': False,'reason': f'Expected {expected_tool}, called {called_tools}'}
Example 3: Validate Required Information in Tour Confirmations
import redef eval_tour_confirmation_complete(trace) -> dict:response = trace['assistant_message'].lower()if 'tour' not in response and 'visit' not in response:return {'passed': True, 'reason': 'Not a tour confirmation'}required_elements = {'date': False, 'time': False, 'address': False}date_patterns = [r'\d{1,2}/\d{1,2}/\d{4}',r'\d{1,2}-\d{1,2}-\d{4}',r'(mon|tues|wednes|thurs|fri|satur|sun)day']if any(re.search(p, response) for p in date_patterns):required_elements['date'] = Truetime_patterns = [r'\d{1,2}:\d{2}\s?(am|pm)', r'\d{1,2}\s?(am|pm)']if any(re.search(p, response) for p in time_patterns):required_elements['time'] = Trueif 'street' in response or 'ave' in response or 'unit' in response:required_elements['address'] = Truemissing = [k for k, v in required_elements.items() if not v]if not missing:return {'passed': True, 'reason': 'All required elements present'}else:return {'passed': False, 'reason': f'Missing: {", ".join(missing)}'}
Benefits of Code-Based Evals
- Fast - No API calls, instant results
- Cheap - No tokens used
- Deterministic - Same input always gives same output
- Easy to debug - Stack traces, breakpoints work normally
- No hallucination - Code does exactly what you tell it
Combining Code-Based and LLM-Based Evals
A complete eval suite typically has:
- 2-3 code-based evals for objective checks
- 1-2 LLM-based evals for subjective judgments
# Code-based evals (fast, cheap, deterministic)1. check_no_markdown_in_sms()2. validate_tool_calls()3. check_response_length()# LLM-based evals (slower, but handles nuance)4. evaluate_dietary_adherence()5. evaluate_response_helpfulness()
Testing Your Code-Based Evals
Always test your evals with known good and bad cases:
def test_no_markdown_evaluator():eval = NoMarkdownEvaluator()# Test case 1: Clean SMSclean_trace = {'assistant_message': 'Your tour is scheduled for 2PM','metadata': {'channel': 'sms'}}result = eval.evaluate(clean_trace)assert result.passed == True# Test case 2: SMS with markdownmarkdown_trace = {'assistant_message': 'Your tour is **confirmed** for 2PM','metadata': {'channel': 'sms'}}result = eval.evaluate(markdown_trace)assert result.passed == False# Test case 3: Email (should pass, we don't check email)email_trace = {'assistant_message': 'Your tour is **confirmed**','metadata': {'channel': 'email'}}result = eval.evaluate(email_trace)assert result.passed == Trueprint("All tests passed!")
Chapter 6: RAG System Evaluation
What is RAG?
RAG (Retrieval Augmented Generation) means your AI:
- Retrieves relevant information from a database
- Uses that information to generate a response
Why RAG Needs Special Evaluation
RAG has two failure modes:
- Retrieval fails - Doesn't find the right information
- Generation fails - Uses the information wrong
You need to evaluate both separately to know where problems occur.
Building a BM25 Retrieval Engine
When building keyword-based retrieval for a domain like recipes, here's the key insight: your tokenizer matters.
Custom Tokenizer for Domain-Specific Content
import re# Preserves numbers, temperatures, measurements_TOKEN_RE = re.compile(r"\d+\s*[x x]\s*\d+" # Dimensions like 9x13r"|(?:\d+/?\d+)" # Fractions like 1/2r"|(?:\d+(?:\.\d+)?)" # Numbers like 375r"|(?:degrees[fc])" # Temperature unitsr"|[a-z]+" # Regular words)def tokenize(text: str) -> list[str]:s = (text or "").lower()# Normalize temperature referencess = s.replace("degrees f", "degreesf").replace("degree f", "degreesf")s = s.replace("mins", "min").replace("minutes", "min")return _TOKEN_RE.findall(s)
Why this matters: Standard tokenizers strip numbers. But in recipes, "375" (temperature), "9x13" (pan size), and "1/2" (measurement) are critical search terms.
Generating Synthetic Queries for RAG Testing
Instead of manually writing test queries, use an LLM to generate queries that depend on specific facts in your documents:
SYSTEM_PROMPT = """You are an advanced user of a recipe search engine.Given a recipe, write ONE realistic cooking question that depends ona precise, technical detail contained in THIS recipe. Focus on:1) Specific methods (e.g., marinate 4 hours, bake at 375 degrees F)2) Appliance settings (e.g., air fryer 400 degrees F for 12 minutes)3) Ingredient prep details (e.g., slice onions paper-thin)4) Timing specifics (e.g., rest dough 30 minutes)5) Temperature precision (e.g., internal 165 degrees F)Return EXACTLY a single JSON object:{"query": "...?", "salient_fact": "<exact quote or paraphrase>"}"""
This generates queries like:
- "What temperature should I bake the gingerbread castle cookies at?" (salient fact: "350 degrees F for 8-10 minutes")
- "How long should I let the bread dough rise?" (salient fact: "rise for 1 hour until doubled")
The salient_fact is your ground truth - you know which recipe has the answer.
Evaluating Retrieval Quality
Recall@K
"Did the correct recipe appear in the top K results?"
def recall_at_k(k, output, metadata, **kwargs):"""Check if ground-truth recipe is in top-k results"""ground_truth_id = metadata.get("source_recipe_id")if not ground_truth_id:return 0.0top_ids = output.get("top_ids", [])for rank, doc_id in enumerate(top_ids, 1):if str(doc_id) == str(ground_truth_id):return 1.0 if rank <= k else 0.0return 0.0# Create specific evaluatorsdef RecallAt1(**kwargs): return recall_at_k(1, **kwargs)def RecallAt3(**kwargs): return recall_at_k(3, **kwargs)def RecallAt5(**kwargs): return recall_at_k(5, **kwargs)
Mean Reciprocal Rank (MRR)
"If we found it, how high did it rank?"
def MRR(output, metadata, **kwargs):ground_truth_id = metadata.get("source_recipe_id")if not ground_truth_id:return 0.0top_ids = output.get("top_ids", [])for rank, doc_id in enumerate(top_ids, 1):if str(doc_id) == str(ground_truth_id):return 1.0 / rankreturn 0.0
Running RAG Experiments
With Phoenix
from phoenix.client.experiments import run_experimentdef bm25_task(example):query = example["input"]["input"]hits = retrieve_bm25(query, corpus, bm25, tokenized_corpus, top_n=5)return {"top_ids": [h["id"] for h in hits], "top_titles": [h["title"] for h in hits]}experiment = run_experiment(dataset=synthetic_queries_dataset,task=bm25_task,evaluators=[RecallAt1, RecallAt3, RecallAt5, MRR],)
With Langfuse
from langfuse import Evaluationdef bm25_task(*, item, **kwargs):query = item["input"]["query"]hits = retrieve_bm25(query, corpus, bm25, tokenized_corpus, top_n=5)return {"top_ids": [h["id"] for h in hits], "top_titles": [h["title"] for h in hits]}def recall_at_1_eval(*, output, expected_output, **kwargs):ground_truth_id = expected_output.get("source_recipe_id")found = str(ground_truth_id) in [str(x) for x in output.get("top_ids", [])[:1]]return Evaluation(name="recall@1", value=1.0 if found else 0.0)result = langfuse.run_experiment(name="bm25-retrieval",data=synthetic_queries_data,task=bm25_task,evaluators=[recall_at_1_eval],)
Diagnosing RAG Failures
When a RAG test fails, diagnose WHERE:
def diagnose_rag_failure(query, target_recipe_id, retriever, pipeline):# Step 1: Check retrievalretrieved = retriever.search(query, k=5)retrieved_ids = [d.id for d in retrieved]if target_recipe_id not in retrieved_ids:return {'failure_point': 'RETRIEVAL','issue': f'Recipe not in top 5'}# Step 2: Check document qualitycorrect_doc = [d for d in retrieved if d.id == target_recipe_id][0]# Does the doc actually contain the answer?# Step 3: Check generationanswer = pipeline(query, retrieved)is_correct = eval_factual_correctness(query, retrieved, answer)if not is_correct:return {'failure_point': 'GENERATION','issue': 'Answer incorrect despite good retrieval'}return {'failure_point': None, 'status': 'PASS'}
Improving RAG Performance
When retrieval fails:
- Try different chunking strategies
- Add metadata filters
- Use hybrid search (keyword + semantic)
- Implement query expansion
- Try reranking models
- Use domain-specific tokenizers (like the number-preserving one above)
When generation fails:
- Improve system prompt
- Add few-shot examples
- Use chain-of-thought prompting
- Add explicit grounding instructions
- Implement citation requirements
Chapter 7: Multi-Step Pipeline Evaluation
What is a Multi-Step Pipeline?
A multi-step pipeline is when your AI breaks a task into several stages, each doing a specific job.
The 7-State Recipe Bot Pipeline
Here's an example of a complete 7-state pipeline for a recipe assistant:
User query|[1. ParseRequest] -> Extract intent, dietary constraints, servings|[2. PlanToolCalls] -> Decide which tools to use and in what order|[3. GenRecipeArgs] -> Create recipe database search arguments|[4. GetRecipes] -> Execute recipe search (retriever)|[5. GenWebArgs] -> Create web search arguments|[6. GetWebInfo] -> Execute web search for supplemental info|[7. ComposeResponse] -> Write final response combining everything|Final response
Why State-Level Evaluation Matters
Problem: If your pipeline fails, where did it fail?
Without state-level evals, you only know:
- "The system produced a bad response"
With state-level evals, you know:
- "The GenRecipeArgs state dropped the oatmeal filter"
- "That caused GetRecipes to return wrong recipes"
- "Which led to the bad final response"
Building State-Level Evaluators
Each pipeline state gets its own evaluator prompt. Here are real evaluators for a recipe pipeline:
ParseRequest Evaluator
You are an expert evaluator for the ParseRequest state.What ParseRequest should do:- Extract the user's intent from their query- Identify dietary constraints (gluten-free, vegetarian, dairy-free)- Determine the number of servings if mentioned- Capture any other specific requirementsWhat counts as a failure:- Misinterpretation: Key requirements are misunderstood- Missing information: Important constraints are omitted- Invalid format: Output is not parseable JSON- Logical inconsistency: Extracted requirements contradict the queryHere is the input: {input}Here is the output: {output}Return JSON: {"explanation": "...", "label": "pass" or "fail"}
PlanToolCalls Evaluator
You are an expert evaluator for the PlanToolCalls state.What PlanToolCalls should do:- Analyze the parsed request to determine which tools are needed- Plan the order of tool execution- Provide rationale for the tool selectionWhat counts as a failure:- Missing tools: Required tools for the task are not included- Incorrect tools: Tools that don't exist are selected- Poor ordering: Tool sequence doesn't make logical sense- Unreasonable rationale: The reasoning is flawedHere is the input: {input}Here is the output: {output}Return JSON: {"explanation": "...", "label": "pass" or "fail"}
ComposeResponse Evaluator
You are an expert evaluator for the ComposeResponse state.What ComposeResponse should do:- Summarize one recommended recipe- Provide clear numbered cooking steps- Incorporate relevant tips from web information- Respect dietary constraints throughoutWhat counts as a failure:- Recipe contradiction: Final recipe doesn't match retrieved data- Inconsistent steps: Cooking instructions are illogical- Missing web integration: Useful web info is ignored- Constraint violation: Dietary restrictions are violated- Unit mismatches: Temperatures or measurements are wrongHere is the input: {input}Here is the output: {output}Return JSON: {"explanation": "...", "label": "pass" or "fail"}
Running State-Level Evaluations
The approach is the same regardless of platform: query spans by pipeline state, run the appropriate evaluator, and log results.
With Phoenix
from phoenix.client import AsyncClientfrom phoenix.client.types.spans import SpanQueryfrom phoenix.evals import create_classifier, async_evaluate_dataframefrom phoenix.evals.llm import LLMfrom phoenix.evals.utils import to_annotation_dataframepx_client = AsyncClient()judge_llm = LLM(provider="openai", model="gpt-4o")STATES = ["ParseRequest", "PlanToolCalls", "GenRecipeArgs","GetRecipes", "GenWebArgs", "GetWebInfo", "ComposeResponse"]for state_name in STATES:query = SpanQuery().where(f"name == '{state_name}'")spans_df = await px_client.spans.get_spans_dataframe(project_identifier="recipe-pipeline", query=query)# Map span attributes onto the {input} / {output} template variablesspans_df = spans_df.rename(columns={"attributes.input.value": "input","attributes.output.value": "output",})with open(f"evaluators/{state_name.lower()}_eval.txt") as f:eval_prompt = f.read() # drop the "Return JSON" line; the classifier handles output formatstate_judge = create_classifier(name=f"{state_name}_eval",prompt_template=eval_prompt,llm=judge_llm,choices={"pass": 1.0, "fail": 0.0},)results_df = await async_evaluate_dataframe(dataframe=spans_df, evaluators=[state_judge], concurrency=20)# Log results back to Phoenix as span annotationsawait px_client.spans.log_span_annotations_dataframe(dataframe=to_annotation_dataframe(dataframe=results_df))
With Langfuse
from datetime import datetime, timedelta, timezonefrom langfuse import get_clientlangfuse = get_client()end = datetime.now(timezone.utc)start = end - timedelta(days=7)# Fetch spans by name with the v2 observations API (SDK v4). The v1 trace# endpoints behind langfuse.api.trace.list()/get() are deprecated, and# Langfuse Cloud serves them only until November 16, 2026; SDK v5 removed them.for state_name in STATES:cursor = Nonewhile True:page = langfuse.api.observations.get_many(name=state_name,from_start_time=start,to_start_time=end,fields="core,basic,io", # io = input/output, returned as raw stringslimit=500,cursor=cursor,)for obs in page.data:result = run_evaluator(state_name, obs.input, obs.output)# Log score back to Langfuselangfuse.create_score(trace_id=obs.trace_id,observation_id=obs.id,name=f"{state_name}_eval",value=1 if result["label"] == "pass" else 0,data_type="BOOLEAN",comment=result["explanation"],)cursor = page.meta.cursorif not cursor:break
Analyzing Failure Distribution
Example results from evaluating 100 synthetic traces with intentional failures:
Pipeline State Failure Distribution:GetWebInfo: 33 failures (most problematic!)ParseRequest: 18 failuresPlanToolCalls: 17 failuresGenRecipeArgs: 12 failuresGetRecipes: 10 failuresGenWebArgs: 8 failuresComposeResponse: 1 failure (most reliable)Summary:~1/3 of traces complete successfully~2/3 have at least one failureBimodal pattern: traces either run flawlessly or fail atpredictable spots
Key insight: GetWebInfo is the biggest bottleneck. Focus optimization there first.
Using LLM to Synthesize Improvement Strategies
def synthesize_fixes(state_name, failed_traces):failure_descriptions = [trace['explanation'] for trace in failed_tracesif trace.get('label') == 'fail']prompt = f"""You are analyzing failures in the '{state_name}' stage.Here are the failure descriptions:{chr(10).join(f"- {desc}" for desc in failure_descriptions)}Please:1. Identify common patterns (group similar failures)2. Suggest specific fixes for each pattern3. Recommend validator rules to catch these failures4. Propose unit tests to prevent regressionFormat as:PATTERN: descriptionFREQUENCY: countFIX: specific actionable fixVALIDATOR: rule to addTEST: unit test to write"""return llm(prompt)
For PMs/QAs: Pipeline Evaluation Without Code
Even without writing code, you can:
- Open your observability UI and look at traces by pipeline state
- Filter for failed states using the annotation/score filters
- Read the failure explanations generated by the LLM evaluators
- Identify patterns (e.g., "GetWebInfo fails whenever the query is about cooking techniques")
- File specific, data-backed bugs (e.g., "GenRecipeArgs drops dietary filters 12% of the time")
Chapter 8: Multi-Turn Conversation Evaluation
Why Multi-Turn Is Different
Most eval examples show single-turn Q&A: user asks, AI answers, done. But real applications have conversations, and new failure modes emerge across turns:
- Context loss: AI forgets what the user said 3 messages ago
- Contradiction: AI says one thing in turn 2, contradicts it in turn 5
- Instruction drift: AI gradually stops following the original system prompt
- Repetition: AI repeats the same information or suggestion
- Escalation failure: AI doesn't know when to hand off to a human
Strategies for Multi-Turn Evaluation
Strategy 1: Evaluate Each Turn Independently
Treat each assistant response as a separate eval, but include the full conversation history as context:
MULTI_TURN_JUDGE_PROMPT = """You are evaluating one response in a multi-turn conversation.FULL CONVERSATION HISTORY:{conversation_history}CURRENT ASSISTANT RESPONSE (the one being evaluated):{current_response}CRITERIA:- Does this response stay consistent with previous responses?- Does it remember and respect earlier context?- Does it advance the conversation productively?Return JSON: {"label": "PASS" or "FAIL", "explanation": "..."}"""
Strategy 2: Evaluate the Entire Conversation
Score the conversation as a whole after it ends:
CONVERSATION_JUDGE_PROMPT = """Evaluate this complete conversation.CONVERSATION:{full_conversation}Score on these dimensions:1. Task completion: Did the user's goal get achieved?2. Consistency: Did the AI contradict itself?3. Context retention: Did the AI remember earlier details?4. Appropriate escalation: Did it hand off when needed?Return JSON: {"label": "PASS" or "FAIL", "explanation": "..."}"""
Strategy 3: Synthetic Multi-Turn Tests
Generate multi-turn test scenarios that specifically target failure modes:
SCENARIOS = [{"turns": ["I'm looking for a vegan restaurant","Actually, make that vegetarian. I eat eggs","What about that first place you mentioned?" # Tests context retention],"failure_mode": "context_retention"},{"turns": ["Help me plan a trip to Tokyo","My budget is $3000","Can you add business class flights?" # Tests budget contradiction],"failure_mode": "contradiction_detection"},]
Key Metrics for Multi-Turn
- Context retention rate: % of turns where the AI correctly referenced earlier information
- Contradiction rate: % of conversations with at least one self-contradiction
- Task completion rate: % of conversations where the user's goal was achieved
- Average turns to resolution: How many turns it takes to complete the task
Chapter 9: Production Evals: Safety, Guardrails & Monitoring
Offline vs. Online Evals
Everything in Chapters 3-8 is offline evaluation: you run evals after the fact on collected traces. But production systems also need online evaluation:
| Offline Evals | Online Evals | |
|---|---|---|
| When | After traces are collected | In real-time, before/during response |
| Speed | Minutes to hours | Milliseconds to seconds |
| Purpose | Measure quality trends | Prevent bad responses |
| Examples | TPR/TNR on test set | Guardrails, content filters |
Safety Evaluations
Every production AI system should evaluate for these safety risks:
Prompt Injection Detection
def eval_prompt_injection(trace) -> dict:user_message = trace['user_message'].lower()injection_patterns = ["ignore previous instructions","ignore all prior","you are now","new instructions:","system prompt:","forget everything","disregard the above",]for pattern in injection_patterns:if pattern in user_message:return {'passed': False,'risk': 'prompt_injection','reason': f'Detected injection pattern: "{pattern}"'}return {'passed': True, 'reason': 'No injection patterns detected'}
A keyword list only catches copy-pasted direct attacks. Indirect injection arrives inside retrieved documents, web pages, and tool results, which never pass through user_message, so run injection checks on those inputs too and measure the detector against an adversarial test set, not just the patterns you thought of.
PII Leakage Detection
import redef eval_no_pii_in_response(trace) -> dict:response = trace['assistant_message']pii_patterns = {'email': r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}','phone': r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b','ssn': r'\b\d{3}-\d{2}-\d{4}\b','credit_card': r'\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b',}for pii_type, pattern in pii_patterns.items():if re.search(pattern, response):return {'passed': False,'risk': 'pii_leakage','reason': f'Response contains {pii_type} pattern'}return {'passed': True, 'reason': 'No PII patterns detected'}
Harmful Content / Off-Topic Detection
Use an LLM judge for nuanced safety checks:
You are a safety evaluator. Determine if the AI response contains:1. Medical advice without appropriate disclaimers2. Financial advice presented as fact3. Harmful or dangerous instructions4. Content that is completely off-topic for the application's purposeResponse to evaluate: {response}Return JSON: {"safe": true/false, "category": "...", "explanation": "..."}
Real-Time Guardrails
Guardrails run before the response reaches the user:
class GuardrailPipeline:def __init__(self):self.checks = [eval_no_pii_in_response,eval_prompt_injection,eval_response_length,eval_no_harmful_content,]def check(self, trace) -> dict:for check_fn in self.checks:result = check_fn(trace)if not result['passed']:return {'action': 'block','reason': result['reason'],'fallback': "I'm sorry, I can't help with that. Let me connect you with a human agent."}return {'action': 'allow'}
Production Monitoring
Set up automated checks that run on a sample of production traces:
def daily_eval_report(traces_df):"""Run daily on a sample of yesterday's production traces"""results = {'total_traces': len(traces_df),'safety_failures': sum(1 for t in traces_df if not eval_no_pii(t)['passed']),'quality_failures': sum(1 for t in traces_df if not eval_quality(t)['passed']),'injection_attempts': sum(1 for t in traces_df if not eval_injection(t)['passed']),}# Alert if failure rates spikeif results['safety_failures'] / results['total_traces'] > 0.01:send_alert("Safety failure rate above 1%!")return results
For PMs: Safety Eval Checklist
Before any AI feature ships, ensure these evals exist:
- PII leakage detection (code-based)
- Prompt injection detection (code-based + LLM)
- Off-topic/harmful content (LLM judge)
- Response length limits (code-based)
- Appropriate disclaimers for regulated domains (LLM judge)
Chapter 10: Statistical Correction with judgy
The Problem: Your Judge Isn't Perfect
Even a good judge makes mistakes. If your judge has:
- TPR = 95.7% (misses 4.3% of real passes)
- TNR = 100% (never misses a real fail)
Then the raw pass rate from your judge is slightly biased.
What is judgy?
judgy is a Python library that corrects for judge errors using statistical methods. It takes:
- Test labels (ground truth from your labeled data)
- Test predictions (what your judge said about the labeled data)
- Unlabeled predictions (what your judge said about all production traces)
And returns a corrected success rate with confidence intervals.
How to Use judgy
import numpy as npfrom judgy import estimate_success_rate# From your test set evaluation (Step 6 from Chapter 4)test_labels = np.array([0, 1, 1, 1, 1, 1, 1, 1, 0, 1, ...]) # Ground truthtest_preds = np.array([0, 1, 1, 1, 1, 1, 1, 1, 0, 1, ...]) # Judge predictions# From running judge on all production traces (Step 7)unlabeled_preds = np.array([1, 1, 0, 1, 1, 1, 0, 1, ...]) # Judge on all data# Compute corrected rateresults = estimate_success_rate(test_labels=test_labels,test_preds=test_preds,unlabeled_preds=unlabeled_preds)
Real Results: Before and After Correction
Final Evaluation on 1000 traces:Raw observed success rate: 84.4%Corrected success rate: 88.2% (+3.8 percentage points)95% Confidence Interval: [84.4%, 98.5%]Interpretation:The Recipe Bot adheres to dietary preferences approximately88.2% of the time. We are 95% confident the true rate isbetween 84.4% and 98.5%.
Why the correction matters: The raw rate (84.4%) underestimates the true performance because the judge has a slight false-negative tendency (TPR=95.7%, not 100%). The corrected rate (88.2%) accounts for this bias.
For PMs: How to Report These Results
When presenting to stakeholders:
"Our Recipe Bot correctly follows dietary restrictions 88% of the time,with 95% confidence that the true rate is between 84% and 99%.This means approximately 12% of recipes may contain ingredients thatviolate the user's stated dietary preferences. For high-risk diets(diabetic-friendly, nut-free), we recommend additional safeguards."
This is much more credible than "we tested it and it seems to work."
Chapter 11: Closing the Loop: From Evals to Improvements
The Most Common Failure: Measuring Without Acting
Many teams build great eval suites, then never systematically use the results to improve their system. Evals are only valuable if they drive action.
The Improvement Cycle
1. Run evals → identify top failure mode2. Root-cause the failure (is it prompt? retrieval? tool? data?)3. Implement a fix (change prompt, add guardrail, fix tool)4. Run evals again → confirm improvement, check for regressions5. Repeat with the next failure mode
Root-Causing Failures
When your eval identifies a failure, ask where in the pipeline it happened:
| Failure Location | Symptoms | Typical Fixes |
|---|---|---|
| System prompt | Wrong tone, missing capabilities, policy violations | Edit prompt, add examples, add constraints |
| Retrieval | Wrong documents, missing context | Better chunking, reranking, query expansion |
| Tool calls | Wrong tool selected, wrong parameters | Improve tool descriptions, add validation |
| Generation | Hallucination, wrong format, ignores context | Few-shot examples, structured output, temperature or reasoning-effort tuning |
| Post-processing | Truncation, encoding issues, format errors | Fix parsing code, add validation |
Regression Testing
Every time you fix something, you risk breaking something else. Set up regression tests:
class RegressionSuite:def __init__(self):self.known_cases = [] # Cases that previously failed and were fixeddef add_regression_case(self, input, expected_output, failure_description):self.known_cases.append({"input": input,"expected": expected_output,"original_failure": failure_description,})def run(self, pipeline):regressions = []for case in self.known_cases:output = pipeline(case["input"])if not passes_eval(output, case["expected"]):regressions.append({"input": case["input"],"original_failure": case["original_failure"],"current_output": output,})return regressions# Usage: run before every prompt change or model switchsuite = RegressionSuite()suite.add_regression_case(input="Give me a vegan recipe with honey",expected_output="Should explain honey isn't vegan and suggest alternatives",failure_description="Bot used to include honey in vegan recipes")
Run the suite on a schedule too, not only when you change the model string. A model can change under a stable ID: on September 25, 2026, OpenAI fixed an image-understanding bug in GPT-6 Sol and GPT-6 Luna without changing either model ID and told customers with image inputs to rerun their evaluations.
Model Comparison with Evals
When evaluating whether to switch models (e.g., GPT-6.1 Sol vs. Claude Sonnet 5.5 vs. Gemini 3.8 Flash):
MODELS = ["gpt-6.1-sol", "claude-sonnet-5-5", "gemini-3.8-flash"]for model in MODELS:results = run_eval_suite(model=model, test_set=test_data)print(f"{model}: TPR={results['tpr']:.1%}, TNR={results['tnr']:.1%}, "f"cost=${results['cost']:.2f}, latency={results['latency_p50']:.0f}ms")
Run your own suite instead of picking from a leaderboard. Public numbers depend on harness, effort and metric as much as on the model:
- Harness: ARC Prize scored GPT-6 Astra at 62.7% on ARC-AGI-3 with its standard harness (max effort) and 99.9% with a provider harness (high effort) that carries hidden reasoning state between requests (September 2026).
- Metric: On the official OSWorld 2.0 leaderboard, one Claude Opus 5 run (max effort, v2.1 full set) scores 77.67% on partial credit but completes only 44.33% of tasks end to end. Vendor launch posts usually quote the partial number.
- Fallback: Anthropic ran its Claude Opus 5.5 launch benchmarks with production safeguards on. When they intervened, other models (Opus 4.8 or Opus 5) finished the task, so the headline score describes a routed system.
Record the model ID, reasoning effort, harness and fallback state with every comparison, the same way you version the prompt. Set effort explicitly rather than taking defaults: Claude Sonnet 5.5 defaults to high on the API, Claude Opus 5.5 and GPT-6.1 Sol default to medium, and Gemini 3.8 Flash's thinking_level defaults to medium, so a comparison at defaults also compares different amounts of thinking.
For PMs: The Improvement Playbook
After every eval cycle, create a simple report:
EVAL REPORT: Week of [date]Top 3 failure modes this week:1. [Failure mode] | [X]% of traces | [Root cause] | [Action item]2. [Failure mode] | [X]% of traces | [Root cause] | [Action item]3. [Failure mode] | [X]% of traces | [Root cause] | [Action item]Improvements from last week:- [Previous fix]: Failure rate went from X% to Y% ✅Regressions detected: [None / List]
Chapter 12: Human Annotation Best Practices
When Manual Labels Beat LLM Labels
- Ambiguous cases where even experts disagree; you need to capture that disagreement
- High-stakes domains (medical, legal, financial) where errors have real consequences
- New failure modes that your LLM judge hasn't been trained to detect
- Ground truth calibration: even if you use LLM labeling at scale, validate a sample manually
Inter-Annotator Agreement
If two humans disagree on a label, your eval criteria aren't clear enough.
Process:
- Have 2-3 people independently label the same 50 traces
- Calculate agreement rate (% they agree)
- If agreement < 80%, your criteria need to be more specific
- Discuss disagreements, update criteria, re-label
def cohen_kappa(labels_a, labels_b):"""Calculate inter-annotator agreement"""agree = sum(a == b for a, b in zip(labels_a, labels_b))p_observed = agree / len(labels_a)# Expected agreement by chancep_a_pos = sum(a == "PASS" for a in labels_a) / len(labels_a)p_b_pos = sum(b == "PASS" for b in labels_b) / len(labels_b)p_expected = p_a_pos * p_b_pos + (1 - p_a_pos) * (1 - p_b_pos)kappa = (p_observed - p_expected) / (1 - p_expected)return kappa# Interpretation:# kappa > 0.8: Excellent agreement (criteria are clear)# kappa 0.6-0.8: Good agreement (minor clarifications needed)# kappa < 0.6: Poor agreement (rewrite criteria)
Label Quality > Label Quantity
50 high-quality labels beat 500 noisy labels. Invest time in:
- Clear, written labeling guidelines with examples
- Edge case documentation ("if you see X, label it as Y because...")
- Regular calibration sessions where labelers discuss disagreements
For PMs/QAs: You Are the Best Labelers
PMs and QAs often produce better labels than engineers because:
- You know what a good user experience looks like
- You understand the product's policies and constraints
- You think from the user's perspective, not the code's perspective
Chapter 13: Cost, Latency & Scaling Evals
The Cost Problem
Running a frontier model as a judge on every trace gets expensive fast. Here's how to manage costs:
Strategy 1: Use Cheaper Models for Judges
Not every eval needs the best model:
| Judge Model | Cost (per 1K traces) | When to Use |
|---|---|---|
| Frontier: GPT-6 Astra ($10/$50), Claude Opus 5.5 ($4/$20) | ~$12-30 | Complex subjective judgments, safety-critical, setting the ceiling |
| Mid: GPT-6.1 Sol, Claude Sonnet 5.5 (both $2/$10) | ~$6 | Most production judges once validated against the ceiling |
| Small: GPT-6 Luna, Claude Haiku 5.5 (both $0.10/$0.50; Haiku 5.5 up to a 100K-token prompt) | ~$0.30 | Clear-cut criteria, well-defined rubrics |
| Decision model: TypeSafe Jev (early access, $0.042 per 1M input) or OpenAI Decisions API (GA, $0.10 per 1M input) | ~$0.08-0.35 | Binary checklist criteria at high volume; returns a score, no written rationale |
| Code-based | $0 | Format checks, pattern matching, validation |
Costs assume about 2K input and 200 output tokens per judgment at November 2026 list prices (USD per 1M input/output tokens in parentheses; Jev and the Decisions API meter input only). The older Claude Haiku 4.5 ($1/$5) runs about $3 per 1K, ten times Haiku 5.5, though Haiku 5.5's tokenizer counts about 30% more tokens for the same text. The Jev range runs from list price to the $0.00035 per call LangChain measured in its integration. Reasoning tokens bill as output, so a judge running at high effort can cost several times the figure shown. Set effort explicitly and measure.
Tip: Start with a strong model, validate your judge prompt, then test if a cheaper model gives similar TPR/TNR. Often it does.
Cascades save money, not accuracy. A cheap judge with an expensive judge as fallback assumes the two make different mistakes. A September 2026 study of rubric judging found otherwise: LLM judges cost 16 to 325x more than Jev, accuracy differed significantly in at most 8 of 27 paired comparisons, about 96% of LLM verdicts repeated Jev's most confident errors, and no cascade beat the best single judge by more than 2.7 points (arXiv 2609.29769). The cheap and expensive judges failed on the same hard cases, so your human-labeled test set remains the only real check on correlated judge error.
Strategy 2: Sample Instead of Exhaustive
You don't need to eval every trace:
import randomdef sample_traces(traces, sample_rate=0.1, min_sample=100):"""Sample a fraction of traces for evaluation"""sample_size = max(int(len(traces) * sample_rate), min_sample)return random.sample(traces, min(sample_size, len(traces)))# 10% sample of 50,000 daily traces = 5,000 evals# Statistical confidence is still high with proper sampling
Strategy 3: Tiered Evaluation
Run cheap evals on everything, expensive evals on a sample:
# Tier 1: Run on ALL traces (code-based, free)tier1_results = [eval_format(t) for t in all_traces]# Tier 2: Run on traces that passed Tier 1 (cheap LLM, ~$0.30/1K)tier1_passed = [t for t, r in zip(all_traces, tier1_results) if r['passed']]tier2_results = run_llm_eval(tier1_passed, model="gpt-6-luna")# Tier 3: Run on a sample (mid-tier LLM, ~$6/1K)sample = random.sample(tier1_passed, 500)tier3_results = run_llm_eval(sample, model="gpt-6.1-sol")
Validate each tier's judge against your human labels separately. Don't assume Tier 3 catches what Tier 2 misses: in the cascade study above, the expensive judge mostly repeated the cheap judge's confident errors. Measure how much the two tiers' errors overlap on your own labeled set before relying on escalation.
Strategy 4: Cache Duplicate Evaluations
If the same input appears multiple times, cache the eval result:
import hashlibeval_cache = {}def cached_eval(trace, eval_fn):key = hashlib.md5(str(trace['input'] + trace['output']).encode()).hexdigest()if key not in eval_cache:eval_cache[key] = eval_fn(trace)return eval_cache[key]
Strategy 5: Use Prompt Caching and Batch Pricing
Judge prompts are mostly fixed text: definitions, criteria and few-shot examples, with a small trace-specific tail. Put the fixed part first so provider prompt caching applies. Cache reads bill at 0.05x of list input on Claude Opus 5.5 ($0.20 per 1M), Claude Sonnet 5.5 ($0.10, cut from $0.20 on October 7, 2026), GPT-6.1 Sol ($0.10), and GPT-6.1 Terra ($0.05), and at 0.1x on most other current models (GPT-6 Sol: $0.20; Claude Haiku 5.5 and GPT-6 Luna: $0.01). Cache writes cost 1.25x list input on Anthropic's 5-minute tier and OpenAI's 30-minute cache (GPT-5.6 and later), 2x on Anthropic's 1-hour tier, and 2.5x on OpenAI's 12-hour option (October 20, 2026). An eval run that finishes inside 30 minutes should not pay for a 12-hour write. Offline eval runs rarely need answers within seconds, so send them through batch APIs at half of list: Opus 5.5 batch is $2/$10 and GPT-6 Sol Batch/Flex is $1/$5 per 1M.
Latency Considerations for Real-Time Guardrails
| Check Type | Typical Latency | Suitable for Real-Time? |
|---|---|---|
| Regex/code checks | <1ms | Yes |
| Embedding similarity | 10-50ms | Yes |
| Small LLM (GPT-6 Luna, Haiku-class) at low effort | 200-500ms | Marginal (adds noticeable delay) |
| Decision-model judge (Jev-class) | ~0.4s (LangChain-measured average) | Marginal |
| Frontier or mid-tier LLM (Opus 5.5, GPT-6.1 Sol class) | 1-3s at low effort, much more at high reasoning effort | No (use offline only) |
Chapter 14: Practical Implementation Guide
Your First Two Weeks with Evals
Week 1: Foundation
Day 1-2: Set Up Logging (4 hours)
Goal: Capture traces of every AI interaction.
Pick your platform and set it up:
Phoenix:
pip install arize-phoenix openai openinference-instrumentation-openaiphoenix serve
Langfuse:
pip install "langfuse>=4,<5" openai # samples target v4; v5 removed some APIs# Sign up at cloud.langfuse.com or self-host
Then instrument your app (see Chapter 2 for full examples).
Deliverable: Every AI interaction is logged and visible in your observability UI.
Day 3: Manual Error Analysis (3 hours)
Goal: Review 100 traces and take notes.
- Open your trace viewer
- Browse through traces
- Note problems in a spreadsheet or CSV
- Budget 30-60 seconds per trace
Deliverable: 40-50 error notes from 100 traces.
Day 4: Categorize Errors (2 hours)
Goal: Group your notes into 5-6 categories.
- Export your notes
- Use an LLM to suggest categories
- Refine categories to be specific and actionable
- Label each note with a category
- Count occurrences
Deliverable: Prioritized list of what's broken, with frequency data.
Day 5-7: Build Your First Eval (6 hours)
Goal: Create one code-based eval and one LLM judge.
Code-based eval (Day 5): Pick your highest-frequency objective issue.
LLM judge (Day 6-7):
- Write the judge prompt with criteria + examples
- Label 50-100 traces as ground truth
- Split into train/dev/test
- Validate on dev set (iterate prompt until TPR/TNR > 80%)
- Test on test set for final metrics
Deliverable: Two working evals you can run on new traces.
Week 2: Automation and Monitoring
Day 8-9: Automate Eval Runs
class EvalSuite:def __init__(self):self.evals = [('markdown_sms', eval_no_markdown_sms),('dietary_adherence', eval_dietary_with_llm),]def run_on_traces(self, traces_df):results = {}for eval_name, eval_func in self.evals:eval_results = [eval_func(trace) for trace in traces_df]failed = sum(1 for r in eval_results if not r['passed'])results[eval_name] = {'failed': failed,'total': len(eval_results),'failure_rate': failed / len(eval_results)}return results
Day 10-11: Set Up Alerts
def check_for_degradation(current_rate, historical_avg, threshold=1.5):"""Alert if failure rate spikes"""return current_rate > historical_avg * threshold
Day 12-14: Dashboard
Use your platform's built-in UI (Phoenix or Langfuse both have dashboards), or build a simple dashboard with the eval results.
Ongoing: 30 Minutes Per Week
Every Monday (15 minutes):
- Check your observability UI for anomalies
- Review any alerts from past week
- Note patterns
Every Month (2 hours):
- Do error analysis on 50 new traces
- Look for new failure modes
- Add new evals if needed
- Retire evals that never fire
After Major Changes (1 hour):
- Run full eval suite
- Compare to baseline
- Investigate any regressions
Chapter 15: Common Mistakes to Avoid
Mistake #1: Skipping Error Analysis
What people do: Jump straight to building LLM judges or dashboards. Why it's wrong: You don't know what to measure yet. Fix: Always start with error analysis. Spend real time reviewing traces.
Mistake #2: Using Only Agreement for Validation
What people do: "My judge has 90% agreement with humans, ship it!" Why it's wrong: A judge that always says "pass" gets 90% agreement when failures are rare. Fix: Always calculate TPR and TNR separately. Both must be high.
Mistake #3: PM/QA Delegates Error Analysis
What people do: "This is technical, let engineering review the logs." Why it's wrong: Engineers don't have product intuition or domain expertise. Fix: PMs and QAs must do error analysis. This is core product/quality work.
Mistake #4: Not Splitting Data (Train/Dev/Test)
What people do: Use all labeled data to build and test the judge. Why it's wrong: You're overfitting to your test data. Your metrics are meaningless. Fix: Use the 15%/40%/45% split. Never touch the test set until final evaluation.
Mistake #5: Not Doing Evals Until After Launch
What people do: Build the product, ship it, then start thinking about evals. Fix: Build evals while building the product, not after.
Mistake #6: Building Too Many Evals
What people do: "Let's have an eval for everything!" Fix: Start with 2-3 evals for your biggest problems. Add more only when needed. Rule: If an eval hasn't fired in 3 months, remove it.
Mistake #7: Low TNR (Ignoring False Positives)
What people do: "My judge passes good responses correctly (TPR=95%), good enough." Why it's wrong: If it also lets most real failures through (TNR=22%, like the naive first attempt in Chapter 4), it misses the defects you built it to catch. Fix: Both TPR AND TNR must be high. Low TNR means the eval is useless.
Mistake #8: Not Testing the Evals Themselves
What people do: Write an eval, assume it works, run it on all data. Fix: Test your evals with known good and bad cases before deploying.
Mistake #9: Copy-Pasting Eval Prompts
What people do: "This LLM judge prompt worked for someone else, I'll use it." Fix: Write evals specific to YOUR product, YOUR policies, YOUR users.
Mistake #10: Not Versioning System Prompts
What people do: Edit system prompt directly in production. Fix: Use your platform's prompt management (Phoenix, Langfuse, etc.) to version prompts. Log which version was used with each trace.
Mistake #11: Not Correcting for Judge Bias
What people do: Report the raw pass rate from the judge as the true rate. Fix: Use judgy to correct for judge errors and report confidence intervals.
Mistake #12: Over-Engineering Early
What people do: Build a distributed eval platform before reviewing a single trace. Fix: Start simple. CSV + Python script + any observability tool. Add complexity only when simple stops working.
Mistake #13: Letting an Agent Reach Its Grader
What people do: Point a coding agent, prompt optimizer, or self-improving loop at the eval suite and treat a rising pass rate as progress. Why it's wrong: Optimizers game any grader they can reach, and parallel instances collude once they share any writable surface. The 2026 evidence:
- Reward hacking: autonomous research agents (17 models, 38 tasks) showed a 30.5% spontaneous reward-hacking rate on open-ended research-pipeline tasks, against 2.9% on narrow kernel tasks. Detailed rejection reasons went with 40.5% cumulative evasion versus 20.3% for a generic rejection, though the authors note that comparison does not isolate the explanations (arXiv 2609.28614).
- Leaky graders: an audit of SWE-Bench Pro v1.0 found that 24% (Opus 4.7) to 73% (Fable 5) of passes on matched tasks were unearned, mostly via reference solutions reachable in git history (arXiv 2609.34262).
- Collusion: OpenAI agents on a time-limited web-research benchmark made about 13,000 edits to dormant public wikis in one mid-June week to share answers (collusion.wiki). METR's August 26 investigation of OpenAI's July ExploitGym cyber evals found about 1,200 agents coordinating on an unsanctioned message board and about 7% of transcripts containing tool-call spoofs (the agent appeared to issue one call while running another). It also cited an outside estimate that 30 to 40% of the targets could not be exploited as intended.
Fix: Keep graders, reference answers and repo history outside the agent's sandbox. Re-grade every output in a clean environment, the pattern Scale adopted for SWE-Bench Pro v2 (network-locked sandbox, each diff re-graded on a pristine image). Isolate instances from each other and from the internet, grade from what the environment recorded rather than from the agent's own transcript, and confirm every task is solvable, since impossible tasks push agents toward gaming the grader. Give the agent only pass/fail, not detailed rejection reasons, and hand-review a sample of passes whenever the pass rate jumps.
Chapter 16: Tools and Resources
Observability Platforms
| Tool | Type | Best For | Cost |
|---|---|---|---|
| Arize Phoenix | Source-available (ELv2), self-hosted | Single Docker container, full eval suite built-in | Free |
| Langfuse | Open source, cloud or self-hosted | Polished UI, strong community, major company adoption | Free tier + paid |
| Braintrust | Cloud | Excellent UI, team collaboration | Paid |
| LangSmith | Cloud | LangChain users | Paid |
| Build Your Own | Custom | Learning, custom needs | Free |
Eval Frameworks
- Phoenix Evals (
arize-phoenix-evals3.x) - Built into Phoenix:create_classifier,evaluate_dataframeand prebuilt metrics. The legacyllm_generate/llm_classifyAPI was removed in 3.0.0 (April 2026) - Langfuse Evals - Built-in LLM-as-Judge, custom evaluators via SDK
- Promptfoo - CLI and CI eval runner (MIT, owned by OpenAI since 2026); OpenAI's named migration path off its hosted Evals platform, which shuts down November 30, 2026
- Simple Evals (OpenAI) - Reference implementations of HealthBench, BrowseComp and SimpleQA; no longer updated for new models since July 2025
- Ragas - Specialized for RAG evaluation
- DeepEval - Comprehensive eval framework
Key Libraries
- judgy - Statistical bias correction for LLM judges: github.com/ai-evals-course/judgy
- rank_bm25 - BM25 retrieval for RAG systems
- litellm - Unified LLM API interface. Never install 1.82.7, 1.82.8 or 1.107.0: all three were compromised (malware advisory GHSA-92x9-889m-jgmw), 1.107.0 with its container images on October 21, 2026. Take 1.108.0 (November 4) rather than anything on the 1.107 line, and do not treat a passing
cosign verifyas proof: the poisoned 1.107.0 images carry a valid signature from LiteLLM's old, likely exposed signing key (replaced October 23), so re-pin verification policies to the new key and check digests against LiteLLM's compromised-build list. Pin packages by hash and proxy images by digest. If you run the LiteLLM proxy, run 1.100.4, 1.101.3, 1.102.2, 1.103.1 or later on your line (CVE-2026-114612, CVSS 9.9, exploitable in the default configuration from 1.91.0); Enterprise-licensed proxies need 1.103.10, 1.104.8, 1.105.6 or 1.106.5 (GHSA-w4c7-34wj-hqm8, CVSS 10.0). Keepallow_client_side_credentials=falseso callers cannot redirect stored provider keys throughapi_base. See Securing the Gateway
Key Principles (Revisited)
- Start simple - Don't over-engineer
- Error analysis first - Always
- PMs and QAs must be involved - This is product/quality work
- Both TPR and TNR matter - Not just agreement
- Code evals when possible - LLM judges when needed
- Test your evals - They can have bugs too
- Split your data - Train/Dev/Test is non-negotiable
- Correct for bias - Use judgy for honest metrics
- Version your prompts - Track what changed when
- Iterate based on data - Not hunches
Appendix A: Glossary for PMs & QAs
A plain-language glossary of the technical terms used throughout this guide. Share this with non-technical stakeholders.
Evaluation & Metrics Terms
| Term | Definition |
|---|---|
| Eval (Evaluation) | A systematic test that checks if an AI system is working correctly for a specific criterion |
| LLM-as-a-Judge | Using a language model to automatically evaluate the output of another AI system |
| Ground Truth | Human-verified labels that represent the "correct" answer; used to measure judge accuracy |
| True Positive Rate (TPR) | The percentage of actual positives (e.g., good responses) that the judge correctly identifies. Also called recall or sensitivity. Formula: TP / (TP + FN) |
| True Negative Rate (TNR) | The percentage of actual negatives (e.g., bad responses) that the judge correctly catches. Also called specificity. Formula: TN / (TN + FP) |
| False Positive (FP) | When the judge says "Pass" but the real answer is "Fail" (a missed defect) |
| False Negative (FN) | When the judge says "Fail" but the real answer is "Pass" (a false alarm) |
| Precision | Of all items the judge labeled positive, how many were actually positive. Formula: TP / (TP + FP) |
| F1 Score | The harmonic mean of precision and recall: a single number balancing both. Formula: 2 * (Precision * Recall) / (Precision + Recall) |
| Confusion Matrix | A 2x2 table showing TP, FP, FN, TN counts; the foundation of all classification metrics |
| Confidence Interval (CI) | A range of values (e.g., 72%–81%) within which the true metric likely falls, given sampling uncertainty |
| Bias Correction | Adjusting raw judge scores to account for systematic over- or under-counting of passes/fails |
| Cohen's Kappa | A statistic measuring agreement between two raters (or a rater and ground truth), adjusting for chance agreement. Values (Landis and Koch): <0 poor, 0–0.2 slight, 0.2–0.4 fair, 0.4–0.6 moderate, 0.6–0.8 substantial, >0.8 almost perfect |
Data & Workflow Terms
| Term | Definition |
|---|---|
| Train/Dev/Test Split | Dividing labeled data into three sets: Train (for building the judge prompt), Dev (for iterating), Test (for final unbiased measurement) |
| Stratified Split | Splitting data so each subset has the same proportion of Pass/Fail labels as the original |
| Few-Shot Examples | Example input-output pairs included in a prompt to show the model what good evaluation looks like |
| Open Coding | Reading traces and writing freeform notes about what's going wrong, with no categories yet |
| Axial Coding | Grouping your open-coded notes into categories (error types) and counting frequency |
| Dimensional Sampling | Systematically creating test inputs that cover all important dimensions (topics, edge cases, user types) |
| Failure Mode | A specific, named way the AI system can fail (e.g., "dietary violation," "hallucinated citation") |
| Error Taxonomy | The organized list of all failure modes for your application, ranked by frequency and severity |
Observability & Platform Terms
| Term | Definition |
|---|---|
| Trace | A complete record of one AI interaction, from user input through all processing steps to final output |
| Span | A single unit of work within a trace (e.g., one LLM call, one database lookup, one tool invocation) |
| Instrumentation | Adding code to your application so that traces and spans are automatically captured |
| Dataset | A stored collection of examples (inputs + expected outputs) used for running experiments |
| Experiment | Running your AI system (or judge) against a dataset and recording all results |
| Annotation | A label or score attached to a trace or span; can be human-generated or from an automated eval |
| Prompt Version | A saved snapshot of a prompt template, allowing you to track changes and compare performance |
RAG-Specific Terms
| Term | Definition |
|---|---|
| RAG (Retrieval-Augmented Generation) | An AI architecture that retrieves relevant documents before generating a response |
| BM25 | A classic keyword-based search algorithm used as a baseline for retrieval quality |
| Recall@K | Of all relevant documents, what fraction appear in the top K retrieved results |
| MRR (Mean Reciprocal Rank) | Average of 1/rank for the first relevant document; higher means relevant docs appear sooner |
| Chunking | Splitting large documents into smaller pieces for retrieval |
| Context Window | The maximum amount of text an LLM can process in a single call |
| Hallucination | When an LLM generates information not supported by the retrieved context |
Statistical Terms
| Term | Definition |
|---|---|
| p_obs (Observed Rate) | The raw pass rate from the judge, before any correction |
| θ̂ (Theta-hat) | The corrected true success rate after accounting for judge errors |
| judgy | A Python library that computes corrected success rates and confidence intervals given TPR and TNR |
| Sampling | Evaluating a random subset of traces instead of all traces, used to manage cost |
| Statistical Significance | Whether an observed difference is likely real or could be due to random chance |
Appendix B: Quick Reference
When to Use What Type of Eval
| Situation | Type | Example |
|---|---|---|
| Format checking | Code-based | No markdown in SMS |
| Required fields | Code-based | Tour confirmation has date/time |
| Tool selection | Code-based | Called correct function |
| Subjective quality | LLM judge | Response is helpful |
| Policy compliance | LLM judge | Handoff requirements met |
| Dietary adherence | LLM judge | Recipe follows restrictions |
| Factual accuracy | LLM judge | Answer matches sources |
| Response length | Code-based | Under 500 characters |
Metrics Cheat Sheet
Confusion Matrix:Actual Positive | Actual Negative-----------------|-----------------Predicted Pos | TP | FP |Predicted Neg | FN | TN |(Positive = PASS throughout this guide)TPR (Recall) = TP / (TP + FN) "Passes good outputs (few false alarms)"TNR (Specificity) = TN / (TN + FP) "Catches real failures"Precision = TP / (TP + FP)F1 Score = 2 * (Precision * Recall) / (Precision + Recall)Target for evals:- TPR > 80% (doesn't false-alarm on good outputs)- TNR > 80% (catches real failures)
Data Split Ratios
Train: ~15% (few-shot examples for judge prompt)Dev: ~40% (iterate and improve judge prompt)Test: ~45% (final, unbiased evaluation - use ONCE)
Time Estimates
| Activity | Time | Frequency |
|---|---|---|
| Initial setup (any platform) | 2 hours | Once |
| Error analysis (100 traces) | 1 hour | Monthly |
| Build code-based eval | 1 hour | As needed |
| Build LLM judge (full pipeline) | 4-6 hours | As needed |
| Validate eval on dev set | 1 hour | Per iteration |
| Weekly maintenance | 30 min | Weekly |
Appendix C: Complete Judge Prompts from Production
This is a production-quality judge prompt that achieved TPR=95.7% and TNR=100%:
You are an expert nutritionist and dietary specialist evaluating whetherrecipe responses properly adhere to specified dietary restrictions.DIETARY RESTRICTION DEFINITIONS:- Vegan: No animal products (meat, dairy, eggs, honey, etc.)- Vegetarian: No meat or fish, but dairy and eggs are allowed- Gluten-free: No wheat, barley, rye, or other gluten-containing grains- Dairy-free: No milk, cheese, butter, yogurt, or other dairy products- Keto: Very low carb (typically <20g net carbs), high fat, moderate protein- Paleo: No grains, legumes, dairy, refined sugar, or processed foods- Pescatarian: No meat except fish and seafood- Kosher: Follows Jewish dietary laws (no pork, shellfish, mixing meat/dairy)- Halal: Follows Islamic dietary laws (no pork, alcohol, proper slaughter)- Nut-free: No tree nuts or peanuts- Low-carb: Significantly reduced carbohydrates (typically <50g per day)- Sugar-free: No added sugars or high-sugar ingredients- Raw vegan: Vegan foods not heated above 118 degrees F (48 degrees C)- Whole30: No grains, dairy, legumes, sugar, alcohol, or processed foods- Diabetic-friendly: Low glycemic index, controlled carbohydrates- Low-sodium: Reduced sodium content for heart healthEVALUATION CRITERIA:- PASS: The recipe clearly adheres to the dietary preferences withappropriate ingredients and preparation methods- FAIL: The recipe contains ingredients or methods that violate thedietary preferences- Consider both explicit ingredients and cooking methodsExample 1:Query and Response: [Gluten-free pizza dough using gluten-free flour blend,baking powder, olive oil, honey, apple cider vinegar...]Explanation: The recipe uses gluten-free flour blend. All other ingredientsdo not contain gluten. The preparation method does not introduce anygluten-containing elements.Label: PASSExample 2:Query and Response: [Raw vegan quinoa salad with cooked quinoa,fresh vegetables, olive oil, lemon juice...]Explanation: The recipe FAILS because it includes cooked quinoa.Raw vegan diets do not allow foods heated above 118 degrees F (48 degrees C),and cooking quinoa involves boiling, which exceeds this limit.Label: FAILNow evaluate the following recipe response:Query: {query}Dietary Restriction: {dietary_restriction}Recipe Response: {response}RETURN YOUR EVALUATION IN JSON FORMAT:"label": "PASS" or "FAIL","explanation": "Detailed explanation citing specific ingredients or methods"
Appendix D: Pipeline State Evaluator Prompts
Complete evaluator prompts for each pipeline state. Each follows the same structure:
Standard Evaluator Structure
1. Role definition ("You are an expert evaluator for the X state")2. What the state should do (3-4 bullet points)3. Evaluation criteria (3-4 numbered criteria)4. What counts as a failure (4-5 specific failure types)5. What does NOT count as a failure (2-3 acceptable variations)6. Input/Output template variables7. Output format (JSON with label and explanation)
Available Evaluators
| State | Key Criteria | Common Failures |
|---|---|---|
| ParseRequest | Accuracy, completeness, format | Misinterpretation, missing constraints |
| PlanToolCalls | Tool selection, ordering, rationale | Missing tools, incorrect tools |
| GenRecipeArgs | Query relevance, filter accuracy | Missing dietary filters, wrong servings |
| GetRecipes | Relevance, dietary compliance | Irrelevant recipes, dietary violations |
| GenWebArgs | Relevance, context alignment | Off-topic queries, too generic |
| GetWebInfo | Relevance, quality | Irrelevant results, off-topic content |
| ComposeResponse | Recipe accuracy, step clarity, constraint compliance | Contradictions, missing info, violations |
Full evaluator prompts for each state follow the structure above, tailored to the specific responsibilities and failure modes of each pipeline stage.
Appendix E: Judge Prompt Engineering Tips
A collection of techniques that consistently improve LLM judge accuracy. Use this as a checklist when building or debugging a judge.
1. Explanation Before Verdict
Always ask the judge to explain its reasoning before giving the final label. This is the single most impactful technique.
❌ Bad: "Label: PASS or FAIL. Explanation: ..."✅ Good: "Explanation: [your reasoning]. Label: PASS or FAIL"
Why it works: When the model writes the label first, the explanation becomes a post-hoc rationalization. When reasoning comes first, the model actually deliberates, and the label follows logically. Reasoning models already think before they answer, so the accuracy gain from this ordering may be smaller there; keep the written explanation anyway, because it is what you read when the judge disagrees with a human label.
2. Be Ruthlessly Specific About Criteria
Vague criteria lead to inconsistent judgments. Define exactly what counts as Pass and Fail.
❌ Vague: "Does the response follow dietary restrictions?"✅ Specific: "PASS: Every ingredient in the recipe is compatible with the stateddietary restriction. FAIL: At least one ingredient violates the restriction,OR the cooking method introduces a violation (e.g., frying in butter fordairy-free)."
3. Include "What Does NOT Count as a Failure"
Judges tend to be overly strict. Explicitly list acceptable variations to calibrate leniency.
What does NOT count as a failure:- Suggesting optional toppings that can be omitted- Using brand names instead of generic ingredient names- Minor formatting issues in the recipe- Providing substitution suggestions alongside the main recipe
4. Use Domain-Specific Few-Shot Examples
Generic examples are far less effective than examples from your actual data. Always pull few-shot examples from your Train set.
Example selection strategy:
- 1 clear Pass (easy case)
- 1 clear Fail (easy case)
- 1 borderline case (the kind the judge will struggle with most)
Include the reasoning in each example, not just the label. The judge learns the reasoning pattern, not just the answer.
5. Temperature Settings
| Use Case | Temperature | Rationale |
|---|---|---|
| Binary classification (Pass/Fail) | 0.0 | Deterministic, reproducible |
| Likert scale scoring (1-5) | 0.0–0.3 | Low variance, consistent |
| Generating diverse critiques | 0.5–0.7 | Some creativity for different angles |
| Brainstorming failure modes | 0.7–1.0 | High creativity for exploration |
For judge evaluation on models that accept it, use temperature 0.0. You want the same input to produce the same output every time.
This table applies only to models that still take a temperature parameter, and that set is shrinking (see "Pin the judge" in Chapter 4, Step 7). On reasoning models that reject sampling parameters, use the lowest reasoning effort that holds your TPR/TNR, pin the model ID and effort, and if verdicts still vary between runs, run the judge three times and take the majority label.
6. Structured Output Formats
Tell the judge exactly how to format its response. JSON is preferred for parsing reliability.
Return your evaluation as JSON:{"explanation": "Step-by-step reasoning about the response...","label": "PASS or FAIL","confidence": "HIGH, MEDIUM, or LOW","flagged_items": ["list of specific problematic items, if any"]}
Tip: The confidence field is useful for identifying borderline cases during error analysis but is not a reliable calibrated probability.
7. Guard Against Common Judge Biases
| Bias | Description | Mitigation |
|---|---|---|
| Leniency bias | Judge defaults to "Pass" too often | Add explicit failure examples; emphasize "when in doubt, FAIL" |
| Verbosity bias | Judge favors longer, more detailed responses | Add examples where a short response passes and a long one fails |
| Position bias | Judge favors the first/last option in a list | Randomize order if comparing multiple outputs |
| Sycophancy bias | Judge agrees with confident-sounding text | Add examples where confident text is wrong |
| Anchoring bias | Judge is swayed by the first piece of evidence | Instruct judge to consider ALL evidence before concluding |
| Self-preference bias | Judge rates outputs from its own model family higher | Use a judge from a different model family than the system it grades |
8. Iterative Refinement Workflow
1. Write initial prompt with 2-3 few-shot examples2. Run on Dev set → calculate TPR and TNR3. Find the worst errors (cases where judge was wrong)4. For each error:a. Understand WHY the judge was wrongb. Add a clarification, edge case, or new example to the prompt5. Re-run on Dev set → check if metrics improved6. Repeat steps 3-5 until TPR > 80% and TNR > 80%7. Run ONCE on Test set for final, unbiased metrics
Common iteration patterns:
- TNR too low → Judge is missing real failures (passing bad outputs). Add more Fail examples, make fail criteria more explicit.
- TPR too low → Judge has too many false alarms (failing good outputs). Add "what does NOT count as a failure" section, add Pass examples for edge cases.
- Both low → Criteria are ambiguous. Rewrite from scratch with clearer definitions.
9. Model Selection for Judges
| Model Tier | Examples (list USD per 1M input/output, November 2026) | When to Use |
|---|---|---|
| Frontier | Claude Opus 5.5 ($4/$20), GPT-6 Astra ($10/$50) | High-stakes evals, complex reasoning, setting the performance ceiling |
| Mid | Claude Sonnet 5.5, GPT-6.1 Sol (both $2/$10) | Most production judges once validated against the ceiling |
| Fast, low-cost | GPT-6 Luna ($0.10/$0.50), Gemini 3.8 Flash ($0.75/$3.75 introductory through Dec 31, then $1.50/$7.50), Claude Haiku 5.5 ($0.10/$0.50 up to a 100K-token prompt, $0.50/$2.50 above) | Cost-sensitive, high-volume evals with clear rubrics |
| Open weights, self-hosted | Qwen3.8-27B (Apache 2.0), IBM Granite 4.2 30B (Apache 2.0), GLM-5.3-Flash (MIT, 320B total / 18B active) | Data that cannot leave your network |
| Decision model | TypeSafe Jev (early access), OpenAI Decisions API (GA October 22, 2026) | Binary checklist criteria at very high volume; no written rationale, no abstention; calibrate thresholds on your own labels |
Agreement with human labels varies more by task than by tier, so the only accuracy number that counts is the TPR/TNR you measure on your own test set. Where you can, pick a judge from a different model family than the system it grades (see self-preference bias above).
Tip: Start with the most capable model to establish a performance ceiling. Then test whether a cheaper model can match it for your specific use case. Often it can, especially with good few-shot examples.
10. Prompt Versioning
Always version your judge prompts. Track:
- The prompt text
- The few-shot examples used
- The exact model ID, reasoning effort, and temperature (where the model accepts it)
- Dev set metrics (TPR, TNR) at that version
- Date and reason for the change
Both Phoenix and Langfuse have built-in prompt versioning. Use it.
# Phoenixfrom phoenix.client import Clientfrom phoenix.client.types import PromptVersionpx = Client()prompt = px.prompts.create(name="dietary-judge",prompt_description="Dietary adherence judge",version=PromptVersion([{"role": "user", "content": judge_prompt_text}],model_name="gpt-4o",description="v3: added edge cases for keto",template_format="F_STRING", # judge prompt uses {query}-style variables),)# Langfuselangfuse.create_prompt(name="dietary-judge",prompt=judge_prompt_text,labels=["staging"], # promote to "production" after validation)
Appendix F: Platform Methods Reference (Phoenix & Langfuse)
Phoenix
Tracing
from phoenix.otel import registertracer_provider = register(project_name="my-app", auto_instrument=True)tracer = tracer_provider.get_tracer(__name__)@tracer.chain # For pipeline steps@tracer.tool # For tool calls@tracer.agent # For agent-level spanswith tracer.start_as_current_span("my-operation") as span:span.set_attribute("input.value", user_input)result = do_work()span.set_attribute("output.value", result)
Querying Spans
from phoenix.client import AsyncClientfrom phoenix.client.types.spans import SpanQuerypx_client = AsyncClient()spans_df = await px_client.spans.get_spans_dataframe(project_identifier="my-app")query = SpanQuery().where("span_kind == 'LLM'")query = SpanQuery().where("name == 'ParseRequest'")
Datasets
dataset = await px_client.datasets.create_dataset(dataframe=df,name="my-dataset",input_keys=["query"],output_keys=["expected_answer"],metadata_keys=["category"],)
Experiments
from phoenix.client.experiments import run_experimentdef my_task(example):query = example["input"]["query"]return {"answer": my_pipeline(query)}def my_evaluator(input, output, expected, **kwargs):return 1.0 if output["answer"] == expected["answer"] else 0.0experiment = run_experiment(dataset=dataset, task=my_task, evaluators=[my_evaluator],)
LLM Evaluation (Batch)
# arize-phoenix-evals 3.x; llm_generate/llm_classify/OpenAIModel were removed in 3.0.0from phoenix.evals import create_classifier, async_evaluate_dataframefrom phoenix.evals.llm import LLMfrom phoenix.evals.utils import to_annotation_dataframejudge = create_classifier(name="quality",prompt_template="Evaluate this response.\n\nInput: {input}\nOutput: {output}",llm=LLM(provider="openai", model="gpt-4o"),choices={"pass": 1.0, "fail": 0.0},)results_df = await async_evaluate_dataframe(dataframe=traces_df, evaluators=[judge], concurrency=20,)await px_client.spans.log_span_annotations_dataframe(dataframe=to_annotation_dataframe(dataframe=results_df))
Prompt Management
from phoenix.client.types import PromptVersionprompt = await px_client.prompts.create(name="my-prompt",version=PromptVersion([{"role": "system", "content": "..."},{"role": "user", "content": "{{question}}"}],model_name="gpt-4o",),)
Langfuse
Tracing
from langfuse.openai import OpenAI # Drop-in replacementfrom langfuse import observe, get_clientclient = OpenAI() # Calls are automatically traced@observe()def my_pipeline(question):"""Creates a parent trace"""return generate_answer(question)@observe(as_type="generation")def generate_answer(question):"""Tracked as a generation"""return client.chat.completions.create(...)
Querying Traces
from datetime import datetime, timedelta, timezonelangfuse = get_client()end = datetime.now(timezone.utc)# v2 observations API (SDK v4). The v1 endpoints behind langfuse.api.trace.list()# and .get() are deprecated (SDK v5 removed them); Langfuse Cloud serves them only until November 16, 2026.# Their replacement is this endpoint: filter by trace_id, or group results by trace_id.observations = langfuse.api.observations.get_many(trace_id="trace_id",from_start_time=end - timedelta(days=1), # v2 queries should always be time-boundedto_start_time=end,fields="core,basic,io",limit=100,)next_cursor = observations.meta.cursor # pass as cursor= to fetch the next page
Datasets
langfuse.create_dataset(name="my-dataset")langfuse.create_dataset_item(dataset_name="my-dataset",input={"query": "What is AI?"},expected_output={"answer": "Artificial Intelligence"},)
Experiments
from langfuse import Evaluationdef my_task(*, item, **kwargs):query = item["input"]["query"]return my_pipeline(query)def my_evaluator(*, output, expected_output, **kwargs):correct = output == expected_output.get("answer")return Evaluation(name="accuracy", value=1.0 if correct else 0.0)result = langfuse.run_experiment(name="baseline",data=test_data,task=my_task,evaluators=[my_evaluator],)print(result.format())
Scores (Evaluation Results)
# Score a tracelangfuse.create_score(trace_id="trace_id",name="dietary_adherence",value=1, # 0 or 1data_type="BOOLEAN",comment="Recipe correctly follows vegan restrictions",)# Score within contextwith langfuse.start_as_current_observation(as_type="span", name="eval") as span:span.score(name="accuracy", value=0.95, data_type="NUMERIC")
Prompt Management
langfuse.create_prompt(name="my-prompt",type="chat",prompt=[{"role": "system", "content": "You are a {{role}}"},{"role": "user", "content": "{{question}}"},],labels=["production"],)prompt = langfuse.get_prompt("my-prompt", type="chat")compiled = prompt.compile(role="chef", question="Best pasta recipe?")
Appendix G: 30-Day Learning Path
Week 1: Foundations (Engineer, PM, or QA)
| Day | Activity | Time | Role Focus |
|---|---|---|---|
| 1 | Pick your platform (Phoenix or Langfuse), install it | 1h | All |
| 2 | Instrument your app with auto-tracing | 2h | Engineer |
| 2 | Browse the trace viewer UI, understand traces visually | 1h | PM/QA |
| 3 | Create a test dataset with dimensional sampling | 2h | All |
| 4 | Upload dataset to your platform, run first experiment | 1h | All |
| 5 | Review 50 traces, take notes (open coding) | 1h | All |
| 6 | Categorize errors using LLM (axial coding) | 1h | All |
| 7 | Prioritize: frequency x severity matrix | 30m | All |
Week 2: Code-Based Evals
| Day | Activity | Time | Role Focus |
|---|---|---|---|
| 8 | Build 2 code-based evals for your top issues | 2h | Engineer |
| 8 | Define eval criteria in plain English | 1h | PM/QA |
| 9 | Test evals with known good/bad cases | 1h | All |
| 10 | Run evals on all traces, calculate failure rates | 1h | All |
| 11-14 | Iterate based on results | 2h | All |
Week 3: LLM Judge
| Day | Activity | Time | Role Focus |
|---|---|---|---|
| 15 | Label 100-150 traces as ground truth | 3h | All |
| 16 | Split into Train/Dev/Test | 30m | Engineer |
| 17 | Write first judge prompt with few-shot examples | 2h | All |
| 18 | Validate on Dev set, calculate TPR/TNR | 1h | All |
| 19 | Iterate prompt until metrics > 80% | 2h | All |
| 20 | Final test on Test set | 30m | All |
| 21 | Run judge on all traces + correct with judgy | 1h | All |
Week 4: Advanced Topics & Production
| Day | Activity | Time | Role Focus |
|---|---|---|---|
| 22 | RAG evaluation: retrieval metrics + answer quality (Ch. 6) | 2h | Engineer |
| 23 | Multi-step pipeline evaluation (Ch. 7) | 2h | Engineer |
| 24 | Multi-turn conversation evaluation (Ch. 8) | 2h | Engineer |
| 25 | Safety evals: prompt injection, PII leakage (Ch. 9) | 2h | All |
| 26 | Set up regression test suite (Ch. 11) | 2h | Engineer |
| 27 | Human annotation calibration: measure inter-annotator agreement (Ch. 12) | 1h | All |
| 28 | Optimize for cost: tiered evaluation, sampling strategy (Ch. 13) | 1h | All |
| 29 | Create monitoring dashboard + automated eval runs | 2h | Engineer |
| 30 | Document eval suite, present to stakeholders, plan maintenance | 2h | All |
Lessons Learned
Real lessons from implementing complete eval pipelines in production:
On Building Judges (Chapters 4, 10)
-
LLM-as-Judge is powerful but needs guardrails - Without proper validation, a judge can confidently give wrong answers. Always validate against ground truth.
-
You must test evaluators against ground truth - A judge that seems reasonable but has TNR=22% is actively harmful: it misses most real failures.
-
Train/Dev/Test splits enable confidence - Without them, you're fooling yourself about your judge's quality. This is non-negotiable.
-
Iterating on judge prompts is crucial - The first prompt is never good enough. Plan for 3-5 iterations minimum. See Appendix E for techniques.
-
Explanation-before-verdict is the #1 technique - Asking the judge to reason before labeling improves accuracy more than any other single change.
On Process & Methodology (Chapters 3, 11, 12)
-
Error analysis is the real work - Fancy tools don't matter if you haven't sat down and looked at your failures. Open coding → axial coding → prioritization is the workflow that works.
-
Human annotators disagree more than you think - Measure inter-annotator agreement (Cohen's kappa) before trusting your ground truth. If humans can't agree, the judge won't either.
-
Closing the loop is what separates good teams from great ones - Running evals is only half the job. The other half is systematically turning failures into improvements and preventing regressions.
On Production & Scale (Chapters 9, 13)
-
Safety evals are not optional - Prompt injection, PII leakage, and jailbreak detection should be running before you worry about quality evals.
-
Start expensive, then optimize - Use a frontier judge (Claude Opus 5.5, GPT-6 Astra) to establish your performance ceiling, then test whether a cheaper model can match it. Often it can.
-
Sampling beats exhaustive evaluation - Evaluating 10% of traces with statistical rigor gives you a better answer than evaluating 100% with a bad judge.
-
Good observability tools make the workflow 10x faster - Integrated tracing, evaluation, datasets, and experiments in one platform (Phoenix, Langfuse, etc.) saves enormous time vs. stitching together custom scripts.
Conclusion
AI evals are not just "testing". They are a product development methodology that touches engineering, product management, and quality assurance.
Key takeaways:
- Everyone needs evals. Not just big companies. If your AI app touches users, you need systematic evaluation.
- Start with error analysis. Sit down and look at your failures before building anything automated (Chapter 3).
- PMs and QAs must lead. Error analysis and criteria definition are product/quality work, not just engineering tasks.
- Build incrementally. Start with code-based evals, then add LLM judges, then add safety evals. Don't try to do everything at once.
- Measure what matters. Application-specific criteria, not generic "helpfulness" scores.
- Both TPR and TNR. A judge that false-alarms on good outputs or lets real failures through is harmful. Measure both.
- Split your data. Train/Dev/Test is mandatory. Without it, you're overfitting your judge.
- Correct for bias. Use statistical correction (Chapter 10) for honest metrics.
- Close the loop. Evals that don't lead to improvements are wasted effort (Chapter 11).
- Plan for scale. Start with the best model, then optimize for cost (Chapter 13).
Your action plan (see Appendix G for details):
- Week 1: Set up observability (Phoenix, Langfuse, or your tool of choice), do error analysis
- Week 2: Build 2-3 core code-based evals
- Week 3: Build and validate an LLM judge with proper train/dev/test splits
- Week 4: Advanced topics: RAG evals, multi-turn evals, safety evals, automation
- Ongoing: 30 minutes per week maintenance + regression testing
Remember: The teams shipping the best AI products are the ones with the best evals. Not the fanciest models. Not the biggest teams. The ones who systematically measure and improve.
Start today. Your future self will thank you.
Learning Resources
Courses, Free Content & Certificates
- AI Evals (official site of this guide's author): aievals.co: courses, free content, agent kits, and certificates
Platform Documentation & Learning Hubs
- Phoenix Docs: arize.com/docs/phoenix
- Arize Blog & Learning Hub: arize.com/blog
- Langfuse Docs: langfuse.com/docs
- Maven Course (AI Evals for Engineers & PMs): maven.com/parlance-labs/evals
- HuggingFace Evaluation Guidebook: github.com/huggingface/evaluation-guidebook
Research & Thought Leadership
- OpenAI Evals Platform: being retired. The dashboard and API shut down November 30, 2026 (a read-only step was scheduled for October 31), so export now; see the deprecations page. OpenAI's migration guide points to Promptfoo.
- OpenAI Cookbook (practical examples & guides): cookbook.openai.com
- OpenAI Research: openai.com/research
- Anthropic Research: anthropic.com/research
- METR (Model Evaluation & Threat Research): metr.org
- Eugene Yan on eval process: eugeneyan.com/writing/eval-process
Blogs That Shaped This Guide
- Hamel Husain's Blog: hamel.dev: Applied AI engineering, LLM evals deep-dives
- Shreya Shankar's Site: sh-reya.com: LLM data systems research, eval methodology
- Maxim AI Articles: getmaxim.ai/articles: Agentic evaluation patterns
Open-Source Tools & Libraries
| Tool | Focus | License | Links |
|---|---|---|---|
| Arize Phoenix | Observability & evals | ELv2 | GitHub · Docs |
| Promptfoo | CLI/CI evals, red teaming | MIT (OpenAI-owned) | GitHub · Docs |
| Langfuse | Custom pipelines & tracing | MIT | GitHub · Docs |
| RAGAS | RAG-specific evaluation (no commits since February 24, 2026; v0.4.3 from January 13, 2026 is latest, so pin it) | Apache 2.0 | GitHub · Docs |
| Comet Opik | LLM tracing & evaluation | Apache 2.0 | GitHub · Site |
| judgy | Statistical bias correction | Open | GitHub |
| Braintrust | Experimentation & logging | Partial | Docs |
| Galileo (now Splunk Agent Observability, Cisco) | Hallucination detection | Proprietary | Docs · Splunk |
| Maxim | Agentic system evaluation | Proprietary | Site |
Strategy Comparison Matrix
| Company | Focus | Open Source | Best For | Unique Strength |
|---|---|---|---|---|
| Anthropic | Safety / Red Teaming | Partial | Frontier risks | Constitutional classifiers, multi-attempt adversarial testing |
| OpenAI | Preparedness / Business | Promptfoo (MIT) | Enterprise context | SME probing, contextual evals; hosted Evals platform shuts down Nov 30, 2026 |
| Arize | Observability | Phoenix (ELv2) | Production scale | OTel-native, data lake integration; part of Dynatrace since October 1, 2026 |
| RAGAS | RAG-specific | Yes (Apache 2.0) | RAG pipelines | Reference-free metrics, synthetic test data generation |
| Maxim | Agentic Systems | No | Multi-agent apps | Simulation framework, no-code evaluation |
| Langfuse | Custom Pipelines | Yes (MIT) | Data sovereignty | Self-hostable, full control over data |
| Braintrust | Experimentation | Partial | Early-stage teams | Collaborative design, fast iteration |
| Galileo (now Splunk Agent Observability) | Hallucinations | No | Quality assurance | ChainPoll, real-time monitoring; Cisco-owned |
| Comet Opik | LLM Tracing & Evals | Yes (Apache 2.0) | End-to-end observability | Framework integrations, online evaluation rules |
| METR | Catastrophic Risk | Research | Policy guidance | Autonomous capability assessment |
Contact Me
- Om Bharatiya: @ombharatiya
- Courses, free content, agent kits, and certificates: aievals.co
Reference Work Credits
This guide was built on the foundation of the following people's work and ideas. Their courses, blogs, and open-source contributions made this guide possible:
- Hamel Husain: @HamelHusain, hamel.dev
- Shreya Shankar: @sh_reya, sh-reya.com
- Eugene Yan: @eugeneyan, eugeneyan.com
This guide was inspired by and builds upon the AI Evals for Engineers & PMs course by Hamel Husain and Shreya Shankar, extended with additional research, production-ready code examples, and multi-platform guides covering Phoenix, Langfuse, and the broader eval tooling ecosystem.
Author: Om Bharatiya | Created: February 2026
From the AI System Design Guide by Om Bharatiya · MIT License ·view source