Error Handling in Chains

What to do when a step in your chain fails or produces poor output.

6 min read
2 quiz questions

In any multi-step process, things go wrong. A step might produce malformed output, hallucinate information, miss key requirements, or simply give a low-quality result. Robust prompt chains need error handling strategies just like robust software needs try/catch blocks.

  1. Format failures: The model ignores your JSON schema and outputs free text instead. More common with weaker models.
  2. Quality degradation: Output is technically correct but shallow, generic, or misses the point. Often happens in later chain steps when context gets stale.
  3. Hallucination injection: Step 2 fabricates details not present in Step 1's output. The fabrication then compounds through subsequent steps.
  4. Context overflow: You paste too much from previous steps, hitting the context limit or degrading attention on the important parts.
  5. Instruction drift: By Step 4 or 5, the model starts drifting from your original intent because each step slightly shifts the framing.

Add a validation step between chain links. This can be a prompt that checks the output meets requirements, or programmatic validation for structured outputs.

Quality Gate

Quality gate that validates intermediate outputs before they proceed in the chain.

Review this output from the previous step and evaluate it:

[PASTE OUTPUT]

Requirements it must meet:
1. [REQUIREMENT 1]
2. [REQUIREMENT 2]
3. [REQUIREMENT 3]

For each requirement, rate: PASS or FAIL with explanation.

If all PASS: Output "APPROVED" followed by the unchanged content.
If any FAIL: Output "REVISION NEEDED" followed by specific instructions for what to fix.

When a step fails, don't just re-run the same prompt. Provide feedback about what went wrong so the model can correct course.

Retry prompt: "Your previous response didn't meet the requirements. Specifically: - The JSON was missing the 'confidence' field in 2 of 5 findings - Finding #3 made a claim not supported by the input data Please regenerate, fixing these specific issues. Here's the original prompt again: [ORIGINAL PROMPT]"

For production systems, have a fallback: if the primary model fails, try a different model or a simplified version of the prompt. This ensures your pipeline doesn't completely break on edge cases.

As chains progress, accumulated context can overwhelm the model. Prune aggressively between steps. Instead of passing the full output of every previous step, pass a summary or just the specific fields the next step needs.

The most robust chains have 3 layers: the happy path (works as expected), validation (catches errors), and fallback (handles failures gracefully). Build all three.

Prompt Templates

Chain Error Recovery

Recovers from a failed chain step by providing specific error feedback.

The previous step in my workflow produced this output:

[PASTE FAILED OUTPUT]

The problems are:
[LIST SPECIFIC PROBLEMS]

The original task was:
[ORIGINAL PROMPT]

Please produce a corrected output that:
1. Fixes the specific problems listed above
2. Meets all original requirements
3. Uses only information from the provided input — do not add fabricated details

Context Pruner

Prunes accumulated chain context to prevent overflow and attention degradation.

I have accumulated outputs from [N] steps in a workflow chain. Summarize only the information needed for the next step.

Next step objective: [WHAT THE NEXT STEP NEEDS TO DO]

Accumulated context:
[PASTE ALL PREVIOUS OUTPUTS]

Extract and summarize ONLY the information relevant to the next step objective. Discard everything else. Be concise but don't lose critical details.

Test Your Knowledge

Knowledge Check

1 / 2

What is "hallucination injection" in a prompt chain?

Key Takeaways

  • Every chain needs error handling — format failures, hallucinations, and quality degradation are common
  • Validation gates between steps catch errors before they compound
  • Retry with specific feedback, not just the same prompt again
  • Prune context aggressively between steps to prevent overflow and attention degradation
  • Production chains need three layers: happy path, validation, and fallback