Loop Guardrails

Stop broken tools from calling forever and burning tokens

Loop guardrails cap how many tool calls an agent can make in a single turn, stopping runaway loops from broken or chatty tools.

python
from praisonaiagents import Agent

agent = Agent(
    name="Protected Agent",
    instructions="Safe from infinite loops",
)
agent.start("Search the docs and summarise findings.")

The user sends a request; the guardrail stops the turn once tool-call limits are reached.

Diagram

Loop Guardrail Protection

Under Limit

At Limit

📝 Request

🔍 Count Tools

🔧 Execute Tools

🛑 Stop with Message

Quick Start

  1. Default Protection

    Every agent gets automatic loop protection:

    python
    from praisonaiagents import Agent
    
    # Default protection (10 tool calls per turn)
    agent = Agent(
        name="Protected Agent",
        instructions="Safe from infinite loops",
        tools=[my_tool]
    )
    
    agent.start("Your request")

    The user starts a workflow loop; guardrails cap iterations and stop runaway tool calls.

  2. Custom Limit

    Adjust the limit for your use case:

    python
    from praisonaiagents import Agent, ExecutionConfig
    
    agent = Agent(
        name="Custom Protected Agent",
        instructions="Custom loop protection",
        tools=[experimental_tool],
        execution=ExecutionConfig(max_tool_calls_per_turn=5)
    )

How It Works

Diagram
ToolCounterLLMAgentUserToolCounterLLMAgentUserRepeat until task complete or limit reachedRequestProcessCheck tool call countCount: 3/10 (OK)Execute toolResultCheck tool call countCount: 10/10 (LIMIT)Stop with message“Tool call limit reached…”

The agent tracks tool_call_count across iterations within a single chat turn:

  1. Before each batch: Checks if tool_call_count >= max_tool_calls_per_turn
  2. At limit: Stops execution with clear message
  3. Batch trimming: If a batch would exceed the limit, it is trimmed before the assistant turn is recorded, so every declared tool_call_id still gets a reply.
  4. Reset: Counter resets for each new chat turn

When the limit is hit

python
# When the limit is reached, you'll see this message:
"Tool call limit reached (10 calls). Task may be too complex or there may be a broken tool causing repeated calls."

The agent stops cleanly instead of burning tokens indefinitely.


Choosing a limit

Diagram

Examples

Yes

No

Yes

No

What type of agent?

Simple single-tool?

3-5 calls

Complex multi-tool?

20-30 calls

10 calls – default

Search agent, Calculator

General assistant, Q&A

Research, Analysis, Multi-step workflows

ScenarioSuggested valueReasoning
Single-tool simple agent3–5Most tasks need 1-2 calls
Default agent10 (default)Balanced for most use cases
Multi-tool research agent20–30Complex workflows need more steps
Long-running autonomous workflowUse autonomy mode insteadDifferent protection mechanism

Common Patterns

Pattern 1: Protecting Experimental Tools

python
from praisonaiagents import Agent, ExecutionConfig

agent = Agent(
    name="Experimental Agent",
    instructions="Testing new tools safely",
    tools=[experimental_api_tool],
    execution=ExecutionConfig(max_tool_calls_per_turn=3)
)

Pattern 2: Complex Research Agent

python
from praisonaiagents import Agent, ExecutionConfig

agent = Agent(
    name="Research Agent",
    instructions="Multi-step analysis and research",
    tools=[search_tool, analyze_tool, summarize_tool],
    execution=ExecutionConfig(max_tool_calls_per_turn=25)
)

Best Practices

Start with Default

Use the default limit (10) unless you have a specific reason to change it. It handles most use cases well.

Lower for Experimental Tools

Set 3-5 calls when testing new or potentially buggy tools to prevent token waste.

Higher for Legitimate Workflows

Increase to 20-30 for agents that need multiple tool calls for complex, multi-step tasks.

Monitor and Adjust

If agents hit the limit frequently on legitimate tasks, increase it. If they waste tokens on broken tools, decrease it.


Related