Preventing Agent Runaway: Budgets and Caps

RoadmapsAI Engineering

Overview

An autonomous agent without limits is like a robotic vacuum with no off switch—if it gets confused, it will relentlessly bump into the same wall until the battery runs out. Step budgets and recursion limits act as the circuit breaker, stopping an agent from entering an infinite loop of failing tool calls.

Without structural limits, an agent that encounters a failing tool or logic gap can infinitely retry identical actions, rapidly consuming compute and costing thousands of dollars in API fees in mere minutes.

Where used: LangGraph state machines, ReAct orchestration loops, Production agent deployments

Why learn this

Code walkthrough

from langgraph.graph import StateGraph

# Compile the graph into an executable app
app = graph.compile()

# Run the agent with a strict recursion limit to prevent infinite loops
final_state = app.invoke(
    {'messages': [('user', 'Research quantum computing')]},
    config={'recursion_limit': 15}  # Stops execution after 15 node transitions
)

Focus: The `recursion_limit` inside the `config` dictionary is the safety net. It explicitly halts the LangGraph execution if the agent transitions between nodes more than 15 times, definitively severing an infinite tool-calling loop.

Common mistakes

Recall questions

Understanding checks

An agent tries to query a SQL database, but the schema has changed. The agent catches the error and decides to try the exact same query again. You have configured the LLM with `max_tokens=500` and the LangGraph orchestrator with `recursion_limit=10`. What happens?

The agent retries the failing query repeatedly until it hits 10 node transitions, at which point the orchestrator halts the execution and throws a recursion limit exception.

The `max_tokens` limit applies per-generation and won't stop the loop since each tool call generation is short. The `recursion_limit` correctly bounds the total number of cycles, acting as the ultimate circuit breaker.

Why can't you just add 'stop if you fail more than 3 times' to the system prompt to prevent runaway agents?

LLMs are probabilistic and may ignore instructions, especially when context windows grow large. System prompts provide soft guidance, whereas hard structural budgets implemented in the orchestration code guarantee termination.

Relying on the LLM to reliably count its own failures and halt itself is an anti-pattern. Orchestration safeguards must always be deterministic code.

Practice tasks

Implement a Hard Step Cap

Given a compiled LangGraph application `app`, invoke it with a user message to 'Analyze this data' and pass a configuration that ensures the graph will terminate if it exceeds 5 state transitions.

Continue learning

Previous: State machines for agents

Previous: Cost & Latency Budgeting

Return to AI Engineering Roadmap