Skip to content

🎓 最佳实践

1. 从简单开始

typescript
// ✅ 先验证基础功能
const simpleAgent = new ToolLoopAgent({
  model: "anthropic/claude-sonnet-4.5",
  instructions: 'You are a helpful assistant.',
});

// 然后逐步添加复杂性

2. 明确的系统指令

typescript
// ✅ 具体、可操作的指令
instructions: `You are a code reviewer.
1. Focus on security vulnerabilities first
2. Then check performance issues
3. Finally suggest readability improvements
4. Always explain why something is a problem`

// ❌ 模糊的指令
instructions: 'Review the code well'

3. 类型安全至上

typescript
// ✅ 使用 Zod 定义明确的 schema
const tools = {
  search: tool({
    inputSchema: z.object({
      query: z.string().min(1).describe('Search query'),
      limit: z.number().optional().default(10),
    }),
    // ...
  }),
};

4. 控制成本

typescript
// ✅ 设置合理的停止条件
stopWhen: [
  stepCountIs(10),  // 步数限制
  budgetExceeded,   // 成本限制
]

5. 优雅的错误处理

typescript
// ✅ 在工具中处理错误
execute: async ({ input }) => {
  try {
    const result = await riskyOperation(input);
    return { success: true, data: result };
  } catch (error) {
    return { success: false, error: error.message };
  }
},

前端面试知识库