ClaudeAgentSDK代理创建Skill claude-agent-sdk-agent-creation

此技能专注于使用Claude Agent SDK在TypeScript中创建和配置AI智能体。包括代理初始化、系统提示配置、工具权限管理、目录结构设置、认证集成等。关键词:AI代理, Claude SDK, TypeScript, 智能体创建, 代理配置, 工具权限, 认证, 大模型应用。

AI智能体 0 次安装 0 次浏览 更新于 3/25/2026

name: claude-agent-sdk-agent-creation user-invocable: false description: 使用代理SDK创建或配置Claude AI代理时使用。涵盖代理初始化、配置和基本设置模式。 allowed-tools:

  • Read
  • Write
  • Edit
  • Bash
  • Grep
  • Glob

Claude Agent SDK - 代理创建

使用TypeScript的Claude Agent SDK创建和配置AI代理。

核心代理初始化

基本代理创建

import { Agent } from '@anthropic-ai/claude-agent-sdk';

const agent = new Agent({
  model: 'claude-3-5-sonnet-20241022',  // 最新模型
  systemPrompt: 'You are a helpful assistant specialized in...',
  settingSources: ['project'],  // 从项目加载 .claude/CLAUDE.md
  allowedTools: ['read_file', 'write_file', 'list_files'],
});

配置选项

系统提示

// 直接系统提示
const agent = new Agent({
  systemPrompt: 'You are an expert code reviewer...',
});

// 从CLAUDE.md加载
const agent = new Agent({
  settingSources: ['project'],  // 加载 ./.claude/CLAUDE.md
});

// 用户级内存
const agent = new Agent({
  settingSources: ['user'],  // 加载 ~/.claude/CLAUDE.md
});

工具权限

// 允许特定工具
const agent = new Agent({
  allowedTools: [
    'read_file',
    'write_file',
    'list_files',
    'grep',
    'bash',
  ],
});

// 阻止特定工具
const agent = new Agent({
  disallowedTools: ['bash', 'web_search'],
});

// 权限模式
const agent = new Agent({
  permissionMode: 'strict',  // 需要显式批准
});

代理目录结构

所需结构

project/
├── .claude/
│   ├── CLAUDE.md              # 项目内存
│   ├── agents/
│   │   └── specialist.md      # 子代理定义
│   ├── skills/
│   │   └── my-skill/
│   │       └── SKILL.md       # 技能定义
└── src/
    └── index.ts               # 你的代码

认证

环境变量

# Anthropic API (主要)
export ANTHROPIC_API_KEY="sk-ant-..."

# 替代提供者
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_REGION="us-east-1"

# Google Vertex AI
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"

# Azure
export AZURE_OPENAI_API_KEY="..."
export AZURE_OPENAI_ENDPOINT="..."

SDK配置

// Anthropic 直接
const agent = new Agent({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

// Amazon Bedrock
const agent = new Agent({
  provider: 'bedrock',
  model: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
});

最佳实践

始终指定模型

// 好
const agent = new Agent({
  model: 'claude-3-5-sonnet-20241022',
});

// 避免:依赖默认模型
const agent = new Agent({});

使用显式设置源

// 好
const agent = new Agent({
  settingSources: ['project'],
});

// 避免:不明确的内存源
const agent = new Agent({
  systemPrompt: '...',
});

分离项目和用户内存

// 项目特定上下文
const projectAgent = new Agent({
  settingSources: ['project'],
});

// 用户偏好
const userAgent = new Agent({
  settingSources: ['user'],
});

反模式

不要硬编码API密钥

// 坏
const agent = new Agent({
  apiKey: 'sk-ant-hardcoded-key',
});

// 好
const agent = new Agent({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

不要混合冲突的权限

// 坏:矛盾的权限
const agent = new Agent({
  allowedTools: ['read_file', 'write_file'],
  disallowedTools: ['read_file'],  // 冲突!
});

// 好:清晰的权限
const agent = new Agent({
  allowedTools: ['read_file'],
});

相关技能

  • tool-integration: 与工具和MCP服务器工作
  • context-management: 管理代理上下文和内存