Skip to main content

Loop Control

You can control both the execution flow and the settings at each step of the agent loop. The loop continues until:
  • A finish reasoning other than tool-calls is returned, or
  • A tool that is invoked does not have an execute function, or
  • A tool call needs approval, or
  • A stop condition is met
The AI SDK provides built-in loop control through two parameters: stopWhen for defining stopping conditions and prepareStep for modifying settings (model, tools, messages, and more) between steps.

Stop Conditions

The stopWhen parameter controls when to stop execution when there are tool results in the last step. By default, agents stop after 20 steps using stepCountIs(20). When you provide stopWhen, the agent continues executing after tool calls until a stopping condition is met. When the condition is an array, execution stops when any of the conditions are met.

Use Built-in Conditions

The AI SDK provides several built-in stopping conditions:

Combine Multiple Conditions

Combine multiple stopping conditions. The loop stops when it meets any condition:

Create Custom Conditions

Build custom stopping conditions for specific requirements:
Custom conditions receive step information across all steps:

Prepare Step

The prepareStep callback runs before each step in the loop and defaults to the initial settings if you don’t return any changes. Use it to modify settings, manage context, or implement dynamic behavior based on execution history.

Dynamic Model Selection

Switch models based on step requirements:

Context Management

Manage growing conversation history in long-running loops:

Tool Selection

Control which tools are available at each step:
You can also force a specific tool to be used:

Message Modification

Transform messages before sending them to the model:

Access Step Information

Both stopWhen and prepareStep receive detailed information about the current execution:

Forced Tool Calling

You can force the agent to always use tools by combining toolChoice: 'required' with a done tool that has no execute function. This pattern ensures the agent uses tools for every step and stops only when it explicitly signals completion.
Key aspects of this pattern:
  • toolChoice: 'required': Forces the model to call a tool at every step instead of generating text directly. This ensures the agent follows a structured workflow.
  • done tool without execute: A tool that has no execute function acts as a termination signal. When the agent calls this tool, the loop stops because there’s no function to execute.
  • Accessing results: The final answer is available in result.staticToolCalls, which contains tool calls that weren’t executed.
This pattern is useful when you want the agent to always use specific tools for operations (like code execution or data retrieval) rather than attempting to answer directly.

Manual Loop Control

For scenarios requiring complete control over the agent loop, you can use AI SDK Core functions (generateText and streamText) to implement your own loop management instead of using stopWhen and prepareStep. This approach provides maximum flexibility for complex workflows.

Implementing a Manual Loop

Build your own agent loop when you need full control over execution:
This manual approach gives you complete control over:
  • Message history management
  • Step-by-step decision making
  • Custom stopping conditions
  • Dynamic tool and model selection
  • Error handling and recovery
Learn more about manual agent loops in the cookbook.