Loading preview…
Loading preview…
Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.
Loading preview…
Requirements, wiring steps, and what this pattern adds to your project.
Pick how you want to pull this pattern in — then wire env vars and routes the same way.
Install from the preview toolbar
Copy the install command above and run it in your project — Pro patterns include a short-lived token.
Add environment variables to .env.local
OPENAI_API_KEY
Point client fetches at your API routes
Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.
Customize the agent and tool files
Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.
Run your dev server and open the pattern route
Install dependencies if needed, then start the app and verify responses.
OPENAI_API_KEYGet keyAI SDK APIs
ToolLoopAgentstopWhenstepCountIshasToolCallStopConditiontoolgatewayProviders
app/page.tsxapp/layout.tsxapp/api/loop-control/route.tscomponents/loop-control-demo.tsxcomponents/step-result-display.tsxlib/stop-mode-config.tslib/stop-agents.tslib/infer-stop-reason.tstools/research.tstools/finalize.tsREADME.mdnpm packages
5
Registry components
lib/stop-agents.tsCore pattern: createLoopControlAgent(mode) returns ToolLoopAgent instances with stepCountIs, hasToolCall, combined stopWhen array, and custom StopCondition.
lib/infer-stop-reason.tsMaps GenerateTextResult steps to a human-readable explanation of which stopWhen condition likely terminated the loop.
loop-control/route.tsAPI route accepting prompt + mode, runs agent.generate(), returns step timeline and stopReason metadata.
tools/research.tsDemo research tool that keeps the agent loop running across multiple steps.
tools/finalize.tsCompletion tool used with hasToolCall('finalize') stop condition demonstrations.
Loading preview…
Early-bird pricing — limited time

Open on desktop for the interactive preview.
Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.