Agent Loops That Don't Spiral: Budgets, Stop Conditions, and Fallbacks
Most agent failures aren't reasoning failures — they're loop failures. Here's how we design budgets, stop conditions, and fallbacks so our agents don't burn $40 proving they can't book a flight.
Most agent failures in production aren't reasoning failures. They're loop failures — the model keeps calling tools, keeps retrying, keeps "thinking," and your bill keeps climbing while the user stares at a spinner. The fix isn't a smarter model. It's a better loop.
This is how we design agent loops at 72Technologies so they terminate cleanly, degrade gracefully, and don't surprise anyone on the invoice.
Why agents spiral
The canonical agent loop is embarrassingly simple:
while True:
response = model.call(messages, tools=tools)
if response.stop_reason == "end_turn":
return response
messages.append(response)
for tool_call in response.tool_calls:
result = execute(tool_call)
messages.append(result)
That while True is doing a lot of load-bearing work. In the happy path, the model calls a tool or two, gets what it needs, and returns an answer. In the unhappy path — a flaky API, an ambiguous query, a tool schema the model subtly misunderstands — it keeps trying. Anthropic's tool use docs and OpenAI's function calling guide both describe the loop pattern but leave termination policy to you, which is correct but easy to under-engineer.
The common failure modes we've seen:
- Retry thrash. A tool returns an error. The model calls it again with nearly identical args. Repeat 14 times.
- Clarification loops. The agent asks itself questions via a
thinktool, generates more questions, and never commits to an action. - Context bloat death spiral. Each tool result adds tokens. Eventually the model is paying attention to the wrong half of a 180k-token context and forgets the original task.
- Silent success. The model completes the task on step 3 but keeps going because the system prompt said "be thorough."
None of these are solved by swapping Claude for GPT or vice versa. They're solved by writing a loop that knows when to stop.
Three budgets, not one
A single max_steps = 10 counter is where most teams start. It's not enough. We run every agent with three independent budgets, and whichever trips first wins.
Step budget
The simplest: how many tool-calling turns before we force termination. Useful because it's deterministic and cheap to reason about. Typical values in our work:
- Simple lookup agents: 3 – 5 steps
- Multi-tool workflow agents: 8 – 15 steps
- Research or planning agents: 20 – 30 steps
If you're setting it higher than 30, you probably want a multi-agent decomposition instead of one long loop.
Token budget
Step count doesn't capture cost. One step with a 120k-token context is more expensive than ten steps with a 4k context. Track cumulative input + output tokens across the loop and cap it:
class LoopBudget:
def __init__(self, max_steps, max_tokens, max_wall_seconds):
self.max_steps = max_steps
self.max_tokens = max_tokens
self.max_wall_seconds = max_wall_seconds
self.steps = 0
self.tokens = 0
self.start = time.monotonic()
def check(self, usage):
self.steps += 1
self.tokens += usage.input_tokens + usage.output_tokens
reasons = []
if self.steps >= self.max_steps:
reasons.append("step_cap")
if self.tokens >= self.max_tokens:
reasons.append("token_cap")
if time.monotonic() - self.start >= self.max_wall_seconds:
reasons.append("wall_clock")
return reasons
Set the token cap based on what the task is actually worth. If a successful run is worth $0.20 to your business, don't let the agent spend $3 trying.
Wall-clock budget
Users abandon. Tools hang. Downstream services rate-limit. A wall-clock cap — 30 to 90 seconds for user-facing agents, a few minutes for background jobs — protects you from the long tail. It's also the only budget that catches a single catastrophically slow tool call.
Stop conditions beyond "model said stop"
The model's own stop_reason or finish_reason is one signal. Trust it, but verify. We layer additional stop conditions:
Repeat detection. Hash each tool call's (name, normalized_args). If you see the same hash three times in a row, something is wrong — either the tool is non-deterministic in a way the model doesn't understand, or the model is stuck. Break out and surface the issue.
No-progress detection. Harder, but valuable. If the last three tool calls produced errors or empty results, the model is unlikely to recover on step four. Short-circuit with a fallback.
Confidence thresholds. For agents that produce structured outputs, run a cheap eval on the partial result after each step. If confidence is already above threshold, stop — don't let the model second-guess itself into a worse answer. This is especially useful for extraction agents.
External cancellation. Your loop needs to check a cancellation token on every iteration. Users close tabs. Jobs get superseded. If you can't abort a running agent, you'll pay for ghosts.
Fallbacks that actually help the user
Hitting a budget cap is not a success. But it shouldn't be a 500 error either. A good agent loop has a graceful degradation path.
The "best effort" summary
When a budget trips, don't return the last raw tool result. Make one final, cheap model call with a tight prompt:
You ran out of budget mid-task. Summarize what you learned, what you tried, and what the user should do next. Be honest that the task is incomplete.
Use a smaller, faster model for this — Haiku, GPT-4o-mini, or Gemini Flash. It's a summarization task, not a reasoning task, and you've already blown your budget.
Deterministic fallback paths
For high-value flows, don't rely on the agent as the only path. If the agent fails to book the meeting, hand off to a rule-based form. If the research agent times out, return the top three search results with a "we couldn't synthesize this — here's the raw data" note. Users forgive incomplete answers; they don't forgive error pages.
Escalation to a bigger model
Sometimes the right fallback is retrying once with a stronger model and a fresh context. If your main loop runs on Sonnet or GPT-4.1-mini and fails, a single-shot retry on Opus or GPT-4.1 with a cleaned-up summary of the state can rescue the request. Cap this to one retry — otherwise you've just moved the spiral up a tier.
What to log so you can actually debug this
Agent loops are the hardest LLM workloads to debug because the failure is spread across many turns. Log at minimum:
- Full message history with token counts per turn
- Every tool call's name, args, latency, and result size
- Which budget tripped (if any) and the values at termination
- A trace ID that ties the whole loop together
We pipe this into whatever observability stack the client already runs — Datadog, Honeycomb, Langfuse, Arize. The specific tool matters less than having per-step visibility. If your logging only captures the final response, you will never understand why your p95 latency doubled last Tuesday.
A small note on evals
Agent evals are not the same as model evals. You're testing the loop, not the model. Build a golden set of tasks where you know the correct number of steps, the correct tools, and the correct final answer. Then measure:
- Task success rate
- Average steps to success (regression here is a leading indicator of prompt drift)
- Budget trip rate by category
- Cost per successful task
If cost per successful task is creeping up week over week and success rate is flat, your loop is getting lazier, not smarter.
Where we'd start
If you have an agent in production right now without explicit budgets, do this today:
- Add a step cap, a token cap, and a wall-clock cap. Pick numbers that feel generous — you can tighten later.
- Log cumulative tokens per loop and alert on the p99.
- Implement repeat detection on tool calls. It's twenty lines of code and it will catch real bugs within a week.
- Write a "best effort summary" fallback for budget trips so users get something useful instead of an error.
Everything else — confidence thresholds, escalation, multi-agent decomposition — is an optimization on top of a loop that already knows how to stop. Get the stop logic right first. If you want to see how we wire this into client projects, our AI engineering work is a good place to start.
Want a team like ours?
72Technologies builds production software for the kind of teams who actually read this blog.
Start a projectKeep reading
Tool Use That Doesn't Hallucinate: Designing Function Schemas LLMs Actually Respect
Function calling fails in boring, predictable ways: invented arguments, wrong enums, nested JSON that half-parses. Here's how we design tool schemas so Claude, GPT, and Gemini stop improvising.
Structured Outputs in 2026: When to Trust the Schema and When to Validate Twice
Structured output modes from OpenAI, Anthropic, and Google look identical on the slide deck. In production they fail in very different ways. Here's how we handle schemas without getting burned.
Prompt Caching in Practice: What Actually Lands on the Bill
Prompt caching sounds like free money until you read the invoice. Here's how Anthropic, OpenAI, and Gemini caches actually behave, and the patterns that make them pay off.
