Back to Blog

AI agent stop conditions: two layers of limits and a worksheet

Stop conditions are the core of loop engineering. Start from the effect you want: platform limits on turns, errors, spend and time, and prompt rules on retries and lookups. Includes a fill-in worksheet, real settings from two agents and a free diagnostic.

AI lab
27/11/2026
Share this article
FacebookLinkedinLINE

AI agent stop conditions are the rules that make an agent that chooses its own next step stop before it runs without end or forces out a wrong answer. They work in two layers: platform limits on turns, errors, spend and time, and prompt rules on how often to retry or look something up. This article starts from the effect you want, shows which setting to use and how high, and gives you a worksheet you can fill in.

‍

‍

What are AI agent stop conditions?

‍

AI agent stop conditions are the rules that decide when an agent stops searching and calling tools, so it neither runs on indefinitely nor forces out a wrong answer. They apply to agents that choose their own next step; an agent that follows a fixed, pre-drawn flow does not need them.

‍

This kind of agent works in cycles: think, call a tool, read the result, think again. Each cycle is one turn (some tools call it an iteration), and the agent keeps going until it decides it can answer. Anthropic's blog calls this design approach loop engineering [1].

‍

Letting the agent decide when to stop creates two problems:

‍

  • It does not stop: it goes in circles, and the bill keeps growing.
  • It stops, but the answer is wrong: this one is harder to catch.

‍

One of our agents was asked, "How many members have not purchased in the past year?" There was no error and no timeout, and the numbers came back neatly arranged. But it had used the date from a different table as "today," and the answer was off by more than a month. It hit no limit, because it ended through the normal exit.

‍

So stop conditions work in two layers, plus a check after launch:

‍

  • Platform layer: limits the system enforces. They handle "it does not stop."
  • Prompt layer: rules written into the agent's instructions, which it follows on its own, so it explains itself when it cannot do the task instead of forcing an answer. For example, when asked about a month with no data, it says "data for this month has not been loaded yet" instead of using another month.
  • Check after launch: catches answers that look right but are calculated wrong.

‍

This article gives you four things, in order:

‍

  • Which setting to use, and how high, for the effect you want.
  • The stop valves a typical loop agent has, and what happens when one is hit.
  • A stop-condition worksheet you can fill in.
  • How to confirm the agent did not stop in the wrong place.

‍

The examples come from two agents we built on AltaBots.ai: one answers analysis questions on public market data, and the other builds audience lists for a beauty e-commerce company.

‍

We will follow one fictional person through the article: Mia, a marketing manager at an e-commerce company. Her agent selects members by condition and exports the list for push campaigns. What happens to her is adapted from the two real agents above.

‍

If you are still deciding whether to let an AI choose its next step at all, start with Autopilot vs. copilot: what it is and a 5-question diagnostic.

‍

‍

What effect do you want, and how should you set stop conditions for it?

‍

Key point: start from the effect you want, then find the setting that delivers it

‍

How you set stop conditions depends on when and how you want the agent to stop: keeping the bill under control, getting an explanation when a lookup is not working, or never receiving a half-finished file each call for different settings. In the table below, the "How to set it" column can be applied as is, and the last column shows how we set it. The examples come from two agents: the market-analysis agent answers analysis questions on public market data and follows the same steps every time; the audience-building agent lets users keep asking follow-up questions and ends by exporting a list.

‍

Effects you may want from stop conditions, how to set each, and our examples
The effect you want How to set it Our example
Keep the bill under control Set the turn limit above the most turns a normal question uses, with some margin; set a spend limit too if your platform has one; keep the prompt short, because it is resent in full every turn Audience building: the prompt is about 5,000 Chinese characters, so a 10-turn question sends it 10 times, which was the main cost
Give the core answer when a lookup is not working, and ask whether to continue Write "at most N queries per question; beyond that, answer the core part first" into the prompt; the platform turn limit must exceed N plus the turns for planning and answering Market analysis: at most 6 queries, 30 platform turns (planning, reading data definitions, 6 queries, verification, plus margin)
Do not keep fixing the same error Write "diagnose the cause first; fix the same error at most twice" into the prompt; set the platform's consecutive-error limit higher Market analysis: 2 fixes in the prompt, 4 consecutive errors on the platform
Do not make things up when data cannot be found Write "widen the search up to N rounds; if still insufficient, offer options for the user to choose" into the prompt Market analysis: up to 3 rounds, then options A, B and C
Explain clearly when it cannot do something, and never substitute List in the prompt the things where "I do not know" is the right answer Market analysis: for a month with no data, say it has not been loaded; if the query service is down, say so
Never hand over a half-finished list or file Add a check at the output step: over the limit, produce no file, never claim success, and suggest narrowing the range Audience building: a list over the row limit produces no file, and the agent suggests narrowing by sales channel or member tier
The number of steps is hard to predict, and you do not want it to guess Skip hard counts; write down what it must never do and what to say when it cannot, and leave the limits to the platform Audience building: users keep asking follow-ups, and the platform limits stay at their initial values
Check its own answer before handing it over Write pre-answer checks into the prompt: if the result is empty, find out why; if the count exactly equals the query limit, say so; compare the size against a reasonable range, and requery or add a warning if it is far off Market analysis: diagnose empty results, flag results that exactly hit the query limit, and compare figures against reference ranges written in advance
Catch it when it stops with a wrong answer After launch, read the execution log and rerun the numbers with an independent query The date error was found because two methods gave different numbers

‍

Before you set anything, ask yourself one question: "Can I say how many steps this agent takes on a normal question?"

‍

  • If you can, as with analysis questions that follow the same few steps every time: use the rows "Give the core answer when a lookup is not working" and "Do not keep fixing the same error," write the counts into the prompt, and let the platform limit act as the backstop.
  • If you cannot, as with conversations where users keep asking and narrowing the conditions: use the row "The number of steps is hard to predict," skip hard counts, and write down what the agent must never do.

‍

Take Mia. She cares about two things: the bill must not run away, and the list must never be half-finished. So she can start with these two settings:

‍

  • Bill: run a batch of conditions she normally uses (say, 20), see the most turns any of them needed, and set the turn limit above that. Keep only the prompt rules that would cause real trouble if left out, because the whole prompt is resent on every turn.
  • List: add a check at the export step, so a list over the row limit produces no file, and the agent suggests narrowing the range by sales channel or member tier instead.

‍

Mia keeps asking follow-up questions, so the number of steps per question is hard to predict. She does not hard-code a lookup count; she writes down what the agent must never do.

‍

The count in the prompt must come before the platform limit. Hitting a platform limit ends the run, and the agent gets no chance to explain:

‍

  • Turns, for example: suppose the prompt says "at most 6 queries; beyond that, answer the core part first," but the platform turn limit is only 5. Planning takes 1 turn, and by the fourth query the 5 turns are used up. The answer turn never comes, the platform cuts the run, and the user sees nothing. At 30 turns, the agent finishes 6 queries and still has turns left to say "here is the core result, do you want me to continue?"
  • Errors, for example: the prompt says fix the same error at most twice, and the platform's consecutive-error limit is 4. A query fails once, the first fix fails (2 in a row), and the second fix fails (3 in a row), so the agent stops and explains, as its prompt says. At that point there have been 3 consecutive errors, still below the platform's 4, so the agent gets to explain. A single success resets the count.

‍

‍

What does a typical loop agent look like, and where does it stop?

‍

Key point: know the four valves, then check what your platform does when one is hit

‍

A typical loop agent repeats "the model decides the next step, a tool is called, the result goes back to the model" until the model gives a final reply with no further tool calls, or a limit is hit. The agent development documentation from OpenAI and Anthropic both describe this structure [2][4]. Where frameworks differ is in how many limits they offer and what happens when one is hit, and those two things decide which rules you still need to add. This applies to agents that choose their own next step; an agent with a fixed flow ends at its last node.

‍

Common development frameworks offer roughly these four limits:

‍

The four stop limits in common development frameworks: what each prevents, its parameter name, and what happens when it is hit
Valve What it prevents Parameter in common frameworks What happens when it is hit
Turn limit Going in circles; runaway cost max_turns [2][4], max_iter [3] Depends on the framework: no final response [2], an error is raised [4], or the agent is asked to give its best answer so far [3]
Error limit (retries or consecutive errors) A broken tool being called again and again max_retry_limit [3] The documentation describes the maximum retries when an error occurs, 2 by default [3]; some platforms count consecutive errors instead and end the run at the limit, so check which yours counts; test before launch to see what the user receives
Spend limit A single task costing too much max_budget_usd [2] Stops based on a spend threshold [2]; the documentation does not say what the user receives, so test it before launch
Time limit Getting stuck or running too long max_execution_time [3] Stops once the set number of seconds is exceeded [3]

‍

Check the last column first. When the turn limit is hit, frameworks behave differently:

‍

  • No final response [2].
  • An error is raised [4].
  • The agent is asked to give its best answer so far [3]. Be especially careful with this one: it hands over an answer before the work is done, which is exactly "it stops, but the answer is wrong."

‍

In Mia's case, if her platform works this way, hitting the limit would hand her a list that was never finished, and she would send the campaign without knowing some members were missing. If you are unsure which kind your framework is, read its documentation on the turn limit, or hit the limit on purpose before launch and see what comes back.

‍

Beyond these four limits, AWS's Well-Architected guidance recommends budget limits per cycle, per task and per day, checked before each model call, with automatic cutoffs at iteration or cost thresholds [5]. For an agent that answers many questions a day, a daily limit is what catches the case where no single question is a problem but the total runs away.

‍

We tried the turn limit on a test customer-service agent, with a question that needs a single knowledge-base lookup:

‍

  • Turn limit 2: it finished the answer.
  • Turn limit 1: it stopped right after the lookup, with no answer.

‍

So even a question with one lookup needs at least 2 turns, because the final answer takes a turn of its own. The Claude Agent SDK documentation counts turns the same way [2].

‍

There are only two ways to stop, and they are caught at different times:

‍

  • Hitting a limit: the system treats it as an exception, so you can trigger it on purpose and catch it before launch.
  • The model deciding it is done: it leaves through the same exit as a success, the system suspects nothing, and only checks after launch will catch it. Our agent with the date error stopped this way.

‍

‍

Stop-condition worksheet: how do you fill in each part?

‍

Key point: three parts, with our setting as the example for each

‍

The stop-condition worksheet has three parts: platform limits, prompt rules and pre-launch tests. Fill them in order, and wherever the agent stops, it has an explanation ready.

‍

  • Platform limits: decide how long the agent can run at most.
  • Prompt rules: decide how it wraps up when it cannot go on.
  • Pre-launch tests: confirm the first two parts actually work.

‍

The examples are our settings, not recommended values; your task takes a different number of steps, so your numbers will differ.

‍

Part one: platform limits. Fill in whichever ones your platform has.

‍

  • Turns and spend are set the same way: run a batch of normal questions in a test environment, record the turns and usage of each, take the highest, and add some margin so the limit never cuts off a normal question. For example, if you run 20 normal questions and the longest uses 12 turns, set the limit between 15 and 20.
  • An often-missed source of spend: every turn resends the whole prompt, so the longer the prompt, the more each turn costs. On the agent where we tracked cost, this resending was the main expense: its prompt is about 5,000 Chinese characters, so a question that runs 10 turns sends that text 10 times.

‍

Worksheet part one: how to set platform limits, with our examples
Item How to set it Our example
Turn limit Add up the steps of a normal question, the retries you allow and the final answer turn, then add margin; run test questions to see the most turns a normal one uses Market analysis: 30 turns, enough for planning, reading data definitions, up to 6 queries and verification
Consecutive-error limit First check whether your platform counts retries per question or consecutive errors; set it higher than the prompt's limit on fixing the same error, so the prompt rule comes first Market analysis: stops after 4 consecutive errors, and one success resets the count; the prompt allows at most 2 fixes for the same error
Spend limit (token budget) Run a batch of normal questions, take the highest usage and add margin; record usage in your platform's unit, money for some (such as max_budget_usd), tokens for others. Without it, turns are the only cost ceiling; the longer the prompt, the more each turn costs Audience building: not set; its prompt is about 5,000 Chinese characters and is resent every turn, which is the main cost
Time limit Fill in only if your platform has it; it guards against an agent that gets stuck and stops responding Not used in either of our agents

‍

Part two: prompt rules. The platform only counts turns and errors; it cannot tell whether the agent is fixing the same error or has switched approach. These rules go into the prompt so the agent follows them itself.

‍

  • Cost: each turn resends a few more lines.
  • Benefit: the right rules save whole turns of trial and error. For example, our agent once did not know that production data allows read-only queries, so it tried other approaches and only switched after they failed. Once that rule was in the prompt, those wasted turns disappeared.
  • What we do: read the execution log, find where the agent keeps trying and failing, write those lessons back into the prompt, and keep only the rules that would be costly to leave out.

‍

Worksheet part two: how to write prompt rules, with our examples
Rule How to set it Our example
How many times to fix the same error Check whether the error message tells you the cause; if not, require the agent to diagnose before fixing 2 times. Our data service returns the same message for "table does not exist" and "query written wrong," so without diagnosis the agent keeps editing a query that was never wrong
Maximum lookups per question Write it only if you can estimate the steps per question; beyond it, answer the core first and ask whether to continue 6 times
How to wrap up when nothing is found Do not let the agent invent definitions or thresholds; offer options for the user to choose Widen the search up to 3 rounds; if still insufficient, offer options A, B and C
What to say when it cannot do the task List the things where "I do not know" is the right answer Never substitute another month for one with no data; if the query service is down, say so instead of piecing numbers together from memory; if a result looks implausible, answer with a "may be wrong" warning

‍

Part three: pre-launch tests. Once the first two parts are written, hit them on purpose to know the agent really stops, and what it leaves the user when it does. For Mia, that could mean entering a condition that pulls in a very large number of members before launch, to see whether the agent says plainly that the list is over the limit instead of handing over half a list.

‍

Worksheet part three: pre-launch tests, how to run them and what to look for
Test How What to look for
Hit the turn limit on purpose On a test agent, lower the turn limit and send a question that normally completes What the user receives; in our test, 2 turns finished the answer and 1 turn returned none
Make a tool fail on purpose Send a question that will make a tool fail Whether it stops within the consecutive-error limit, and whether the user gets an explanation
Compare with reference answers Prepare a few questions whose answers can be recomputed independently, such as numbers and lists Whether replies match the reference answers; for market analysis we prepared a dozen or so queries verified against the live database

‍

When you are done, the numbers in parts one and two should fit together: the counts in the prompt should come before the platform limits.

‍

‍

The agent stops reliably, but how do you know it stopped in the right place?

‍

Key point: a reply that looks right needs one recalculation to prove it

‍

To know whether the agent stopped in the wrong place, do not rely on its reply alone: look at what it actually queried for that question, then recalculate the number yourself. The reply is written by the model, and when it is wrong it sounds exactly the same as when it is right.

‍

Back to Mia. On Monday morning she asks the agent, "How many members have not bought anything in the past year? I want to send a win-back offer." The agent answers quickly, with a neat table, and nothing looks wrong. Here is how she can check, in three steps:

‍

  1. See what it actually queried. Open the execution log for this question (it records which tool was called on each turn, with what conditions, and what came back). It shows that when the agent calculated "the past year," it started from the day the member tag table was last updated, not from today.
  2. Run its query again, unchanged. The number comes out the same, so it really was retrieved, not made up. But the starting date is wrong, so matching numbers do not help.
  3. Calculate it yourself from scratch. Rewrite the query with today as the starting point. The result is noticeably higher: the start date was more than a month too early, so a group of members who should get the offer was left out.

‍

Without that check, Mia would send the offer to a list missing a whole group of members.

‍

Mia is fictional; the error is real, and our own agent made it. We found it while reviewing real conversations, when the same question, run a second way, produced a different number. The fix was simple: a prompt rule saying that "the past N days" must start from today, or from the latest date in the transaction data, and never from another table's update date. After the fix, the recalculated number matched our own.

‍

You do not have to do these three steps yourself; you can ask whoever is responsible to run them before every launch and after every rule change. Numbers and lists can always be recalculated this way. If the answer is a recommendation or a summary with no single correct answer, review a few at regular intervals against a scoring guide instead.

‍

To find recurring errors across a large batch of conversations without checking them one at a time, read What are evals? A 6-step error analysis SOP for AI agents.

‍

‍

Hit one of these problems? Here is how AltaBots.ai handles them

‍

Key point: every framework has limits; the difference is who can change them, test them and trace them

‍

If your agent has to check several sources and decide each step from the last result, AltaBots.ai lets you set, change and trace its stop conditions without writing code. Every framework offers limits. What it saves you is the work around them: who can change them, how to test a change, how to trace a stop, and who helps you fill in the first part of the worksheet. These are the four places we got stuck most often while building our two agents.

‍

Problem 1: changing a limit or rule means waiting for an engineer. On AltaBots.ai, turns, consecutive errors and token budget are three fields on the settings page; you fill them in directly. A new agent starts at 25 turns, 5 errors and no token limit, so remember to adjust them to your task. Rules go into the agent's system prompt: test a change in the debugging environment first, publish only when it works, and roll back to the previous version if it does not.

‍

Problem 2: you want to know what the agent actually did on a question. Open the Logtree node-level log to see, in time order, which tool each step called, what it sent and what came back. In Mia's case, the wrong start date is visible as soon as the log is open, with no need to ask someone to pull records.

‍

Problem 3: the reference answers are ready, but no one has time to check them one by one. AI quality check lets you upload a Q&A file as the benchmark, choose a time range, and score real conversations in one batch. In the conversation table, the AI score sits next to the human label, so you can start with the lowest-scoring ones. For example, Mia could put a dozen or so conditions with their correct list counts into a Q&A file, manually run a score on the previous week's conversations once a week, and open Logtree for the low scorers.

‍

Problem 4: you cannot fill in the first part of the worksheet. How many steps a normal question takes, and what the agent must never guess, can only be answered by someone who knows your data and processes. If no one on your team can take that on right away, our consultants can fill it in with you and design the pre-launch tests along the way.

‍

Tasks that suit this kind of agent:

‍

  • Analysis questions that check several sources and decide each step from the last result. For example, "How did a sector perform in July?" first needs to confirm whether the user means price change or period return.
  • List building where users keep narrowing the conditions and finally export a list. For example, "members who have not bought in a year, have linked their LINE account (a messaging app widely used in Taiwan) and allow push messages."
  • Multi-step customer service and automation that calls several tools.

‍

Tasks that do not: fixed processes that run the same way every time, where consistency and cost per run matter most. An agent with a pre-drawn flow is enough; it does not need to choose its own next step.

‍

Stop conditions are only one part of keeping an agent governable. To go further:

‍

‍

‍

Closing: fill in the worksheet before you launch

‍

The numbers we adjusted came from the steps of each task; how many steps your task takes decides your numbers. The hardest part of the worksheet is the first box.

‍

Do you have an agent that chooses its own next step, but cannot fill in the first box, or are unsure which prompt rules to write? Data-DI offers a free 30-minute AI agent adoption diagnostic. Our AltaBots.ai consultants will fill in this worksheet with you: list the steps a normal question takes, mark what the agent must never guess, and design a test that hits the limit before launch. You do not need to prepare data; bring how it works today and we can begin. It is also fine to go over the numbers only, without discussing adoption.

‍

Book a consultant conversation.

‍

References

[1] Anthropic. (2026.06). Loop engineering: Getting started with loops. Claude blog.
‍https://claude.com/blog/getting-started-with-loops

[2] Anthropic. (2026.11). How the agent loop works. Claude Agent SDK documentation.
‍https://code.claude.com/docs/en/agent-sdk/agent-loop

[3] CrewAI. (2026.11). Agents. CrewAI documentation.
‍https://docs.crewai.com/en/concepts/agents

[4] OpenAI. (2026.09). Running agents. OpenAI Agents SDK documentation.
‍https://openai.github.io/openai-agents-python/running_agents/

[5] Amazon Web Services. (2026.11). AGENTCOST07-BP01 Implement automated cost controls with intelligent cutoffs. AWS Well-Architected Agentic AI Lens.
‍https://docs.aws.amazon.com/wellarchitected/latest/agentic-ai-lens/agentcost07-bp01.html

FAQ

FAQ

FAQs about this article

How are loop engineering and stop conditions related?

Loop engineering is the practice of designing a system that lets an agent run turn after turn, and stop conditions are the rules inside it that decide when to stop. Without them, the loop either never ends or ends on a plausible wrong answer.

Is a higher turn limit always safer?

No. The turn limit is also a cost ceiling, because every extra turn calls the model again and resends the whole prompt. Set it too high and a failing run lasts longer and costs more. Run test questions first, see the most turns a normal one needs, then add some margin.

How is a stop condition different from human review?

Stop conditions decide when the agent stops on its own, while human review decides which decisions a person must look at first. One works like a brake and the other like a checkpoint, so they cover different things and can be used together.

One question takes 3 steps and the next takes 30. How should I set the limit?

When step counts vary this much, a single turn limit will be wrong at one end. Set the turn limit above the longer kind of task, then use a prompt rule for the maximum number of lookups per question so shorter tasks stop on their own.

My agent keeps repeating the same action and will not stop. What should I check first?

Read the execution log and see whether it is calling the same tool with the same conditions again and again. If it is, write into the prompt how many times the same error may be retried, and keep the consecutive-error limit as the backstop.

Newsletter

訂閱我們的電子報

每月 8 日定期發送最新趨勢與實用資訊!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.