Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Refining System Instructions with Failure Analysis

Introduction

In our previous lesson, we established how to enforce the structure of an agent's output using constrained decoding and schema definitions like Pydantic. This is a crucial step for reliability, as it guarantees that an agent's response is machine-readable. However, a perfectly structured response can still be factually incorrect, logically flawed, or fail to meet the user's intent.

This lesson tackles that exact problem. Our goal is to iteratively refine system instructions using failure case analysis. This is the core engine of agentic system development—the disciplined process that transforms a fragile prototype into a robust, production-grade solution. You'll learn to move beyond ad-hoc "prompt hacking" and adopt a systematic, evidence-based methodology for improving your agents' performance.

This process is directly analogous to the test-driven development and continuous integration cycles in traditional software engineering. By identifying, categorizing, and designing tests for failures, you can prove that your interventions work and ensure that improvements in one area don't cause regressions elsewhere. This is how you deliver on the promise of "evidence, not vibes."

1. The Evaluation-Driven Development Loop

The fundamental challenge in developing reliable LLM-based systems is their non-deterministic nature. The same input can produce different outputs, and subtle changes to instructions can have unpredictable effects. To manage this complexity, we need a structured, iterative process.

How to Systematically Setup LLM Evals (Metrics, Unit Tests, LLM-as-a-Judge)

The video 'How to Systematically Setup LLM Evals' by Dave Ebbelaar provides an excellent overview of the challenges in LLM development and introduces the iterative loop for improvement.

Please watch from 02:07 to 09:22. Pay close attention to the three core challenges identified (understanding data, specification gaps, inconsistent behavior) and the three components of success (evaluation quality, debugging, change behavior). This sets the stage for the entire iterative process.

The video introduces a powerful mental model for this process: the Analyze, Measure, Improve lifecycle. This cycle is the cornerstone of what we can call "evaluation-driven development" for AI systems.

Prompt Optimization Infographic
This infographic illustrates the cyclical nature of improving an agent over time. The process involves collecting data, identifying and modeling failures, and using those insights to run experiments and optimize prompts, leading to a more robust agent.

Let's break down this loop:

  1. Analyze: Collect examples of your system's behavior, especially where it fails. The key here is not just to collect them, but to systematically categorize them. This is failure case analysis.
  2. Measure: Translate these qualitative observations into quantitative metrics. This allows you to track progress objectively. Are your changes actually making the system better?
  3. Improve: Based on your analysis and measurements, make targeted refinements to your system. This could involve changing system instructions, modifying the agent's architecture, or updating its knowledge base.

We will now dive deep into each of these steps, starting with the most critical one: analysis.

2. Step 1: Analyzing Failures with a Taxonomy

To analyze failures systematically, you need a structured vocabulary to describe them. A simple "bug list" is insufficient because it's often disorganized and uses vague language. The solution is to create a Failure Mode Taxonomy.

LLM Evals: Failure Mode Taxonomy Tutorial

The article 'LLM Evals: Failure Mode Taxonomy Tutorial' by Rajnish Khatri provides a clear, practical guide on how to build and maintain such a taxonomy.

Please read the following sections to understand the fundamentals of creating a failure taxonomy: 'What is a Failure Mode Taxonomy?': Understand the difference between a bug list and a structured taxonomy. 'Anatomy of a Failure Mode': Learn the three essential components: Title, Definition, and Examples. 'Writing Testable Definitions': This is crucial. A good definition must be specific, objective, and reproducible, much like a good unit test assertion. 'Iterative Refinement': Understand that a taxonomy is a living document that you will split, merge, and expand as your understanding of the system's failure patterns evolves.

This concept is not just a practical development aid; it's a field of active research. Building a robust, empirically grounded taxonomy is a significant undertaking that helps formalize our understanding of why complex systems fail.

Why Do Multi-Agent LLM Systems Fail?

For a state-of-the-art example from academia, let's look at the paper 'Why Do Multi-Agent LLM Systems Fail?'. This paper introduces MAST, a formal taxonomy for failures in multi-agent systems, developed using a rigorous methodology.

You don't need to read this entire paper, but browse these key sections to appreciate the depth and rigor involved: Abstract and Section 1 (Introduction): Understand the motivation and high-level contribution. Section 3 (Study Methodology): Skim this to see how they used Grounded Theory and inter-annotator agreement to iteratively build the taxonomy. This is failure analysis in action. Section 4 (Study Findings) and 4.1 (Taxonomy): Review the final taxonomy structure with its three main categories (Specification Issues, Inter-Agent Misalignment, Task Verification). Appendix A (MAST Failure Categories: Deep Dive): Read the specific definitions for the 14 failure modes. This is a concrete example of a well-defined taxonomy.

By creating your own project-specific taxonomy, you create a shared language for your team and a structured framework for improvement. Each new failure you observe can be classified, and the frequency of each failure mode can be tracked, guiding your refinement efforts to where they will have the most impact.

3. Steps 2 & 3: A Practical Workflow for Measurement and Improvement

With a taxonomy in place, how do you use it to measure and drive improvements? The most effective workflow involves a human-in-the-loop process to create a reliable, automated evaluation system. This is often called creating an "aligned evaluator."

The core idea is to first ensure your evaluation criteria (and the LLM judge that applies them) align perfectly with expert human judgment. Only then can you trust the automated evaluation to measure the impact of your system changes.

How to Systematically Setup LLM Evals (Metrics, Unit Tests, LLM-as-a-Judge)

The 'LLM Evals' video provides an exceptionally clear, step-by-step walkthrough of this alignment process using a simple spreadsheet. This is one of the most important practical skills in building reliable agents.

Please watch from 09:37 to 31:21. This is the heart of the lesson. Focus on this workflow: The 'Analyze, Measure, Improve' Lifecycle (09:37): A quick review of the core loop. Human and Model Evaluation (17:46): The critical concept that you cannot skip the human evaluation step. The goal is to make the model-based judge agree with a human expert. The Practical Excel Walkthrough (23:27): This demonstrates the full process: Collect input/output pairs (failure cases). Have an LLM critique them (Model Critique). In parallel, have a human expert critique them (Human Critique - this is your ground truth). Compare the outcomes to get an 'Agreement Score'. Use the discrepancies and the human's reasoning to refine the judge's prompt until agreement is high. Once the judge is aligned, you can confidently use it to score the outputs of your main agent as you iterate on its system instructions.

This process of collecting human-labeled examples of good and bad outputs to create an evaluation set is the direct equivalent of building a test suite in software engineering. Each entry in your evaluation dataset is a unit test for your agent's behavior. When you refine your system instructions, you re-run the agent against this entire suite to check for improvements and prevent regressions.

4. Advanced Refinement Techniques

The manual and semi-automated workflows are foundational. As a system architect, you can also leverage more programmatic approaches that embed this iterative refinement loop directly into your development framework.

Programmatic Prompt Optimization

Frameworks like DSPy formalize the structure of a prompt into a Signature (inputs, outputs, instructions) and a Program that can be compiled and optimized. An optimizer can automatically test different instructions and even few-shot examples to find a combination that maximizes a given metric on a "training set" of your failure cases.

Complete DSPy Course | Automatic and Programmatic Prompt Optimization | Complete Course

The 'Complete DSPy Course' video explains this concept well. We'll focus on the theory of how an optimizer works.

Watch these two sections to understand the concept of programmatic optimization: The 'MEIRO' Optimizer (25:29 - 33:39): This explains how an optimizer can be built. It's an LLM that is given context (your program's goal, failure examples) and is prompted to propose a new, better instruction for your agent. Automatic Optimization with DSPy (56:10 - 01:06:59): This section shows how a framework automates the process. Notice how it tests numerous auto-generated prompts and evaluates them against a metric to find the best-performing one. This is a powerful demonstration of automating the 'Improve' step of our cycle.

Self-Refinement

Another powerful architectural pattern is SELF-REFINE, where an agent is designed to critique and improve its own work in an iterative loop.

SELF-REFINE Framework for Iterative Output Refinement
The SELF-REFINE framework illustrates an autonomous loop. The model generates an initial output, then generates feedback on that output, and finally uses that feedback to produce a refined version. This process can be repeated to progressively improve quality.

In this pattern, you essentially build a two-agent system within a single agent's workflow: a "Worker" that produces the initial output and a "Critic" that provides feedback based on a set of rules (your failure taxonomy!). The output and feedback are then passed back to the Worker for another attempt. This automates the refinement loop at inference time.

Conclusion

Today we've established the core methodology for building robust agentic systems. Moving beyond simple prototypes requires a disciplined, iterative process of analyzing failures and making evidence-based improvements.

Key Takeaways:

  • Adopt the Analyze-Measure-Improve Loop: This is the fundamental cycle for evaluation-driven development of AI systems.
  • Build a Failure Mode Taxonomy: Systematically categorize failures with testable definitions and concrete examples. This turns a messy "bug list" into an actionable analytical tool.
  • Align Your Evaluators: Before automating evaluation, ensure your metrics and LLM-as-a-judge prompts align with human expert judgment. Your evaluation is only as good as your ground truth.
  • Use Failures as Your Test Suite: A curated dataset of failure cases is the equivalent of a unit test suite for your agent. Use it to measure progress and prevent regressions.
  • Leverage Programmatic Tools: Frameworks like DSPy can automate the search for optimal instructions, scaling your refinement efforts.
  • Consider Self-Refinement Architectures: Design agents that can critique and improve their own work as a powerful pattern for real-time quality improvement.

Preview of the Next Lesson

We now have a robust process for defining an agent's behavior through system instructions and iteratively refining those instructions against failures. But all of this assumes we know what the agent is supposed to do in the first place. How do we translate a vague business problem into a concrete, buildable agentic workflow?

In our next module, we will step back and focus on Requirements Engineering for Agent Systems. Our first lesson will be to learn how to write a product requirements document (PRD) for an agentic workflow that defines goals, constraints, inputs, and outputs. This is the critical first step of any client engagement: defining "what" to build before you design "how" to build it.

Can't find a good explanation? Sign up and we'll make it for you

Sign up