Overview
This cookbook addresses a recurring challenge in agentic systems: the performance plateau that follows an initial proof-of-concept. Early LLM demos often showcase automation potential, but production readiness requires the ability to autonomously diagnose and correct failures—especially edge cases exposed by real-world data. The proposed solution is a repeatable, structured retraining loop that captures failures, learns from feedback, and iteratively promotes improvements back into production.
The Self-Evolving Loop: Five Stages
1. Baseline Agent
The loop starts with a deliberately simple baseline agent—in this cookbook, a summarization assistant for regulatory documents—with a generic prompt such as:
> "You are a summarization assistant. Given a section of text, produce a summary."
The baseline serves as a stable, reproducible benchmark. Simplicity here underscores a key principle: significant gains come from systematic, data-driven refinement, not just complex initial engineering.
2. Feedback Collection
Feedback comes from two sources:
- Human review: valuable for subtle edge cases and rich contextual insights, especially during prototyping or where SMEs are available. The OpenAI Evals platform supports binary ratings plus detailed textual feedback.
- LLM-as-a-judge: a separate powerful LLM scores outputs against a rubric, providing consistent, scalable evaluation with rationales—ideal for rapid iteration and CI/CD pipelines.
- Summarizer: the core agent optimized by the loop.
- Compliance checker: validates summaries against regulatory requirements such as FDA's 21 CFR Part 11.
- Production agents fail not at the demo stage, but at scaling past it; autonomous failure diagnosis is the bottleneck.
- A closed loop of execution → evaluation → optimization → promotion enables continuous improvement without constant manual engineering.
- Combining human judgment with automated LLM-as-a-judge scoring balances nuance and scalability.
- Multi-grader evaluation enables targeted, dimension-specific prompt improvements.
- Versioning and empirical promotion criteria ensure auditability and safe rollback in regulated, high-stakes environments.
3. Evaluation and Scoring
A suite of graders—specialized evaluation functions—assesses outputs against predefined criteria. For the regulatory summarization use case, four graders are defined, each with a pass threshold. Scores are aggregated into a composite score. Granular per-dimension feedback (e.g., length, factual accuracy, preservation of chemical names) tells the optimizer not just *that* the agent failed, but *why*.
4. Prompt Optimization
Three strategies are presented, from manual to fully automated:
1. OpenAI Evals "Optimize" button — quick, human-in-the-loop iteration for prototyping. 2. Metaprompt agent — a dedicated LLM that takes the original prompt, output, source text, and grader feedback, then generates an improved prompt suitable for automated pipelines. 3. Genetic-Pareto (GEPA) framework — an evolutionary approach combining quantitative scores and qualitative reflection to produce more robust, generalized prompts.
5. Updated Agent Promotion
A new prompt is promoted only if it demonstrably outperforms the baseline and meets thresholds. Full version history and performance metrics provide an audit trail and rollback capability, then the improved agent becomes the baseline for the next iteration.
Use Case: Pharmaceutical Regulatory Document Summarization
The framework is demonstrated on drafting regulatory documents for the pharmaceutical industry—an ideal high-stakes testbed demanding accuracy, precision, and compliance. Errors in documents submitted to the FDA can delay approval of life-saving treatments.
The agent summarizes sections of a Chemistry, Manufacturing, and Controls (CMC) document and must preserve critical information (chemical names, molecular formulas, regulatory citations) while producing concise summaries. The baseline architecture includes:
The self-evolving loop fits this human-in-the-loop scenario: it learns from expert corrections, gradually shifting human effort from detailed line-by-line correction to high-level strategic oversight—improving efficiency without compromising quality.