Agent generates response or tool call
AI Models & Assistants / Knowledge library
Agent Termination Condition
Defines when an agent succeeds, fails, pauses, or stops because a budget is exhausted
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.
- Define Agent Termination Condition and identify its place in an agent architecture.
- Choose a task where Agent Termination Condition improves an agent system.
- Diagnose one failure caused by misusing Agent Termination Condition.
- Interpret a trace or state transition involving Agent Termination Condition.
- Explain one operational tradeoff of Agent Termination Condition.
Documentation and sources
- OpenAI Agents SDK: Running agents (openai.github.io)
Connect the ideas
Explore next
Real World Example
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.
- Define an Agent instance with specific research instructions.
- Initialize a RunConfig object with a strict max_turns integer value.
- Execute the agent using the Runner class passing the custom RunConfig.
- Catch the MaxTurnsExceeded exception in the application logic to handle budget-exhausted states.
- Log the final state of the agent for audit and manual review.
The lifecycle of an agent turn and how the runner evaluates termination.
Runner increments current turn count
Compare current turn vs max_turns
Agent provides final output
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.
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.