The Exit Condition First Rule: Why Your AI Agent Needs a Done-State Before a First Tool Call
Source: Dev.to
The Loop Trap
Here is what happens without an exit condition:
- Agent starts task
- Agent makes progress
- Agent runs out of obvious next steps
- Agent loops — refining, re‑checking, re‑doing
- Task ends when: token limit hits, timeout fires, or someone manually stops it
None of those are done. They are interrupted.
An interrupted agent is not reliable. It is expensive and unpredictable.
Write the Done‑State First
Before you write a single tool call or system prompt, answer three questions:
-
What does success look like?
Not vague. Observable. “File written tooutput/report.jsonwith status: complete” is observable. “Task is finished” is not. -
What is the minimum acceptable output?
This prevents perfectionist loops. The agent should stop when it hits the minimum — not keep iterating toward a theoretical maximum. -
When should it stop and ask instead of continuing?
Ambiguity plus irreversibility equals a mandatory pause. If the agent cannot determine which path to take and the action cannot be undone, it should write the ambiguity tooutbox.jsonand stop.
The Done‑State Pattern
Add this to your current-task.json:
{
"task": "Generate weekly performance report",
"done_when": "report.json exists and contains status: complete",
"stop_if": "data source unavailable after 3 retries",
"minimum_viable_output": "report.json with at least 3 of 5 sections populated",
"max_iterations": 10
}
The agent checks done_when after every action. If true, it stops. If stop_if is triggered, it writes to outbox.json and waits. If max_iterations is reached without completing, it logs what it finished and exits cleanly.
Why This Changes Everything
With a defined done‑state:
- Cost becomes predictable. Loops terminate. You know roughly how many tool calls a task requires.
- Failures become visible. If the agent hits
max_iterationswithout reachingdone_when, that is a signal — not a silent runaway. - Recovery becomes possible. A clean exit with partial output is recoverable. A crash mid‑loop is not.
One team I work with reduced their per‑task cost by 34 % just by adding max_iterations and done_when to their task files. Same model. Same tools. Better stopping.
The Audit Question
For every agent you run right now, ask: “If I left this running for 6 hours, would it stop on its own?”
If the answer is no — or if you are not sure — you do not have an exit condition. You have a hope.
Fix that before you optimize anything else.
The Library at has real current-task.json templates with done‑states built in — for cron agents, multi‑agent teams, and single‑task automations. Updated from production configs, not theory.