Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Expert guidance for building AI agents with ToolLoopAgent (AI SDK v6+). Use when creating agents, configuring stopWhen/prepareStep, callOptionsSchema/prepareCall, dynamic tool selection, tool loops, or agent workflows (sequential, routing, evaluator-optimizer, orchestrator-worker). Triggers: ToolLoopAgent, agent loop, stopWhen, stepCountIs, prepareStep, callOptionsSchema, prepareCall, hasToolCall, InferAgentUIMessage, agent workflows.
.claude/skills/majiayu000-ai-sdk-agents/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -14% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 5% | 0% |
| case-03 | ✗→✓ | ▲ Improved | -13% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 19% | 0% |
| case-07 | ✗→✓ | ▲ Improved | -8% | 0% |
Build autonomous agents with ToolLoopAgent: reusable model + tools + loop control.
Assume Zod v4.3.5 for schema typing.
tsimport { ToolLoopAgent, tool } from 'ai'; import { anthropic } from '@ai-sdk/anthropic'; import { z } from 'zod'; const weatherAgent = new ToolLoopAgent({ model: anthropic('claude-sonnet-4-20250514'), tools: { weather: tool({ description: 'Get the weather in a location (F)', inputSchema: z.object({ location: z.string() }), execute: async ({ location }) => ({ location, temperature: 72 }), }), }, }); const result = await weatherAgent.generate({ prompt: 'What is the weather in San Francisco?', });
tsimport { ToolLoopAgent, Output } from 'ai'; import { z } from 'zod'; const analysisAgent = new ToolLoopAgent({ model: 'openai/gpt-4o', output: Output.object({ schema: z.object({ sentiment: z.enum(['positive', 'neutral', 'negative']), summary: z.string(), }), }), });
tsconst stream = myAgent.stream({ prompt: 'Summarize this report' }); for await (const chunk of stream.textStream) { process.stdout.write(chunk); }
tsimport { createAgentUIStreamResponse } from 'ai'; export async function POST(request: Request) { const { messages } = await request.json(); return createAgentUIStreamResponse({ agent: myAgent, messages }); }
tsimport { ToolLoopAgent, InferAgentUIMessage } from 'ai'; const myAgent = new ToolLoopAgent({ model, tools }); export type MyAgentUIMessage = InferAgentUIMessage<typeof myAgent>;
stopWhen (default: stepCountIs(20)) for safety.hasToolCall('finalAnswer') to stop on terminal actions.prepareStep to swap models, compress messages, or limit tools per step.callOptionsSchema to define type-safe runtime options.prepareCall to select model/tools or inject RAG context once per call.prepareStep for per-step decisions (budget limits, dynamic tools).| Reference | When to Use | |-----------|-------------| | references/fundamentals.md | ToolLoopAgent basics, Output types, streaming | | references/loop-control.md | stopWhen, hasToolCall, prepareStep patterns | | references/configuration.md | callOptionsSchema, prepareCall vs prepareStep | | references/workflow-patterns.md | multi-agent workflows and routing | | references/real-world.md | RAG, multimodal, file processing | | references/production.md | monitoring, safety, cost control | | references/migration.md | v6 migration notes |
Other measured skills in the registry, with their headline benchmark lift.