content/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx
stepCountIs()Creates a stop condition that stops when the number of steps reaches a specified count.
This function is used with stopWhen in generateText and streamText to control when a tool-calling loop should stop based on the number of steps executed.
import { generateText, stepCountIs } from 'ai';
__PROVIDER_IMPORT__;
const result = await generateText({
model: __MODEL__,
tools: {
// your tools
},
// Stop after 5 steps
stopWhen: stepCountIs(5),
});
<Snippet text={import { stepCountIs } from "ai"} prompt={false} />
<PropertiesTable content={[ { name: 'count', type: 'number', description: 'The maximum number of steps to execute before stopping the tool-calling loop.', }, ]} />
A StopCondition function that returns true when the step count reaches the specified number. The function can be used with the stopWhen parameter in generateText and streamText.
Stop after 3 steps:
import { generateText, stepCountIs } from 'ai';
const result = await generateText({
model: yourModel,
tools: yourTools,
stopWhen: stepCountIs(3),
});
You can combine multiple stop conditions in an array:
import { generateText, stepCountIs, hasToolCall } from 'ai';
const result = await generateText({
model: yourModel,
tools: yourTools,
// Stop after 10 steps OR when finalAnswer tool is called
stopWhen: [stepCountIs(10), hasToolCall('finalAnswer')],
});