# Defining "Done" — Agent Loops, Stop Conditions and Token Budgets

Source: https://www.geekswithgeeks.com/en/agent-loops/stop-success

> Write a success condition that code can check instead of trusting the model's claim.

## Verifiable beats claimed

A model saying "I'm done" is a claim, not proof. Where possible define done as something code can verify: tests pass, a file exists, a schema validates, a record shows the new status. Then the loop ends on evidence, and a false "done" is caught.

## Four ways a loop ends

A loop can finish successfully, hit a hard limit, detect no progress, or be stopped from outside.

![Four exits: success, limit, no progress, external stop.](assets/figures/agent-loops/section-2-map.svg) — Figure 2.1 — Success, hard limit, stalled and cancelled.

## Check before accepting

If the check fails, feed the failure back as the next message instead of returning. `tests_pass` runs your real test command.

```python
if reply.stop_reason == "end_turn":
    ok, report = tests_pass()
    if ok:
        return final_text(reply)
    messages.append({"role": "user",
                     "content": f"Tests still fail:\n{report}\nPlease fix them."})
```

## Bound the retries too

"Feed the failure back" is itself a loop. Count how many times verification failed and give up after a small number, such as three, with a clear report.

**Quiz:** Which is the strongest "done" condition?

- [ ] The model says it is finished
- [ ] The loop ran 10 times
- [x] The test suite passes
- [ ] The reply was long

*Answer:* The test suite passes. A passing test suite is evidence code can check; a claim is not.
