Drop in for a virtual coffee, Wednesdays at 5:30pm

Validate and retry

Structured output gives you the shape; validating is checking the values are actually right, and when they are not, feeding back the specific failure so the AI corrects it rather than starting cold.

yesnoagainSourceAI extractsValid?Show the errorsDone
Validate the extracted card, and when it fails hand back the specific errors so it corrects that exact thing, looping until it holds. The exception: if the information simply is not in the source, no retry will conjure it, so you stop and flag instead.

Doing this with Claude Code: Validate and retry

When the output has to be right, do not just rerun it. Validate it, and hand back the specific failure so Claude corrects that exact thing rather than guessing again. The loop also sharpens your own sense of what right means, you tend to pin the requirement down properly only as you see the results.

The loop earns its keep in three ways: say why and show, know when a retry will not help, and let the output check itself.

Say why and show. A bare try again gets the same mistake back. Hand over the exact error, what failed and where, so the retry fixes that one thing. On the recipe card that is the serves field is empty and the butter has no amount, not that is wrong.

Know when a retry will not help. A retry fixes how it answered, a format or structure problem, but cannot invent what was never in the source. When the data is genuinely absent, stop and flag rather than loop.

Let the output check itself. Extract a calculated value next to the stated one, plus a conflict flag, so a discrepancy surfaces in the output instead of you catching it by eye. On the card, a stated serves sitting next to a calculated one.

Common questions

  1. Why is a bare "try again" not enough?
    A bare retry gives the AI nothing to work with, so it guesses and often makes the same mistake a second time. Hand back the specific error, what failed and where, so the retry targets that exact thing.
  2. Why can the AI not work out what "right" looks like on its own?
    Because only you know the required outcome. The AI cannot see it, so the feedback you give is you steering it toward a target that is invisible from where it sits.
  3. When will a retry not help at all?
    A retry fixes how something was answered, a format or structure problem, but it cannot invent what was never in the source. When the data is genuinely absent, stop and flag it rather than loop.
  4. Can the output check itself?
    Have it extract a calculated value next to the stated one, plus a flag for a conflict between them, so a discrepancy surfaces in the output rather than waiting for you to catch it by eye.

Back to For developers. The craft around it is Working with AI.