⚙️ 配置选项
模型与系统指令
typescript
import { ToolLoopAgent } from 'ai';
const agent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-4.5",
instructions: 'You are an expert software engineer.',
});工具配置
typescript
import { ToolLoopAgent, tool } from 'ai';
import { z } from 'zod';
const codeAgent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-4.5",
tools: {
runCode: tool({
description: 'Execute Python code',
inputSchema: z.object({
code: z.string(),
}),
execute: async ({ code }) => {
return { output: 'Code executed successfully' };
},
}),
},
});工具选择模式
typescript
const agent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-4.5",
tools: { /* your tools */ },
toolChoice: 'required', // 强制使用工具
// toolChoice: 'none' // 禁用工具
// toolChoice: 'auto' // 让模型决定(默认)
});
// 强制使用特定工具
const specificToolAgent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-4.5",
tools: {
weather: weatherTool,
cityAttractions: attractionsTool,
},
toolChoice: {
type: 'tool',
toolName: 'weather',
},
});结构化输出
typescript
import { ToolLoopAgent, Output, stepCountIs } from 'ai';
import { z } from 'zod';
const analysisAgent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-4.5",
output: Output.object({
schema: z.object({
sentiment: z.enum(['positive', 'neutral', 'negative']),
summary: z.string(),
keyPoints: z.array(z.string()),
}),
}),
stopWhen: stepCountIs(10),
});
const { output } = await analysisAgent.generate({
prompt: 'Analyze customer feedback from the last quarter',
});