Skip to content

[FEATURE]: Add max_iterations control to the Agent execution loop #2298

@samarthpatel-bizom

Description

@samarthpatel-bizom

Problem Statement

Problem Description
Currently, when using the Agent class in the Strands SDK, the agent operates in a continuous loop until the LLM decides to provide a final response. While this allows for complex reasoning, it creates two significant risks in a production environment:

Infinite Reasoning Loops: If an agent encounters a tool error or a confusing prompt, it may enter a "hallucination loop," calling tools repeatedly without reaching a conclusion.

Unpredictable Latency/Cost: Without an explicit depth limit, a single user request could potentially trigger dozens of model turns, leading to high token costs and poor user experience.

Proposed Solution

I propose adding a max_iterations (or max_depth) parameter to the Agent configuration and its execution methods. This would track the number of "Model Turn -> Tool Call -> Tool Result" cycles and gracefully terminate if the limit is reached.

Suggested changes:

Add max_iterations: int = 10 to the Agent constructor or the run() method.

Implement a counter within the execution loop (likely in agent.py).

Raise a specific AgentLimitReachedException or return a partial response when the limit is hit, allowing developers to handle the timeout gracefully.

Use Case

Example Usage

agent = Agent(
tools=[my_tool],
max_iterations=5 # Agent will stop after 5 reasoning cycles
)

try:
response = agent.run("Perform a complex multi-step task")
except MaxIterationsReachedError:
print("Agent was unable to finish within the allowed steps.")

Alternatives Solutions

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions