Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.
Use this when you are tuning at which step an agent loop halts and want to see each step on a timeline as your stop rules trigger.
Similar patterns, and when each one is the better fit.

Multi-Step Tool PatternUse this when a chat agent must call several typed tools in a row, feeding each result into the next step, with a step limit to stop runaway loops.
PrepareStep: Trim Message HistoryUse this when you want to drop stale turns but keep recent tool results before every agent step, and see per-step telemetry on what was cut.
Tool Input Lifecycle HooksUse this when you want UI state, like a spinner or progress label, to react to tool input starting, streaming, and becoming fully available.
Human in the Loop Plan Builder AgentUses ToolLoopAgent pattern to gather requirements and generate plans. Generates markdown plans with structured todo lists.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.
Download → Next.js app
Use Download → Next.js app in the preview toolbar. You get a zip scaffold (Pro downloads rewrite preview `/view/.../api/*` paths to `/api/*` automatically).
Extract the zip and install dependencies
Unzip the archive, then run `pnpm install` (or npm / yarn) in the project folder.
Fill .env.local
The download includes .env.local placeholders when needed. Add real values for: OPENAI_API_KEY.
Customize the agent and tool files
Adapt prompts, tools, and stop conditions in the generated project for your product.
Run the app and open the pattern route
Run `pnpm dev`, open the setup/checklist page if present, then navigate to the generated pattern route 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.
Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.
Use this when you are tuning at which step an agent loop halts and want to see each step on a timeline as your stop rules trigger.
Similar patterns, and when each one is the better fit.

Multi-Step Tool PatternUse this when a chat agent must call several typed tools in a row, feeding each result into the next step, with a step limit to stop runaway loops.
PrepareStep: Trim Message HistoryUse this when you want to drop stale turns but keep recent tool results before every agent step, and see per-step telemetry on what was cut.
Tool Input Lifecycle HooksUse this when you want UI state, like a spinner or progress label, to react to tool input starting, streaming, and becoming fully available.
Human in the Loop Plan Builder AgentUses ToolLoopAgent pattern to gather requirements and generate plans. Generates markdown plans with structured todo lists.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.
Download → Next.js app
Use Download → Next.js app in the preview toolbar. You get a zip scaffold (Pro downloads rewrite preview `/view/.../api/*` paths to `/api/*` automatically).
Extract the zip and install dependencies
Unzip the archive, then run `pnpm install` (or npm / yarn) in the project folder.
Fill .env.local
The download includes .env.local placeholders when needed. Add real values for: OPENAI_API_KEY.
Customize the agent and tool files
Adapt prompts, tools, and stop conditions in the generated project for your product.
Run the app and open the pattern route
Run `pnpm dev`, open the setup/checklist page if present, then navigate to the generated pattern route 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.
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.