Skip to main content

04. Agentic Workflows

Error Handling, Recovery and Feedback Loops

Agentic systems fail often: timeouts, bad args, business denials, flaky tools. Error handling classifies failures; recovery retries, replans, compensates, or escalates; feedback loops feed those outcomes into the next Observe–Think–Act step.

What you will learn

  • Classify agent errors before choosing a recovery
  • Apply retry, replan, compensate, and escalate patterns
  • Wire failures as structured observations (feedback)
  • Bound recovery so agents cannot thrash forever
  • Answer interview questions with short, solid answers
Detect, recover, and feedback stages for agent errors
Interview staple: classify the failure, recover deliberately, feed the result forward.

Definition

Error handling is detecting and classifying failures in the agent loop (model, tools, validators, humans timing out). Recovery is the controlled response: retry, alternate tool, replan, compensate a partial write, or escalate. Feedback loops ensure the failure (and recovery outcome) becomes an observation that changes the next decision β€” not a swallowed exception.

In Agentic AI, silent failures are worse than loud ones: the model may invent success if the runtime returns empty noise.

Introduction

This lesson closes Module 04 (Agentic Workflows). You have workflows, ReAct, patterns, and HITL β€” errors are how those graphs behave under stress.

Module 05 will cover design patterns that often encode these recovery moves (reflection, routing, retries).

Why this matters

  • Tool APIs are flaky; agents amplify retries
  • Wrong retries cause duplicate side effects
  • Users need honest status, not stuck spinners
  • Evals and on-call need typed failure codes

Error classes

Do not β€œretry everything.” Classification drives the strategy.

Transient, validation, business, and fatal error classes
Transient β†’ backoff. Validation β†’ fix args. Business β†’ replan. Fatal β†’ stop.
  • Transient - timeouts, 429, brief 5xx β†’ retry with backoff (if idempotent)
  • Validation - schema/arg errors β†’ repair args or choose another tool
  • Business - not found, denied, conflict β†’ replan path or ask user
  • Fatal / unknown - auth broken, corruption β†’ stop and escalate
  • Model errors - refusals, empty tool calls β†’ reflect or degrade gracefully

Recovery strategies

  • Retry - same action with backoff/jitter and max attempts
  • Alternate - different tool or endpoint for the same intent
  • Replan - change task subtree after reflection
  • Compensate - undo or offset a partial side effect (saga-style)
  • Degrade - return partial useful result with caveats
  • Escalate - HITL or on-call with full context pack

Feedback loops

A feedback loop closes when error/recovery results update state the next Think step can see.

Act to error to strategy to feedback into the next think step
Typed error observations beat empty failures the model must guess about.
  • Immediate loop - observation includes error code + hint
  • Task loop - mark Failed; scheduler unlocks replan tasks
  • Memory loop - store episodic β€œwhat failed / what worked” carefully
  • Ops loop - metrics/alerts on error rates by tool and class
  • Learning loop - offline evals from failure fixtures (not silent prod fine-tunes)

Idempotency and compensation

  • Prefer idempotent writes when autonomous retries exist
  • Use idempotency keys for payments/ticket creates
  • Track side-effect checkpoints to know what to compensate
  • Never blind-retry non-idempotent POSTs

Bounds and anti-thrash rules

  • Max retries per tool and per error class
  • Max replans per goal; then escalate
  • Circuit break a tool after repeated failures
  • Detect identical Action retries with no new Observation
  • Budget: stop when tokens/time/cost caps hit

Program / implementation notes

Normalize tool failures to {ok:false, class, code, retryable, message, details}. Map HTTP statuses in adapters. Unit-test recovery policy tables.

  • Keep stack traces out of model context; keep codes + short hints in
  • Correlate tool_call_id across failure and recovery attempts
  • Surface user-safe status separately from internal diagnostics
  • Module 04 complete - next: Design Patterns (Module 05)

Advantages and limitations

  • Advantage: higher success under flaky dependencies
  • Advantage: safer side effects with classify-then-act recovery
  • Advantage: better debugging via typed feedback
  • Limitation: over-retry increases cost and duplicate risk
  • Limitation: compensation logic can be harder than the happy path
  • Limitation: noisy errors without classification confuse the LLM

Key points

  • Classify errors: transient, validation, business, fatal.
  • Recover with retry, alternate, replan, compensate, degrade, or escalate.
  • Feed structured failure observations back into the loop.
  • Idempotency gates which failures may retry.
  • Bound retries/replans; circuit-break bad tools.
  • Never let empty errors become hallucinated success.

FAQs

Should the LLM decide all retries?

No. Put retry/backoff policy in the runtime for transient classes; let the model replan when business logic must change.

Is a feedback loop only for ML training?

No. Here it means closing the control loop so failures update state for the next decision β€” plus ops metrics and optional offline learning.

What is compensation in agent recovery?

An intentional undo/offset after a partial successful side effect when the overall goal cannot complete cleanly.

How do HITL and errors interact?

After fatal or ambiguous failures, escalate to HITL with the error class, attempts so far, and proposed next options.

Interview questions

  1. How should AI agents handle tool errors?

    Answer Return typed error observations, classify them, apply a bounded recovery policy, and never pretend success when the tool failed.

  2. Transient vs business errors?

    Answer Transient failures may succeed on retry (timeouts/429). Business errors need a different plan (not found/denied), not blind retries.

  3. What recovery strategies are common?

    Answer Retry with backoff, alternate tools, replan, compensate partial writes, degrade gracefully, or escalate to a human.

  4. What is a feedback loop in this context?

    Answer Routing error and recovery outcomes back into agent state/observations (and metrics) so the next think step adapts.

  5. How do you prevent retry storms?

    Answer Cap attempts, use jittered backoff, require idempotency for write retries, and circuit-break repeatedly failing tools.

  6. Why are structured error codes important for LLM agents?

    Answer They give the model a reliable signal to replan; raw empty failures encourage hallucination or useless thrashing.

  7. Give a concrete recovery example.

    Answer get_order 503 β†’ retry twice with backoff; still failing β†’ circuit-break orders API and escalate with partial cart context.

Related topics

Practice task

Complete each numbered practice step in a notebook or doc before opening the next lesson.

  1. Build a table of 8 failures and label each error class.
  2. For each class, write the allowed recovery actions and max attempts.
  3. Design the JSON shape of a tool error observation.
  4. Sketch Act -> Error -> Strategy -> Feedback for a failed payment capture.
  5. Answer interview questions 1, 2, and 5 in two sentences each.