AI Engineering & LLMs (50 Qs)•Part 3: Agentic Workflows, Function Calling & MCP
Autonomous Agent Guardrails: Runaway Loop Prevention, Quotas & Recursion Limits
1. The Multi-Thousand Dollar Runaway Loop Incident
Without strict safeguards, autonomous agents can enter runaway loops:
- An agent encounters an unexpected error and attempts 10,000 continuous retries overnight.
- Two collaborating agents get stuck in recursive conversational ping-pong.
- An agent loops continuously over a 5,000-file directory, burning hundreds of dollars in API tokens per hour.
2. Enterprise Safeguard Layers
vbnet
Agent Request
│
┌───────────────────────────────┴───────────────────────────────┐
▼ ▼ ▼
[ Max Step Guard ] [ Cost Budget Guard ] [ Loop Cycle Detector ]
Max 15 iterations / task Max $2.00 token spend / task Identifies repetitive
Hard termination on breach Hard termination on breach action signatures
3. Production Multi-Layer Guardrail Implementation
python
class AgentBudgetGuard:
def __init__(self, max_steps: int = 15, max_cost_usd: float = 2.00):
self.max_steps = max_steps
self.max_cost_usd = max_cost_usd
self.current_steps = 0
self.current_cost_usd = 0.0
self.action_history = []
def record_step(self, action_signature: str, token_cost_usd: float):
self.current_steps += 1
self.current_cost_usd += token_cost_usd
self.action_history.append(action_signature)
# 1. Step Quota Check
if self.current_steps > self.max_steps:
raise RuntimeError(f"Terminated: Exceeded maximum allowed step limit ({self.max_steps}).")
# 2. Budget Quota Check
if self.current_cost_usd > self.max_cost_usd:
raise RuntimeError(f"Terminated: Exceeded financial cost budget (${self.max_cost_usd:.2f}).")
# 3. Cycle Detection: Check if the last 3 actions are identical
if len(self.action_history) >= 3 and len(set(self.action_history[-3:])) == 1:
raise RuntimeError("Terminated: Detected infinite cyclic tool invocation loop.")