Runaway AI Agent Loops: Token and Step Budgets With Alerts
Stop AI agent loops with step, token and time budgets set in the harness, plus alerts that tell a named person what stopped and what to decide.

An AI agent stops looping when the harness around it enforces hard limits on steps, tokens and elapsed time, instead of leaving that decision to the model. When a limit is reached, the harness halts the next action, saves the task state and alerts one named person with the reason and the last known state. The rest of this article explains how to set those budgets, what the harness should do the moment a limit fires, and how to write an alert that someone can act on.
Key takeaways
- Put step, token and time limits in the harness, outside the model, so a confused agent cannot argue its way past them.
- Set each budget from observed runs of one task type, not from a single round number.
- Treat a limit hit as an incident with a reason code, not as a generic error.
- Alert one accountable owner with a backup, on a channel that person reads, and include what was tried.
- Stop, checkpoint and wait for a decision. Do not retry automatically with a larger budget.
Why agents loop even when each step looks sensible
An agent runs a cycle: it reads the current state, chooses the next action, calls a tool, reads the result and decides again. The cycle ends when the model judges the work complete. Loops appear when that judgment never arrives. A tool returns an ambiguous error and the agent retries with the same arguments. A search finds nothing and the agent rephrases the same query with small variations. Two steps undo each other, such as a record set to open and then closed, over and over. Any single step in the transcript looks reasonable, which is why loops are easy to miss until the cost or the backlog shows them.
The cost is not only tokens. A loop that repeats a write action can send duplicate messages, create duplicate records or trigger the same external request many times. For this reason the budget belongs next to the permissions and tool definitions, not only on a cost dashboard. For the mechanics of the cycle, see how an AI agent decides what to do next.
Prompting the model to stop after a few failed attempts helps in ordinary conditions, but the instruction lives inside the same system that is failing. A budget that the harness counts from its own logs does not depend on the model noticing that it is stuck.
The three budgets: steps, tokens and time
Use three separate limits, because each one catches a different failure.
- Step budget: the maximum number of model turns or tool calls for one task. It catches cycles of small actions.
- Token budget: the maximum input and output tokens for one task, summed across every call, including retrieved context that is sent back on each turn. It catches verbose reasoning and oversized context.
- Time budget: the maximum elapsed time for one task. It catches a stalled external service, because one hung call can run for a long time without adding many steps.
Add a ceiling above the task level as well, such as a daily limit per agent role. A task budget protects one run. A role ceiling protects the account when many runs fail the same way at once, which often happens when one upstream service becomes unavailable.
Cap retries inside each tool too. A failed tool call should retry a small, fixed number of times with spacing, then return the failure to the agent as data. Retries count against the task budget, so they cannot hide inside it.
How to set limits without guessing
Start from observed runs, not from a number that sounds safe. The OWASP Top 10 for LLM Applications lists unbounded consumption as a named risk category, and the practical answer is measurement before configuration.
- Choose one task type and run it several times in a staging environment. Record steps, tokens and elapsed time for each run that completes.
- Look at the typical completed run, then at the unusual but legitimate one, such as a task that needs an extra lookup because a customer name has two spellings.
- Set a soft limit with a comfortable margin above the typical run. Crossing it writes a warning event and asks the agent to summarize its progress, but the work continues.
- Set a hard limit above the soft limit, high enough that the unusual legitimate case still finishes, and low enough that a loop costs little before it stops.
- Give each task type its own budget. A quote lookup and a monthly report should not share one number.
- Write the budget into the task contract so a reviewer can test it against the expected run. See how to write an AI task contract your team can test.
- Measure again after any change to the prompt, the model or the tools, because each change moves what a normal run looks like.
What the harness does the moment a limit fires
The response to a limit should be fixed in advance, not improvised by the model. A workable sequence looks like this.
- Refuse the next tool call and any further model turn for that task.
- Write a checkpoint with the inputs, completed steps, open items and the last tool result. The mechanics are described in AI agent checkpoints and how to resume unfinished work.
- Record a structured event with a reason code: steps exhausted, tokens exhausted or time exhausted.
- Set the task status to paused for review. Do not mark it as failed, and do not retry it.
- Send the alert described in the next section.
- Release any locks or reserved resources the task still holds.
Illustrative scenario: a quote assistant for a small cabinet maker is asked to match a customer's order lines to a supplier's price list. The supplier's lookup tool times out, and the agent keeps issuing the same query with minor changes until the step budget ends the run. The harness stops the task, saves the lines already matched, and marks the unmatched line as open. The owner receives an alert naming the tool, the query pattern and the last error, and can check the supplier's availability before deciding whether to resume.
How to alert a person without flooding them
An alert is useful only if the recipient can decide from it without opening the full transcript. Include these fields:
- The agent role and task identifier, with a link to the task record.
- Which limit fired and the value reached, such as the step count against the hard limit.
- The last three actions, described in plain language rather than raw model output.
- The last tool error, quoted exactly as returned.
- What has been saved, and what has not been done.
- The options available: resume with corrected input, ask the owner to change the budget, or close the task.
Route the alert to one accountable owner with a named backup, on the channel that person actually reads during working hours. A notice that lands in a shared inbox nobody checks is the same as no notice. Group repeated triggers with the same reason and the same tool into one message with a count, so the owner does not receive many identical notices. If the alert is not acknowledged within a window agreed in advance, escalate to the backup.
Limit events should also be logged as structured telemetry. The OpenTelemetry observability primer describes the traces, metrics and logs that make events like this countable over time. A rising count of exhausted budgets for one task type usually means the task, a tool or the model has changed, even when no single run looked wrong. If alerts or resume signals arrive from external systems as webhooks, verify each signature before acting on it. Stripe's webhook documentation shows the signed-delivery pattern that many providers follow.
Who may change a budget, and how changes are reviewed
The agent must not be able to raise its own budget. Keep limit values in configuration that the agent can read but cannot write, and treat every change as a reviewed change with a named owner, a reason and a record of what changed. When a task hits its limit repeatedly, the first response is to find the cause: a changed tool response, an ambiguous input, a missing rule in the task definition. Raising the number is the last option. The owner should also be able to lower a budget quickly during an incident. Permission boundaries like these belong with the wider set of controls covered in AI agent guardrails for business.
How Be Human runs budgets and alerts in managed operations
In a Be Human engagement, the first step defines one useful role, the memory and tools it needs, its approval boundaries and one observable success criterion. The step, token and time limits are set in the same definition, tested against the expected runs, and written into the operating context so the harness enforces them. Managed operations then review limit events, adjust budgets through controlled changes, update alert routes when people change roles, and record each incident with its cause. Be Human does not promise that an agent will never reach a limit. The aim is that when it does, the stop is safe, the state is saved and the right person knows what to decide. If you are not sure which of your tasks is ready for a budget and an alert, the free Be Human AI diagnostic can help you find the first task worth handing to an agent.
Frequently asked questions
Can a good prompt stop an agent from looping? Instructions help in ordinary cases, but they live inside the system that may be failing, so they cannot be the only control. Enforce the limits in the harness, where the counts come from logs the model cannot edit. Keep the prompt instruction as well, because it reduces wasted cycles before a limit fires.
Should an agent retry automatically after it hits a limit? Not for the same task with the same inputs and the same budget. A retry without a changed input or a reviewed cause usually repeats the loop. Resume from the checkpoint only after the owner has reviewed the reason code and decided what should change.
How do I choose between step limits and token limits? Use both, together with a time limit. Steps catch repetitive cycles, tokens catch long contexts and verbose reasoning, and time catches stalled external calls. Each one misses failures that the others catch.
Who should receive the alert? Send it to one accountable owner for the agent role, with a named backup and a channel that person reads during working hours. The alert should contain enough detail to decide without opening the transcript, and repeated triggers for the same cause should be grouped into one message.
Your next step
See where an AI agent fits in your company first.
Take the free Be Human AI diagnostic. A few questions about your work show which task to hand to an agent first, what it needs to know and where a person approves.
Be Human editorial guidance. Examples are illustrative unless explicitly attributed. Sources provide background; implementation choices depend on your workflow.


