The Agent class provides a structured way to encapsulate LLM configuration, tools, and behavior into reusable components. It handles the agent loop for you, allowing the LLM to call tools multiple times in sequence to accomplish complex tasks. Define agents once and use them across your application.
Define an agent by instantiating the Agent class with your desired configuration:
import { Experimental_Agent as Agent } from 'ai';
const myAgent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: 'You are a helpful assistant.',
tools: {
// Your tools here
},
});
import { Experimental_Agent as Agent } from 'ai';
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: 'You are an expert software engineer.',
});
By default, agents run for a single step (stopWhen: stepCountIs(1)). In each step, the model either generates text or calls a tool. If it generates text, the agent completes. If it calls a tool, the AI SDK executes that tool.
To let agents call multiple tools in sequence, configure stopWhen to allow more steps. After each tool execution, the agent triggers a new generation where the model can call another tool or generate text:
import { Experimental_Agent as Agent, stepCountIs } from 'ai';
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
stopWhen: stepCountIs(20), // Allow up to 20 steps
});
Each step represents one generation (which results in either text or a tool call). The loop continues until:
The model generates text instead of calling a tool, or
A stop condition is met
You can combine multiple conditions:
import { Experimental_Agent as Agent, stepCountIs } from 'ai';
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
stopWhen: [
stepCountIs(20), // Maximum 20 steps
yourCustomCondition(), // Custom logic for when to stop
],
});
import { Experimental_Agent as Agent } from 'ai';
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
tools: {
// your tools here
},
toolChoice: 'required', // Force tool use
// or toolChoice: 'none' to disable tools
// or toolChoice: 'auto' (default) to let the model decide
});
You can also force the use of a specific tool:
import { Experimental_Agent as Agent } from 'ai';
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
tools: {
weather: weatherTool,
cityAttractions: attractionsTool,
},
toolChoice: {
type: 'tool',
toolName: 'weather', // Force the weather tool to be used
},
});
System prompts define your agent's behavior, personality, and constraints. They set the context for all interactions and guide how the agent responds to user queries and uses tools.
const agent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system:
'You are an expert data analyst. You provide clear insights from complex data.',
});
const codeReviewAgent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: `You are a senior software engineer conducting code reviews.
Your approach:
- Focus on security vulnerabilities first
- Identify performance bottlenecks
- Suggest improvements for readability and maintainability
- Be constructive and educational in your feedback
- Always explain why something is an issue and how to fix it`,
});
const customerSupportAgent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: `You are a customer support specialist for an e-commerce platform.
Rules:
- Never make promises about refunds without checking the policy
- Always be empathetic and professional
- If you don't know something, say so and offer to escalate
- Keep responses concise and actionable
- Never share internal company information`,
tools: {
checkOrderStatus,
lookupPolicy,
createTicket,
},
});
const researchAgent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: `You are a research assistant with access to search and document tools.
When researching:
1. Always start with a broad search to understand the topic
2. Use document analysis for detailed information
3. Cross-reference multiple sources before drawing conclusions
4. Cite your sources when presenting information
5. If information conflicts, present both viewpoints`,
tools: {
webSearch,
analyzeDocument,
extractQuotes,
},
});
Control the output format and communication style:
const technicalWriterAgent = new Agent({
model: "anthropic/claude-sonnet-5.5",
system: `You are a technical documentation writer.
Writing style:
- Use clear, simple language
- Avoid jargon unless necessary
- Structure information with headers and bullet points
- Include code examples where relevant
- Write in second person ("you" instead of "the user")
Always format responses in Markdown.`,
});
import { Experimental_Agent as Agent, Experimental_InferAgentUIMessage as InferAgentUIMessage,} from 'ai';const myAgent = new Agent({ // ... configuration});// Infer the UIMessage type for UI components or persistenceexport type MyAgentUIMessage = InferAgentUIMessage<typeof myAgent>;
Use this type in your client components with useChat:
components/chat.tsx
'use client';import { useChat } from '@ai-sdk/react';import type { MyAgentUIMessage } from '@/agent/my-agent';export function Chat() { const { messages } = useChat<MyAgentUIMessage>(); // Full type safety for your messages and tools}