The Exit Condition First Rule: Why Your AI Agent Needs a Done-State Before a First Tool Call

Published: (March 8, 2026 at 02:57 PM EDT)
3 min read
Source: Dev.to

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:

  1. What does success look like?
    Not vague. Observable. “File written to output/report.json with status: complete” is observable. “Task is finished” is not.

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

  3. 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 to outbox.json and 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_iterations without reaching done_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.

0 views
Back to Blog

Related posts

Read more »