Stopping Conditions: Goal Achievement
You'll learn how to detect when your AI agent has successfully completed its objective and should stop executing.
The Core Idea
While max iterations prevent runaway loops, your agent should ideally stop because it succeeded, not because it ran out of tries. Goal achievement detection means teaching your agent to recognize when its task is done—like a chef knowing when the dish is ready, not just when the timer beeps.
Your agent needs explicit criteria to evaluate: "Have I accomplished what I set out to do?" This requires clear success signals built into your loop termination logic.
How Goal Detection Works
At the end of each agent iteration, you evaluate whether the goal state has been reached. This might mean:
- Explicit goal markers: The agent declares "task complete" in its output
- Observable state changes: A specific condition is now true (file exists, query answered, approval received)
- Validation checks: The result passes predefined quality or completeness tests
Unlike max iterations (which you set once), goal achievement checking happens dynamically based on what the agent observes and produces.
Practical Patterns
Common goal detection strategies:
- Agent self-declaration: The LLM explicitly outputs a "done" signal or uses a specific tool like
task_complete() - State validation: Check if tracked state matches success criteria (e.g.,
state.tickets_resolved >= target_count) - Output pattern matching: Look for phrases like "Task finished" or structured completion markers
- External verification: A separate validator confirms the work meets requirements
You typically combine this with iteration limits as a safety net: stop when goal achieved OR max iterations reached.
Key Takeaway: Goal achievement detection lets your agent stop gracefully when successful rather than exhausting iterations—check explicit success signals, state conditions, or validation results at each loop iteration.