Task 4.4
Validation & Retry Loops
Specific error feedback for self-correcting extraction
📘What You Need to Know
The Validation-Retry Pattern
Production extraction does not stop at tool_use. The validation-retry pattern adds a self-correction loop: extract data using tool_use, validate output against business rules, and if validation fails, append specific error details to the conversation and retry. The model corrects based on explicit feedback rather than guessing what went wrong.
This pattern separates format errors (caught by schema) from semantic errors (caught by business rules). Retry loops fix semantic errors when the data exists in the source document but was extracted incorrectly. They do not fix missing data — if the invoice has no tax line, no amount of retrying will invent one.
Retry loops fix format and semantic errors, not missing data. Know this distinction for the exam.
Specific vs Generic Retry Feedback
Generic retry messages — "there were errors, please try again" — give Claude nothing to correct. It may repeat the same mistake, hallucinate a fix, or change unrelated fields. Specific feedback names the failing field, the actual value, the expected value, and the rule that was violated.
Specific feedback turns retry into targeted correction. The model adjusts the exact field that failed without disturbing correctly extracted values.
# Wrong retry feedback (generic)
"There were errors, please try again."
# Right retry feedback (specific)
"Validation failed:
1. Line items total ($450.00) does not match subtotal field ($500.00). Recalculate from line items.
2. tax_amount contains '10%' (percentage) but schema requires dollar amount (e.g., 45.00).
3. invoice_date '2024-13-45' is not a valid date. Use format YYYY-MM-DD."Building the Retry Loop
A production validation-retry loop has four components: the extraction call (tool_use), the validator (business rules returning structured errors), the retry message builder (formats specific errors for the model), and a retry budget (max 2–3 attempts before escalating to human review).
On each retry, append the validation errors as a new user message — do not replace the original document context. Claude needs both the source document and the specific errors to correct accurately.
- •Extract via tool_use with forced specific tool_choice
- •Run business rule validation returning field-level error details
- •On failure: append specific errors as user message, preserve original context
- •Set retry budget (2–3 attempts) before escalating to human review
- •On success: return validated extraction to downstream pipeline
Tracking Systematic Failures
When the same validation error recurs across many documents, it is a systematic failure — not a one-off extraction mistake. Track these with detected_pattern fields in your validation output. Examples: "tax_field_consistently_percentage", "date_format_european_on_us_invoices", "missing_line_items_on_multi_page_docs".
Systematic patterns indicate schema design problems, missing few-shot examples, or document types not covered by your pipeline. Retrying individual documents will not fix a systematic issue — you need schema or prompt changes.
If the same error appears on >10% of documents, stop retrying and fix the root cause in schema or prompt design.
When to Retry vs Escalate
Retry when the error is likely correctable — wrong total, wrong format, misclassified enum. Escalate to human review when data is genuinely missing from the source, when retries are exhausted, or when conflicting values cannot be resolved automatically.
The exam tests the feedback quality on retry (specific vs generic) and the decision to keep retrying vs escalate. Never retry indefinitely — set a budget and route persistent failures to humans with the validation errors attached.
⚠️Exam Traps
Trap: Generic retry: "there were errors, please try again"
Why it's wrong: Claude cannot correct without knowing which field failed and what the expected value is.
Trap: Replacing original document context on retry instead of appending errors
Why it's wrong: Claude loses the source document and cannot re-extract the correct values.
Trap: Retrying indefinitely when data is missing from the source document
Why it's wrong: Retry loops fix extraction errors, not missing data — infinite retry wastes tokens and time.
Trap: Using retry loops to fix systematic failures affecting >10% of documents
Why it's wrong: Systematic patterns need schema or prompt fixes, not per-document retries.
✓Concept Check
Extraction validation fails because line items total ($450) does not match subtotal ($500) and tax is formatted as a percentage. What feedback should you send on retry?
🔨Build Exercise
Advanced50 minutesImplement a Validation-Retry Loop
What you'll learn
- •Build self-correcting extraction with specific error feedback
- •Set retry budgets and escalation paths
- •Track systematic failure patterns