Library

Volume 2 · Prompt Engineering

Notebook
Sign in

Section 16

Prompt Optimization & A/B Testing

494 words · about 2 min

16.1 Optimization Loop

Define baseline prompt
     │
     ▼
Run evaluation suite (golden Q&A set)
     │
     ▼
Identify failure modes (wrong format, hallucination, verbosity)
     │
     ▼
Modify prompt (one change at a time)
     │
     ▼
Re-run evaluation → Compare metrics
     │
     ▼
If improved → New baseline
If degraded → Revert and try different change

16.2 A/B Testing Framework

class PromptABTest:
    def __init__(self, control_prompt, variant_prompt, traffic_split=0.1):
        self.control = control_prompt      # 90% of traffic
        self.variant = variant_prompt      # 10% of traffic
        self.metrics = {"control": [], "variant": []}

    def route(self, request_id: str) -> str:
        if hash(request_id) % 100 < self.traffic_split * 100:
            return self.variant
        return self.control

    def record_metric(self, group, quality_score, latency, cost):
        self.metrics[group].append({
            "quality": quality_score, "latency": latency, "cost": cost
        })

    def analyze(self) -> dict:
        # Statistical significance test
        # Compare quality, latency, cost between groups
        # Recommend: promote variant, continue test, or reject
        pass

16.3 Key Metrics for Prompt Evaluation

Metric Description Target
Task Accuracy Does the output correctly answer the question? >95%
Format Compliance Does output match the requested format? >99%
Hallucination Rate % of outputs with unsupported claims <2%
Average Latency Time to first token + full generation <2s
Token Efficiency Output quality per token consumed Maximize
Refusal Rate % of valid queries incorrectly refused <1%

Section 16 — Interview Q&A

Q: How do you measure and improve the quality of prompts over time? A: Start with a golden dataset (100-500 representative Q&A pairs, including edge cases and adversarial examples, human-reviewed, refreshed quarterly). Build an automated evaluation pipeline that scores every prompt candidate on accuracy, faithfulness, format compliance, latency, and token usage against that dataset. Track metrics per prompt version on a dashboard, with alerts on regression, plus a weekly automated regression report. Close the loop with user feedback — thumbs up/down on responses flags candidates for review, and clustering the downvoted responses reveals patterns that inform the next prompt iteration. This turns prompt engineering from ad hoc tweaking into a continuous, measurable improvement process, identical in spirit to how you’d manage a traditional software regression suite.

Q: How would you handle a situation where prompt engineering alone isn’t sufficient to hit a quality bar? A: I follow an escalation ladder, cheapest/fastest fix first: (1) optimize the prompt — restructure, add examples, add CoT (1-2 days); (2) add retrieval (RAG) for factual grounding (1-2 weeks); (3) fine-tune if the issue is style/format/reasoning pattern, not knowledge (2-4 weeks); (4) upgrade to a more capable model (often just 1 day of testing); (5) combine multiple techniques; (6) add a human-in-the-loop review step for the highest-stakes outputs. Diagnostically: if the issue is knowledge, add RAG; if it’s reasoning, try CoT or fine-tuning; if it’s format/style, fine-tune or enforce structured output; if it’s accuracy, add a verification step; if it’s consistency, use self-consistency, temperature=0, and structured output constraints.