ai-sdk-agents
AI SDK Agents
Build autonomous agents with ToolLoopAgent: reusable model + tools + loop control.
Quick Start
Assume Zod v4.3.5 for schema typing.
import { 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?',
});
When to Use ToolLoopAgent vs Core Functions
- Use ToolLoopAgent for dynamic, multi-step tasks where the model decides which tools to call.
- Use generateText/streamText for deterministic flows or strict ordering.
Essential Patterns
Structured Output
import { 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(),
}),
}),
});
Streaming Agent
const stream = myAgent.stream({ prompt: 'Summarize this report' });
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}
API Route
import { createAgentUIStreamResponse } from 'ai';
export async function POST(request: Request) {
const { messages } = await request.json();
return createAgentUIStreamResponse({ agent: myAgent, messages });
}
Type-Safe Client Integration
import { ToolLoopAgent, InferAgentUIMessage } from 'ai';
const myAgent = new ToolLoopAgent({ model, tools });
export type MyAgentUIMessage = InferAgentUIMessage<typeof myAgent>;
Loop Control Checklist
- Set
stopWhen(default:stepCountIs(20)) for safety. - Use
hasToolCall('finalAnswer')to stop on terminal actions. - Use
prepareStepto swap models, compress messages, or limit tools per step.
Runtime Configuration
- Use
callOptionsSchemato define type-safe runtime options. - Use
prepareCallto select model/tools or inject RAG context once per call. - Use
prepareStepfor per-step decisions (budget limits, dynamic tools).
Reference Files
| 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 |
More from bjornmelin/dev-skills
streamdown
|
13zod-v4
Expert guidance for Zod v4 schema validation in TypeScript. Use when designing schemas, migrating from Zod 3, handling validation errors, generating JSON Schema/OpenAPI, using codecs/transforms, or integrating with React Hook Form, tRPC, Hono, or Next.js. Covers all Zod v4 APIs including top-level string formats, strictObject/looseObject, metadata, registries, branded types, and recursive schemas.
9ai-sdk-core
|
5supabase-ts
Production-ready Supabase integration patterns for Next.js/React/TypeScript applications. Use when working with Supabase for (1) SSR authentication with @supabase/ssr, (2) Database operations and migrations, (3) Row Level Security (RLS) policies, (4) Storage buckets and file uploads, (5) Realtime channels and presence, (6) Edge Functions with Deno, (7) pgvector embeddings and semantic search, (8) Vercel deployment and connection pooling, (9) CLI operations (type generation, migrations). Triggers on Supabase client setup, auth patterns, RLS policies, storage uploads, realtime subscriptions, Edge Functions, vector search, or Vercel+Supabase deployment.
2gh-deps-intel
Runtime-aware dependency upgrade intelligence for JavaScript/TypeScript and Python repositories (including monorepos/turborepos), using package-manager outdated checks plus GitHub API release/changelog mining to produce definitive Markdown+JSON upgrade/refactor reports. Use when auditing dependency upgrades, planning compatible version bumps, mapping deprecations/breaking changes, standardizing GitHub API/CLI dependency workflows, or when asked to fully upgrade a specific dependency/package (for example: "use $gh-deps-intel to fully upgrade workflow").
1gh-pr-review-fix
Fetch unresolved GitHub PR review threads, normalize them, fix them end-to-end, verify the results, and re-check until the PR is clean or blocked. Use when the user wants GitHub PR comments resolved with minimal verified fixes. Do not use for local review files, Codex reviews, or Zen reviews; review-remediation owns those.
1