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
Thanks! π
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.
Building custom API to use .generate() with options:
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.5Thanks! π
4 Replies
Hey @Dough_Nut ! We support
stopWhen
natively, you can use it when calling generate
or stream
:
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
Hmm, unfortunately that is the only way to add it.
Alrighty, Thanks alot for the helpπ