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.
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}")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."
自测 · 学完检查一下
想真正动手做题、记进度、攒连胜?到互动课里练。
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.