Explicit Termination Rules: Stopping the Agent

Explicit Termination Rules: Stopping the Agent

Learn how to define 'Success and Failure' for autonomous agents. Master the implementation of termination nodes to prevent infinite reasoning loops.

Explicit Termination Rules: Stopping the Agent

One of the most dangerous traits of an LLM is its "Helpfulness." When an agent fails to find an answer, its default behavior is to try again, perhaps with a slightly different search query. Without an Explicit Termination Rule, the agent enters a "Recursive Descent" into your token budget.

In this lesson, we learn how to define hard boundaries for agentic missions. We’ll explore Success Criteria, Failure Thresholds, and the "Give Up" Node architecture.


1. The "Helpfulness" Trap

An agent without termination rules behaves like this:

  1. Goal: Find the SKU for a 1995 Blue Sweater.
  2. Action: Search DB. (0 results).
  3. Reasoning: "Maybe I should search Google."
  4. Action: Search Google. (1,000 messy results).
  5. Reasoning: "I will read all 1,000 results to be helpful."
  6. Result: $50.00 in tokens to tell the user the sweater is out of stock.

2. Defining "Success" and "Failure" Nodes

In a well-designed agent (like those in LangGraph), "End" is not a single state. It is two states: SUCCESS and FATAL_FAILURE.

The "Failure Threshold"

You must define a Maximum Number of Attempts for any specific tool.

  • If search_tool has been called 3 times with 0 results: TERMINATE.
graph TD
    A[Start] --> B[Tool Call]
    B --> C{Result?}
    C -->|Yes| D[Success Node]
    C -->|No| E{Attempts < 3?}
    E -->|Yes| B
    E -->|No| F[Give Up Node]
    
    style F fill:#f66
    style D fill:#6f6

3. Implementation: The Hard-Cap Governor (Python)

Python Code: The Termination Check

class AgentGovernor:
    def __init__(self, max_turns=5):
        self.turns = 0
        self.max_turns = max_turns

    def check_termination(self, result):
        self.turns += 1
        
        # Condition 1: Goal achieved
        if "FINAL_ANSWER" in result:
            return "SUCCESS"
            
        # Condition 2: Budget exhausted
        if self.turns >= self.max_turns:
            return "MAX_TURNS_REACHED"
        
        # Condition 3: No new info found
        if result == "NO_NEW_DATA":
            return "UNRESOLVABLE"
            
        return "CONTINUE"

4. The "I Don't Know" Reward Pattern

In your system prompt, you must explicitly Reward the agent for stopping. LLMs are trained on datasets where "I don't know" is often penalized. You must reverse this.

System Prompt Fragment:

"Efficiency is your primary directive. If after two searches you cannot find the answer, DO NOT apologize and DO NOT try a third time. Simply output 'STATUS: NOT_FOUND' and stop. Stopping early on a difficult task is considered a success."


5. Token Savings: Preventing the Tail

The majority of token waste happens in the "Tail" of a conversation—the last 3-4 turns where the agent is struggling to find a solution. By cutting that tail, you save the most expensive tokens (the ones where context is at its largest).


6. Summary and Key Takeaways

  1. Explicit Exit Conditions: Every loop needs a break statement.
  2. Attempts Property: Track how many times a tool has failed.
  3. Negative Constraint: Tell the agent that "Giving Up" is an acceptable outcome.
  4. Budget Alignment: Match max_turns to the user's tier (e.g. Free users get 3 turns, Pro users get 10).

In the next lesson, Bounded Planning Depth, we look at چگونه to stop the agent from thinking too far into the future.


Exercise: The Loop Breaker

  1. Modify an agent script to include a turn_count variable.
  2. Set max_turns = 2.
  3. Give the agent an impossible task (e.g., "Find the secret password for the Moon").
  4. Observe: Did the agent stop after 2 turns?
  5. Analyze: How many tokens were saved compared to letting it run until the context window was full?

Congratulations on completing Module 10 Lesson 1! You are now a responsible agentic governor.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn