03 Tool loop and max iterations
Before you start
Complete 02 Attach tools to agents. You should have at least one tool attached to an agent in a workflow step.
Concept
When an agent has tools, the runtime may enter a tool loop: the model proposes a tool call, your execute function runs, and the result returns to the model until the step finishes or a limit is hit.
Bound that loop with ToolExecutionConfig on the agent:
| Field | Purpose |
|---|---|
mode | "llm_select" (default) lets the model choose tools; "legacy_eager" uses eager execution semantics |
max_iterations | Maximum tool loop rounds for this agent (1 to 20 inclusive) |
Each iteration is one select-and-execute cycle. If the agent needs many tool calls, raise max_iterations within the allowed cap. If you want tighter cost control, lower it.
Invalid values raise WorkflowConfigurationError at construction time (for example max_iterations=0 or an unknown mode).
Tool invocation timeouts are separate: each Tool has its own timeout_seconds. A slow execute function can raise ToolExecutionError even when loop iterations remain.
Example
Save as tool_loop_config.py:
Run:
Verify
| Check | Expected |
|---|---|
| Valid config | Agent constructs without error |
max_iterations=0 | Raises WorkflowConfigurationError |
max_iterations=21 | Raises WorkflowConfigurationError |
| Invalid mode | mode="auto" raises WorkflowConfigurationError |
Bounds check:
Next
04 Common tools bundle introduces prebuilt web and document tools you can attach instead of writing your own.