# 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.

AI and LLM interview question Q194, Agents and tool use. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-design-stop-conditions

## Say it in 30 seconds

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.

## Say it in 2 minutes

An agent runs 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, or it runs until the money or the patience runs out. A stop condition is the rule that ends it, and it should say why.

The first rule is done. The model stops on its own when it writes a reply with no tool calls, but that alone does not prove the job is finished. So I check a saved result in code, such as a decision record on an insurance claim. If the record exists, the run ends as finished. The model's words do not count as proof.

The second group is limits. I cap turns, spend and time, and I add a repeat check that counts failures from one system. These must halt the run, because an alert arrives after the money is spent. When a limit is hit, the run stops and goes to a person with a reason code, the steps taken and one clear question.

When two rules fire together, I set the order once in code. A saved result comes first, then the repeat check, then the caps. Then I test each stop path, because a stop rule nobody has run is a guess.

## 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.

- **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.

*Diagram, Where the stop checks sit: 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.*
1. After each turn, the model's step goes to the checks first.
2. A saved decision ends the run as finished.
3. A repeated failure from one system hands it to a person.
4. A hit cap ends the run and hands it to a person.

> **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:

```text
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.

*Diagram, What a stop leaves behind: 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. The run stops and the code writes down why.
2. The reason and the work so far go to a person, with one question.
3. Only after a yes does the run resume, or the claim close.

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. **Keep the work:** Save the steps taken, the documents read and any draft, so nobody repeats them.
3. **Ask one question:** Tell the person the one decision they need to make, with the key facts beside it.
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. **Saved result first:** A saved decision ends the run as finished, even if a cap was hit on the same turn.
2. **Repeat check next:** A system that keeps failing hands the claim to a person before any cap is reached.
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." Better: It often says so, but a saved record is the proof. Check the record in code.
- Trap: "Just add a max loop count." Better: A count alone misses spend and repeats. Add the spend cap and the repeat check too.
- Trap: "Print a warning when it gets expensive." Better: A 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?

A. Stop when a decision record is saved.
B. Stop after 15 tool calls, then hand the claim to an adjuster.
C. Send an alert to the team chat when spend passes 0.50 dollars.
D. Stop after two failed policy-system calls and hand over the notes.

**Answer:** C. An alert only tells someone after the money is spent. It does not stop the run. Make the spend cap halt the run and hand the claim to a person.

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

A. Trust the model and close the claim
B. Check the saved record in code, and hand over if it is missing
C. Restart the run from step one
D. Raise the step cap so it can finish

**Answer:** B. The model's words are not proof. Code checks the saved record. If it is missing, the run is not finished, so hand it to a person, and do not close or restart it.

## Can you answer these from memory?

**Flash cards**

- **What ends a healthy run?** A saved, checked result, such as a decision record. The model's words alone are not proof.
- **Why not trust the model's 'done'?** It can say the work is finished when nothing was saved. Check the record in code.
- **Which four kinds of limit?** Turns, spend, time, and a repeat check.
- **Alert or cap?** A cap halts the run. An alert only tells someone after the cost is spent.
- **What must a stop leave behind?** A reason code, the work so far, and one clear question for the person.
- **Which rule wins first?** A saved, checked result first, then the repeat check, then the caps.

## Likely follow-up questions

- Q187 How do you control agent cost? https://designervenkat.online/interview/ai/how-do-you-control-agent-cost
- Q190 How do you trace and debug an agent run? https://designervenkat.online/interview/ai/how-do-you-trace-and-debug-an-agent-run
- Q193 When should an agent ask a clarifying question? (full answer coming)
- Q195 Synchronous vs background agents? (full answer coming)
- Q198 How do you roll out a new agent version safely? (full answer coming)
