Module 10 of 10
Chapter 10 • Enterprise & Production

Prompt Evaluation, LLM-as-a-Judge & DSPy

In software engineering, you would never ship code without unit tests and CI/CD pipelines. Yet in AI, developers routinely deploy prompts based on subjective "vibe checks". In this final chapter, learn how to build quantitative evaluation pipelines using LLM-as-a-Judgeand replace manual prompt tweaking with automated compilation using Stanford DSPy.

1. Moving Beyond "Vibe Checks": The Evaluation Triad

When you modify a prompt to fix one edge case, you frequently degrade performance across three other tasks without realizing it. A production evaluation suite consists of three layers:

1. Golden Test Dataset

A curated collection of 50 to 500 representative, hard, and adversarial inputs paired with expected ground-truth answers.

2. Deterministic Code Evals

Fast, cost-free programmatic checks: JSON parse validation, regex assertion matching, latency limits, and token cost tracking.

3. Model-Based Evals (LLM Judge)

Using a frontier model (GPT-4o / Claude 3.5 Sonnet) with scoring rubrics to evaluate qualitative criteria (hallucination, tone, conciseness).

2. LLM-as-a-Judge: The G-Eval Rubric Pattern

Asking an LLM judge "Is this answer good?" yields noisy, subjective ratings. To achieve high correlation with human experts, provide a discrete 1-to-5 scoring rubric with explicit criteria:

# LLM-as-a-Judge Evaluation Rubric
judge_system_prompt = """
You are an impartial expert evaluator for AI customer support responses.
Evaluate the model's response based strictly on the provided Context and User Inquiry.

Score Criteria (Faithfulness to Context):
- Score 1: Direct Hallucination. The response contradicts the context or invents non-existent policies.
- Score 2: Unsupported claims. The response includes facts not mentioned in the context.
- Score 3: Partially supported. The main answer is accurate, but minor details are assumed.
- Score 4: Fully faithful. Every single claim is directly backed by the context, with minor wordiness.
- Score 5: Flawless. 100% grounded in the context, perfectly concise, and directly answers the query.

Provide your output in JSON:
{
  "reasoning": "<detailed step-by-step critique>",
  "score": <integer from 1 to 5>
}
"""

3. Automated Prompt Compilation: Stanford DSPy

Developed by Stanford NLP, DSPy (Declarative Self-improving Python) introduces a revolutionary paradigm:stop writing raw prompt strings. Instead, define your task as code signatures and let teleprompter optimizers synthesize the optimal few-shot exemplars and instructions:

import dspy

# 1. Configure the Language Model
turbo = dspy.LM('openai/gpt-4o-mini', api_key="sk-...")
dspy.configure(lm=turbo)

# 2. Define Input / Output Signatures (No string prompts!)
class QAWithContext(dspy.Signature):
    """Answer technical questions based on the provided reference context."""
    context = dspy.InputField(desc="Documentation snippets")
    question = dspy.InputField(desc="User inquiry")
    answer = dspy.OutputField(desc="Precise, grounded answer")

# 3. Define a Modular Pipeline
class RAGPipeline(dspy.Module):
    def __init__(self):
        super().__init__()
        # Chains of Thought reasoning module
        self.generate_answer = dspy.ChainOfThought(QAWithContext)

    def forward(self, context, question):
        return self.generate_answer(context=context, question=question)

# 4. Automated Prompt Optimization (MIPRO / BootstrapFewShot)
from dspy.teleprompt import BootstrapFewShot

# Train/validation dataset
trainset = [ ... ]

# Define validation metric (e.g. semantic match or LLM judge)
def validate_answer(example, pred, trace=None):
    return example.answer.lower() in pred.answer.lower()

# The Optimizer tunes instructions and selects optimal exemplars automatically!
teleprompter = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=4)
compiled_rag = teleprompter.compile(RAGPipeline(), trainset=trainset)

4. Production Cost Optimization: Prompt Caching

When building production chatbots or RAG systems with massive context (such as a 50,000-token employee handbook), re-sending those tokens on every user turn is costly and slow. Modern providers (Anthropic Prompt Caching, OpenAI Cached Tokens) reuse pre-computed attention keys and values:

Provider FeatureCost DiscountLatency ReductionRequirement
Anthropic Prompt Caching90% cheaper for cached read tokensUp to 80% lower Time-to-First-Token (TTFT)Minimum 1,024 tokens prefix; cache lasts 5 minutes with automatic renewal.
OpenAI Cached Inputs50% cheaper for cached input tokensAutomatic caching on identical prefixes over 1,024 tokens.Keep the system prompt and static RAG context at the very top of your message array.

🎉 Congratulations on Completing the Prompt Engineering Masterclass!

You have mastered the science of Context Engineering: from autoregressive token physics and chat roles, to advanced Chain-of-Thought, ReAct autonomous agent loops, deterministic JSON Schemas, grounded RAG prompt design, jailbreak defense-in-depth, and automated compilation with DSPy.

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools