An agent loop that has no stop condition runs until it hits a budget or an error, and a loop that cannot tell it is done will keep calling tools after the task is finished. The fix is to define done before the task starts: a condition the agent can check after each tool call, such as the answer being found, or the step budget being reached. Either condition stops the loop.
Prerequisites
- The task, with a clear done condition.
- A step budget as a backstop.
- A way to check the condition after each tool call.
Steps
Step 1: Define done
Write the done condition in one sentence: ‘the task is done when the supplier’s price is found and written in the answer.’ The condition must be checkable from the tool results, not a feeling.
Step 2: Check after each call
After each tool call, check the condition against the results so far. If it holds, stop the loop; if it does not, continue. Checking after each call, not only at the end, saves wasted calls.
Step 3: Stop and answer
When the condition holds, send the final message: answer from the results collected, and stop. Do not make one more call ‘to be sure’; the condition is the surety.
Worked example: Bluebell Bakery flour price check
The fictional Bluebell Bakery’s agent looks for a flour price. The done condition is: the price is found and written in the answer.
| Call | Result | Done condition | Action |
|---|---|---|---|
| 1 | Supplier list | No price yet | Continue |
| 2 | Price: 4.50 per bag | Price found | Stop and answer |
| 3 | Not made | Already done | Saved 1 call |
The loop stops after call 2, when the price 4.50 is found, and the answer gives the price instead of making a third call.
Expected result. The agent stops as soon as the done condition holds, and the answer is given from the results collected. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- A vague done condition, such as ‘when it looks right’, cannot be checked; write it so a reader can test it.
- A step budget is still needed as a backstop, in case the condition never holds.
- This guide does not cover asking the user whether the task is done.
