Quick Start
How It Works
| Decision | Effect | User sees |
|---|---|---|
ALLOW | Tool runs normally | Normal result |
WARN | Tool runs; warning logged | Result + _loop_guard warning metadata |
BLOCK | Tool execution replaced | {"error": "[loop-guard] ...", "loop_blocked": True} |
HALT | Non-retryable exception raised | ToolExecutionError propagates out |
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
| Option | Type | Default | Description |
|---|---|---|---|
enabled | bool | True | Enable/disable Loop Guard entirely |
idempotent_warn_threshold | int | 5 | Warning threshold for idempotent tools |
idempotent_block_threshold | int | 8 | Block threshold for idempotent tools |
idempotent_halt_threshold | int | 12 | Halt threshold for idempotent tools |
mutating_warn_threshold | int | 3 | Warning threshold for mutating tools |
mutating_block_threshold | int | 5 | Block threshold for mutating tools |
mutating_halt_threshold | int | 7 | Halt threshold for mutating tools |
max_time_per_turn | float | 120.0 | Maximum seconds per turn before timeout |
no_progress_warn | int | 4 | Warning threshold for no-progress detection |
no_progress_halt | int | 8 | Halt threshold for no-progress detection |
ALLOW, WARN, BLOCK, HALT
LoopGuardDecision fields: action, code, message, metadata
What happens at each threshold
| Decision | Pre-execution effect | Post-execution effect | User sees |
|---|---|---|---|
ALLOW | Tool runs normally | Counter incremented | Normal result |
WARN | Tool runs; warning logged | Warning appended/attached to result | _loop_guard key in dict result, or [loop-guard] ... suffix on string result |
BLOCK | Tool not executed | Counter still incremented | Tool result replaced with {"error": "[loop-guard] ...", "loop_blocked": True} |
HALT | ToolExecutionError raised, is_retryable=False | N/A | Exception propagates out of agent.chat() / agent.start() |
Common Patterns
Polling a slow status endpoint
Strict mode for production database agents
Observability with stats
Relationship to Loop Detection
Loop Guard and Loop Detection are complementary features with different purposes:| Feature | Opt-in? | Where | What it catches |
|---|---|---|---|
| Loop Detection | Yes — loop_detection=True | Identical-call, no-progress poll, ping-pong patterns | |
| Loop Guard | No — always-on for every Agent | Per-turn tool-call counts with idempotent-vs-mutating classification and graduated warn→block→halt responses |
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

