esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

How Do You Design Stop Conditions?

How to decide when an AI agent stops: a done check in code, hard caps on turns and spend, and a clean hand-off to a person, explained for your AI interview.

In short

A stop condition is the rule that ends an agent's run. Good ones check a saved result in code, cap turns, spend and time, and hand unfinished work to a person with the reason. The model's own word that it is done is not enough.

12 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

A stop condition is the rule that ends an agent's run. I never trust the model to say it's finished. My code checks for a saved result, like a decision record on a claim. I also set caps on turns, spend and time, and a check for one system that keeps failing. If a cap is hit, the run stops and goes to a person with the reason and the work so far. A cap has to stop the run.

What is a stop condition, in plain words?

An AI agent works in a loop. The model picks a step, your code runs a tool, and the result goes back to the model. Something has to end that loop. A stop condition is the rule that ends it, and it should also say why.

Think of a bank's fraud team. A case closes when the money is recovered, when it goes up to a senior analyst, or when the shift ends. Nobody works a case forever. An agent needs the same three kinds of exit.

Take a claims agent at a car insurer. It reads a damage claim, pulls the policy and drafts a decision for an adjuster. Without stop rules it could keep pulling records, retry a broken system or spend money on a claim it will never close.

Key words

Stop condition
A rule in your code that ends a run and records why.
Turn
One pass where the model answers and any tools it asked for run.
Done check
A test in code that the job really finished, such as a saved record.
Hand-off
Passing an unfinished run to a person, with notes on what was tried.

What counts as done, and who decides?

A model stops on its own when it writes a reply with no tool calls. The Claude Agent SDK describes its loop this way. That is not proof the work is done. A model can say the refund is sent when no refund was ever made.

So the code decides. Done means a record exists: a saved decision, a sent reply or a closed ticket. The model's words are only a hint.

Where the stop checks sit

Model

picks the next step or writes a reply

Checked in code, every turn

Decision saved?

the saved record, not the model's word

System keeps failing?

repeated failures from one system

Cap reached?

turns, spend or time

Finished

the job is done and recorded

Handed to a person

with the reason and notes

  1. 1After each turn, the model's step goes to the checks first.
  2. 2A saved decision ends the run as finished.
  3. 3A repeated failure from one system hands it to a person.
  4. 4A hit cap ends the run and hands it to a person.
After each turn, code checks the saved record, then any repeated failure, then the caps. A saved decision ends the run as finished, while a repeated failure or a hit cap hands it to a person. The model stopping on its own is not enough proof.

Tip

In the interview, name your done check in one line, such as a saved decision record, and say who writes it.

Which limits should you add, and why?

  • “The task often terminates upon completion, but it's also common to include stopping conditions (such as a maximum number of iterations) to maintain control.”

    Anthropic, Building effective agents

  • “These must be enforcement mechanisms that halt inference when exceeded, rather than alerting thresholds that fast-accumulating workloads can outpace.”

    OWASP, LLM06:2026

Use four kinds of limit, each for a different failure: a turn cap for runaway steps, a spend cap for runaway cost, a time cap for a run that hangs, and a repeat check for one system that keeps failing. The Agent SDK gives you turn and spend caps as options, max_turns and max_budget_usd. The time cap and repeat check you write in your own code.

The cap has to halt the run. OWASP, the open web security project that publishes top risks for LLM apps, puts it bluntly: a threshold that only alerts can be outpaced by fast workloads.

  • Too loose

    Money runs away

    No caps at all. The SDK's docs say the loop then runs until the model finishes, which can be long on open-ended tasks.

  • Too tight

    Good work gets cut off

    A cap set below a normal claim stops real work half way and creates hand-offs nobody needs.

  • Right

    Set from real runs

    Measure a typical claim, then set the cap above that and below the worst case you can accept.

Here is a made-up claims triage agent with three limits: 15 steps, 50 cents of spend per claim, and a hand-off after two failed calls to the policy system. The code runs three claims. The model is scripted, so the output is fixed.

Python
# A simplified claims triage run. The model is scripted, so the output is fixed.
MAX_STEPS = 15       # made-up cap on tool calls for one claim
MAX_SPEND = 0.50     # made-up budget in dollars for one claim
COST_PER_STEP = 0.03 # made-up cost of one model call and tool run
def stop_reason(s):
    if s["decision"]:                       # done: a decision record exists
        return "finished, decision saved"
    if s["policy_errors"] >= 2:             # repeat check: policy system keeps failing
        return "handed to adjuster, policy system failing"
    if s["steps"] >= MAX_STEPS:             # step cap: halts the run
        return "handed to adjuster, step cap hit"
    if s["spend"] > MAX_SPEND:              # spend cap: halts the run
        return "handed to adjuster, budget used up"
    return None
def run(actions):
    s = {"steps": 0, "spend": 0.0, "decision": None, "policy_errors": 0}
    for action in actions:
        s["steps"] += 1
        s["spend"] += COST_PER_STEP
        if action == "policy_down":
            s["policy_errors"] += 1
        if action == "decide":
            s["decision"] = "approve"
        reason = stop_reason(s)
        if reason:
            return f"step {s['steps']}: {reason}"
    return "still running"
print(run(["read_claim", "decide"]))
print(run(["read_claim", "policy_down", "policy_down"]))
print(run(["read_claim"] * 20))

Output

step 2: finished, decision saved
step 3: handed to adjuster, policy system failing
step 15: handed to adjuster, step cap hit

The first claim finishes at step 2, once a decision is saved. The second hands off at step 3, after the second failed policy call. The third hits the step cap at step 15. The spend cap would have fired at step 17, so the step cap is the one that stops it first.

What happens when a stop fires?

A good stop ends the run cleanly. The run finishes with a reason a person can read, and the work so far is kept. The Claude Agent SDK works this way: each run ends with a result subtype, a label that says whether the task succeeded or hit a limit. Every subtype carries a session ID, a tag you can use to resume the run.

What a stop leaves behind

Run stops

at a cap, a repeat or a hand-off

Left behind

Reason code

finished, step cap or repeat

Work so far

steps, documents and the draft

One clear question

the single decision a person needs

Resume or close

only after a person says yes

  1. 1The run stops and the code writes down why.
  2. 2The reason and the work so far go to a person, with one question.
  3. 3Only after a yes does the run resume, or the claim close.
A stop leaves three things behind: a reason code, the work so far, and one clear question for the person. The run does not restart on its own. It waits for a yes.
  1. 1

    Name the reason

    Write one code for each stop, such as finished, step cap hit or repeated error, so reports can count them.

  2. 2

    Keep the work

    Save the steps taken, the documents read and any draft, so nobody repeats them.

  3. 3

    Ask one question

    Tell the person the one decision they need to make, with the key facts beside it.

  4. 4

    Resume on a yes

    A person raises the cap or decides. The run never restarts by itself.

Anthropic's post Building effective agents makes the same point: agents can pause for human feedback at checkpoints or when they meet a blocker, so build that pause in on purpose.

How would you order two stop rules that fire at once?

Say a claim gets a saved decision on the same turn the spend cap is hit. Which rule wins? Decide the order once, in code, and test it. The example code checks in this order:

  1. 1

    Saved result first

    A saved decision ends the run as finished, even if a cap was hit on the same turn.

  2. 2

    Repeat check next

    A system that keeps failing hands the claim to a person before any cap is reached.

  3. 3

    Caps last

    Caps stop only unfinished runs, and they hand those to a person.

A real system may add a rule above these that needs a person to approve first, such as a payout above an approval limit. The example code above does not have one. Write the order down, and name the risk each rule guards: a lost decision, a run that never ends, or a wrong payout.

What does the interviewer want to hear?

A strong answer covers

  • The run stops on a checked done state, not the model's words.
  • Four kinds of limit: turns, spend, time and repeated errors.
  • Every stop gives a reason code and keeps the work so far.
  • A hand-off to a person when the run cannot finish.
  • Caps halt the run; alerts alone do not.
  • Each stop path has a test.

Common traps

  • Trap"The model knows when it is done."

    BetterIt often says so, but a saved record is the proof. Check the record in code.

  • Trap"Just add a max loop count."

    BetterA count alone misses spend and repeats. Add the spend cap and the repeat check too.

  • Trap"Print a warning when it gets expensive."

    BetterA warning does not stop a bill. Make the cap halt the run and hand it over.

Can you spot the weak stop rule?

Spot the weak rule

A claims agent has these stop rules. Which one is the weakest?

Quick check

An agent's model says the claim is complete, but no decision record exists. What should the stop logic do?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q187How do you control agent cost?13 min
  2. Q190How do you trace and debug an agent run?11 min
  3. Q193When should an agent ask a clarifying question?Coming soon
  4. Q195Synchronous vs background agents?Coming soon
  5. Q198How do you roll out a new agent version safely?Coming soon

Last updated on 26 November 2026

Building an agent that could run up cost or loop forever?

I design and build AI agents with stop rules in place: done checks in code, hard caps, clean hand-offs and tests for each stop. Book a short call and tell me what your agent needs to do.