CC Guide
中級

カスタムエージェントで専門タスクを委任する

.claude/agents/ にエージェント定義を作成し、専門的なタスクをサブエージェントに委任する方法

agentssub-agentsdelegation

カスタムエージェントで専門タスクを委任する

Claude Code のカスタムエージェント機能を使うと、特定の役割に特化したサブエージェントを定義し、メインセッションからタスクを委任できます。各エージェントは独自のツール制限、モデル、メモリスコープを持ちます。

エージェント定義の構造

.claude/agents/ ディレクトリに Markdown ファイルを作成します。

.claude/agents/
  code-reviewer.md
  test-generator.md
  security-reviewer.md
  refactorer.md

エージェント定義の例

---
name: security-reviewer
description: "Security-focused code review. Use when reviewing authentication, authorization, data handling, or API endpoint changes."
model: sonnet
tools: ["Read", "Grep", "Glob", "Bash"]
disallowedTools: ["Write", "Edit"]
permissionMode: default
---

You are a senior security reviewer. Analyze code for:

1. **Injection vulnerabilities** - SQL injection, XSS, command injection
2. **Authentication issues** - Session handling, token validation
3. **Data exposure** - Sensitive data in logs, responses, error messages
4. **Access control** - Missing authorization checks

For each finding, provide:
- Severity: CRITICAL / HIGH / MEDIUM / LOW
- Location: file:line
- Description: What the issue is
- Recommendation: How to fix it

Frontmatter の設定項目

項目説明
namestring一意な識別子(小文字・ハイフン)
descriptionstringいつ委任すべきかの定義(重要)
toolsarray利用可能なツール一覧
disallowedToolsarray除外するツール
modelstringsonnet, opus, haiku、またはフルモデルID
permissionModestringdefault, acceptEdits, dontAsk, plan
maxTurnsinteger最大ターン数
skillsarray起動時にロードするスキル
mcpServersobjectサブエージェント用MCPサーバー
hooksobjectサブエージェントにスコープされたフック
memorystringuser, project, local
backgroundbooleanバックグラウンドタスクとして実行
isolationstringworktree を設定すると独立環境で実行

ツール制限の連鎖

エージェント定義の tools フィールドで、そのエージェントがスポーンできるエージェントタイプも制限できます。

# 制限付き: worker と researcher のみスポーン可能
tools: ["Agent(worker, researcher)", "Read", "Write"]

# 無制限: 任意のエージェントをスポーン可能
tools: ["Agent"]

# Agent 省略: サブエージェントのスポーン不可
tools: ["Read", "Write"]

実践的なエージェント構成例

---
name: test-generator
description: "Generate unit tests for TypeScript/JavaScript code. Use after writing new functions or when test coverage is low."
model: sonnet
tools: ["Read", "Write", "Glob", "Grep", "Bash"]
permissionMode: acceptEdits
effort: high
---

Generate comprehensive unit tests using vitest. Follow these rules:
- Test file next to source file: `auth.ts` -> `auth.test.ts`
- Cover: happy path, edge cases, error handling
- Use descriptive test names in Japanese
- Target 80%+ coverage

エージェントの呼び出し

メインセッションでエージェントを呼び出すと、Claude が description に基づいて適切なタイミングで委任します。また、--agent フラグで直接起動することも可能です。

claude --agent security-reviewer

エージェントの description は「いつ使うべきか」を明確に記述することが重要です。Claude はこの description を見て委任を判断します。


あわせて読む

関連コンテンツ