M
MastraAIβ€’23h ago
Dough_Nut

How to configure stopWhen conditions in Mastra agents?

Built a web automation agent with 4 tools (navigate, observe, act, extract) and it's working great actions execute smoothly but the issue is that the agent stops after exactly 5 tool calls even when the task isn't finished. For complex workflows (like multi-step booking flows), I need it to keep going until the task is actually complete. What I need is to configure stopWhen conditions or max steps so the agent can complete longer tasks. Looking for config options when creating the agent, I've tried these two but nothing works.
export const tsukiAgent = new Agent({
name: "Tsuki Web Automation Agent",
// Can't find where to set maxSteps or stopWhen?
});

export const mastra = new Mastra({
agents: { tsukiAgent },
// Or configure it here?
});
export const tsukiAgent = new Agent({
name: "Tsuki Web Automation Agent",
// Can't find where to set maxSteps or stopWhen?
});

export const mastra = new Mastra({
agents: { tsukiAgent },
// Or configure it here?
});
Building custom API to use .generate() with options:
typescriptconst result = await mastra.getAgent("tsukiAgent").generate(instruction, {
stopWhen: stepCountIs(20)
});
typescriptconst result = await mastra.getAgent("tsukiAgent").generate(instruction, {
stopWhen: stepCountIs(20)
});
This works in theory, but ran into Playwright bundling issues (ReferenceError: __name is not defined) that I'd rather avoid if there's a simpler way. Is there a way to set stopWhen or max steps directly when registering an agent with Mastra, so I can use mastra dev without building a custom API wrapper? Tech Stack: @mastra/core: 0.21.1 playwright: 1.56.0 crawlee: 3.15.1 AI Models tested - Gemini 2.5 Flash, GPT 5, GPT 4o-mini, GPT 4o, Haiku 4.5
Thanks! πŸ™
4 Replies
_roamin_
_roamin_β€’17h ago
Hey @Dough_Nut ! We support stopWhen natively, you can use it when calling generate or stream:
const result = await weatherAgent.stream(
"...",
{
stopWhen: stepCountIs(20)
}
);
const result = await weatherAgent.stream(
"...",
{
stopWhen: stepCountIs(20)
}
);
Dough_Nut
Dough_NutOPβ€’17h ago
I tried using that with crawlee but i keep getting errors unless I use mastra dev with the mastra UI. Was hoping if there was an alternative route to override it. Using crawlee (playwright version) without mastra kept creating a __name not defined issue. So thats why I wanted a simpler way to add a stopWhen
_roamin_
_roamin_β€’17h ago
Hmm, unfortunately that is the only way to add it.
Dough_Nut
Dough_NutOPβ€’17h ago
Alrighty, Thanks alot for the helpπŸ™Œ

Did you find this page helpful?