🤖 The Modern AI Developer · Context Engineering: Feed the AI Exactly the Right Info

How AI Agents Self-Correct: Feed Errors Back and Retry

Feed the error back so it can self-correct

一句话先懂 · TL;DR

Learn the self-correction loop for AI coding: run the code, show the model the error, let it fix and rerun, plus how to stop it from retrying forever.

Errors Aren't Scary—Not Seeing Them Is

It's perfectly normal for the AI's first pass of code to not run, or its answer to have holes. The real problem is: it has no idea it's wrong—because the error never made it back onto its desk.

🔆It's like a student who turns in homework but never sees the teacher's corrections—they'll make the same mistake again. Feeding the error back is like handing the red-pen notes to it.

The Self-Correction Loop: Run → See Error → Fix → Run Again

The way to let the AI fix itself is just a loop: have it produce → actually run it → feed the error back verbatim → have it revise accordingly → run again… until it passes.

code = llm("写一个函数:判断回文")
for _ in range(3):                  # 最多试 3 轮
    err = run(code)                 # 真的去跑
    if err is None:
        break                       # 通过了就停
    code = llm(f"这段代码报错了:\n{err}\n请修复:\n{code}")
💡There's just one key move: feed the real error back verbatim. The more specific the error message, the more precisely it can pinpoint the problem.

Don't Let It Spin Forever

The loop is handy, but you need to give it a brake—set a cap on how many rounds it can try, or "stop after two rounds with no progress."

⚠️Without a stop condition, the AI may spin forever between fixing A and breaking B, then fixing B and breaking A—burning money and time for nothing. Always leave an exit.

自测 · 学完检查一下

想真正动手做题、记进度、攒连胜?到互动课里练。

What's the core move of "let the AI fix itself by looking at the error"?

答案:Feed the post-run error back verbatim so it can revise accordingly

The key to self-correction is sending the real error back into its context, so it can see what's wrong and fix it.

Judge: if you don't feed the error back to the AI, it usually can't tell what was wrong with its last version.

答案:True

The AI can't see what isn't on the desk. Without the error fed back, it's like a student who never saw the corrections—it repeats the mistake.

Which feedback is most likely to help the AI actually fix the code?

答案:"Error on line 8: variable total is undefined"

A specific, locatable error (which line, what error) lets the model fix precisely; vague blame provides no usable information.

The more ___ the feedback you give back (e.g. with line numbers and the exact error), the more precisely the AI can locate and fix the problem.

答案:specific

The more specific the feedback, the better the model can pinpoint the problem. A vague "wrong, rewrite" is almost no information at all.

Judge: a self-correction loop should keep running until the AI gets it right, with no cap set.

答案:False

You must set a stop condition (e.g. a max of N rounds). Otherwise the AI may spin forever between two errors, wasting time and money.

To keep the AI from spinning forever in the loop, set a stop condition, e.g. "try at most a few ___."

答案:rounds

Setting a cap on the loop (try at most a few rounds) is the essential brake that prevents an infinite loop from burning resources.

想边练边学,而不只是读?

到互动课里答题、记进度、攒连胜——游客即可试学,无需注册。

进入互动课程 →

Learn something new — don't miss updates

New courses, features and learning tips. Occasional emails, unsubscribe anytime.