Lesson 6 of 9 · 25m
Failure Recovery and Loop Control
Handle flaky tools, partial state, retries, cancellation, and contradictory evidence without letting the agent spin or repeat side effects.
Real environments are not demo environments. APIs time out after performing the operation. Browsers render half a page. Tickets get edited while the agent is thinking. The model is told the operation failed even though the world changed. Your loop control determines whether this is a recoverable nuisance or a repeated-write incident.
Failure is ambiguous by default
A timeout is not proof of failure. A 500 is not proof nothing happened. Even a clean error response may describe only one part of a distributed workflow. Encode uncertainty explicitly:
tool outcome:
success_verified mutation confirmed by independent read
success_reported tool claims success, not independently verified
failed_before_write validation rejected it; no side effect expected
unknown timeout, disconnect, inconsistent response
failed_after_write write occurred, downstream step failedRetry policy depends on this classification. Blindly retrying unknown is how duplicate charges happen.
Reconcile before retrying
After an uncertain mutation, inspect state:
if result.status == "unknown":
current = get_refund_by_idempotency_key(key)
if current:
mark_step_done(current)
else:
retry_with_same_idempotency_key(key)This is why idempotency keys matter. They give you a handle with which to reconcile the world. Without one, you are left guessing whether absence of evidence means absence of side effects.
Detect loops structurally
Do not rely on the model to notice it is stuck. Track repeated states, repeated tool calls, and lack of progress:
signature = hash((state_digest, next_action.name, canonical_args))
if signatures.count(signature) >= 2:
stop_or_escalate("repeated identical action without progress")Progress is not "messages generated." It is a measurable reduction in remaining work or uncertainty.
Cancellation is part of correctness
Users will cancel runs. Deploys will kill workers. Cloud providers will reboot machines. Persist checkpoints and design compensating actions:
step 1 create draft order reversible
step 2 authorize payment externally visible
step 3 send confirmation cosmetic but user-visibleIf cancellation lands after step 2, your resume logic must know whether to continue, compensate, or escalate. A process manager that simply restarts the whole script is not recovery; it is roulette.
Contradictions require escalation
Sometimes the system of record says one thing and a downstream service says another. Agents should not resolve authoritative conflicts by averaging them in prose. Record both observations, identify which source is authoritative for which field, and stop if the conflict affects a consequential decision.
Field notes
- Add jittered backoff for retryable infrastructure failures, but not for validation failures or policy denials.
- Every retry of a mutating operation should use the same idempotency key, never a fresh one.
- Treat stale reads as normal. Record timestamps or versions with important observations.
- A graceful stop with evidence beats an energetic agent improvising around ambiguous state.
Was this lesson helpful?