Prompt Engineering & Structured Output/Structured Output & Scale
Intermediate10 min

Validation, Retry & Feedback Loops

How to build validation loops that catch semantic errors in Claude's output and retry with targeted error feedback. Covers retryable vs. non-retryable errors, the detected_pattern field for tracking dismissals, and self-correction via calculated vs. stated totals.

Quick Reference

  • Retry-with-error-feedback: append specific validation errors to the next attempt
  • Retries are effective for format/calculation errors but NOT when info is absent from source
  • Always include the original document + failed extraction + specific error messages on retry
  • Set a max retry limit (typically 2-3) to avoid infinite loops and cost explosion
  • Use a detected_pattern field to track recurring dismissal patterns across reviews
  • Self-correction: have Claude compute calculated_total from line items and compare to stated_total
  • conflict_detected boolean flags discrepancies for human review instead of silently choosing one
  • Validation happens AFTER schema parsing -- schemas catch syntax, validation catches semantics
  • Non-retryable errors (missing source data) should escalate to human review, not retry

Why Validation is Separate from Schema

In the previous article, we saw that tool_use schemas guarantee syntactically valid JSON. But valid JSON does not mean correct data. Validation is the second layer: it checks that the VALUES make semantic sense. This is where most production extraction errors actually live.

ExtractClaude processes inputValidateschema + business rulesValid?YESDonequality assuredNOError Feedbackspecific error messagesRe-extractwith error contextRetryn / 3

Extract, validate, and retry with specific error feedback until quality passes

Error typeSchema catches it?Validation catches it?Example
Missing required fieldYesN/ANo vendor_name in output
Wrong field typeYesN/Atotal is string instead of number
Invalid enum valueYesN/Acategory = 'misc' not in enum
Line items don't sum to totalNoYesItems sum to $550 but total says $500
Date in wrong formatPartialYesSchema says string, validation checks ISO 8601
Vendor name from wrong sectionNoYesExtracted 'Bill To' instead of 'Bill From'
Fabricated data not in sourceNoDifficultDate fabricated when source has none
Missing data from sourceNoNoSource has PO number but extraction missed it
The validation layer

Validation sits between schema parsing (automatic) and human review (expensive). Good validation catches the errors that schemas miss while keeping humans focused on the cases that automation truly cannot handle.