Thanksgiving Week Special – Get 50% OFF on FileTranz
Claim Your OfferDesign a self-evaluation pipeline for LLM applications using multi-layer evaluation, failure analysis, adaptive routing, correction, and controlled retries.
LLMs can generate useful responses, but that does not mean every response is reliable. The response can have erroneous facts, generate hallucinations, invalid JSON or not adhere to instructions.
During this project work, it became clear to me that checking pass/fail is not sufficient. In addition to whether the response failed or not, it was also important to understand why the response failed and what needs to be done next.
This motivated me to develop the Failure-Aware LLM Evaluation Pipeline which evaluates the response, analyzes failures, their gravity and decides on the next steps.
The basic flow is:
User Prompt
↓
LLM Response
↓
Multi-Layer Evaluation
↓
Failure Analysis
↓
Severity Analysis
↓
Decision
↓
Accept / Correct / Retry / Stop
As opposed to the uniform treatment of failures, the pipeline selects the subsequent step based on failure data. For instance, some errors can be addressed, whereas critical errors can halt the pipeline.
During testing, I found that one evaluator was not enough because different failures require different types of checks. The pipeline therefore combines four evaluation methods:
Programmatic Evaluation
Reference Evaluation
Rule-Based Evaluation
LLM-as-a-Judge
This handles deterministic checks such as:
JSON validity
Required fields
Data types
Output format
Numeric values
For instance, suppose the output should be:
{
"name": "Mandy",
"age": 23
} The evaluator can verify that the required fields and types are present.
This contrasts the generated output with the desired answer. For instance:
Requirement: Capital of France.
Output: Capital city of France.
Both the phrases carry similar meanings even though they differ in words used. Depending on the particular test case, the comparison may adopt an exact match approach or semantic similarity approach.
This checks predefined requirements such as:
JSON-only output
Required information
Specific formats
Protected instruction restrictions
Output rules
This provides predictable checks for known requirements.
Open-ended questions are harder to evaluate with exact matching because several answers may be valid. For these cases, another LLM evaluates the response based on:
Correctness
Relevance
Completeness
Instruction following
Overall quality
I use this as one evaluation signal rather than relying on it alone.
The project combines the evaluator scores using:
Programmatic Evaluation → 30%
Reference Evaluation → 25%
Rule Evaluation → 20%
LLM Judge → 25%
The scoring logic is:
score = (
programmatic_score * 0.30 +
reference_score * 0.25 +
rule_score * 0.20 +
judge_score * 0.25
)
accuracy_score = round(score * 100, 2)
It provides more than one signal for the pipeline rather than one person deciding the total outcome.
One issue I found during testing was that strict evaluation could incorrectly reject valid answers to ambiguous questions. To handle this, I added special logic:
if case_type == "ambiguous":
if judge_score >= 85:
accuracy_score = judge_score
This allows a semantically strong response to be accepted even when it does not exactly match the reference. The goal is to distinguish between:
Incorrect response vs Correct response written differently
A failed score alone does not explain the problem. The pipeline therefore uses a Failure Classifier followed by a Severity Analyzer. Possible failure types include:
Incorrect information
Invalid output
Formatting failure
Reference mismatch
Rule violation
Instruction-following failure
Adversarial behavior
The flow is:
Failed Response
↓
Failure Classifier
↓
Failure Type
↓
Severity Analyzer
↓
Severity
This allows the system to treat different failures differently.
After the cause and impact of the error has been established, the Evaluation Router determines the next course of action as follows:
Evaluation Outcome
↓
Error Type
↓
Severity Level
↓
Accept/Correct/Retry/Stop
For example, a formatting error may be corrected while an important error might lead to stopping of the pipeline. This prevents from simply trying again for every failed reply.
Correction and Controlled Retries
For recoverable failures, the Correction Engine creates a targeted correction instruction. For example:
The previous response failed evaluation
The output did not meet the necessary JSON format requirement
Fixing the above problem and providing the response.
The new response will be evaluated:
Previous Failure
↓
Failure Analysis
↓
Correction Prompt
↓
LLM
↓
New Response
↓
Evaluation Thus, creating a feedback loop based on the previous failure to inform the next attempt.
An unlimited number of retries may cause higher API costs and delays. The values below were employed:
QUALITY_THRESHOLD = 0.80
MAX_RETRIES = 2
MIN_IMPROVEMENT = 5
The general logic is:
if score >= QUALITY_THRESHOLD:
return response
if attempt >= MAX_RETRIES:
return response
if improvement < MIN_IMPROVEMENT:
return response
In other words, the system stops when:
The response is good enough
The retry limit is reached
Another attempt does not provide enough improvement
The following test cases were created:
Normal – regular inputs
Edge – atypical but still valid cases
Ambiguous – multiple valid answers
Invalid – non-conformant or unsupported inputs
Adversarial – trying to exploit the system
Regression – old behavior tested
For example, among the adversarial cases there were cases of:
Getting instructions
Deliberately producing an incorrect answer
Disregarding the main task
Forcing a certain output
These cases helped check whether the pipeline could detect behavior not conforming to the task.
The initial result was not attained by the first iteration. Tests were run, failures were detected, their causes were analyzed, the evaluation or routing logic was changed, tests were conducted once again. The process was:
Run Tests
↓
Detect Failures
↓
Analyze Failure
↓
Modify Pipeline
↓
Run Tests Again
As a result, the reported pass rate went up through several iterations:

52% → 63% → 77% → 89% → 94% → 100%The pass rate improved due to:
Scoring evaluation improvements
Handling ambiguous cases
Failure analysis
Routing
Correction
Retry logic
After the final optimization of the pipeline:

TOTAL: 18
SUCCESS: 18
FAILURE: 0
INVALID: 0
PASS RATE: 100.0%
This 100% pass rate relates to 18 test cases. It should not be interpreted as a guarantee that unseen real-world inputs will never fail.
The important part was the improvement process and understanding why each test case failed before changing the pipeline.
The same approach becomes more important with AI agents because agents can perform multiple steps:
User Request
↓
Reasoning
↓
Tool Selection
↓
Tool Call
↓
Tool Result
↓
Further Processing
↓
Final Response
A failure can happen at any stage.
The evaluation architecture can therefore be extended to check:
Agent actions
Tool selection
Tool parameters
Tool responses
Structured outputs
Intermediate results
Final responses
This makes failure-aware evaluation a useful foundation for more reliable LLM applications and AI agents.
Building a reliable LLM application is not only about generating a response. The system also needs to evaluate the response, understand failures, and decide what to do next.
This project combines:
Multi-layer evaluation
↓
Failure analysis
↓
Severity analysis
↓
Adaptive routing
↓
Correction
↓
Controlled retries
↓
Iterative testing
Using 18 test cases, the reported pass rate improved from 52% to 100%, with the final run achieving:
18 successful cases
0 failures
0 invalid cases
100% pass rate
The overall point is that: A good LLM application must do more than answer questions; it must also judge and understand its own mistakes. “Where LLM failures become the next step toward better answers!”