This article covers implementing robust Agent tool calling loops with parameter parsing error handling, timeout retry, permission checks, and result validation. Complete error handling architecture with Python examples.
Tool calling loops are the core execution mechanism of Agents, requiring handling various error conditions. This article introduces robust error handling architecture.
from typing import Any, Callable
from dataclasses import dataclass
import asyncio
class ToolError(Exception):
pass
class ParameterError(ToolError):
pass
@dataclass
class ToolResult:
success: bool
result: Any = None
error: str = ""
retryable: bool = False
class ToolExecutor:
def __init__(self, max_retries: int = 3):
self.max_retries = max_retries
async def execute_with_retry(
self,
tool_func: Callable,
parameters: dict
) -> ToolResult:
for attempt in range(self.max_retries):
try:
validated_params = self._validate_params(parameters)
if asyncio.iscoroutinefunction(tool_func):
result = await tool_func(**validated_params)
else:
result = tool_func(**validated_params)
return ToolResult(success=True, result=result)
except ParameterError as e:
return ToolResult(success=False, error=str(e), retryable=False)
except TimeoutError:
if attempt < self.max_retries - 1:
await asyncio.sleep(2 ** attempt)
return ToolResult(success=False, error="Max retries exceeded")
| Error Type | Strategy | Retry |
|---|---|---|
| Parameter error | Return validation error | No |
| Permission error | Return permission error | No |
| Timeout | Exponential backoff | Up to 3 |
| Server error | Wait and retry | Up to 3 |
Auto-repair applied and deterministic inspection checks passed.
逻辑完整正确
代码示例验证通过