Dayward AIWeChat Mini Program
Build Your Own Coding Agent in 21 Days
Week 1 · D6🔒 LockedAbout 4 hours

Error Handling and Self-Correction: Feeding Failures Back, Backoff Retries, Loop Detection, and Cancellation

Clean up six real failure categories in one pass: tool failure, invalid arguments, gateway rate limiting, command timeout, stream disconnection, and a model spinning in circles. One principle only — feed back any error the model can fix itself, surface to the user only what it can't — and the user can always press cancel.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Split agent runtime errors into feed-back-for-self-correction, retry-with-backoff, and surface-directly, and explain the criteria
  2. Implement backoff retry with jitter, and explain why a tool failure shouldn't retry the whole turn
  3. Implement a cancellation signal that travels all the way from a keypress to the subprocess, and detect a model spinning in circles

Over five days we met several failure classes piecemeal; today we handle them together. When you finish, scroll back up and tick the three goals off.

Plain-Language Walkthrough

What to do after a mistake: handing the error back beats fixing it for them

A new hire will hit a wall on their first attempt: they edit a file and the tests go red, they type a command with a wrong flag.

Their mentor has two options: fix it for them, or paste the error verbatim with one sentence — "look, this line says the indentation does not match." The first is more efficient today, and they still cannot do it next time.

With an Agent it is more extreme: "fix it for it" is not an option at all. You can change files and commands; you cannot change what it wants to do next, which depends only on what it reads in its context. So there is exactly one way to handle an Agent's errors: turn the error into text it will read next turn. That is tool result feedback, in use since day three, and today it becomes a discipline.

The discipline has a counterintuitive corollary: a tool's failure message is not a log for humans but a behavioural specification for the model. The same failure in two wordings produces entirely different behavior:

TextText
version 1: edit_file failed: no match
version 2: that old_string cannot be found in src/calc.js; nothing was changed.
           Commonly the indentation or newlines differ, or the file already changed -
           read_file first and retry.

With the first, the model resends the identical call (assuming a transient blip). With the second, it reads the file first. The difference between those two lines is the difference between an Agent that can get itself out of trouble and one that cannot — and it lives not in the model but in the sentence you wrote. Today extends the discipline to six failure classes, and admits its limits.

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • How do you classify errors at agent runtime? Which ones go back to the model to self-correct, and which surface to the user?Agent 运行时的错误怎么分类?哪些该回灌给模型让它自纠,哪些该直接报给用户?
    Common in ChinaCommon overseasBasic#error-handling#agent-loop

    🔒 Unlock this chapter to see the answer keys.

  • How would you implement retries and backoff? Why add jitter, and which errors must never be retried?重试与退避你会怎么实现?为什么要加抖动?哪些错误绝对不该重试?
    Common in ChinaCommon overseasIntermediate#retry#backoff

    🔒 Unlock this chapter to see the answer keys.

  • When the user presses cancel, which layers must the cancellation signal reach before things have really stopped?用户按下取消,你的取消信号要穿过哪几层才算真的停下来?
    Common in ChinaCommon overseasDeep dive#cancellation#subprocess

    🔒 Unlock this chapter to see the answer keys.

Comments