AI Space

AI Models & Assistants / Knowledge library

Agent Termination Condition

Defines when an agent succeeds, fails, pauses, or stops because a budget is exhausted

Agent loop control rule

Why learn Agent Termination Condition?

It bounds autonomy and prevents useless, repetitive, or unexpectedly expensive execution.

The problem it solves

It gives every agent loop explicit exit behavior instead of relying on indefinite model judgment.

Where it came from

Termination can depend on a typed final output, evaluator approval, goal completion, no remaining actions, a deadline, turn count, token cost, or unrecoverable error. A model merely saying it is done is not always sufficient evidence.

Build your understanding

Work through these exercises in order. Try the task before opening a reference.

  1. Define Agent Termination Condition and identify its place in an agent architecture.
  2. Choose a task where Agent Termination Condition improves an agent system.
  3. Diagnose one failure caused by misusing Agent Termination Condition.
  4. Interpret a trace or state transition involving Agent Termination Condition.
  5. Explain one operational tradeoff of Agent Termination Condition.

Documentation and sources

Connect the ideas

Explore next

Agent TurnAgent Evaluation LoopAgent Retry LoopEvaluator-Optimizer Loop

Real World Example

Illustrative business scenario · AI-generated from linked documentation · Not a measured customer case study

Automated Research Budget Guardrails

Financial analysts performing automated market research using AI agents.

The business problem

Unbounded agent loops can lead to excessive API costs or infinite reasoning cycles when research tasks become overly complex or ambiguous.

How this topic solves it

Implementing a MaxTurnsExceeded termination condition via the runner configuration to force-stop agents after a predefined number of interaction cycles.

  1. Define an Agent instance with specific research instructions.
  2. Initialize a RunConfig object with a strict max_turns integer value.
  3. Execute the agent using the Runner class passing the custom RunConfig.
  4. Catch the MaxTurnsExceeded exception in the application logic to handle budget-exhausted states.
  5. Log the final state of the agent for audit and manual review.
Agent Termination Flow · Process flow

The lifecycle of an agent turn and how the runner evaluates termination.

LLM Call

Agent generates response or tool call

Turn Counter

Runner increments current turn count

Limit Check

Compare current turn vs max_turns

Success

Agent provides final output

Failure

MaxTurnsExceeded exception raised

  • LLM Call → process output → Turn Counter
  • Turn Counter → evaluate → Limit Check
  • Limit Check → within limit → Success
  • Limit Check → exceeds limit → Failure

Implementation example

This code demonstrates setting a hard limit on agent interactions to ensure the Automated Research Budget Guardrails are enforced.

python · Illustrative code; not executed or production-validated.

from agents import Agent, Runner, RunConfig
# Define agent with research goal
agent = Agent(name="Researcher", instructions="Analyze market trends.")
# Configure max turns to prevent infinite loops
config = RunConfig(max_turns=5)
# Run with explicit limit
try:
    result = await Runner.run(agent, "Analyze sector X", run_config=config)
except MaxTurnsExceeded:
    # Handle budget exhaustion
    print("Research stopped: budget exhausted.")

Expected business outcome

Predictable resource consumption and prevention of runaway agent costs during complex research tasks.

Limitations and checks

The MaxTurnsExceeded limit is a hard stop that may interrupt valid, long-running research; developers must check if the agent reached a natural conclusion before the limit.

Sources

Generated 2026-10-18 with gemini-3.1-flash-lite. Your assessment includes two questions about this example.

Catalog source: technology-maxxing. This page reproduces the existing catalog’s learning material and source links.