59 lines
1.4 KiB
Markdown
59 lines
1.4 KiB
Markdown
# Phase 3: Error Handling & Validation
|
|
|
|
**Objective**: Handle errors and edge cases in agent workflows
|
|
|
|
## Key Error Scenarios
|
|
|
|
### 1. Tool Execution Failures
|
|
|
|
```python
|
|
def execute_tool_safely(tool_name: str, tool_input: Dict) -> Dict:
|
|
"""Execute tool with error handling."""
|
|
try:
|
|
tool_function = TOOL_FUNCTIONS[tool_name]
|
|
result = tool_function(**tool_input)
|
|
return {"success": True, "data": result}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Tool {tool_name} failed: {e}")
|
|
return {
|
|
"success": False,
|
|
"error": str(e),
|
|
"message": "Tool execution failed. Please try again."
|
|
}
|
|
```
|
|
|
|
### 2. Safety Refusals
|
|
|
|
```python
|
|
if response.stop_reason == "refusal":
|
|
logger.warning("Agent refused request")
|
|
# Don't retry - respect safety boundaries
|
|
return {"error": "Request cannot be completed"}
|
|
```
|
|
|
|
### 3. Max Turns Exceeded
|
|
|
|
```python
|
|
if turn >= max_turns:
|
|
logger.warning("Agent exceeded max turns")
|
|
return {
|
|
"error": "Task too complex",
|
|
"partial_progress": extract_progress(messages)
|
|
}
|
|
```
|
|
|
|
### 4. Invalid Tool Name
|
|
|
|
```python
|
|
# With strict mode, tool names are guaranteed valid
|
|
# But external factors can cause issues
|
|
if tool_name not in TOOL_FUNCTIONS:
|
|
logger.error(f"Unknown tool: {tool_name}")
|
|
return {"error": f"Tool {tool_name} not implemented"}
|
|
```
|
|
|
|
## Output
|
|
|
|
Robust error handling for production agent workflows.
|