Looking for bot-to-bot reply-loop protection? See Bot-to-Bot Loop Protection — a separate gateway-layer primitive that caps how many exchanges a pair of bots can trade.
Quick Start
1
It's already on
2
Tune the thresholds
For power users, you can customize thresholds after agent creation:
3
Turn it off
How It Works
User Interaction Flow
Here’s what happens when an agent repeatedly calls the same tool: The agent experiences escalating resistance: warnings first, then blocked execution, then complete halt.Tool Classification
Loop Guard categorizes tools into two buckets with different thresholds: Idempotent tools (safe to repeat):read_file, list_files, search_files, web_search, get_memory, git_status, git_log, db_query, etc.
Mutating tools (state-changing): write_file, edit_file, delete_file, execute_code, shell, git_commit, git_push, sql_insert, install_package, etc.
Configuration Options
GuardAction values:
ALLOW, WARN, BLOCK, HALT
LoopGuardDecision fields: action, code, message, metadata
What happens at each threshold
Common Patterns
Polling a slow status endpoint
Strict mode for production database agents
Observability with stats
Relationship to Loop Detection
Loop Detection is also always-on as of PR #3005 — everyAgent runs result-aware name + args + result-hash detection in the same tool-execution path. The two are complementary: Loop Guard counts per-turn tool calls with idempotent-vs-mutating thresholds, while Loop Detection catches repeated identical fingerprints at any frequency and back-fills result hashes so polling with progress is not flagged. Both fire through the same blocked_result path, so trace spans, stream events, and AFTER_TOOL hooks stay consistent regardless of which one triggers.
Best Practices
Trust the defaults
Trust the defaults
The default thresholds (5/8/12 for idempotent, 3/5/7 for mutating) work well for most agents. Only customize when you have specific use cases like status polling or strict production environments.
Tune thresholds, not the on/off switch
Tune thresholds, not the on/off switch
Resist the urge to disable Loop Guard entirely. Instead, adjust thresholds to match your agent’s workflow. A monitoring agent might need higher idempotent thresholds, but even monitoring agents can benefit from mutating tool limits.
Name custom tools so the heuristic works
Name custom tools so the heuristic works
If your custom tool isn’t in the explicit
IDEMPOTENT_TOOLS or MUTATING_TOOLS sets, name it descriptively. Tools named check_status, read_config, or search_logs will be classified as idempotent automatically.Use get_stats() for observability
Use get_stats() for observability
Monitor your agents’ tool usage patterns with
agent._loop_guard.get_stats(). High tool counts might indicate the agent is struggling with a task and needs different instructions or tools.Related
Loop Detection
Agent Autonomy

