|
| 1 | +# Error Recovery Hooks |
| 2 | + |
| 3 | +Keep the LLM investigating when tools fail instead of giving up with a partial result. |
| 4 | + |
| 5 | +## Problem |
| 6 | + |
| 7 | +When a shell command returns an error or a file operation hits a permission denial, the LLM tends to stop and apologize rather than trying a different approach. This produces incomplete results in agentic workflows where resilience matters. |
| 8 | + |
| 9 | +## Solution |
| 10 | + |
| 11 | +Use the SDK's hooks system (`on_post_tool_use`, `on_error_occurred`) to classify tool results by category and append continuation instructions that nudge the LLM to keep going. |
| 12 | + |
| 13 | +```python |
| 14 | +from enum import Enum |
| 15 | + |
| 16 | + |
| 17 | +class ToolResultCategory(str, Enum): |
| 18 | + SHELL_ERROR = "shell_error" |
| 19 | + PERMISSION_DENIED = "permission_denied" |
| 20 | + NORMAL = "normal" |
| 21 | + |
| 22 | + |
| 23 | +class SDKErrorCategory(str, Enum): |
| 24 | + CLIENT_ERROR = "client_error" # 4xx — not retryable |
| 25 | + TRANSIENT = "transient" # 5xx / timeout |
| 26 | + NON_RECOVERABLE = "non_recoverable" |
| 27 | + |
| 28 | + |
| 29 | +# Phrases that signal permission issues in tool output |
| 30 | +PERMISSION_DENIAL_PHRASES = [ |
| 31 | + "permission denied", |
| 32 | + "access denied", |
| 33 | + "not permitted", |
| 34 | + "operation not allowed", |
| 35 | + "eacces", |
| 36 | + "eperm", |
| 37 | + "403 forbidden", |
| 38 | +] |
| 39 | + |
| 40 | +SHELL_ERROR_PHRASES = [ |
| 41 | + "command not found", |
| 42 | + "no such file or directory", |
| 43 | + "exit code", |
| 44 | + "errno", |
| 45 | + "traceback", |
| 46 | +] |
| 47 | + |
| 48 | +CONTINUATION_MESSAGES = { |
| 49 | + ToolResultCategory.SHELL_ERROR: ( |
| 50 | + "\n\n[SYSTEM NOTE: This command encountered an error. " |
| 51 | + "This does NOT mean you should stop. Retry with different " |
| 52 | + "arguments, try a different tool, or move on.]" |
| 53 | + ), |
| 54 | + ToolResultCategory.PERMISSION_DENIED: ( |
| 55 | + "\n\n[SYSTEM NOTE: Permission was denied for this specific " |
| 56 | + "action. Continue using alternative approaches.]" |
| 57 | + ), |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +def classify_tool_result(tool_name: str, result_text: str) -> ToolResultCategory: |
| 62 | + result_lower = result_text.lower() |
| 63 | + if any(phrase in result_lower for phrase in PERMISSION_DENIAL_PHRASES): |
| 64 | + return ToolResultCategory.PERMISSION_DENIED |
| 65 | + if any(phrase in result_lower for phrase in SHELL_ERROR_PHRASES): |
| 66 | + return ToolResultCategory.SHELL_ERROR |
| 67 | + return ToolResultCategory.NORMAL |
| 68 | + |
| 69 | + |
| 70 | +def classify_sdk_error(error_msg: str, recoverable: bool) -> SDKErrorCategory: |
| 71 | + error_lower = error_msg.lower() |
| 72 | + if any(kw in error_lower for kw in ("timeout", "503", "502", "429", "retry")): |
| 73 | + return SDKErrorCategory.TRANSIENT |
| 74 | + if any(kw in error_lower for kw in ("401", "403", "404", "400", "422")): |
| 75 | + return SDKErrorCategory.CLIENT_ERROR |
| 76 | + return SDKErrorCategory.TRANSIENT if recoverable else SDKErrorCategory.NON_RECOVERABLE |
| 77 | +``` |
| 78 | + |
| 79 | +## Hook Registration |
| 80 | + |
| 81 | +Wire the classifiers into the SDK's hook system: |
| 82 | + |
| 83 | +```python |
| 84 | +def on_post_tool_use(input_data, env): |
| 85 | + """Append continuation hints to failed tool results.""" |
| 86 | + tool_name = input_data.get("toolName", "") |
| 87 | + result = str(input_data.get("toolResult", "")) |
| 88 | + category = classify_tool_result(tool_name, result) |
| 89 | + if category in CONTINUATION_MESSAGES: |
| 90 | + return {"toolResult": result + CONTINUATION_MESSAGES[category]} |
| 91 | + return None |
| 92 | + |
| 93 | + |
| 94 | +def on_error_occurred(input_data, env): |
| 95 | + """Retry transient errors, skip non-recoverable ones gracefully.""" |
| 96 | + error_msg = input_data.get("error", "") |
| 97 | + recoverable = input_data.get("recoverable", False) |
| 98 | + category = classify_sdk_error(error_msg, recoverable) |
| 99 | + if category == SDKErrorCategory.TRANSIENT: |
| 100 | + return {"errorHandling": "retry", "retryCount": 2} |
| 101 | + return { |
| 102 | + "errorHandling": "skip", |
| 103 | + "userNotification": "Error occurred — continuing investigation.", |
| 104 | + } |
| 105 | +``` |
| 106 | + |
| 107 | +## Tips |
| 108 | + |
| 109 | +- **Tune the phrase lists** for your domain — add patterns from your actual tool output. |
| 110 | +- **Log classified categories** so you can track how often each failure mode fires and whether the LLM actually recovers. |
| 111 | +- **Cap continuation depth** — if the same tool fails 3+ times in a row, let the LLM give up rather than looping. |
| 112 | +- The `SYSTEM NOTE` framing works well because the LLM treats it as authoritative instruction rather than user commentary. |
| 113 | + |
| 114 | +## Runnable Example |
| 115 | + |
| 116 | +See [`recipe/error_recovery_hooks.py`](recipe/error_recovery_hooks.py) for a complete working example. |
0 commit comments