Lesson 18 / 25

Tool Errors and Thrashing

Return useful errors to the model but stop when failures keep stacking up.

Let it recover, but only so far

When a tool fails, send the error back as the tool result (flagged as an error) so the model can adjust, for example fix a wrong argument. But if errors keep coming, the model is thrashing. Count consecutive failures, and after a small number stop and escalate with the last error attached.

Returning an error result

The is_error flag tells the model the call failed. The consecutive counter resets on any success.

try:
    content, ok = run_tool(block.name, block.input), True
except Exception as exc:
    content, ok = f"Tool failed: {exc}", False

results.append({"type": "tool_result", "tool_use_id": block.id,
                "content": content, "is_error": not ok})
failures = 0 if ok else failures + 1
if failures >= 3:
    return escalate("3 consecutive tool failures", last_error=content)

Quick check: Why count consecutive tool failures?

  • To detect a model that is thrashing and not recovering
  • To make tools slower
  • Because errors are good
  • To hide failures
Answer

To detect a model that is thrashing and not recovering — A run of failures without a success means more retries are unlikely to help.