Overseas access: www.kdjingpai.com
Bookmark Us
Current Position:fig. beginning " Course materials

12-Factor Agents 9. Compressing error messages into context windows

2025-07-22 22

This is a small point, but worth mentioning. One of the benefits of an agent is "self-healing" - for a short task, a large language model (LLM) may call a tool that fails. There is a good chance that a good LLM will be able to read an error message or stack trace and figure out what needs to be changed in subsequent tool calls.

Most frameworks implement this, but you can also implement just this without having to implement the other 11 elements. Here is an example:

thread = {"events": [initial_message]}
while True:
next_step = await determine_next_step(thread_to_prompt(thread))
thread["events"].append({
"type": next_step.intent,
"data": next_step,
})
try:
result = await handle_next_step(thread, next_step) # 我们的 switch 语句
except Exception as e:
# 如果我们得到一个错误,我们可以将它添加到上下文窗口中再试一次
thread["events"].append({
"type": 'error',
"data": format_error(e),
})
# 在这里循环,或者做任何其他事情来尝试恢复

You may want to implement an errorCounter for specific tool calls, limit the number of attempts for a single tool to about 3, or use any other logic that fits your use case.

consecutive_errors = 0
while True:
# ... 现有代码 ...
try:
result = await handle_next_step(thread, next_step)
thread["events"].append({
"type": next_step.intent + '_result',
data: result,
})
# 成功!重置错误计数器
consecutive_errors = 0
except Exception as e:
consecutive_errors += 1
if consecutive_errors < 3:
# 进行循环并重试
thread["events"].append({
"type": 'error',
"data": format_error(e),
})
else:
# 中断循环,重置部分上下文窗口,上报给人类,或做任何你想做的事
break
}
}

When a certain continuous error threshold is reached, it may be a good time to Report to Humanity , either by modeling decisions or by deterministically taking over the control flow.

12-Factor Agents 9. Compression of error messages into context windows - 1

 

Pros:

  1. self-healing: A large language model can read the error messages and find out what needs to be changed in subsequent calls to the tool.
  2. durability: even if a tool invocation fails, the intelligence can continue to run

I'm sure you'll find that if you overuse this method, your intelligences can get out of control and may repeat the same mistakes over and over again.

At this point.Element 8 - Take Control of Your Flow of Control cap (a poem) Element 3 - Take Control of Your Contextual Builds comes in handy - you don't need to just put the original error message back, you can completely refactor its representation, remove previous events from the context window, or take whatever deterministic approach you think works to get the smart body back on track.

But the number one way to prevent mistakes from getting out of hand is to adopt Element 10 - Small, Focused Intelligence The

Recommended

Can't find AI tools? Try here!

Just type in the keyword Accessibility Bing SearchYou can quickly find all the AI tools on this site.

inbox

Contact Us

Top

en_USEnglish