AI Engineering & LLMs (50 Qs)•Part 3: Agentic Workflows, Function Calling & MCP
Tool Execution Resilience: Timeout Budgets, Exponential Backoff & Circuit Breakers
1. Flaky Real-World Tooling in Autonomous Agents
When autonomous agents interact with external tools (GitHub API, Slack webhooks, Cloud APIs, databases), tools will inevitably fail:
- Rate limiting (
HTTP 429 Too Many Requests). - Transient 5xx server gateway errors.
- Network timeouts and hangs.
If an agent has no error resilience, an uncaught API timeout either crashes the entire agent loop or causes the LLM to hallucinate that the operation succeeded!
2. The Agent Circuit Breaker Pattern
sql
[ Tool Call Request ]
│
▼
[ Circuit State ]
┌────────────────────────────────────────────────────────┐
│ CLOSED (Normal): Pass through to external API │
│ OPEN (Tripped): Fail fast! Return synthetic fallback │
│ HALF-OPEN: Send canary test probe to verify recovery │
└────────────────────────────────────────────────────────┘
3. Resilient Tool Execution Implementation in Python
python
import asyncio
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
class ToolTimeoutError(Exception): pass
class ResilientToolWrapper:
def __init__(self, tool_func, timeout_seconds: float = 10.0):
self.tool_func = tool_func
self.timeout_seconds = timeout_seconds
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=10),
retry=retry_if_exception_type((TimeoutError, ConnectionError))
)
async def execute(self, *args, **kwargs):
try:
return await asyncio.wait_for(
self.tool_func(*args, **kwargs),
timeout=self.timeout_seconds
)
except asyncio.TimeoutError:
raise ToolTimeoutError(f"Tool execution timed out after {self.timeout_seconds}s")
# In agent scratchpad: Return clean error message so the LLM can self-correct!
async def safe_agent_tool_caller(tool_wrapper, *args):
try:
return await tool_wrapper.execute(*args)
except Exception as e:
return f"Tool Execution Failed: {str(e)}. Try an alternative approach."